OpenAI API client implemented using tokio and reqwest. Refer to tests folder in the repo or to code comments for usage examples.
toml
[dependencies]
openai-req="1"
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:
toml
key = "{YOUR_KEY}"
Function that constructs client by reading key.toml will look like this:
```rust
#[derive(Deserialize)]
struct Config{
key: String
}
fn getclient() -> OpenAiClient{ let keyconfig= fs::readtostring("key.toml") .expect("failed reading config file"); let openai:Config = toml::fromstr(&keyconfig) .expect("can't parse config file");
return OpenAiClient::new(&openai.key);
} ```
rust
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?)
}
get
function on response type.
Usually that type is called SomethingListResponse:
rust
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:
rust
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
}