Class: Langchain::Assistant::LLM::Adapters::Anthropic
- Defined in:
- lib/langchain/assistant/llm/adapters/anthropic.rb
Instance Method Summary collapse
-
#allowed_tool_choices ⇒ Object
Get the allowed assistant.tool_choice values for Anthropic.
-
#available_tool_names(tools) ⇒ Array<String>
Get the available tool function names for Anthropic.
-
#build_chat_params(messages:, instructions:, tools:, tool_choice:, parallel_tool_calls:) ⇒ Hash
Build the chat parameters for the Anthropic API.
-
#build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ Messages::AnthropicMessage
Build an Anthropic message.
-
#build_tools(tools) ⇒ Object
Build the tools for the Anthropic API.
-
#extract_tool_call_args(tool_call:) ⇒ Array
Extract the tool call information from the Anthropic tool call hash.
- #support_system_message? ⇒ Boolean
- #tool_role ⇒ Object
Instance Method Details
#allowed_tool_choices ⇒ Object
Get the allowed assistant.tool_choice values for Anthropic
64 65 66 |
# File 'lib/langchain/assistant/llm/adapters/anthropic.rb', line 64 def allowed_tool_choices ["auto", "any"] end |
#available_tool_names(tools) ⇒ Array<String>
Get the available tool function names for Anthropic
72 73 74 |
# File 'lib/langchain/assistant/llm/adapters/anthropic.rb', line 72 def available_tool_names(tools) build_tools(tools).map { |tool| tool.dig(:name) } end |
#build_chat_params(messages:, instructions:, tools:, tool_choice:, parallel_tool_calls:) ⇒ Hash
Build the chat parameters for the Anthropic API
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/langchain/assistant/llm/adapters/anthropic.rb', line 16 def build_chat_params( messages:, instructions:, tools:, tool_choice:, parallel_tool_calls: ) params = {messages: } if tools.any? params[:tools] = build_tools(tools) params[:tool_choice] = build_tool_choice(tool_choice, parallel_tool_calls) end params[:system] = instructions if instructions params end |
#build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ Messages::AnthropicMessage
Build an Anthropic message
40 41 42 43 44 |
# File 'lib/langchain/assistant/llm/adapters/anthropic.rb', line 40 def (role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) Langchain.logger.warn "WARNING: Image URL is not supported by Anthropic currently" if image_url Messages::AnthropicMessage.new(role: role, content: content, tool_calls: tool_calls, tool_call_id: tool_call_id) end |
#build_tools(tools) ⇒ Object
Build the tools for the Anthropic API
59 60 61 |
# File 'lib/langchain/assistant/llm/adapters/anthropic.rb', line 59 def build_tools(tools) tools.map { |tool| tool.class.function_schemas.to_anthropic_format }.flatten end |
#extract_tool_call_args(tool_call:) ⇒ Array
Extract the tool call information from the Anthropic tool call hash
50 51 52 53 54 55 56 |
# File 'lib/langchain/assistant/llm/adapters/anthropic.rb', line 50 def extract_tool_call_args(tool_call:) tool_call_id = tool_call.dig("id") function_name = tool_call.dig("name") tool_name, method_name = function_name.split("__") tool_arguments = tool_call.dig("input").transform_keys(&:to_sym) [tool_call_id, tool_name, method_name, tool_arguments] end |
#support_system_message? ⇒ Boolean
80 81 82 |
# File 'lib/langchain/assistant/llm/adapters/anthropic.rb', line 80 def Messages::AnthropicMessage::ROLES.include?("system") end |
#tool_role ⇒ Object
76 77 78 |
# File 'lib/langchain/assistant/llm/adapters/anthropic.rb', line 76 def tool_role Messages::AnthropicMessage::TOOL_ROLE end |