Class: Langchain::Assistant::Messages::AnthropicMessage
- Defined in:
- lib/langchain/assistant/messages/anthropic_message.rb
Constant Summary collapse
- ROLES =
[ "assistant", "user", "tool_result" ].freeze
- TOOL_ROLE =
"tool_result"
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, tool_calls: [], tool_call_id: nil) ⇒ AnthropicMessage
constructor
Initialize a new Anthropic message.
-
#llm? ⇒ Boolean
Check if the message came from an LLM.
-
#system? ⇒ Boolean
Anthropic does not implement system prompts.
-
#to_hash ⇒ Hash
Convert the message to an Anthropic API-compatible hash.
-
#tool? ⇒ Boolean
Check if the message is a tool call.
Methods inherited from Base
Constructor Details
#initialize(role:, content: nil, tool_calls: [], tool_call_id: nil) ⇒ AnthropicMessage
Initialize a new Anthropic message
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 21 def initialize(role:, content: 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) } @role = role # Some Tools return content as a JSON hence `.to_s` @content = content.to_s @tool_calls = tool_calls @tool_call_id = tool_call_id end |
Instance Method Details
#assistant? ⇒ Boolean
Check if the message came from an LLM
70 71 72 |
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 70 def assistant? role == "assistant" end |
#llm? ⇒ Boolean
Check if the message came from an LLM
77 78 79 |
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 77 def llm? assistant? end |
#system? ⇒ Boolean
Anthropic does not implement system prompts
63 64 65 |
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 63 def system? false end |
#to_hash ⇒ Hash
Convert the message to an Anthropic API-compatible hash
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 35 def to_hash {}.tap do |h| h[:role] = tool? ? "user" : role h[:content] = if tool? [ { type: "tool_result", tool_use_id: tool_call_id, content: content } ] elsif tool_calls.any? tool_calls else content end end end |
#tool? ⇒ Boolean
Check if the message is a tool call
58 59 60 |
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 58 def tool? role == "tool_result" end |