Class: Langchain::Assistant::Messages::AnthropicMessage

Inherits:
Base
  • Object
show all
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

Methods inherited from Base

#image, #standard_role, #user?

Constructor Details

#initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ AnthropicMessage

Initialize a new Anthropic message

Parameters:

  • role (String)

    The role of the message

  • content (String) (defaults to: nil)

    The content of the message

  • tool_calls (Array<Hash>) (defaults to: [])

    The tool calls made in the message

  • tool_call_id (String) (defaults to: nil)

    The ID of the tool call

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 21

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) }

  @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

Returns:

  • (Boolean)

    true/false whether this message was produced by an LLM



136
137
138
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 136

def assistant?
  role == "assistant"
end

#assistant_hashHash

Convert the message to an Anthropic API-compatible hash

Returns:

  • (Hash)

    The message as an Anthropic API-compatible hash, with the role as “assistant”



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 55

def assistant_hash
  content_array = []
  if content && !content.empty?
    content_array << {type: "text", text: content}
  end

  {
    role: "assistant",
    content: content_array.concat(tool_calls)
  }
end

#build_content_arrayArray<Hash>

Builds the content value for the message hash

Returns:

  • (Array<Hash>)

    An array of content hashes



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 97

def build_content_array
  content_details = []

  if content && !content.empty?
    content_details << {
      type: "text",
      text: content
    }
  end

  if image
    content_details << {
      type: "image",
      source: {
        type: "base64",
        data: image.base64,
        media_type: image.mime_type
      }
    }
  end

  content_details
end

#llm?Boolean

Check if the message came from an LLM

Returns:

  • (Boolean)

    true/false whether this message was produced by an LLM



143
144
145
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 143

def llm?
  assistant?
end

#system?Boolean

Anthropic does not implement system prompts

Returns:

  • (Boolean)


129
130
131
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 129

def system?
  false
end

#to_hashHash

Convert the message to an Anthropic API-compatible hash

Returns:

  • (Hash)

    The message as an Anthropic API-compatible hash



42
43
44
45
46
47
48
49
50
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 42

def to_hash
  if assistant?
    assistant_hash
  elsif tool?
    tool_hash
  elsif user?
    user_hash
  end
end

#tool?Boolean

Check if the message is a tool call

Returns:

  • (Boolean)

    true/false whether this message is a tool call



124
125
126
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 124

def tool?
  role == "tool_result"
end

#tool_hashHash

Convert the message to an Anthropic API-compatible hash

Returns:

  • (Hash)

    The message as an Anthropic API-compatible hash, with the role as “user”



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 70

def tool_hash
  {
    role: "user",
    # TODO: Tool can also return images
    # https://docs.anthropic.com/en/docs/build-with-claude/tool-use#handling-tool-use-and-tool-result-content-blocks
    content: [
      {
        type: "tool_result",
        tool_use_id: tool_call_id,
        content: content
      }
    ]
  }
end

#user_hashHash

Convert the message to an Anthropic API-compatible hash

Returns:

  • (Hash)

    The message as an Anthropic API-compatible hash, with the role as “user”



88
89
90
91
92
93
# File 'lib/langchain/assistant/messages/anthropic_message.rb', line 88

def user_hash
  {
    role: "user",
    content: build_content_array
  }
end