Struct OpenAIClientBuilder

Source
pub struct OpenAIClientBuilder { /* private fields */ }

Implementations§

Source§

impl OpenAIClientBuilder

Source

pub fn new() -> Self

Source

pub fn with_api_key(self, api_key: impl Into<String>) -> Self

Examples found in repository?
examples/audio_translations.rs (line 8)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
9
10    let req = AudioTranslationRequest::new(
11        "examples/data/problem_cn.mp3".to_string(),
12        WHISPER_1.to_string(),
13    );
14
15    let result = client.audio_translation(req).await?;
16    println!("{:?}", result);
17
18    Ok(())
19}
More examples
Hide additional examples
examples/embedding.rs (line 9)
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
9    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
10
11    let mut req = EmbeddingRequest::new(
12        TEXT_EMBEDDING_3_SMALL.to_string(),
13        vec!["story time".to_string(), "Once upon a time".to_string()],
14    );
15    req.dimensions = Some(10);
16
17    let result = client.embedding(req).await?;
18    println!("{:?}", result.data);
19
20    Ok(())
21}
examples/model.rs (line 7)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
7    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
8
9    let result = client.list_models().await?;
10    let models = result.data;
11
12    for model in models {
13        println!("Model id: {:?}", model.id);
14    }
15
16    let result = client.retrieve_model("gpt-4.1".to_string()).await?;
17    println!("Model id: {:?}", result.id);
18    println!("Model object: {:?}", result.object);
19
20    Ok(())
21}
examples/audio_speech.rs (line 8)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
9
10    let req = AudioSpeechRequest::new(
11        TTS_1.to_string(),
12        String::from("Money is not the problem, the problem is no money."),
13        audio::VOICE_ALLOY.to_string(),
14        String::from("examples/data/problem.mp3"),
15    );
16
17    let result = client.audio_speech(req).await?;
18    println!("{:?}", result);
19
20    Ok(())
21}
examples/completion.rs (line 8)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
9
10    let req = CompletionRequest::new(
11        completion::GPT3_TEXT_DAVINCI_003.to_string(),
12        String::from("What is Bitcoin?"),
13    )
14    .max_tokens(3000)
15    .temperature(0.9)
16    .top_p(1.0)
17    .stop(vec![String::from(" Human:"), String::from(" AI:")])
18    .presence_penalty(0.6)
19    .frequency_penalty(0.0);
20
21    let result = client.completion(req).await?;
22    println!("{:}", result.choices[0].text);
23
24    Ok(())
25}
examples/openrouter.rs (line 11)
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
9    let mut client = OpenAIClient::builder()
10        .with_endpoint("https://openrouter.ai/api/v1")
11        .with_api_key(api_key)
12        .build()?;
13
14    let req = ChatCompletionRequest::new(
15        GPT4_O_MINI.to_string(),
16        vec![chat_completion::ChatCompletionMessage {
17            role: chat_completion::MessageRole::user,
18            content: chat_completion::Content::Text(String::from("What is bitcoin?")),
19            name: None,
20            tool_calls: None,
21            tool_call_id: None,
22        }],
23    );
24
25    let result = client.chat_completion(req).await?;
26    println!("Content: {:?}", result.choices[0].message.content);
27    println!("Response Headers: {:?}", client.response_headers);
28
29    Ok(())
30}
Source

pub fn with_endpoint(self, endpoint: impl Into<String>) -> Self

Examples found in repository?
examples/openrouter.rs (line 10)
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
9    let mut client = OpenAIClient::builder()
10        .with_endpoint("https://openrouter.ai/api/v1")
11        .with_api_key(api_key)
12        .build()?;
13
14    let req = ChatCompletionRequest::new(
15        GPT4_O_MINI.to_string(),
16        vec![chat_completion::ChatCompletionMessage {
17            role: chat_completion::MessageRole::user,
18            content: chat_completion::Content::Text(String::from("What is bitcoin?")),
19            name: None,
20            tool_calls: None,
21            tool_call_id: None,
22        }],
23    );
24
25    let result = client.chat_completion(req).await?;
26    println!("Content: {:?}", result.choices[0].message.content);
27    println!("Response Headers: {:?}", client.response_headers);
28
29    Ok(())
30}
Source

pub fn with_organization(self, organization: impl Into<String>) -> Self

Source

pub fn with_proxy(self, proxy: impl Into<String>) -> Self

Source

pub fn with_timeout(self, timeout: u64) -> Self

Source

pub fn with_header( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Source

pub fn build(self) -> Result<OpenAIClient, Box<dyn Error>>

Examples found in repository?
examples/audio_translations.rs (line 8)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
9
10    let req = AudioTranslationRequest::new(
11        "examples/data/problem_cn.mp3".to_string(),
12        WHISPER_1.to_string(),
13    );
14
15    let result = client.audio_translation(req).await?;
16    println!("{:?}", result);
17
18    Ok(())
19}
More examples
Hide additional examples
examples/embedding.rs (line 9)
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
9    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
10
11    let mut req = EmbeddingRequest::new(
12        TEXT_EMBEDDING_3_SMALL.to_string(),
13        vec!["story time".to_string(), "Once upon a time".to_string()],
14    );
15    req.dimensions = Some(10);
16
17    let result = client.embedding(req).await?;
18    println!("{:?}", result.data);
19
20    Ok(())
21}
examples/model.rs (line 7)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
7    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
8
9    let result = client.list_models().await?;
10    let models = result.data;
11
12    for model in models {
13        println!("Model id: {:?}", model.id);
14    }
15
16    let result = client.retrieve_model("gpt-4.1".to_string()).await?;
17    println!("Model id: {:?}", result.id);
18    println!("Model object: {:?}", result.object);
19
20    Ok(())
21}
examples/audio_speech.rs (line 8)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
9
10    let req = AudioSpeechRequest::new(
11        TTS_1.to_string(),
12        String::from("Money is not the problem, the problem is no money."),
13        audio::VOICE_ALLOY.to_string(),
14        String::from("examples/data/problem.mp3"),
15    );
16
17    let result = client.audio_speech(req).await?;
18    println!("{:?}", result);
19
20    Ok(())
21}
examples/completion.rs (line 8)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
9
10    let req = CompletionRequest::new(
11        completion::GPT3_TEXT_DAVINCI_003.to_string(),
12        String::from("What is Bitcoin?"),
13    )
14    .max_tokens(3000)
15    .temperature(0.9)
16    .top_p(1.0)
17    .stop(vec![String::from(" Human:"), String::from(" AI:")])
18    .presence_penalty(0.6)
19    .frequency_penalty(0.0);
20
21    let result = client.completion(req).await?;
22    println!("{:}", result.choices[0].text);
23
24    Ok(())
25}
examples/openrouter.rs (line 12)
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
9    let mut client = OpenAIClient::builder()
10        .with_endpoint("https://openrouter.ai/api/v1")
11        .with_api_key(api_key)
12        .build()?;
13
14    let req = ChatCompletionRequest::new(
15        GPT4_O_MINI.to_string(),
16        vec![chat_completion::ChatCompletionMessage {
17            role: chat_completion::MessageRole::user,
18            content: chat_completion::Content::Text(String::from("What is bitcoin?")),
19            name: None,
20            tool_calls: None,
21            tool_call_id: None,
22        }],
23    );
24
25    let result = client.chat_completion(req).await?;
26    println!("Content: {:?}", result.choices[0].message.content);
27    println!("Response Headers: {:?}", client.response_headers);
28
29    Ok(())
30}

Trait Implementations§

Source§

impl Default for OpenAIClientBuilder

Source§

fn default() -> OpenAIClientBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,