1use serde::{Deserialize, Serialize};
2use serde_json::Value as JsonValue;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
6pub enum Content {
7 #[serde(rename = "text")]
8 Text(String),
9 #[serde(rename = "json")]
11 Json(JsonValue),
12 #[serde(rename = "image")]
14 Image {
15 url: Option<String>,
16 mime: Option<String>,
17 name: Option<String>,
18 },
19 #[serde(rename = "audio")]
21 Audio {
22 url: Option<String>,
23 mime: Option<String>,
24 },
25}
26
27impl Content {
28 pub fn as_text(&self) -> String {
30 match self {
31 Content::Text(s) => s.clone(),
32 Content::Json(v) => v.to_string(),
33 Content::Image { url, .. } => url.clone().unwrap_or_default(),
34 Content::Audio { url, .. } => url.clone().unwrap_or_default(),
35 }
36 }
37
38 pub fn new_text<S: Into<String>>(s: S) -> Self {
40 Content::Text(s.into())
41 }
42
43 pub fn new_json(v: JsonValue) -> Self {
45 Content::Json(v)
46 }
47
48 pub fn new_image(url: Option<String>, mime: Option<String>, name: Option<String>) -> Self {
50 Content::Image { url, mime, name }
51 }
52
53 pub fn new_audio(url: Option<String>, mime: Option<String>) -> Self {
55 Content::Audio { url, mime }
56 }
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct Message {
61 pub role: Role,
62 pub content: Content,
63 pub function_call: Option<crate::types::function_call::FunctionCall>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub enum Role {
69 #[serde(rename = "system")]
70 System,
71 #[serde(rename = "user")]
72 User,
73 #[serde(rename = "assistant")]
74 Assistant,
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct Choice {
79 pub index: u32,
80 pub message: Message,
81 pub finish_reason: Option<String>,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct Usage {
86 pub prompt_tokens: u32,
87 pub completion_tokens: u32,
88 pub total_tokens: u32,
89}