async_llm/types/
user_content.rs

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
use serde::{Deserialize, Serialize};

use super::{ImageUrl, InputAudio};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum UserContent {
    Text(String),
    Array(Vec<UserContentPart>),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum UserContentPart {
    Text { text: String },
    ImageUrl { image_url: ImageUrl },
    Audio { input_audio: InputAudio },
}

impl UserContentPart {
    pub fn image(image_url: impl Into<ImageUrl>) -> Self {
        Self::ImageUrl {
            image_url: image_url.into(),
        }
    }

    pub fn text(text: impl Into<String>) -> Self {
        Self::Text { text: text.into() }
    }
}

impl Default for UserContent {
    fn default() -> Self {
        Self::Text("".into())
    }
}

impl From<&str> for UserContent {
    fn from(value: &str) -> Self {
        Self::Text(value.into())
    }
}

impl From<&str> for UserContentPart {
    fn from(value: &str) -> Self {
        Self::text(value)
    }
}

impl From<String> for UserContentPart {
    fn from(value: String) -> Self {
        Self::text(value)
    }
}

impl From<Vec<&str>> for UserContent {
    fn from(value: Vec<&str>) -> Self {
        Self::Array(
            value
                .into_iter()
                .map(|v| UserContentPart::Text {
                    text: v.to_string(),
                })
                .collect(),
        )
    }
}