1 unstable release
new 0.1.0 | Dec 16, 2024 |
---|
#1021 in Web programming
32KB
725 lines
OpenAI Tools
API Wrapper for OpenAI API.
Installation
To start using the openai-tools
, add it to your projects's dependencies in the `Cargo.toml' file:
cargo add openai-tools
API key is necessary to access OpenAI API.
Set it in the .env
file:
OPENAI_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
Then, import the necesarry modules in your code:
use openai_tools::OpenAI;
Usage
Chat Completion
-
Simple Chat
let mut openai = OpenAI::new(); let messages = vec![ Message::new("user".to_string(), "Hi there!".to_string()) ]; openai .model_id("gpt-4o-mini") .messages(messages) .temperature(1.0); let response: Response = openai.chat().unwrap(); println!("{}", &response.choices[0].message.content); // Hello! How can I assist you today?
-
Chat with Json Schema
#[derive(Debug, Serialize, Deserialize)] struct Weather { location: String, date: String, weather: String, error: String, } let mut openai = OpenAI::new(); let messages = vec![Message::new( "user".to_string(), "Hi there! How's the weather tomorrow in Tokyo? If you can't answer, report error." .to_string(), )]; // build json schema let mut json_schema = JsonSchema::new("weather".to_string()); json_schema.add_property( "location".to_string(), "string".to_string(), Option::from("The location to check the weather for.".to_string()), ); json_schema.add_property( "date".to_string(), "string".to_string(), Option::from("The date to check the weather for.".to_string()), ); json_schema.add_property( "weather".to_string(), "string".to_string(), Option::from("The weather for the location and date.".to_string()), ); json_schema.add_property( "error".to_string(), "string".to_string(), Option::from("Error message. If there is no error, leave this field empty.".to_string()), ); // configure chat completion model openai .model_id("gpt-4o-mini") .messages(messages) .temperature(1.0) .response_format(ResponseFormat::new("json_schema".to_string(), json_schema)); // execute chat let response = openai.chat().unwrap(); let answer: Weather = serde_json::from_str::<Weather>(&response.choices[0].message.content) println!("{:?}", answer) // Weather { // location: "Tokyo", // date: "2023-10-01", // weather: "Temperatures around 25°C with partly cloudy skies and a slight chance of rain.", // error: "", // }
Dependencies
~1–1.9MB
~39K SLoC