Class: Langchain::Assistant::Messages::MistralAIMessage
- Defined in:
- lib/langchain/assistant/messages/mistral_ai_message.rb
Constant Summary collapse
- ROLES =
MistralAI uses the following roles:
[ "system", "assistant", "user", "tool" ].freeze
- TOOL_ROLE =
"tool"
Instance Attribute Summary
Attributes inherited from Base
#content, #image_url, #role, #tool_call_id, #tool_calls
Instance Method Summary collapse
-
#assistant? ⇒ Boolean
Check if the message came from an LLM.
-
#assistant_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash.
-
#build_content_array ⇒ Array<Hash>
Builds the content value for the message hash.
-
#initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ MistralAIMessage
constructor
Initialize a new MistralAI message.
-
#llm? ⇒ Boolean
Check if the message came from an LLM.
-
#system? ⇒ Boolean
Check if the message are system instructions.
-
#system_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash.
-
#to_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash.
-
#tool? ⇒ Boolean
Check if the message is a tool call.
-
#tool_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash.
-
#user_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash.
Methods inherited from Base
Constructor Details
#initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ MistralAIMessage
Initialize a new MistralAI message
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 24 def initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) # TODO: Implement image_file: reference (https://platform.openai.com/docs/api-reference/messages/object#messages/object-content) raise ArgumentError, "Role must be one of #{ROLES.join(", ")}" unless ROLES.include?(role) raise ArgumentError, "Tool calls must be an array of hashes" unless tool_calls.is_a?(Array) && tool_calls.all? { |tool_call| tool_call.is_a?(Hash) } @role = role # Some Tools return content as a JSON hence `.to_s` @content = content.to_s # Make sure you're using the Pixtral model if you want to send image_url @image_url = image_url @tool_calls = tool_calls @tool_call_id = tool_call_id end |
Instance Method Details
#assistant? ⇒ Boolean
Check if the message came from an LLM
62 63 64 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 62 def assistant? role == "assistant" end |
#assistant_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash
82 83 84 85 86 87 88 89 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 82 def assistant_hash { role: "assistant", content: content, tool_calls: tool_calls, prefix: false } end |
#build_content_array ⇒ Array<Hash>
Builds the content value for the message hash
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 121 def build_content_array content_details = [] if content && !content.empty? content_details << { type: "text", text: content } end if image_url content_details << { type: "image_url", image_url: image_url } end content_details end |
#llm? ⇒ Boolean
Check if the message came from an LLM
40 41 42 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 40 def llm? assistant? end |
#system? ⇒ Boolean
Check if the message are system instructions
69 70 71 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 69 def system? role == "system" end |
#system_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash
93 94 95 96 97 98 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 93 def system_hash { role: "system", content: build_content_array } end |
#to_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 47 def to_hash if assistant? assistant_hash elsif system? system_hash elsif tool? tool_hash elsif user? user_hash end end |
#tool? ⇒ Boolean
Check if the message is a tool call
76 77 78 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 76 def tool? role == "tool" end |
#tool_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash
102 103 104 105 106 107 108 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 102 def tool_hash { role: "tool", content: content, tool_call_id: tool_call_id } end |
#user_hash ⇒ Hash
Convert the message to an MistralAI API-compatible hash
112 113 114 115 116 117 |
# File 'lib/langchain/assistant/messages/mistral_ai_message.rb', line 112 def user_hash { role: "user", content: build_content_array } end |