#openai-api #openai #api #api-bindings #api-key #completion #file

ru-openai

ru-openai 是一个用于 OpenAI API 的 Rust 库

4 个版本

0.1.3 2023年4月30日
0.1.2 2023年3月10日
0.1.1 2023年3月9日
0.1.0 2023年3月8日

#2114 in 网页开发

MIT 许可证

91KB
1.5K SLoC

Ru-OpenAi

Rust 对 OpenAI API 的库。实际上,这可能是在 Rust 中最好的 crate。

关于 OpenAiAPI

您可以从 OpenAI API 参考 中了解您想了解的一切。

示例

关于如何调用 create completion API 的示例。

请确保您已经在 Cargo.toml 中添加了依赖项

[dependencies]
ru-openai = "0.1.3"

然后在您的项目根目录中创建一个名为 .env 的文件。在其中输入您自己的 openai api-key

API_KEY = your-api-key

然后您可以像这样调用 API

use dotenv::vars;
use ru_openai::{configuration::Configuration, api::*};
#[tokio::main]
async fn main() {
    // Load API key from .env file
    let api_key = vars().find(|(key, _)| key == "API_KEY").unwrap_or(("API_KEY".to_string(),"".to_string())).1;
    // Create a configuration object, with a proxy if you need one. If not, just remove the proxy method.
    let configuration = Configuration::new_personal(api_key)
        .proxy("http://127.0.0.1:7890".to_string());

    let openai_api = OpenAIApi::new(configuration);
    // Create a request object
    let request = CreateCompletionRequest {
        model: "text-davinci-003".to_string(),
        prompt: Some(vec!["Once upon a time".to_string()]),
        max_tokens: Some(7),
        temperature: Some(0.7),
        ..Default::default()
    };
    // Call the API: `create_completion`
    let response = openai_api.create_completion(request).await.unwrap();
    println!("{:#?}", response);
}

依赖项

~12–29MB
~405K SLoC