pub struct UnifiedMessage {
pub role: MessageRole,
pub content: MessageContent,
pub attributes: MessageAttributes,
pub timestamp: DateTime<Utc>,
}Expand description
A provider-agnostic message for LLM interactions.
This is the core type of multi-llm. UnifiedMessage works across all supported
providers (OpenAI, Anthropic, Ollama, LM Studio) and provides built-in support
for caching hints and priority-based ordering.
§Creating Messages
Use the convenience constructors for common cases:
use multi_llm::UnifiedMessage;
// Simple messages
let user = UnifiedMessage::user("What's the weather?");
let system = UnifiedMessage::system("You are a helpful assistant.");
let assistant = UnifiedMessage::assistant("The weather is sunny.");
// Semantic messages with caching defaults
let instruction = UnifiedMessage::system_instruction(
"You are a weather bot.".to_string(),
Some("weather-system-v1".to_string())
);§Caching
For Anthropic prompt caching (90% cost savings), use the builder methods:
use multi_llm::UnifiedMessage;
// 5-minute cache (good for development/testing)
let cached = UnifiedMessage::system("Large context...")
.with_ephemeral_cache();
// 1-hour cache (good for production)
let long_cached = UnifiedMessage::system("Large context...")
.with_extended_cache();§Tool Calling
For function calling workflows:
use multi_llm::UnifiedMessage;
// Assistant requests a tool call
let tool_request = UnifiedMessage::tool_call(
"call_abc123".to_string(),
"get_weather".to_string(),
serde_json::json!({"city": "London"})
);
// Send the tool result back
let tool_response = UnifiedMessage::tool_result(
"call_abc123".to_string(),
"Sunny, 22°C".to_string(),
false // not an error
);Fields§
§role: MessageRoleRole of this message (system, user, assistant, or tool).
content: MessageContentContent of this message.
attributes: MessageAttributesAttributes controlling caching, priority, and metadata.
timestamp: DateTime<Utc>Timestamp for secondary ordering (after priority).
When messages have equal priority, they’re sorted by timestamp.
Implementations§
Source§impl UnifiedMessage
impl UnifiedMessage
Sourcepub fn new(role: MessageRole, content: MessageContent) -> Self
pub fn new(role: MessageRole, content: MessageContent) -> Self
Create a new message with default attributes
Sourcepub fn with_attributes(
role: MessageRole,
content: MessageContent,
attributes: MessageAttributes,
) -> Self
pub fn with_attributes( role: MessageRole, content: MessageContent, attributes: MessageAttributes, ) -> Self
Create a new message with custom attributes
Sourcepub fn system_instruction(content: String, cache_key: Option<String>) -> Self
pub fn system_instruction(content: String, cache_key: Option<String>) -> Self
Create a system instruction message (cacheable, high priority)
Sourcepub fn tool_definition(content: String, cache_key: Option<String>) -> Self
pub fn tool_definition(content: String, cache_key: Option<String>) -> Self
Create a tool definition message (cacheable, high priority)
Sourcepub fn context(content: String, cache_key: Option<String>) -> Self
pub fn context(content: String, cache_key: Option<String>) -> Self
Create a context message (cacheable, medium priority)
Sourcepub fn history(role: MessageRole, content: String) -> Self
pub fn history(role: MessageRole, content: String) -> Self
Create a history message (cacheable, lower priority)
Sourcepub fn current_user(content: String) -> Self
pub fn current_user(content: String) -> Self
Create a current user message (not cacheable, lowest priority)
Sourcepub fn tool_call(id: String, name: String, arguments: Value) -> Self
pub fn tool_call(id: String, name: String, arguments: Value) -> Self
Create a tool call message
Sourcepub fn tool_result(
tool_call_id: String,
content: String,
is_error: bool,
) -> Self
pub fn tool_result( tool_call_id: String, content: String, is_error: bool, ) -> Self
Create a tool result message
Sourcepub fn simple(role: MessageRole, content: impl Into<String>) -> Self
pub fn simple(role: MessageRole, content: impl Into<String>) -> Self
Create a simple text message
Sourcepub fn with_ephemeral_cache(self) -> Self
pub fn with_ephemeral_cache(self) -> Self
Mark this message for ephemeral caching (5-minute TTL)
Sourcepub fn with_extended_cache(self) -> Self
pub fn with_extended_cache(self) -> Self
Mark this message for extended caching (1-hour TTL)
Trait Implementations§
Source§impl Clone for UnifiedMessage
impl Clone for UnifiedMessage
Source§fn clone(&self) -> UnifiedMessage
fn clone(&self) -> UnifiedMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more