api_ollama/
messages.rs

1//! Message types for chat conversations.
2//!
3//! Provides message structures for Ollama chat API, including support for
4//! vision-enabled models and tool calling functionality.
5
6#[ cfg( feature = "enabled" ) ]
7mod private
8{
9  use serde::{ Serialize, Deserialize };
10  use core::hash::{ Hash, Hasher };
11
12  /// Message in chat conversation
13  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
14  #[ cfg_attr( feature = "request_caching", derive( Hash ) ) ]
15  pub struct Message
16  {
17    /// Role of the message sender (e.g., "user", "assistant")
18    pub role : String,
19    /// Content of the message
20    pub content : String,
21  }
22
23  /// Message roles for vision-enabled chat
24  #[ cfg( feature = "vision_support" ) ]
25  #[ derive( Debug, Clone, Serialize, Deserialize, Default, PartialEq ) ]
26  #[ cfg_attr( feature = "request_caching", derive( Hash ) ) ]
27  #[ serde( rename_all = "lowercase" ) ]
28  pub enum MessageRole
29  {
30    /// User message
31    #[ default ]
32    User,
33    /// Assistant message
34    Assistant,
35    /// System message
36    System,
37    /// Tool response message
38    #[ cfg( feature = "tool_calling" ) ]
39    Tool,
40  }
41
42  /// Enhanced message with vision support
43  #[ cfg( feature = "vision_support" ) ]
44  #[ derive( Debug, Clone, Serialize, Deserialize, Default ) ]
45  #[ cfg_attr( feature = "request_caching", derive( Hash ) ) ]
46  pub struct ChatMessage
47  {
48    /// Role of the message sender
49    pub role : MessageRole,
50    /// Content of the message
51    pub content : String,
52    /// Optional base64-encoded images for vision models
53    #[ serde( skip_serializing_if = "Option::is_none" ) ]
54    pub images : Option< Vec< String > >,
55    /// Optional tool calls made by the assistant
56    #[ cfg( feature = "tool_calling" ) ]
57    #[ serde( skip_serializing_if = "Option::is_none" ) ]
58    pub tool_calls : Option< Vec< ToolCall > >,
59  }
60
61  /// Tool definition for function calling
62  #[ cfg( feature = "tool_calling" ) ]
63  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
64  pub struct ToolDefinition
65  {
66    /// Name of the tool/function
67    pub name : String,
68    /// Description of what the tool does
69    pub description : String,
70    /// JSON schema defining the function parameters
71    pub parameters : serde_json::Value,
72  }
73
74  #[ cfg( all( feature = "tool_calling", feature = "request_caching" ) ) ]
75  impl Hash for ToolDefinition
76  {
77    #[ inline ]
78    fn hash< H : Hasher >( &self, state : &mut H )
79    {
80      self.name.hash( state );
81      self.description.hash( state );
82      self.parameters.to_string().hash( state );
83    }
84  }
85
86  /// Tool call information
87  #[ cfg( feature = "tool_calling" ) ]
88  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
89  pub struct ToolCall
90  {
91    /// Unique identifier for this tool call
92    pub id : String,
93    /// Function call details
94    pub function : serde_json::Value,
95  }
96
97  #[ cfg( all( feature = "tool_calling", feature = "request_caching" ) ) ]
98  impl Hash for ToolCall
99  {
100    #[ inline ]
101    fn hash< H : Hasher >( &self, state : &mut H )
102    {
103      self.id.hash( state );
104      self.function.to_string().hash( state );
105    }
106  }
107
108  /// Tool message for function responses
109  #[ cfg( feature = "tool_calling" ) ]
110  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
111  #[ cfg_attr( feature = "request_caching", derive( Hash ) ) ]
112  pub struct ToolMessage
113  {
114    /// Role should be "tool"
115    pub role : MessageRole,
116    /// Result content from tool execution
117    pub content : String,
118    /// ID linking this response to the tool call
119    pub tool_call_id : String,
120  }
121}
122
123#[ cfg( feature = "enabled" ) ]
124crate ::mod_interface!
125{
126  exposed use
127  {
128    Message,
129  };
130
131  #[ cfg( feature = "vision_support" ) ]
132  exposed use
133  {
134    MessageRole,
135    ChatMessage,
136  };
137
138  #[ cfg( feature = "tool_calling" ) ]
139  exposed use
140  {
141    ToolDefinition,
142    ToolCall,
143    ToolMessage,
144  };
145}