Skip to content

Commit

Permalink
Add chat gpt api
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongri Jin committed Mar 2, 2023
1 parent 6c80991 commit 6a68775
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 191 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openai-api-rs"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
authors = ["Dongri Jin <[email protected]>"]
license = "MIT"
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,38 @@
Cargo.toml
```toml
[dependencies]
openai-api-rs = "0.1"
openai-api-rs = "0.1.2"
```

## Example:
```bash
export OPENAI_API_KEY={YOUR_API}
```

### Chat
```rust
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest {
model: chat_completion::GPT3_5_TURBO.to_string(),
messages: vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: String::from("NFTとは?"),
}],
};
let result = client.chat_completion(req).await?;
println!("{:?}", result.choices[0].message.content);

Ok(())
}
```

### Completion
```rust
use openai_api_rs::v1::completion::{self, CompletionRequest};
use openai_api_rs::v1::api::Client;
Expand Down
21 changes: 21 additions & 0 deletions examples/chat_completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest {
model: chat_completion::GPT3_5_TURBO.to_string(),
messages: vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: String::from("NFTとは?"),
}],
};
let result = client.chat_completion(req).await?;
println!("{:?}", result.choices[0].message.content);

Ok(())
}

// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example chat_completion
6 changes: 3 additions & 3 deletions examples/completion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use openai_api_rs::v1::completion::{self, CompletionRequest};
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::completion::{self, CompletionRequest};
use std::env;

#[tokio::main]
Expand All @@ -22,11 +22,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
best_of: None,
logit_bias: None,
user: None,
};
};
let result = client.completion(req).await?;
println!("{:}", result.choices[0].text);

Ok(())
}

// cargo run --package openai-rs --example completion
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example completion
Loading

0 comments on commit 6a68775

Please sign in to comment.