llm_cascade/models/
conversation.rs1use serde::{Deserialize, Serialize};
4
5use crate::models::ToolDefinition;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "lowercase")]
10pub enum MessageRole {
11 System,
13 User,
15 Assistant,
17 Tool,
19}
20
21impl std::fmt::Display for MessageRole {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 match self {
24 MessageRole::System => write!(f, "system"),
25 MessageRole::User => write!(f, "user"),
26 MessageRole::Assistant => write!(f, "assistant"),
27 MessageRole::Tool => write!(f, "tool"),
28 }
29 }
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct Message {
35 pub role: MessageRole,
37 pub content: String,
39 #[serde(skip_serializing_if = "Option::is_none")]
41 pub tool_call_id: Option<String>,
42}
43
44impl Message {
45 pub fn system(content: impl Into<String>) -> Self {
47 Self {
48 role: MessageRole::System,
49 content: content.into(),
50 tool_call_id: None,
51 }
52 }
53
54 pub fn user(content: impl Into<String>) -> Self {
56 Self {
57 role: MessageRole::User,
58 content: content.into(),
59 tool_call_id: None,
60 }
61 }
62
63 pub fn assistant(content: impl Into<String>) -> Self {
65 Self {
66 role: MessageRole::Assistant,
67 content: content.into(),
68 tool_call_id: None,
69 }
70 }
71
72 pub fn tool(content: impl Into<String>, tool_call_id: impl Into<String>) -> Self {
74 Self {
75 role: MessageRole::Tool,
76 content: content.into(),
77 tool_call_id: Some(tool_call_id.into()),
78 }
79 }
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct Conversation {
85 pub messages: Vec<Message>,
87 #[serde(skip_serializing_if = "Option::is_none")]
89 pub tools: Option<Vec<ToolDefinition>>,
90}
91
92impl Conversation {
93 pub fn new(messages: Vec<Message>) -> Self {
95 Self {
96 messages,
97 tools: None,
98 }
99 }
100
101 pub fn with_tools(mut self, tools: Vec<ToolDefinition>) -> Self {
103 self.tools = Some(tools);
104 self
105 }
106
107 pub fn single_user_prompt(prompt: impl Into<String>) -> Self {
109 Self {
110 messages: vec![Message::user(prompt.into())],
111 tools: None,
112 }
113 }
114}