#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub role: MessageRole,
pub content: Vec<ContentBlock>,
}
impl Message {
pub fn new(role: MessageRole, content: Vec<ContentBlock>) -> Self {
Self { role, content }
}
pub fn user(text: impl Into<String>) -> Self {
Self {
role: MessageRole::User,
content: vec![ContentBlock::Text(TextBlock::new(text))],
}
}
pub fn assistant(content: Vec<ContentBlock>) -> Self {
Self {
role: MessageRole::Assistant,
content,
}
}
pub fn system(text: impl Into<String>) -> Self {
Self {
role: MessageRole::System,
content: vec![ContentBlock::Text(TextBlock::new(text))],
}
}
pub fn user_with_blocks(content: Vec<ContentBlock>) -> Self {
Self {
role: MessageRole::User,
content,
}
}
pub fn user_with_image(
text: impl Into<String>,
image_url: impl Into<String>,
) -> crate::Result<Self> {
Ok(Self {
role: MessageRole::User,
content: vec![
ContentBlock::Text(TextBlock::new(text)),
ContentBlock::Image(ImageBlock::from_url(image_url)?),
],
})
}
pub fn user_with_image_detail(
text: impl Into<String>,
image_url: impl Into<String>,
detail: ImageDetail,
) -> crate::Result<Self> {
Ok(Self {
role: MessageRole::User,
content: vec![
ContentBlock::Text(TextBlock::new(text)),
ContentBlock::Image(ImageBlock::from_url(image_url)?.with_detail(detail)),
],
})
}
pub fn user_with_base64_image(
text: impl Into<String>,
base64_data: impl AsRef<str>,
mime_type: impl AsRef<str>,
) -> crate::Result<Self> {
Ok(Self {
role: MessageRole::User,
content: vec![
ContentBlock::Text(TextBlock::new(text)),
ContentBlock::Image(ImageBlock::from_base64(base64_data, mime_type)?),
],
})
}
}