Class: Langchain::Assistant::Messages::OllamaMessage
- Defined in:
- lib/langchain/assistant/messages/ollama_message.rb
Constant Summary collapse
- ROLES =
OpenAI 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.
-
#initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ OllamaMessage
constructor
Initialize a new OpenAI message.
-
#llm? ⇒ Boolean
Check if the message came from an LLM.
-
#system? ⇒ Boolean
Check if the message are system instructions.
-
#to_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash.
-
#tool? ⇒ Boolean
Check if the message is a tool call.
Methods inherited from Base
#image, #standard_role, #user?
Constructor Details
#initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ OllamaMessage
Initialize a new OpenAI message
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/langchain/assistant/messages/ollama_message.rb', line 24 def initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) 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) } raise ArgumentError, "image_url must be a valid url" if image_url && !URI::DEFAULT_PARSER.make_regexp.match?(image_url) @role = role # Some Tools return content as a JSON hence `.to_s` @content = content.to_s @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
60 61 62 |
# File 'lib/langchain/assistant/messages/ollama_message.rb', line 60 def assistant? role == "assistant" end |
#llm? ⇒ Boolean
Check if the message came from an LLM
53 54 55 |
# File 'lib/langchain/assistant/messages/ollama_message.rb', line 53 def llm? assistant? end |
#system? ⇒ Boolean
Check if the message are system instructions
67 68 69 |
# File 'lib/langchain/assistant/messages/ollama_message.rb', line 67 def system? role == "system" end |
#to_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash
40 41 42 43 44 45 46 47 48 |
# File 'lib/langchain/assistant/messages/ollama_message.rb', line 40 def to_hash {}.tap do |h| h[:role] = role h[:content] = content if content # Content is nil for tool calls h[:images] = [image.base64] if image h[:tool_calls] = tool_calls if tool_calls.any? h[:tool_call_id] = tool_call_id if tool_call_id end end |
#tool? ⇒ Boolean
Check if the message is a tool call
74 75 76 |
# File 'lib/langchain/assistant/messages/ollama_message.rb', line 74 def tool? role == "tool" end |