|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module ActiveAgent |
| 4 | + module Providers |
| 5 | + module Bedrock |
| 6 | + # Client for AWS Bedrock using bearer token (API key) authentication. |
| 7 | + # |
| 8 | + # Subclasses Anthropic::Client directly to reuse its built-in bearer |
| 9 | + # token support via the +auth_token+ parameter, while adding Bedrock- |
| 10 | + # specific request transformations (URL path rewriting, anthropic_version |
| 11 | + # injection) copied from Anthropic::BedrockClient. |
| 12 | + # |
| 13 | + # This avoids Anthropic::BedrockClient which requires SigV4 credentials |
| 14 | + # and would fail when only a bearer token is available. |
| 15 | + # |
| 16 | + # @see https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys-use.html |
| 17 | + class BearerClient < ::Anthropic::Client |
| 18 | + BEDROCK_VERSION = "bedrock-2023-05-31" |
| 19 | + |
| 20 | + # @return [String] |
| 21 | + attr_reader :aws_region |
| 22 | + |
| 23 | + # @param aws_region [String] AWS region for the Bedrock endpoint |
| 24 | + # @param bearer_token [String] AWS Bedrock API key (bearer token) |
| 25 | + # @param base_url [String, nil] Override the default Bedrock endpoint |
| 26 | + # @param max_retries [Integer] |
| 27 | + # @param timeout [Float] |
| 28 | + # @param initial_retry_delay [Float] |
| 29 | + # @param max_retry_delay [Float] |
| 30 | + def initialize( |
| 31 | + aws_region:, |
| 32 | + bearer_token:, |
| 33 | + base_url: nil, |
| 34 | + max_retries: self.class::DEFAULT_MAX_RETRIES, |
| 35 | + timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, |
| 36 | + initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, |
| 37 | + max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY |
| 38 | + ) |
| 39 | + @aws_region = aws_region |
| 40 | + |
| 41 | + base_url ||= "https://bedrock-runtime.#{aws_region}.amazonaws.com" |
| 42 | + |
| 43 | + super( |
| 44 | + auth_token: bearer_token, |
| 45 | + api_key: nil, |
| 46 | + base_url: base_url, |
| 47 | + max_retries: max_retries, |
| 48 | + timeout: timeout, |
| 49 | + initial_retry_delay: initial_retry_delay, |
| 50 | + max_retry_delay: max_retry_delay |
| 51 | + ) |
| 52 | + |
| 53 | + @messages = ::Anthropic::Resources::Messages.new(client: self) |
| 54 | + @completions = ::Anthropic::Resources::Completions.new(client: self) |
| 55 | + @beta = ::Anthropic::Resources::Beta.new(client: self) |
| 56 | + end |
| 57 | + |
| 58 | + private |
| 59 | + |
| 60 | + # Intercepts request building to apply Bedrock-specific transformations |
| 61 | + # before the parent class processes the request. |
| 62 | + def build_request(req, opts) |
| 63 | + fit_req_to_bedrock_specs!(req) |
| 64 | + req = super |
| 65 | + body = req.fetch(:body) |
| 66 | + req[:body] = StringIO.new(body.to_a.join) if body.is_a?(Enumerator) |
| 67 | + req |
| 68 | + end |
| 69 | + |
| 70 | + # Rewrites Anthropic API paths to Bedrock endpoint paths and injects |
| 71 | + # the Bedrock anthropic_version field. |
| 72 | + # |
| 73 | + # Adapted from Anthropic::Helpers::Bedrock::Client#fit_req_to_bedrock_specs! |
| 74 | + def fit_req_to_bedrock_specs!(request_components) |
| 75 | + if (body = request_components[:body]).is_a?(Hash) |
| 76 | + body[:anthropic_version] ||= BEDROCK_VERSION |
| 77 | + body.transform_keys!("anthropic-beta": :anthropic_beta) |
| 78 | + end |
| 79 | + |
| 80 | + case request_components[:path] |
| 81 | + in %r{^v1/messages/batches} |
| 82 | + raise NotImplementedError, "The Batch API is not supported in Bedrock yet" |
| 83 | + in %r{v1/messages/count_tokens} |
| 84 | + raise NotImplementedError, "Token counting is not supported in Bedrock yet" |
| 85 | + in %r{v1/models\?beta=true} |
| 86 | + raise NotImplementedError, |
| 87 | + "Please instead use https://docs.anthropic.com/en/api/claude-on-amazon-bedrock#list-available-models " \ |
| 88 | + "to list available models on Bedrock." |
| 89 | + else |
| 90 | + end |
| 91 | + |
| 92 | + if %w[ |
| 93 | + v1/complete |
| 94 | + v1/messages |
| 95 | + v1/messages?beta=true |
| 96 | + ].include?(request_components[:path]) && request_components[:method] == :post && body.is_a?(Hash) |
| 97 | + model = body.delete(:model) |
| 98 | + model = URI.encode_www_form_component(model.to_s) |
| 99 | + stream = body.delete(:stream) || false |
| 100 | + request_components[:path] = |
| 101 | + stream ? "model/#{model}/invoke-with-response-stream" : "model/#{model}/invoke" |
| 102 | + end |
| 103 | + |
| 104 | + request_components |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | +end |
0 commit comments