pub struct OpenAIClientBuilder { /* private fields */ }
Implementations§
Source§impl OpenAIClientBuilder
impl OpenAIClientBuilder
pub fn new() -> Self
Sourcepub fn with_api_key(self, api_key: impl Into<String>) -> Self
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
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}
Sourcepub fn with_endpoint(self, endpoint: impl Into<String>) -> Self
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}
pub fn with_organization(self, organization: impl Into<String>) -> Self
pub fn with_proxy(self, proxy: impl Into<String>) -> Self
pub fn with_timeout(self, timeout: u64) -> Self
pub fn with_header( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Sourcepub fn build(self) -> Result<OpenAIClient, Box<dyn Error>>
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
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
impl Default for OpenAIClientBuilder
Source§fn default() -> OpenAIClientBuilder
fn default() -> OpenAIClientBuilder
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for OpenAIClientBuilder
impl RefUnwindSafe for OpenAIClientBuilder
impl Send for OpenAIClientBuilder
impl Sync for OpenAIClientBuilder
impl Unpin for OpenAIClientBuilder
impl UnwindSafe for OpenAIClientBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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