-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from rellfy/credentials
Use Credentials struct for auth
- Loading branch information
Showing
14 changed files
with
410 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "openai" | ||
version = "1.0.0-alpha.16" | ||
version = "1.0.0-alpha.17" | ||
authors = ["Lorenzo Fontoura <[email protected]>", "valentinegb"] | ||
edition = "2021" | ||
description = "An unofficial Rust library for the OpenAI API." | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use dotenvy::dotenv; | ||
use openai::{ | ||
chat::{ChatCompletion, ChatCompletionMessage, ChatCompletionMessageRole}, | ||
Credentials, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Make sure you have a file named `.env` with the `OPENAI_KEY` environment variable defined! | ||
dotenv().unwrap(); | ||
// Relies on OPENAI_KEY and optionally OPENAI_BASE_URL. | ||
let credentials = Credentials::from_env(); | ||
let messages = vec![ | ||
ChatCompletionMessage { | ||
role: ChatCompletionMessageRole::System, | ||
content: Some("You are a helpful assistant.".to_string()), | ||
name: None, | ||
function_call: None, | ||
}, | ||
ChatCompletionMessage { | ||
role: ChatCompletionMessageRole::User, | ||
content: Some("Tell me a random crab fact".to_string()), | ||
name: None, | ||
function_call: None, | ||
}, | ||
]; | ||
let chat_completion = ChatCompletion::builder("gpt-4o", messages.clone()) | ||
.credentials(credentials.clone()) | ||
.create() | ||
.await | ||
.unwrap(); | ||
let returned_message = chat_completion.choices.first().unwrap().message.clone(); | ||
// Assistant: Sure! Here's a random crab fact: Crabs communicate with each other by drumming or waving their pincers. | ||
println!( | ||
"{:#?}: {}", | ||
returned_message.role, | ||
returned_message.content.unwrap().trim() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.