openai_tools/lib.rs
1//! # openai-tools
2//! This crate provides a simple interface to interact with OpenAI's Chat Completion API.
3//! # Example Usage
4//!
5//! ```rust
6//! # use openai_tools::common::Message;
7//! # use openai_tools::chat::ChatCompletionResponse;
8//! # use openai_tools::chat::ChatCompletion;
9//! # #[tokio::main]
10//! # async fn main() {
11//! let mut chat = ChatCompletion::new();
12//! let messages = vec![
13//! Message::from_string(String::from("user"), String::from("Hi there!"))
14//! ];
15//!
16//! chat
17//! .model_id(String::from("gpt-4o-mini"))
18//! .messages(messages)
19//! .temperature(1.0);
20//!
21//! let response: ChatCompletionResponse = chat.chat().await.unwrap();
22//! println!("{}", &response.choices[0].message.content);
23//! // Hello! How can I assist you today?
24//! # }
25//! ```
26//!
27//! ### Simple Chat Completion
28//!
29//! ```rust
30//! # use openai_tools::common::Message;
31//! # use openai_tools::chat::{ChatCompletion, ChatCompletionResponse};
32//! # #[tokio::main]
33//! # async fn main() {
34//! let mut chat = ChatCompletion::new();
35//! let messages = vec![
36//! Message::from_string(String::from("user"), String::from("Hi there!"))
37//! ];
38//
39//! chat
40//! .model_id(String::from("gpt-4o-mini"))
41//! .messages(messages)
42//! .temperature(1.0);
43//
44//! let response: ChatCompletionResponse = chat.chat().await.unwrap();
45//! println!("{}", &response.choices[0].message.content);
46//! // Hello! How can I assist you today?
47//! # }
48//! ```
49//
50//! ### Chat with Json Schema
51//
52//! ```rust
53//! # use openai_tools::structured_output::Schema;
54//! # use openai_tools::common::Message;
55//! # use openai_tools::chat::{
56//! # ChatCompletion, ChatCompletionResponse, ChatCompletionResponseFormat
57//! # };
58//! # use serde::{Deserialize, Serialize};
59//! # use serde_json;
60//! # use std::env;
61//! # #[tokio::main]
62//! # async fn main() {
63//! #[derive(Debug, Serialize, Deserialize)]
64//! struct Weather {
65//! location: String,
66//! date: String,
67//! weather: String,
68//! error: String,
69//! }
70//
71//! let mut chat = ChatCompletion::new();
72//! let messages = vec![Message::from_string(
73//! String::from("user"),
74//! String::from("Hi there! How's the weather tomorrow in Tokyo? If you can't answer, report error."),
75//! )];
76//
77//! // build json schema
78//! let mut json_schema = Schema::chat_json_schema("weather".to_string());
79//! json_schema.add_property(
80//! String::from("location"),
81//! String::from("string"),
82//! Option::from(String::from("The location to check the weather for.")),
83//! );
84//! json_schema.add_property(
85//! String::from("date"),
86//! String::from("string"),
87//! Option::from(String::from("The date to check the weather for.")),
88//! );
89//! json_schema.add_property(
90//! String::from("weather"),
91//! String::from("string"),
92//! Option::from(String::from("The weather for the location and date.")),
93//! );
94//! json_schema.add_property(
95//! String::from("error"),
96//! String::from("string"),
97//! Option::from(String::from("Error message. If there is no error, leave this field empty.")),
98//! );
99//
100//! // configure chat completion model
101//! chat
102//! .model_id(String::from("gpt-4o-mini"))
103//! .messages(messages)
104//! .temperature(1.0)
105//! .response_format(ChatCompletionResponseFormat::new(String::from("json_schema"), json_schema));
106//!
107//! // execute chat
108//! let response = chat.chat().await.unwrap();
109//
110//! let answer: Weather = serde_json::from_str::<Weather>(&response.choices[0].message.content).unwrap();
111//! println!("{:?}", answer)
112//! // Weather {
113//! // location: "Tokyo",
114//! // date: "2023-10-01",
115//! // weather: "Temperatures around 25°C with partly cloudy skies and a slight chance of rain.",
116//! // error: "",
117//! // }
118//! # }
119//! ```
120//!
121//! ### Details
122//! - `chat` -> [`ChatCompletion`](chat::ChatCompletion)
123//! - `responses` -> [`Response`](responses::Responses)
124//!
125pub mod chat;
126pub mod common;
127pub mod errors;
128pub mod responses;
129pub mod structured_output;