Skip to content

Commit 5b3b940

Browse files
committed
Fixed Bedrock Anthropic errors
1 parent e12fa65 commit 5b3b940

File tree

18 files changed

+398
-358
lines changed

18 files changed

+398
-358
lines changed

lib/ruby_llm/connection.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RubyLLM
44
# Connection class for managing API connections to various providers.
55
class Connection
6-
attr_reader :provider, :config
6+
attr_reader :provider, :connection, :config
77

88
def initialize(provider, config)
99
@provider = provider
@@ -18,15 +18,15 @@ def initialize(provider, config)
1818
end
1919
end
2020

21-
def post(url, payload)
21+
def post(url, payload, &)
2222
body = payload.is_a?(Hash) ? JSON.generate(payload, ascii_only: false) : payload
2323
@connection.post url, body do |req|
2424
req.headers.merge! @provider.headers(@config) if @provider.respond_to?(:headers)
2525
yield req if block_given?
2626
end
2727
end
2828

29-
def get(url)
29+
def get(url, &)
3030
@connection.get url do |req|
3131
req.headers.merge! @provider.headers(@config) if @provider.respond_to?(:headers)
3232
yield req if block_given?

lib/ruby_llm/providers/bedrock.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,7 @@ module Bedrock
2121
module_function
2222

2323
def api_base(config)
24-
@api_base ||= "https://bedrock-runtime.#{config.bedrock_region}.amazonaws.com"
25-
end
26-
27-
def post(connection, config, url, payload)
28-
signature = sign_request("#{connection.url_prefix}#{url}", config:, payload:)
29-
connection.post url, payload do |req|
30-
req.headers.merge! build_headers(signature.headers, streaming: block_given?)
31-
32-
yield req if block_given?
33-
end
24+
"https://bedrock-runtime.#{config.bedrock_region}.amazonaws.com"
3425
end
3526

3627
def parse_error(response) # rubocop:disable Metrics/MethodLength

lib/ruby_llm/providers/bedrock/chat.rb

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ module Providers
55
module Bedrock
66
# Chat methods for the AWS Bedrock API implementation
77
module Chat
8+
def sync_response(connection, payload)
9+
signature = sign_request("#{connection.connection.url_prefix}#{completion_url}", config: connection.config,
10+
payload:)
11+
response = connection.post completion_url, payload do |req|
12+
req.headers.merge! build_headers(signature.headers, streaming: block_given?)
13+
end
14+
parse_completion_response response
15+
end
16+
817
private
918

1019
def completion_url

lib/ruby_llm/providers/bedrock/streaming/base.rb

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ def stream_url
2929
"model/#{@model_id}/invoke-with-response-stream"
3030
end
3131

32+
def stream_response(connection, payload, &block) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
33+
signature = sign_request("#{connection.connection.url_prefix}#{stream_url}", config: connection.config,
34+
payload:)
35+
accumulator = StreamAccumulator.new
36+
37+
connection.post stream_url, payload do |req|
38+
req.headers.merge! build_headers(signature.headers, streaming: block_given?)
39+
req.options.on_data = handle_stream do |chunk|
40+
accumulator.add chunk
41+
block.call chunk
42+
end
43+
end
44+
45+
accumulator.to_message
46+
end
47+
3248
def handle_stream(&block)
3349
buffer = String.new
3450
proc do |chunk, _bytes, env|

spec/fixtures/vcr_cassettes/chat_basic_chat_functionality_bedrock_anthropic_claude-3-5-haiku-20241022-v1_0_can_handle_multi-turn_conversations.yml

+32-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/chat_basic_chat_functionality_bedrock_anthropic_claude-3-5-haiku-20241022-v1_0_can_have_a_basic_conversation.yml

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)