Class: LittleGhost::Providers::Bedrock::HTTPClient
- Inherits:
-
Object
- Object
- LittleGhost::Providers::Bedrock::HTTPClient
- Defined in:
- lib/little_ghost/providers/bedrock/http_client.rb
Overview
Stdlib Bedrock Runtime client with the small ConverseStream surface used by the adapter.
Defined Under Namespace
Classes: Response
Constant Summary collapse
- DEFAULT_MAX_RESPONSE_BYTES =
256 * 1024 * 1024
Instance Method Summary collapse
- #converse_stream(**parameters) ⇒ Object
-
#initialize(region:, credentials: nil, credential_resolver: nil, endpoint: nil, open_timeout: 10, read_timeout: 120, max_response_bytes: DEFAULT_MAX_RESPONSE_BYTES, max_frame_bytes: EventStreamDecoder::DEFAULT_MAX_FRAME_BYTES, clock: -> { Time.now.utc }) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
Constructor Details
#initialize(region:, credentials: nil, credential_resolver: nil, endpoint: nil, open_timeout: 10, read_timeout: 120, max_response_bytes: DEFAULT_MAX_RESPONSE_BYTES, max_frame_bytes: EventStreamDecoder::DEFAULT_MAX_FRAME_BYTES, clock: -> { Time.now.utc }) ⇒ HTTPClient
Returns a new instance of HTTPClient.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/little_ghost/providers/bedrock/http_client.rb', line 15 def initialize(region:, credentials: nil, credential_resolver: nil, endpoint: nil, open_timeout: 10, read_timeout: 120, max_response_bytes: DEFAULT_MAX_RESPONSE_BYTES, max_frame_bytes: EventStreamDecoder::DEFAULT_MAX_FRAME_BYTES, clock: -> { Time.now.utc }) @region = region.to_s raise ConfigurationError, "Bedrock region is required" if @region.empty? @credential_resolver = credential_resolver || -> { credentials || CredentialResolver.new.call } @endpoint = URI(endpoint || "https://bedrock-runtime.#{@region}.amazonaws.com") raise ConfigurationError, "Bedrock endpoint must use HTTPS" unless @endpoint.scheme == "https" @max_frame_bytes = Integer(max_frame_bytes) @clock = clock @http_client = Support::HTTPClient.new(open_timeout:, read_timeout:, max_response_bytes:) end |
Instance Method Details
#converse_stream(**parameters) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/little_ghost/providers/bedrock/http_client.rb', line 30 def converse_stream(**parameters) model_id = parameters.delete(:model_id) body = JSON.generate(camelize(parameters)) uri = URI.join(@endpoint.to_s, "/model/#{escape_path(model_id)}/converse-stream") credentials = @credential_resolver.call signer = AwsSigV4.new(service: "bedrock", region: @region, credentials:, clock: @clock) headers = signer.headers(method: :post, uri:, headers: {"content-type" => "application/json"}, body:) stream = Enumerator.new do |events| decoder = EventStreamDecoder.new(max_frame_bytes: @max_frame_bytes) @http_client.each_chunk(uri:, method: :post, headers:, body:, label: "Bedrock request") do |chunk| decoder.<<(chunk).each { |event_headers, payload| events << event(event_headers, payload) } end decoder.finish end Response.new(stream:) end |