Module: LLM::AnthropicMethods
- Included in:
- Anthropic
- Defined in:
- lib/scout/llm/backends/anthropic.rb
Instance Method Summary collapse
- #client(options, messages = nil) ⇒ Object
- #embed_query(client, text, options = {}) ⇒ Object
- #extra_options(options, messages = nil) ⇒ Object
- #format_tool_call(message) ⇒ Object
- #format_tool_definitions(tools) ⇒ Object
- #format_tool_output(message, last_id = nil) ⇒ Object
- #parse_tool_call(info) ⇒ Object
- #process_response(messages, response, tools, options, &block) ⇒ Object
- #query(client, messages, tools = [], parameters = {}) ⇒ Object
Instance Method Details
#client(options, messages = nil) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/scout/llm/backends/anthropic.rb', line 20 def client(, = nil) url, key = IndiferentHash. , :url, :key Object::Anthropic::Client.new(access_token: key) end |
#embed_query(client, text, options = {}) ⇒ Object
93 94 95 |
# File 'lib/scout/llm/backends/anthropic.rb', line 93 def (client, text, = {}) raise 'Anthropic does not offer embeddings' end |
#extra_options(options, messages = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/scout/llm/backends/anthropic.rb', line 7 def (, = nil) format, max_tokens = IndiferentHash. , :format, :max_tokens, max_tokens: 1000 [:max_tokens] = max_tokens case format.to_sym when :json, :json_object [:response_format] = { type: 'json_object' } else [:response_format] = { type: format } end if format end |
#format_tool_call(message) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/scout/llm/backends/anthropic.rb', line 44 def format_tool_call() tool_call = IndiferentHash.setup(JSON.parse([:content])) arguments = tool_call.delete('arguments') || tool_call[:function].delete('arguments') || '{}' arguments = JSON.parse arguments if String === arguments name = tool_call[:name] id = tool_call.delete('call_id') || tool_call.delete('id') || tool_call.delete('tool_use_id') tool_call['id'] = id tool_call['type'] = 'tool_use' tool_call['name'] ||= name tool_call['input'] = arguments tool_call.delete :function { role: 'assistant', content: [tool_call] } end |
#format_tool_definitions(tools) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/scout/llm/backends/anthropic.rb', line 33 def format_tool_definitions(tools) tools.values.collect do |obj, info| info = obj if Hash === obj IndiferentHash.setup(info) info[:type] = 'custom' if info[:type] == 'function' info[:input_schema] = info.delete('parameters') if info['parameters'] info[:description] ||= '' info end end |
#format_tool_output(message, last_id = nil) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/scout/llm/backends/anthropic.rb', line 58 def format_tool_output(, last_id = nil) info = JSON.parse([:content]) id = info.delete('call_id') || info.delete('id') || info.delete('tool_use_id') || info[:function].delete('id') tool_output = { type: 'tool_result', tool_use_id: id, content: info[:content] } { role: 'user', content: [tool_output] } end |
#parse_tool_call(info) ⇒ Object
65 66 67 68 |
# File 'lib/scout/llm/backends/anthropic.rb', line 65 def parse_tool_call(info) arguments, id, name = IndiferentHash. info, :input, :id, :name { arguments: arguments, id: id, name: name } end |
#process_response(messages, response, tools, options, &block) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/scout/llm/backends/anthropic.rb', line 70 def process_response(, response, tools, , &block) IndiferentHash.setup response response[:content].collect do |output| IndiferentHash.setup output case output[:type].to_s when 'text' IndiferentHash.setup({ role: :assistant, content: output[:text] }) when 'reasoning' next when 'tool_use' tool_call = parse_tool_call(output) LLM.process_calls(tools, [tool_call], &block) when 'web_search_call' next else eee response eee output raise end end.compact.flatten end |
#query(client, messages, tools = [], parameters = {}) ⇒ Object
27 28 29 30 31 |
# File 'lib/scout/llm/backends/anthropic.rb', line 27 def query(client, , tools = [], parameters = {}) parameters[:messages] = parameters[:tools] = format_tool_definitions(tools) if tools && tools.any? client.(parameters: parameters) end |