pub struct Client { /* private fields */ }
Implementations§
Source§impl Client
impl Client
Sourcepub fn from_url(api_key: &str, base_url: &str) -> Self
pub fn from_url(api_key: &str, base_url: &str) -> Self
Create a new OpenAI client with the given API key and base API URL.
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Create a new OpenAI client from the OPENAI_API_KEY
environment variable.
Panics if the environment variable is not set.
Sourcepub fn embedding_model(&self, model: &str) -> EmbeddingModel
pub fn embedding_model(&self, model: &str) -> EmbeddingModel
Create an embedding model with the given name.
Note: default embedding dimension of 0 will be used if model is not known.
If this is the case, it’s better to use function embedding_model_with_ndims
§Example
use rig::providers::openai::{Client, self};
// Initialize the OpenAI client
let openai = Client::new("your-open-ai-api-key");
let embedding_model = openai.embedding_model(openai::TEXT_EMBEDDING_3_LARGE);
Sourcepub fn embedding_model_with_ndims(
&self,
model: &str,
ndims: usize,
) -> EmbeddingModel
pub fn embedding_model_with_ndims( &self, model: &str, ndims: usize, ) -> EmbeddingModel
Create an embedding model with the given name and the number of dimensions in the embedding generated by the model.
§Example
use rig::providers::openai::{Client, self};
// Initialize the OpenAI client
let openai = Client::new("your-open-ai-api-key");
let embedding_model = openai.embedding_model("model-unknown-to-rig", 3072);
Sourcepub fn embeddings<D: Embed>(
&self,
model: &str,
) -> EmbeddingsBuilder<EmbeddingModel, D>
pub fn embeddings<D: Embed>( &self, model: &str, ) -> EmbeddingsBuilder<EmbeddingModel, D>
Create an embedding builder with the given embedding model.
§Example
use rig::providers::openai::{Client, self};
// Initialize the OpenAI client
let openai = Client::new("your-open-ai-api-key");
let embeddings = openai.embeddings(openai::TEXT_EMBEDDING_3_LARGE)
.simple_document("doc0", "Hello, world!")
.simple_document("doc1", "Goodbye, world!")
.build()
.await
.expect("Failed to embed documents");
Sourcepub fn completion_model(&self, model: &str) -> CompletionModel
pub fn completion_model(&self, model: &str) -> CompletionModel
Create a completion model with the given name.
§Example
use rig::providers::openai::{Client, self};
// Initialize the OpenAI client
let openai = Client::new("your-open-ai-api-key");
let gpt4 = openai.completion_model(openai::GPT_4);
Sourcepub fn agent(&self, model: &str) -> AgentBuilder<CompletionModel>
pub fn agent(&self, model: &str) -> AgentBuilder<CompletionModel>
Create an agent builder with the given completion model.
§Example
use rig::providers::openai::{Client, self};
// Initialize the OpenAI client
let openai = Client::new("your-open-ai-api-key");
let agent = openai.agent(openai::GPT_4)
.preamble("You are comedian AI with a mission to make people laugh.")
.temperature(0.0)
.build();
Sourcepub fn extractor<T: JsonSchema + for<'a> Deserialize<'a> + Serialize + Send + Sync>(
&self,
model: &str,
) -> ExtractorBuilder<T, CompletionModel>
pub fn extractor<T: JsonSchema + for<'a> Deserialize<'a> + Serialize + Send + Sync>( &self, model: &str, ) -> ExtractorBuilder<T, CompletionModel>
Create an extractor builder with the given completion model.
Sourcepub fn transcription_model(&self, model: &str) -> TranscriptionModel
pub fn transcription_model(&self, model: &str) -> TranscriptionModel
Create a transcription model with the given name.
§Example
use rig::providers::openai::{Client, self};
// Initialize the OpenAI client
let openai = Client::new("your-open-ai-api-key");
let gpt4 = openai.transcription_model(openai::WHISPER_1);