Module: LLM::Bedrock
- Defined in:
- lib/scout/llm/backends/bedrock.rb
Class Method Summary collapse
- .ask(question, options = {}, &block) ⇒ Object
- .client(region, access_key, secret_key) ⇒ Object
- .embed(text, options = {}) ⇒ Object
- .messages_to_prompt(messages) ⇒ Object
-
.tool_calls(message) ⇒ Object
ScoutCoder: Bedrock/Anthropic style payloads wrap the individual calls in a content entry (=> "tool_use", "tool_calls" => [...]).
Class Method Details
.ask(question, options = {}, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/scout/llm/backends/bedrock.rb', line 39 def self.ask(question, = {}, &block) client, region, access_key, secret_key, type = IndiferentHash. , :client, :region, :access_key, :secret_key, :type = IndiferentHash.pull_keys , :model model = IndiferentHash. , :model if client.nil? region ||= Scout::Config.get(:region, :bedrock_ask, :ask, :bedrock, env: 'AWS_REGION') access_key ||= LLM.get_url_config(:access_key, nil, :bedrock_ask, :ask, :bedrock, env: 'AWS_ACCESS_KEY_ID') secret_key ||= LLM.get_url_config(:secret_key, nil, :bedrock_ask, :ask, :bedrock, env: 'AWS_SECRET_ACCESS_KEY') client = self.client(region, access_key, secret_key) end model ||= Scout::Config.get(:model, :bedrock_ask, :ask, :bedrock, env: 'BEDROCK_MODEL_ID') type ||= Scout::Config.get(:type, model, default: :messages) role, previous_response_id, tools = IndiferentHash. , :role, :previous_response_id, :tools # ScoutCoder: LLM.parse does not exist (the parser lives in Chat and is # exposed as LLM.messages, which delegates to Chat.parse); this call # raised NoMethodError on every ask. Use the LLM.messages delegation. = LLM.(question, role) case type.to_sym when :messages body = .merge({ system: .select{|m| m[:role] == 'system'}.collect{|m| m[:content]}*"\n", messages: .select{|m| m[:role] == 'user'} }) when :prompt system, user = body = .merge({ prompt: user }) else raise "Unkown type #{type}" end Log.debug "Calling bedrock with model: #{model} parameters: #{Log.fingerprint body}" response = client.invoke_model( model_id: model, content_type: 'application/json', body: body.to_json ) result = JSON.parse(response.body.string) Log.debug "Response: #{Log.fingerprint result}" = result tool_calls = self.tool_calls while tool_calls && tool_calls.any? << cpus = Scout::Config.get :cpus, :tool_calling, default: 3 tool_calls.each do |tool_call| = LLM.tool_response(tool_call, &block) << end body[:messages] = .compact Log.debug "Calling bedrock with parameters: #{Log.fingerprint body}" response = client.invoke_model( model_id: model, content_type: 'application/json', body: body.to_json ) result = JSON.parse(response.body.string) Log.debug "Response: #{Log.fingerprint result}" = result tool_calls = self.tool_calls end .dig('content').collect{|m| m['text'] } * "\n" end |
.client(region, access_key, secret_key) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/scout/llm/backends/bedrock.rb', line 7 def self.client(region, access_key, secret_key) credentials = Aws::Credentials.new(access_key, secret_key) if access_key and secret_key = {} [:region] = region if region [:credentials] = credentials if credentials Aws::BedrockRuntime::Client.new() end |
.embed(text, options = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/scout/llm/backends/bedrock.rb', line 117 def self.(text, = {}) client, region, access_key, secret_key, model = IndiferentHash. , :client, :region, :access_key, :secret_key, :model if client.nil? region ||= Scout::Config.get(:region, :bedrock_embed, :embed, :bedrock, env: 'AWS_REGION') access_key ||= LLM.get_url_config(:access_key, nil, :bedrock_embed, :embed, :bedrock, env: 'AWS_ACCESS_KEY_ID') secret_key ||= LLM.get_url_config(:secret_key, nil, :bedrock_embed, :embed, :bedrock, env: 'AWS_SECRET_ACCESS_KEY') client = self.client(region, access_key, secret_key) end model ||= Scout::Config.get(:model, :bedrock_embed, :embed, :bedrock, env: 'BEDROCK_EMBED_MODEL_ID', default: 'amazon.titan-embed-text-v1') response = client.invoke_model( model_id: model, content_type: 'application/json', body: { inputText: text }.to_json ) result = JSON.parse(response.body.string) result['embedding'] end |
.messages_to_prompt(messages) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/scout/llm/backends/bedrock.rb', line 25 def self.() system = [] user = [] .each do |info| role, content = info.values_at :role, :content if role.to_s == 'system' system << content else user << content end end [system*"\n", user*"\n"] end |
.tool_calls(message) ⇒ Object
ScoutCoder: Bedrock/Anthropic style payloads wrap the individual calls in a content entry (=> "tool_use", "tool_calls" => [...]). The ask loop used to pass that wrapper itself to LLM.tool_response, which then found neither 'function' nor 'name'/'arguments' and called the tool block with (nil, nil). Unwrap and return the inner tool call entries.
21 22 23 |
# File 'lib/scout/llm/backends/bedrock.rb', line 21 def self.tool_calls() [.dig('content')].compact.flatten.select{|m| m['tool_calls']}.collect{|m| m['tool_calls']}.flatten end |