Crates.io | openai-req |
lib.rs | openai-req |
version | 2.0.1 |
source | src |
created_at | 2023-03-22 14:24:15.714649 |
updated_at | 2023-03-22 17:36:53.826192 |
description | Client for OpenAI API, written with reqwest and tokio |
homepage | |
repository | https://github.com/ArseniiRudenko/openai-req |
max_upload_size | |
id | 817195 |
size | 98,539 |
OpenAI API client implemented using tokio and reqwest. Refer to tests folder in the repo or to code comments for usage examples.
Import library
[dependencies]
openai-req="2"
First you will need to construct client. Minimal client only requires API key. Here is a simple example with reading key from toml file.
Let's say you have key in key.toml file, that looks like this:
key = "{YOUR_KEY}"
Function that constructs client by reading key.toml will look like this:
#[derive(Deserialize)]
struct Config{
key: String
}
fn get_client() -> OpenAiClient{
let key_config=
fs::read_to_string("key.toml")
.expect("failed reading config file");
let openai:Config =
toml::from_str(&key_config)
.expect("can't parse config file");
return OpenAiClient::new(&openai.key);
}
Next you generally construct Request structure, and trigger run method, passing client reference to it. here is an example for chat completion:
async fn chat() -> Result<ChatSuccess,anyhow::Error> {
let client = get_client();
let messages = vec!(Message{
role: Role::User,
content: "hello!".to_string(),
});
let chat_request = ChatRequest::new(messages);
Ok(chat_request.run(&client).await?)
}
For get requests that do not take any parameters, you generally call static get
function on response type.
Usually that type is called SomethingListResponse:
async fn models() -> Result<ModelListResponse,anyhow::Error> {
let client = get_client();
Ok(ModelListResponse::get(&client).await?)
}
And finally, for download file requests, request type will have two methods:
async fn download_to_file(&self, client:&OpenAiClient, target_path:&str) -> Result<()>
and
async fn download(&self, client:&OpenAiClient) -> Result<Pin<Box<dyn Stream<Item=Result<Bytes, reqwest::Error>>>>>
First one takes path on local fs, and creates downloaded file there. Second one just returns async data stream, and lets you figure out what to do with it.
Here is an example:
async fn file_download() -> Result<(),anyhow::Error> {
let client = get_client();
let files = FileListResponse::get(&client).await?;
let info = files.data.first().ok_or(anyhow!("No files available"))?;
let download_request: FileDownloadRequest = info.clone().into();
download_request.download_to_file(&client, "fine-tune2.json").await
}