71 lines
1.7 KiB
Rust
71 lines
1.7 KiB
Rust
use serde::{Deserialize, Serialize};
|
|||
|
|
|
||
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||
|
|
pub enum Role {
|
||
|
|
#[serde(rename = "user")]
|
||
|
|
User,
|
||
|
|
#[serde(rename = "assistant")]
|
||
|
|
Assistant,
|
||
|
|
#[serde(rename = "system")]
|
||
|
|
System,
|
||
|
|
#[serde(rename = "tool")]
|
||
|
|
Tool,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Role {
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
|
|
pub struct ChatMessage {
|
||
|
|
pub role: Role,
|
||
|
|
pub content: Option<String>,
|
||
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||
|
|
pub tool_calls: Option<Vec<super::tool::ToolCall>>,
|
||
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||
|
|
pub tool_call_id: Option<String>,
|
||
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||
|
|
pub name: Option<String>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl ChatMessage {
|
||
|
|
pub fn user(content: impl Into<String>) -> Self {
|
||
|
|
ChatMessage {
|
||
|
|
role: Role::User,
|
||
|
|
content: Some(content.into()),
|
||
|
|
tool_calls: None,
|
||
|
|
tool_call_id: None,
|
||
|
|
name: None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn assistant(content: Option<String>) -> Self {
|
||
|
|
ChatMessage {
|
||
|
|
role: Role::Assistant,
|
||
|
|
content,
|
||
|
|
tool_calls: None,
|
||
|
|
tool_call_id: None,
|
||
|
|
name: None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn system(content: impl Into<String>) -> Self {
|
||
|
|
ChatMessage {
|
||
|
|
role: Role::System,
|
||
|
|
content: Some(content.into()),
|
||
|
|
tool_calls: None,
|
||
|
|
tool_call_id: None,
|
||
|
|
name: None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn tool_result(tool_call_id: String, content: String) -> Self {
|
||
|
|
ChatMessage {
|
||
|
|
role: Role::Tool,
|
||
|
|
content: Some(content),
|
||
|
|
tool_calls: None,
|
||
|
|
tool_call_id: Some(tool_call_id),
|
||
|
|
name: None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|