Skip to content

Commit b763a66

Browse files
committed
Add openrouter docs
1 parent c5bec93 commit b763a66

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,28 @@ openai-api-rs = "5.2.4"
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();
2426
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
2527
```
2628

29+
### Create OpenRouter client
30+
```rust
31+
let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
32+
let client = OpenAIClient::builder()
33+
.with_endpoint("https://openrouter.ai/api/v1")
34+
.with_api_key(api_key)
35+
.build()?;
36+
```
37+
2738
### Create request
2839
```rust
2940
let req = ChatCompletionRequest::new(
@@ -79,6 +90,41 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
7990
Ok(())
8091
}
8192
```
93+
94+
## Example for OpenRouter
95+
```rust
96+
use openai_api_rs::v1::api::OpenAIClient;
97+
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
98+
use openai_api_rs::v1::common::GPT4_O_MINI;
99+
use std::env;
100+
101+
#[tokio::main]
102+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
103+
let api_key = env::var("OPENROUTER_API_KEY").unwrap().to_string();
104+
let client = OpenAIClient::builder()
105+
.with_endpoint("https://openrouter.ai/api/v1")
106+
.with_api_key(api_key)
107+
.build()?;
108+
109+
let req = ChatCompletionRequest::new(
110+
GPT4_O_MINI.to_string(),
111+
vec![chat_completion::ChatCompletionMessage {
112+
role: chat_completion::MessageRole::user,
113+
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
114+
name: None,
115+
tool_calls: None,
116+
tool_call_id: None,
117+
}],
118+
);
119+
120+
let result = client.chat_completion(req).await?;
121+
println!("Content: {:?}", result.choices[0].message.content);
122+
println!("Response Headers: {:?}", result.headers);
123+
124+
Ok(())
125+
}
126+
```
127+
82128
More Examples: [examples](https://github.com/dongri/openai-api-rs/tree/main/examples)
83129

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

examples/openrouter.rs

Lines changed: 32 additions & 0 deletions
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 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: {:?}", result.headers);
28+
29+
Ok(())
30+
}
31+
32+
// OPENROUTER_API_KEY=xxxx cargo run --package openai-api-rs --example openrouter

0 commit comments

Comments
 (0)