1use std::fmt::Display;
17
18use serde::{Deserialize, Serialize};
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct GenericMessage {
28 pub content: Option<String>,
29 pub role: GenericRole,
30 pub name: Option<String>,
31 pub tool_calls: Option<Vec<GenericFunctionCallIntent>>,
32 pub tool_call_id: Option<String>,
33}
34
35impl GenericMessage {
36 pub fn new(message: String, role: GenericRole) -> Self {
46 Self {
47 content: Some(message),
48 role,
49 name: None,
50 tool_call_id: None,
51 tool_calls: None,
52 }
53 }
54
55 pub fn new_tool_call(tool_call_id: String, tool_calls: Vec<GenericFunctionCallIntent>) -> Self {
56 Self {
57 content: None,
58 role: GenericRole::Assistant,
59 name: None,
60 tool_calls: Some(tool_calls),
61 tool_call_id: Some(tool_call_id),
62 }
63 }
64
65 pub fn with_name(mut self, name: impl ToString) -> Self {
66 self.name = Some(name.to_string());
67 self
68 }
69
70 pub fn with_tool_call_id(mut self, tool_call_id: impl ToString) -> Self {
71 self.tool_call_id = Some(tool_call_id.to_string());
72 self
73 }
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq, Eq)]
81#[serde(rename_all = "snake_case")]
82pub enum GenericRole {
83 System,
85 Assistant,
87 User,
89 Tool,
92}
93
94impl Display for GenericRole {
95 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96 match self {
97 GenericRole::System => write!(f, "system"),
98 GenericRole::Assistant => write!(f, "assistant"),
99 GenericRole::User => write!(f, "user"),
100 GenericRole::Tool => write!(f, "tool"),
101 }
102 }
103}
104
105#[derive(Debug)]
106pub struct GenericChatCompletionResponse<T> {
107 pub content: ResponseContent<T>,
108 pub usage: Option<GenericUsageReport>,
109}
110
111#[derive(Debug)]
112pub enum ResponseContent<T> {
113 Finished(T),
114 ToolCalls(GenericMessage),
115}
116
117#[derive(Debug, Clone)]
118pub struct GenericUsageReport {
119 pub prompt_tokens: i64,
120 pub completion_tokens: i64,
121 pub total_tokens: i64,
122}
123
124#[derive(Debug, Clone, Serialize, Deserialize)]
125pub struct GenericFunctionCallIntent {
126 pub id: String,
127 pub function: GenericFunctionCall,
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct GenericFunctionCall {
132 pub name: String,
133 pub arguments: serde_json::Value,
134}
135
136#[derive(Debug, Clone, Copy)]
137pub enum GenericStramingChatChunk {
138 Created,
139 Completed,
140 Failed,
141 OutputTextDelta,
142 OutputTextDone,
143}
144
145#[derive(Debug, Clone)]
146pub struct GenericFunctionSpec {
147 pub name: String,
148 pub description: String,
149 pub parameters: serde_json::Value,
150}
151
152#[derive(Debug, Clone)]
153pub enum StreamEvent {
154 TextDelta(String),
156
157 ToolCallStart {
159 index: usize,
160 id: Option<String>,
161 name: Option<String>,
162 },
163
164 ToolCallArgumentsDelta {
166 index: usize,
167 arguments_fragment: String,
168 },
169
170 ToolCallComplete {
172 index: usize,
173 intent: GenericFunctionCallIntent,
174 },
175
176 MessageEnd,
178
179 Usage(GenericUsageReport),
181}
182
183pub trait StreamingEventsProvider: crate::provider::ChatCompletionProvider {
186 type EventStream<'s>: futures_core::stream::Stream<Item = crate::error::Result<StreamEvent>>
187 + Send
188 + 's
189 where
190 Self: 's;
191
192 fn chat_complete_events_stream<'p, M>(
193 &self,
194 params: crate::provider::ChatCompleteParameters<M>,
195 ) -> Self::EventStream<'p>
196 where
197 M: Into<Self::Message> + Clone + Send + Sync + 'p;
198}