32
33
34
35
36
37
38
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
|
# File 'lib/scout/llm/backends/bedrock.rb', line 32
def self.ask(question, options = {}, &block)
client, region, access_key, secret_key, type = IndiferentHash.process_options options, :client, :region, :access_key, :secret_key, :type
model_options = IndiferentHash.pull_keys options, :model
model = IndiferentHash.process_options model_options, :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.process_options options, :role, :previous_response_id, :tools
messages = LLM.parse(question, role)
case type.to_sym
when :messages
body = model_options.merge({
system: messages.select{|m| m[:role] == 'system'}.collect{|m| m[:content]}*"\n",
messages: messages.select{|m| m[:role] == 'user'}
})
when :prompt
system, user = messages_to_prompt messages
body = model_options.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}"
message = result
tool_calls = message.dig('content').select{|m| m['tool_calls']}
while tool_calls && tool_calls.any?
messages << message
cpus = Scout::Config.get :cpus, :tool_calling, default: 3
tool_calls.each do |tool_call|
response_message = LLM.tool_response(tool_call, &block)
messages << response_message
end
body[:messages] = 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}"
message = result
tool_calls = message.dig('content').select{|m| m['tool_calls']}
end
message.dig('content').collect{|m|
m['text']
} * "\n"
end
|