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
use serde::{Deserialize, Serialize};
use super::base::{BaseMessageFields, MessageContent};
use super::multimodal::ContentPart;
/// A message from a human user.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct HumanMessage {
#[serde(flatten)]
pub base: BaseMessageFields,
}
impl HumanMessage {
/// Create a text-only human message.
pub fn new(content: impl Into<String>) -> Self {
Self {
base: BaseMessageFields::new(MessageContent::Text(content.into())),
}
}
/// Create a human message from content blocks.
pub fn with_blocks(blocks: Vec<super::content::ContentBlock>) -> Self {
Self {
base: BaseMessageFields::new(MessageContent::Blocks(blocks)),
}
}
/// Create a multi-modal human message from content parts.
///
/// # Example
///
/// ```
/// use cognis_core::messages::{HumanMessage, ContentPart};
///
/// let msg = HumanMessage::from_parts(vec![
/// ContentPart::text("What is in this image?"),
/// ContentPart::image_url("https://example.com/photo.jpg"),
/// ]);
/// assert!(msg.base.content.is_multimodal());
/// ```
pub fn from_parts(parts: Vec<ContentPart>) -> Self {
Self {
base: BaseMessageFields::new(MessageContent::from_parts(parts)),
}
}
/// Create a human message with text and an image URL.
pub fn with_image_url(text: impl Into<String>, url: impl Into<String>) -> Self {
Self {
base: BaseMessageFields::new(MessageContent::with_image_url(text, url)),
}
}
/// Create a human message with text and a base64-encoded image.
pub fn with_image_base64(
text: impl Into<String>,
base64: impl Into<String>,
mime_type: impl Into<String>,
) -> Self {
Self {
base: BaseMessageFields::new(MessageContent::with_image_base64(
text, base64, mime_type,
)),
}
}
}