Module: RubyLLM::Providers::Bedrock::Chat
- Included in:
- RubyLLM::Providers::Bedrock
- Defined in:
- lib/ruby_llm/providers/bedrock/chat.rb
Overview
Chat methods for the AWS Bedrock API implementation
Class Method Summary collapse
- .build_base_payload(chat_messages, model, thinking) ⇒ Object
- .completion_url ⇒ Object
- .format_basic_message_with_thinking(msg, thinking_enabled) ⇒ Object
- .format_message(msg, thinking: nil) ⇒ Object
- .format_tool_call_with_thinking(msg, thinking_enabled) ⇒ Object
-
.render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument,Metrics/ParameterLists.
- .sync_response(connection, payload, additional_headers = {}) ⇒ Object
Class Method Details
.build_base_payload(chat_messages, model, thinking) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby_llm/providers/bedrock/chat.rb', line 48 def build_base_payload(, model, thinking) payload = { anthropic_version: 'bedrock-2023-05-31', messages: .map { |msg| (msg, thinking: thinking) }, max_tokens: model.max_tokens || 4096 } thinking_payload = Anthropic::Chat.build_thinking_payload(thinking) payload[:thinking] = thinking_payload if thinking_payload payload end |
.completion_url ⇒ Object
33 34 35 |
# File 'lib/ruby_llm/providers/bedrock/chat.rb', line 33 def completion_url "model/#{@model_id}/invoke" end |
.format_basic_message_with_thinking(msg, thinking_enabled) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ruby_llm/providers/bedrock/chat.rb', line 61 def (msg, thinking_enabled) content_blocks = [] if msg.role == :assistant && thinking_enabled thinking_block = Anthropic::Chat.build_thinking_block(msg.thinking) content_blocks << thinking_block if thinking_block end Anthropic::Chat.append_formatted_content(content_blocks, msg.content) { role: Anthropic::Chat.convert_role(msg.role), content: content_blocks } end |
.format_message(msg, thinking: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ruby_llm/providers/bedrock/chat.rb', line 19 def (msg, thinking: nil) thinking_enabled = thinking&.enabled? if msg.tool_call? format_tool_call_with_thinking(msg, thinking_enabled) elsif msg.tool_result? Anthropic::Tools.format_tool_result(msg) else (msg, thinking_enabled) end end |
.format_tool_call_with_thinking(msg, thinking_enabled) ⇒ Object
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 |
# File 'lib/ruby_llm/providers/bedrock/chat.rb', line 77 def format_tool_call_with_thinking(msg, thinking_enabled) if msg.content.is_a?(RubyLLM::Content::Raw) content_blocks = msg.content.value content_blocks = [content_blocks] unless content_blocks.is_a?(Array) content_blocks = Anthropic::Chat.prepend_thinking_block(content_blocks, msg, thinking_enabled) return { role: 'assistant', content: content_blocks } end content_blocks = Anthropic::Chat.prepend_thinking_block([], msg, thinking_enabled) content_blocks << Anthropic::Media.format_text(msg.content) unless msg.content.nil? || msg.content.empty? msg.tool_calls.each_value do |tool_call| content_blocks << { type: 'tool_use', id: tool_call.id, name: tool_call.name, input: tool_call.arguments } end { role: 'assistant', content: content_blocks } end |
.render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument,Metrics/ParameterLists
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby_llm/providers/bedrock/chat.rb', line 37 def render_payload(, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil) # rubocop:disable Lint/UnusedMethodArgument,Metrics/ParameterLists @model_id = model.id , = Anthropic::Chat.() system_content = Anthropic::Chat.build_system_content() build_base_payload(, model, thinking).tap do |payload| Anthropic::Chat.add_optional_fields(payload, system_content:, tools:, temperature:) end end |
.sync_response(connection, payload, additional_headers = {}) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/ruby_llm/providers/bedrock/chat.rb', line 10 def sync_response(connection, payload, additional_headers = {}) signature = sign_request("#{connection.connection.url_prefix}#{completion_url}", payload:) response = connection.post completion_url, payload do |req| req.headers.merge! build_headers(signature.headers, streaming: block_given?) req.headers = additional_headers.merge(req.headers) unless additional_headers.empty? end Anthropic::Chat.parse_completion_response response end |