Skip to content

Commit

Permalink
Merge pull request #84 from dongri/fix-readme
Browse files Browse the repository at this point in the history
Fix readme
  • Loading branch information
dongri authored Apr 15, 2024
2 parents 6553ba3 + 91fdaf3 commit df82373
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,32 @@ The library needs to be configured with your account's secret key, which is avai
$ export OPENAI_API_KEY=sk-xxxxxxx
```

### Set OPENAI_API_BASE to environment variable (optional)
```bash
$ export OPENAI_API_BASE=https://api.openai.com/v1
```

### Create client
```rust
use openai_api_rs::v1::api::Client;
use std::env;
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
```

### Create request
```rust
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use openai_api_rs::v1::common::GPT4;
let req = ChatCompletionRequest::new(
GPT4.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("Hello OpenAI!")),
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
name: None,
}],
);
```

### Send request
```rust
let result = client.completion(req)?;
println!("{:?}", result.choices[0].text);
let result = client.chat_completion(req)?;
println!("Content: {:?}", result.choices[0].message.content);
```

### Set OPENAI_API_BASE to environment variable (optional)
```bash
$ export OPENAI_API_BASE=https://api.openai.com/v1
```

## Example of chat completion
Expand All @@ -59,16 +55,20 @@ use std::env;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());

let req = ChatCompletionRequest::new(
GPT4.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is Bitcoin?")),
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
name: None,
}],
);

let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
println!("Content: {:?}", result.choices[0].message.content);
println!("Response Headers: {:?}", result.headers);

Ok(())
}
```
Expand Down

0 comments on commit df82373

Please sign in to comment.