1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! Chat completions, including streaming and tool calling.
//!
//! This module provides the primary interface for interacting with the chat
//! completions API. It supports both unary (non-streaming) and streaming
//! responses, as well as tool calling (function calling) and reasoning modes.
//!
//! # Key Components
//!
//! - [`Chat`]: The main struct for performing chat completion operations.
//! - [`chat_request`]: A convenient function for creating chat request parameters.
//! - [`ChatCompletion`]: The response type for unary chat completions.
//! - [`ChatCompletionChunk`]: The response type for streaming chat completions.
//! - [`ChatCompletionMessageParam`]: Represents a message in the chat history.
//! - [`ChatCompletionTool`]: Defines a tool (function) that the model can call.
//!
//! # Examples
//!
//! ## Unary (Non-Streaming) Chat Completion
//!
//! ```rust,no_run
//! use openai4rs::*;
//! use dotenvy::dotenv;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! dotenv().ok();
//! let client = OpenAI::from_env()?;
//! let messages = vec![user!("What is Rust?")];
//! let request = chat_request("gpt-4", &messages);
//! let response = client.chat().create(request).await?;
//! println!("{:#?}", response);
//! Ok(())
//! }
//! ```
//!
//! ## Streaming Chat Completion
//!
//! ```rust,no_run
//! use openai4rs::*;
//! use futures::StreamExt;
//! use dotenvy::dotenv;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! dotenv().ok();
//! let client = OpenAI::from_env()?;
//! let messages = vec![user!("Tell me a short story.")];
//! let request = chat_request("gpt-4", &messages);
//! let mut stream = client.chat().create_stream(request).await?;
//!
//! while let Some(chunk) = stream.next().await {
//! let chunk = chunk?;
//! if let Some(choice) = chunk.choices.first() {
//! if let Some(content) = &choice.delta.content {
//! print!("{}", content);
//! }
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! ## Tool Calling (Function Calling)
//!
//! ```rust,no_run
//! use openai4rs::*;
//! use openai4rs::chat::{ChatCompletionToolParam, tool_parameters::Parameters};
//! use dotenvy::dotenv;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! dotenv().ok();
//! let client = OpenAI::from_env()?;
//!
//! // Define a tool using the type-safe Parameters builder
//! let tool_params = Parameters::object()
//! .property(
//! "location",
//! Parameters::string()
//! .description("The city and state, e.g. San Francisco, CA")
//! .build()
//! )
//! .require("location")
//! .build()
//! .unwrap();
//!
//! let tool = ChatCompletionToolParam::function(
//! "get_current_weather",
//! "Get the current weather in a given location",
//! tool_params
//! );
//!
//! let messages = vec![user!("What's the weather like in Boston?")];
//! let request = chat_request("gpt-4", &messages).tools(vec![tool]);
//! let response = client.chat().create(request).await?;
//!
//! // Check if the model wants to call a tool
//! if let Some(choice) = response.choices.first() {
//! if let Some(tool_calls) = &choice.message.tool_calls {
//! for tool_call in tool_calls {
//! println!("Tool call: {:#?}", tool_call);
//! // Here you would actually call the function and return the result
//! }
//! }
//! }
//!
//! Ok(())
//! }
//! ```
pub use Chat;
pub use chat_request;
pub use Parameters;
pub use *;