Skip to content

Commit 70bd625

Browse files
authored
Merge branch 'dongri:main' into main
2 parents 449a682 + f59a642 commit 70bd625

30 files changed

+301
-133
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openai-api-rs"
3-
version = "5.2.4"
3+
version = "6.0.0"
44
edition = "2021"
55
authors = ["Dongri Jin <[email protected]>"]
66
license = "MIT"

README.md

+62-6
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,32 @@ Check out the [docs.rs](https://docs.rs/openai-api-rs/).
77
Cargo.toml
88
```toml
99
[dependencies]
10-
openai-api-rs = "5.2.4"
10+
openai-api-rs = "6.0.0"
1111
```
1212

1313
## Usage
1414
The library needs to be configured with your account's secret key, which is available on the [website](https://platform.openai.com/account/api-keys). We recommend setting it as an environment variable. Here's an example of initializing the library with the API key loaded from an environment variable and creating a completion:
1515

16-
### Set OPENAI_API_KEY to environment variable
16+
### Set OPENAI_API_KEY or OPENROUTER_API_KEY to environment variable
1717
```bash
1818
$ export OPENAI_API_KEY=sk-xxxxxxx
19+
or
20+
$ export OPENROUTER_API_KEY=sk-xxxxxxx
1921
```
2022

21-
### Create client
23+
### Create OpenAI client
2224
```rust
2325
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
24-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
26+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
27+
```
28+
29+
### Create OpenRouter client
30+
```rust
31+
let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
32+
let mut client = OpenAIClient::builder()
33+
.with_endpoint("https://openrouter.ai/api/v1")
34+
.with_api_key(api_key)
35+
.build()?;
2536
```
2637

2738
### Create request
@@ -42,6 +53,10 @@ let req = ChatCompletionRequest::new(
4253
```rust
4354
let result = client.chat_completion(req)?;
4455
println!("Content: {:?}", result.choices[0].message.content);
56+
57+
for (key, value) in client.headers.unwrap().iter() {
58+
println!("{}: {:?}", key, value);
59+
}
4560
```
4661

4762
### Set OPENAI_API_BASE to environment variable (optional)
@@ -59,7 +74,7 @@ use std::env;
5974
#[tokio::main]
6075
async fn main() -> Result<(), Box<dyn std::error::Error>> {
6176
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
62-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
77+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
6378

6479
let req = ChatCompletionRequest::new(
6580
GPT4_O.to_string(),
@@ -74,11 +89,52 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
7489

7590
let result = client.chat_completion(req).await?;
7691
println!("Content: {:?}", result.choices[0].message.content);
77-
println!("Response Headers: {:?}", result.headers);
92+
93+
for (key, value) in client.headers.unwrap().iter() {
94+
println!("{}: {:?}", key, value);
95+
}
96+
97+
Ok(())
98+
}
99+
```
100+
101+
## Example for OpenRouter
102+
```rust
103+
use openai_api_rs::v1::api::OpenAIClient;
104+
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
105+
use openai_api_rs::v1::common::GPT4_O_MINI;
106+
use std::env;
107+
108+
#[tokio::main]
109+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
110+
let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
111+
let mut client = OpenAIClient::builder()
112+
.with_endpoint("https://openrouter.ai/api/v1")
113+
.with_api_key(api_key)
114+
.build()?;
115+
116+
let req = ChatCompletionRequest::new(
117+
GPT4_O_MINI.to_string(),
118+
vec![chat_completion::ChatCompletionMessage {
119+
role: chat_completion::MessageRole::user,
120+
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
121+
name: None,
122+
tool_calls: None,
123+
tool_call_id: None,
124+
}],
125+
);
126+
127+
let result = client.chat_completion(req).await?;
128+
println!("Content: {:?}", result.choices[0].message.content);
129+
130+
for (key, value) in client.headers.unwrap().iter() {
131+
println!("{}: {:?}", key, value);
132+
}
78133

79134
Ok(())
80135
}
81136
```
137+
82138
More Examples: [examples](https://github.com/dongri/openai-api-rs/tree/main/examples)
83139

84140
Check out the [full API documentation](https://platform.openai.com/docs/api-reference/completions) for examples of all the available functions.

examples/assistant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::env;
1010
#[tokio::main]
1111
async fn main() -> Result<(), Box<dyn std::error::Error>> {
1212
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
13-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
13+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
1414

1515
let mut tools = HashMap::new();
1616
tools.insert("type".to_string(), "code_interpreter".to_string());

examples/audio_speech.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::env;
55
#[tokio::main]
66
async fn main() -> Result<(), Box<dyn std::error::Error>> {
77
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
8+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
99

1010
let req = AudioSpeechRequest::new(
1111
TTS_1.to_string(),

examples/audio_transcriptions.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use openai_api_rs::v1::api::OpenAIClient;
22
use openai_api_rs::v1::audio::{AudioTranscriptionRequest, WHISPER_1};
33
use std::env;
4+
use std::fs::File;
5+
use std::io::Read;
46

57
#[tokio::main]
68
async fn main() -> Result<(), Box<dyn std::error::Error>> {
79
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
10+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
911

10-
let req = AudioTranscriptionRequest::new(
11-
"examples/data/problem.mp3".to_string(),
12-
WHISPER_1.to_string(),
13-
);
12+
let file_path = "examples/data/problem.mp3";
13+
14+
// Test with file
15+
let req = AudioTranscriptionRequest::new(file_path.to_string(), WHISPER_1.to_string());
1416

1517
let req_json = req.clone().response_format("json".to_string());
1618

@@ -22,7 +24,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2224
let result = client.audio_transcription_raw(req_raw).await?;
2325
println!("{:?}", result);
2426

27+
// Test with bytes
28+
let mut file = File::open(file_path)?;
29+
let mut buffer = Vec::new();
30+
file.read_to_end(&mut buffer)?;
31+
32+
let req = AudioTranscriptionRequest::new_bytes(buffer, WHISPER_1.to_string());
33+
34+
let req_json = req.clone().response_format("json".to_string());
35+
36+
let result = client.audio_transcription(req_json).await?;
37+
println!("{:?}", result);
38+
2539
Ok(())
2640
}
2741

28-
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_translations
42+
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_transcriptions

examples/audio_translations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::env;
55
#[tokio::main]
66
async fn main() -> Result<(), Box<dyn std::error::Error>> {
77
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
8+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
99

1010
let req = AudioTranslationRequest::new(
1111
"examples/data/problem_cn.mp3".to_string(),

examples/batch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::str;
1010
#[tokio::main]
1111
async fn main() -> Result<(), Box<dyn std::error::Error>> {
1212
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
13-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
13+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
1414

1515
let req = FileUploadRequest::new(
1616
"examples/data/batch_request.json".to_string(),

examples/chat_completion.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::env;
66
#[tokio::main]
77
async fn main() -> Result<(), Box<dyn std::error::Error>> {
88
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
9-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
9+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
1010

1111
let req = ChatCompletionRequest::new(
1212
GPT4_O_MINI.to_string(),
@@ -21,7 +21,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2121

2222
let result = client.chat_completion(req).await?;
2323
println!("Content: {:?}", result.choices[0].message.content);
24-
println!("Response Headers: {:?}", result.headers);
24+
25+
// print response headers
26+
for (key, value) in client.headers.unwrap().iter() {
27+
println!("{}: {:?}", key, value);
28+
}
2529

2630
Ok(())
2731
}

examples/completion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::env;
55
#[tokio::main]
66
async fn main() -> Result<(), Box<dyn std::error::Error>> {
77
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
8+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
99

1010
let req = CompletionRequest::new(
1111
completion::GPT3_TEXT_DAVINCI_003.to_string(),

examples/embedding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::env;
66
#[tokio::main]
77
async fn main() -> Result<(), Box<dyn std::error::Error>> {
88
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
9-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
9+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
1010

1111
let mut req = EmbeddingRequest::new(
1212
TEXT_EMBEDDING_3_SMALL.to_string(),

examples/function_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn get_coin_price(coin: &str) -> f64 {
1818
#[tokio::main]
1919
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2020
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
21-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
21+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
2222

2323
let mut properties = HashMap::new();
2424
properties.insert(

examples/function_call_role.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn get_coin_price(coin: &str) -> f64 {
1818
#[tokio::main]
1919
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2020
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
21-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
21+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
2222

2323
let mut properties = HashMap::new();
2424
properties.insert(

examples/openrouter.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use openai_api_rs::v1::api::OpenAIClient;
2+
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
3+
use openai_api_rs::v1::common::GPT4_O_MINI;
4+
use std::env;
5+
6+
#[tokio::main]
7+
async 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.headers);
28+
29+
Ok(())
30+
}
31+
32+
// OPENROUTER_API_KEY=xxxx cargo run --package openai-api-rs --example openrouter

examples/vision.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::env;
66
#[tokio::main]
77
async fn main() -> Result<(), Box<dyn std::error::Error>> {
88
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
9-
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
9+
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
1010

1111
let req = ChatCompletionRequest::new(
1212
GPT4_O.to_string(),

src/realtime/types.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@ pub struct Session {
3030
#[serde(rename_all = "lowercase")]
3131
pub enum RealtimeVoice {
3232
Alloy,
33-
Shimmer,
33+
Ash,
34+
Ballad,
35+
Coral,
3436
Echo,
37+
Sage,
38+
Shimmer,
39+
Verse,
3540
}
3641

3742
#[derive(Debug, Serialize, Deserialize, Clone)]
3843
pub enum AudioFormat {
3944
#[serde(rename = "pcm16")]
4045
PCM16,
41-
#[serde(rename = "g711-ulaw")]
46+
#[serde(rename = "g711_ulaw")]
4247
G711ULAW,
43-
#[serde(rename = "g711-alaw")]
48+
#[serde(rename = "g711_alaw")]
4449
G711ALAW,
4550
}
4651

0 commit comments

Comments
 (0)