openai_tools/
lib.rs

1//! # Usage
2//! ## Chat Completion
3//!
4//! ### Simple Chat Completion
5//!
6//! ```rust
7//! # use openai_tools::common::Message;
8//! # use openai_tools::chat::{ChatCompletion, ChatCompletionResponse};
9//! # #[tokio::main]
10//! # async fn main() {
11//!     let mut chat = ChatCompletion::new();
12//!     let messages = vec![
13//!         Message::new(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//! ### Chat with Json Schema
28//
29//! ```rust
30//! # use openai_tools::json_schema::JsonSchema;
31//! # use openai_tools::common::Message;
32//! # use openai_tools::chat::{
33//! #   ChatCompletion, ChatCompletionResponse, ChatCompletionResponseFormat
34//! # };
35//! # use serde::{Deserialize, Serialize};
36//! # use serde_json;
37//! # use std::env;
38//! # #[tokio::main]
39//! # async fn main() {
40//!     #[derive(Debug, Serialize, Deserialize)]
41//!     struct Weather {
42//!         location: String,
43//!         date: String,
44//!         weather: String,
45//!         error: String,
46//!     }
47//
48//!     let mut chat = ChatCompletion::new();
49//!     let messages = vec![Message::new(
50//!         String::from("user"),
51//!         String::from("Hi there! How's the weather tomorrow in Tokyo? If you can't answer, report error."),
52//!     )];
53//
54//!     // build json schema
55//!     let mut json_schema = JsonSchema::new("weather".to_string());
56//!     json_schema.add_property(
57//!         String::from("location"),
58//!         String::from("string"),
59//!         Option::from(String::from("The location to check the weather for.")),
60//!     );
61//!     json_schema.add_property(
62//!         String::from("date"),
63//!         String::from("string"),
64//!         Option::from(String::from("The date to check the weather for.")),
65//!     );
66//!     json_schema.add_property(
67//!         String::from("weather"),
68//!         String::from("string"),
69//!         Option::from(String::from("The weather for the location and date.")),
70//!     );
71//!     json_schema.add_property(
72//!         String::from("error"),
73//!         String::from("string"),
74//!         Option::from(String::from("Error message. If there is no error, leave this field empty.")),
75//!     );
76//
77//!     // configure chat completion model
78//!     chat
79//!         .model_id(String::from("gpt-4o-mini"))
80//!         .messages(messages)
81//!         .temperature(1.0)
82//!         .response_format(ChatCompletionResponseFormat::new(String::from("json_schema"), json_schema));
83//!
84//!     // execute chat
85//!     let response = chat.chat().await.unwrap();
86//
87//!     let answer: Weather = serde_json::from_str::<Weather>(&response.choices[0].message.content).unwrap();
88//!     println!("{:?}", answer)
89//!     // Weather {
90//!     //     location: "Tokyo",
91//!     //     date: "2023-10-01",
92//!     //     weather: "Temperatures around 25°C with partly cloudy skies and a slight chance of rain.",
93//!     //     error: "",
94//!     // }
95//! # }
96//! ```
97//!
98//! ### Details
99//! - `chat` -> [`ChatCompletion`](chat::ChatCompletion)
100//! - `responses` -> [`Response`](responses::Responses)
101//!
102pub mod chat;
103pub mod common;
104pub mod json_schema;
105pub mod responses;