This commit is contained in:
Dongri Jin
2025-03-05 07:42:32 +09:00
parent d66dc550e8
commit f59a6423b9
2 changed files with 18 additions and 8 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "openai-api-rs" name = "openai-api-rs"
version = "5.2.7" version = "6.0.0"
edition = "2021" edition = "2021"
authors = ["Dongri Jin <dongrium@gmail.com>"] authors = ["Dongri Jin <dongrium@gmail.com>"]
license = "MIT" license = "MIT"

View File

@ -7,7 +7,7 @@ Check out the [docs.rs](https://docs.rs/openai-api-rs/).
Cargo.toml Cargo.toml
```toml ```toml
[dependencies] [dependencies]
openai-api-rs = "5.2.7" openai-api-rs = "6.0.0"
``` ```
## Usage ## Usage
@ -23,13 +23,13 @@ $ export OPENROUTER_API_KEY=sk-xxxxxxx
### Create OpenAI client ### Create OpenAI client
```rust ```rust
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string(); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?; let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
``` ```
### Create OpenRouter client ### Create OpenRouter client
```rust ```rust
let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string(); let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder() let mut client = OpenAIClient::builder()
.with_endpoint("https://openrouter.ai/api/v1") .with_endpoint("https://openrouter.ai/api/v1")
.with_api_key(api_key) .with_api_key(api_key)
.build()?; .build()?;
@ -53,6 +53,10 @@ let req = ChatCompletionRequest::new(
```rust ```rust
let result = client.chat_completion(req)?; let result = client.chat_completion(req)?;
println!("Content: {:?}", result.choices[0].message.content); println!("Content: {:?}", result.choices[0].message.content);
for (key, value) in client.headers.unwrap().iter() {
println!("{}: {:?}", key, value);
}
``` ```
### Set OPENAI_API_BASE to environment variable (optional) ### Set OPENAI_API_BASE to environment variable (optional)
@ -70,7 +74,7 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string(); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?; let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
let req = ChatCompletionRequest::new( let req = ChatCompletionRequest::new(
GPT4_O.to_string(), GPT4_O.to_string(),
@ -85,7 +89,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let result = client.chat_completion(req).await?; let result = client.chat_completion(req).await?;
println!("Content: {:?}", result.choices[0].message.content); println!("Content: {:?}", result.choices[0].message.content);
println!("Response Headers: {:?}", result.headers);
for (key, value) in client.headers.unwrap().iter() {
println!("{}: {:?}", key, value);
}
Ok(()) Ok(())
} }
@ -101,7 +108,7 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string(); let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder() let mut client = OpenAIClient::builder()
.with_endpoint("https://openrouter.ai/api/v1") .with_endpoint("https://openrouter.ai/api/v1")
.with_api_key(api_key) .with_api_key(api_key)
.build()?; .build()?;
@ -119,7 +126,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let result = client.chat_completion(req).await?; let result = client.chat_completion(req).await?;
println!("Content: {:?}", result.choices[0].message.content); println!("Content: {:?}", result.choices[0].message.content);
println!("Response Headers: {:?}", result.headers);
for (key, value) in client.headers.unwrap().iter() {
println!("{}: {:?}", key, value);
}
Ok(()) Ok(())
} }