3 stable releases
2.0.1 | Mar 22, 2023 |
---|---|
1.0.1 | Mar 22, 2023 |
#804 in Machine learning
46 downloads per month
86KB
2K
SLoC
openai-req
OpenAI API client implemented using tokio and reqwest. Refer to tests folder in the repo or to code comments for usage examples.
Usage.
-
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 }
Supported APIs:
- Models:
- List
- Retrieve
- Completions:
- Create
- Chat:
- Create
- Edits:
- Create
- Images:
- Create
- Create edit
- Create variation
- Embeddings:
- Create
- Audio
- Create transcription
- Create translation
- Files
- List
- Upload
- Delete
- Retrieve file
- Retrieve file content
- Fine-tunes
- Create fine-tune
- List fine-tunes
- Retrieve fine-tune
- List fine-tune events
- Delete fine-tune model
- Moderations
- Create moderation
Dependencies
~8–21MB
~297K SLoC