#api-key #ai #chat-completion #ai-api #async #ChatGPT #async-client

打击区

打击区:AI 模型 API 的非官方库

8 个版本

0.1.6 2023 年 12 月 16 日
0.1.5 2023 年 11 月 29 日
0.0.0 2023 年 10 月 3 日

#1882 in 网络编程

Download history 8/week @ 2024-03-28 4/week @ 2024-04-04

114 每月下载量

MIT 许可证

62KB
1.5K SLoC

打击区

打击区是一个针对最佳 AI 模型的非官方 async Rust 客户端库。它目前支持 ModelfarmPplxOllama

Modelfarm 客户端无需任何 API 密钥即可直接使用,Pplx 客户端需要 API 密钥,而 Ollama 客户端则需要您本地构建和运行 Ollama 服务器。

Modelfarm 和 Ollama 支持 Chat、Completions 和 Embeddings。Pplx 支持 Chat 和 Completions。

我将很快为 Pplx 客户端添加比特币和 ecash 支付选项,届时您将不再需要 API 密钥。

对于开发者和贡献者: 贡献

从 OpenAI Dive 分支: https://github.com/tjardoo/pplx-client

您可以使用 cargo run --example <example_name>examples 目录中运行任何示例

Modelfarm 示例

#[tokio::main]
async fn main() {
    let modelfarm = Modelfarm::new();

    let chat_session = ChatSession {
        context: "You are a programmer bot".to_string(),
        examples: vec![ChatExample {
            input: ChatMessage {
                content: "1 + 1".to_string(),
                author: "user".to_string(),
            },
            output: ChatMessage {
                content: "2".to_string(),
                author: "assistant".to_string(),
            },
        }],
        messages: vec![ChatMessage {
            content: "How do I write a nix flake for a rust project?".to_string(),
            author: "user".to_string(),
        }],
    };

    let req = ModelfarmChatCompletionRequest {
        model: ModelfarmChatModel::ChatBison,
        parameters: ModelfarmChatParameters {
            prompts: vec![chat_session],
            temperature: 0.2,
            max_output_tokens: 1024,
        },
    };

    let result = modelfarm.chat(req).await.unwrap();

    println!("{:?}", result);
}

Pplx 示例

#[tokio::main]
async fn main() {
    dotenv().ok();
    let api_key = env::var("PPLX_API_KEY").expect("$PPLX_API_KEY is not set");

    let client = Pplx::new(api_key);

    let parameters = PplxChatCompletionParameters {
        model: PplxChatModel::Mistral7bInstruct,
        messages: vec![
            ChatMessage {
                role: Role::User,
                content: "Hello!".to_string(),
                ..Default::default()
            },
            ChatMessage {
                role: Role::User,
                content: "Tell me a story?".to_string(),
                ..Default::default()
            },
        ],
        temperature: None,
        top_p: None,
        top_k: None,
        max_tokens: Some(1000),
        presence_penalty: None,
        frequency_penalty: None,
    };

    let mut stream = client.stream_chat(parameters).await.unwrap();

    while let Some(response) = stream.next().await {
        match response {
            Ok(chat_response) => chat_response.choices.iter().for_each(|choice| {
                if let Some(content) = choice.delta.content.as_ref() {
                    print!("{}", content);
                }
            }),
            Err(e) => eprintln!("{}", e),
        }
    }
}

Ollama 示例

#[tokio::main]
async fn main() {
    let ollama = Ollama::new(); // Must have the Ollama server running locally

    let req = OllamaCompletionRequest {
        model: OllamaModel::Mixtral,
        prompt: "How do I write a nix flake for a rust project?".to_string(),
        max_tokens: Some(100),
        temperature: Some(0.2),
        ..Default::default()
    };

    let mut stream = ollama.chat_stream(req).await.unwrap();

    while let Some(response) = stream.next().await {
        print!("{}", response.unwrap().response);
    }
}

贡献

欢迎贡献!请打开一个问题或提交一个 pull request。

开发环境

这是一个稳定的 rust 2021 项目。您可以使用 rustup 安装最新的稳定 rust 工具链

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

该项目还使用 Nix Flakes 来管理开发环境。您可以使用以下方法安装 Nix

curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install

安装 Nix 后,您可以使用以下命令进入开发环境

nix develop

然后使用 just 命令构建项目和运行命令

# Available just recipes:
    build *ARGS="--workspace --all-targets" # run `cargo build` on everything
    b *ARGS="--workspace --all-targets" # alias for `build`
    check *ARGS="--workspace --all-targets" # run `cargo check` on everything
    c *ARGS="--workspace --all-targets" # alias for `check`
    clippy *ARGS="--locked --offline --workspace --all-targets" # run `cargo clippy` on everything
    clippy-fix *ARGS="--locked --offline --workspace --all-targets" # run `cargo clippy --fix` on everything
    final-check          # run all checks recommended before opening a PR
    format               # run code formatters
    lint                 # run lints (git pre-commit hook)
    semgrep              # run `semgrep`
    test                 # run tests
    t                    # alias for `test`
    typos *PARAMS        # check typos
    typos-fix-all        # fix all typos
    watch *ARGS="-x run" # run and restart on changes

依赖项

~8–22MB
~361K SLoC