This is a Rust library for interacting with the OpenAI API, specifically designed for language model completions and text embeddings. This library also has some helper tools for dealing with embeddings distance.
The CompletionRequest
struct allows you to create requests for language model
completions using OpenAI's API. You can easily construct
messages in a conversation format and receive responses. Additionally, macros
have been introduced for convenient message creation.
use openai_api::prelude::*;
fn main() {
let request = gpt35![
system!("You are a helpful assistant"),
assistant!("How may I help you today?"),
user!("What is the meaning of life?")
];
let response = request.get();
match response {
Ok(response) => {
println!("Model response: {}", response.default_choice());
},
Err(error) => {
eprintln!("Error: {}", error.message);
},
}
}
The EmbeddingRequest
struct allows you to obtain text embeddings using
OpenAI's text-embedding-ada-002 model. You can provide a text input, and the
library will return the corresponding embeddings.
use openai_api::prelude::*;
let input_text = String::from("Hello, how are you?");
let request = EmbeddingRequest::new(input_text);
let response = request.get();
match response {
Ok(response) => {
if let Some(embeddings) = response.get_embeddings() {
println!("Embeddings: {:?}", embeddings);
}
},
Err(error) => {
eprintln!("Error: {}", error.message);
},
}
reqwest
: A simple HTTP client for Rust.serde
: A framework for serializing and deserializing Rust data structures efficiently.rayon
: This is used for parallel processing of the embedding vectors when comparing distances.
Add the following to your Cargo.toml
file:
[dependencies]
openai_api = {git = "https://github.com/JustBobinAround/openai_api.git" }
- Most common usages only need the prelude:
use openai_api::prelude::*;
-
Create requests using the provided structs and methods.
-
Handle responses and errors accordingly.
This library is licensed under the MIT License - see the LICENSE file for details.
Special thanks to the contributors to the used
crates (reqwest
, serde
, rayon
) for their valuable work.