async_openai/types/chat/
impls.rs

1use std::fmt::Display;
2
3use crate::types::chat::{
4    ChatCompletionFunctionCall, ChatCompletionMessageCustomToolCall, ChatCompletionMessageToolCall,
5    ChatCompletionMessageToolCalls, ChatCompletionNamedToolChoice,
6    ChatCompletionRequestAssistantMessage, ChatCompletionRequestAssistantMessageContent,
7    ChatCompletionRequestDeveloperMessage, ChatCompletionRequestDeveloperMessageContent,
8    ChatCompletionRequestFunctionMessage, ChatCompletionRequestMessage,
9    ChatCompletionRequestMessageContentPartAudio, ChatCompletionRequestMessageContentPartImage,
10    ChatCompletionRequestMessageContentPartText, ChatCompletionRequestSystemMessage,
11    ChatCompletionRequestSystemMessageContent, ChatCompletionRequestToolMessage,
12    ChatCompletionRequestToolMessageContent, ChatCompletionRequestUserMessage,
13    ChatCompletionRequestUserMessageContent, ChatCompletionRequestUserMessageContentPart,
14    ChatCompletionTool, ChatCompletionTools, CustomToolChatCompletions, FunctionName, ImageUrl,
15    Role,
16};
17
18impl From<ChatCompletionRequestUserMessage> for ChatCompletionRequestMessage {
19    fn from(value: ChatCompletionRequestUserMessage) -> Self {
20        Self::User(value)
21    }
22}
23
24impl From<ChatCompletionRequestSystemMessage> for ChatCompletionRequestMessage {
25    fn from(value: ChatCompletionRequestSystemMessage) -> Self {
26        Self::System(value)
27    }
28}
29
30impl From<ChatCompletionRequestDeveloperMessage> for ChatCompletionRequestMessage {
31    fn from(value: ChatCompletionRequestDeveloperMessage) -> Self {
32        Self::Developer(value)
33    }
34}
35
36impl From<ChatCompletionRequestAssistantMessage> for ChatCompletionRequestMessage {
37    fn from(value: ChatCompletionRequestAssistantMessage) -> Self {
38        Self::Assistant(value)
39    }
40}
41
42impl From<ChatCompletionRequestFunctionMessage> for ChatCompletionRequestMessage {
43    fn from(value: ChatCompletionRequestFunctionMessage) -> Self {
44        Self::Function(value)
45    }
46}
47
48impl From<ChatCompletionRequestToolMessage> for ChatCompletionRequestMessage {
49    fn from(value: ChatCompletionRequestToolMessage) -> Self {
50        Self::Tool(value)
51    }
52}
53
54impl From<ChatCompletionRequestUserMessageContent> for ChatCompletionRequestUserMessage {
55    fn from(value: ChatCompletionRequestUserMessageContent) -> Self {
56        Self {
57            content: value,
58            name: None,
59        }
60    }
61}
62
63impl From<ChatCompletionRequestSystemMessageContent> for ChatCompletionRequestSystemMessage {
64    fn from(value: ChatCompletionRequestSystemMessageContent) -> Self {
65        Self {
66            content: value,
67            name: None,
68        }
69    }
70}
71
72impl From<ChatCompletionRequestDeveloperMessageContent> for ChatCompletionRequestDeveloperMessage {
73    fn from(value: ChatCompletionRequestDeveloperMessageContent) -> Self {
74        Self {
75            content: value,
76            name: None,
77        }
78    }
79}
80
81impl From<ChatCompletionRequestAssistantMessageContent> for ChatCompletionRequestAssistantMessage {
82    fn from(value: ChatCompletionRequestAssistantMessageContent) -> Self {
83        Self {
84            content: Some(value),
85            ..Default::default()
86        }
87    }
88}
89
90impl From<&str> for ChatCompletionRequestUserMessageContent {
91    fn from(value: &str) -> Self {
92        ChatCompletionRequestUserMessageContent::Text(value.into())
93    }
94}
95
96impl From<String> for ChatCompletionRequestUserMessageContent {
97    fn from(value: String) -> Self {
98        ChatCompletionRequestUserMessageContent::Text(value)
99    }
100}
101
102impl From<&str> for ChatCompletionRequestSystemMessageContent {
103    fn from(value: &str) -> Self {
104        ChatCompletionRequestSystemMessageContent::Text(value.into())
105    }
106}
107
108impl From<String> for ChatCompletionRequestSystemMessageContent {
109    fn from(value: String) -> Self {
110        ChatCompletionRequestSystemMessageContent::Text(value)
111    }
112}
113
114impl From<&str> for ChatCompletionRequestDeveloperMessageContent {
115    fn from(value: &str) -> Self {
116        ChatCompletionRequestDeveloperMessageContent::Text(value.into())
117    }
118}
119
120impl From<String> for ChatCompletionRequestDeveloperMessageContent {
121    fn from(value: String) -> Self {
122        ChatCompletionRequestDeveloperMessageContent::Text(value)
123    }
124}
125
126impl From<&str> for ChatCompletionRequestAssistantMessageContent {
127    fn from(value: &str) -> Self {
128        ChatCompletionRequestAssistantMessageContent::Text(value.into())
129    }
130}
131
132impl From<String> for ChatCompletionRequestAssistantMessageContent {
133    fn from(value: String) -> Self {
134        ChatCompletionRequestAssistantMessageContent::Text(value)
135    }
136}
137
138impl From<&str> for ChatCompletionRequestToolMessageContent {
139    fn from(value: &str) -> Self {
140        ChatCompletionRequestToolMessageContent::Text(value.into())
141    }
142}
143
144impl From<String> for ChatCompletionRequestToolMessageContent {
145    fn from(value: String) -> Self {
146        ChatCompletionRequestToolMessageContent::Text(value)
147    }
148}
149
150impl From<&str> for ChatCompletionRequestUserMessage {
151    fn from(value: &str) -> Self {
152        ChatCompletionRequestUserMessageContent::Text(value.into()).into()
153    }
154}
155
156impl From<String> for ChatCompletionRequestUserMessage {
157    fn from(value: String) -> Self {
158        value.as_str().into()
159    }
160}
161
162impl From<&str> for ChatCompletionRequestSystemMessage {
163    fn from(value: &str) -> Self {
164        ChatCompletionRequestSystemMessageContent::Text(value.into()).into()
165    }
166}
167
168impl From<&str> for ChatCompletionRequestDeveloperMessage {
169    fn from(value: &str) -> Self {
170        ChatCompletionRequestDeveloperMessageContent::Text(value.into()).into()
171    }
172}
173
174impl From<String> for ChatCompletionRequestSystemMessage {
175    fn from(value: String) -> Self {
176        value.as_str().into()
177    }
178}
179
180impl From<String> for ChatCompletionRequestDeveloperMessage {
181    fn from(value: String) -> Self {
182        value.as_str().into()
183    }
184}
185
186impl From<&str> for ChatCompletionRequestAssistantMessage {
187    fn from(value: &str) -> Self {
188        ChatCompletionRequestAssistantMessageContent::Text(value.into()).into()
189    }
190}
191
192impl From<String> for ChatCompletionRequestAssistantMessage {
193    fn from(value: String) -> Self {
194        value.as_str().into()
195    }
196}
197
198impl From<Vec<ChatCompletionRequestUserMessageContentPart>>
199    for ChatCompletionRequestUserMessageContent
200{
201    fn from(value: Vec<ChatCompletionRequestUserMessageContentPart>) -> Self {
202        ChatCompletionRequestUserMessageContent::Array(value)
203    }
204}
205
206impl From<ChatCompletionRequestMessageContentPartText>
207    for ChatCompletionRequestUserMessageContentPart
208{
209    fn from(value: ChatCompletionRequestMessageContentPartText) -> Self {
210        ChatCompletionRequestUserMessageContentPart::Text(value)
211    }
212}
213
214impl From<ChatCompletionRequestMessageContentPartImage>
215    for ChatCompletionRequestUserMessageContentPart
216{
217    fn from(value: ChatCompletionRequestMessageContentPartImage) -> Self {
218        ChatCompletionRequestUserMessageContentPart::ImageUrl(value)
219    }
220}
221
222impl From<ChatCompletionRequestMessageContentPartAudio>
223    for ChatCompletionRequestUserMessageContentPart
224{
225    fn from(value: ChatCompletionRequestMessageContentPartAudio) -> Self {
226        ChatCompletionRequestUserMessageContentPart::InputAudio(value)
227    }
228}
229
230impl From<&str> for ChatCompletionRequestMessageContentPartText {
231    fn from(value: &str) -> Self {
232        ChatCompletionRequestMessageContentPartText { text: value.into() }
233    }
234}
235
236impl From<String> for ChatCompletionRequestMessageContentPartText {
237    fn from(value: String) -> Self {
238        ChatCompletionRequestMessageContentPartText { text: value }
239    }
240}
241
242impl From<&str> for ChatCompletionFunctionCall {
243    fn from(value: &str) -> Self {
244        match value {
245            "auto" => Self::Auto,
246            "none" => Self::None,
247            _ => Self::Function { name: value.into() },
248        }
249    }
250}
251
252impl From<&str> for FunctionName {
253    fn from(value: &str) -> Self {
254        Self { name: value.into() }
255    }
256}
257
258impl From<String> for FunctionName {
259    fn from(value: String) -> Self {
260        Self { name: value }
261    }
262}
263
264impl From<&str> for ChatCompletionNamedToolChoice {
265    fn from(value: &str) -> Self {
266        Self {
267            function: value.into(),
268        }
269    }
270}
271
272impl From<String> for ChatCompletionNamedToolChoice {
273    fn from(value: String) -> Self {
274        Self {
275            function: value.into(),
276        }
277    }
278}
279
280impl Default for ChatCompletionRequestDeveloperMessageContent {
281    fn default() -> Self {
282        ChatCompletionRequestDeveloperMessageContent::Text("".into())
283    }
284}
285
286impl Default for ChatCompletionRequestSystemMessageContent {
287    fn default() -> Self {
288        ChatCompletionRequestSystemMessageContent::Text("".into())
289    }
290}
291
292impl Default for ChatCompletionRequestToolMessageContent {
293    fn default() -> Self {
294        ChatCompletionRequestToolMessageContent::Text("".into())
295    }
296}
297
298impl Default for ChatCompletionRequestUserMessageContent {
299    fn default() -> Self {
300        ChatCompletionRequestUserMessageContent::Text("".into())
301    }
302}
303
304impl From<&str> for ImageUrl {
305    fn from(value: &str) -> Self {
306        Self {
307            url: value.into(),
308            detail: Default::default(),
309        }
310    }
311}
312
313impl From<String> for ImageUrl {
314    fn from(value: String) -> Self {
315        Self {
316            url: value,
317            detail: Default::default(),
318        }
319    }
320}
321
322impl Display for Role {
323    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
324        write!(
325            f,
326            "{}",
327            match self {
328                Role::User => "user",
329                Role::System => "system",
330                Role::Assistant => "assistant",
331                Role::Function => "function",
332                Role::Tool => "tool",
333            }
334        )
335    }
336}
337
338impl From<ChatCompletionTool> for Vec<ChatCompletionTools> {
339    fn from(value: ChatCompletionTool) -> Self {
340        vec![ChatCompletionTools::Function(value)]
341    }
342}
343
344impl From<CustomToolChatCompletions> for Vec<ChatCompletionTools> {
345    fn from(value: CustomToolChatCompletions) -> Self {
346        vec![ChatCompletionTools::Custom(value)]
347    }
348}
349
350impl From<ChatCompletionRequestUserMessage> for Vec<ChatCompletionRequestMessage> {
351    fn from(value: ChatCompletionRequestUserMessage) -> Self {
352        vec![value.into()]
353    }
354}
355
356impl From<ChatCompletionRequestSystemMessage> for Vec<ChatCompletionRequestMessage> {
357    fn from(value: ChatCompletionRequestSystemMessage) -> Self {
358        vec![value.into()]
359    }
360}
361
362impl From<ChatCompletionRequestDeveloperMessage> for Vec<ChatCompletionRequestMessage> {
363    fn from(value: ChatCompletionRequestDeveloperMessage) -> Self {
364        vec![value.into()]
365    }
366}
367
368impl From<ChatCompletionRequestAssistantMessage> for Vec<ChatCompletionRequestMessage> {
369    fn from(value: ChatCompletionRequestAssistantMessage) -> Self {
370        vec![value.into()]
371    }
372}
373
374impl From<ChatCompletionRequestFunctionMessage> for Vec<ChatCompletionRequestMessage> {
375    fn from(value: ChatCompletionRequestFunctionMessage) -> Self {
376        vec![value.into()]
377    }
378}
379
380impl From<ChatCompletionRequestToolMessage> for Vec<ChatCompletionRequestMessage> {
381    fn from(value: ChatCompletionRequestToolMessage) -> Self {
382        vec![value.into()]
383    }
384}
385
386impl From<ChatCompletionMessageToolCall> for ChatCompletionMessageToolCalls {
387    fn from(value: ChatCompletionMessageToolCall) -> Self {
388        ChatCompletionMessageToolCalls::Function(value)
389    }
390}
391
392impl From<ChatCompletionMessageCustomToolCall> for ChatCompletionMessageToolCalls {
393    fn from(value: ChatCompletionMessageCustomToolCall) -> Self {
394        ChatCompletionMessageToolCalls::Custom(value)
395    }
396}
397
398impl From<ImageUrl> for ChatCompletionRequestMessageContentPartImage {
399    fn from(value: ImageUrl) -> Self {
400        ChatCompletionRequestMessageContentPartImage { image_url: value }
401    }
402}