class Anthropic::Helpers::Bedrock::Client

def build_request(req, opts)

Returns:
  • (Hash{Symbol=>Object}) -

Options Hash: (**opts)
  • :timeout (Float, nil) --
  • :max_retries (Integer, nil) --
  • :extra_body (Object, nil) --
  • :extra_headers (Hash{String=>String, nil}, nil) --
  • :extra_query (Hash{String=>Array, String, nil}, nil) --
  • :idempotency_key (String, nil) --
  • :model (Anthropic::Internal::Type::Converter, Class, nil) --
  • :stream (Class, nil) --
  • :page (Class, nil) --
  • :unwrap (Symbol, Integer, Array, Proc, nil) --
  • :body (Object, nil) --
  • :headers (Hash{String=>String, Integer, Array, nil}, nil) --
  • :query (Hash{String=>Array, String, nil}, nil) --
  • :path (String, Array) --
  • :method (Symbol) --

Parameters:
  • opts (Hash{Symbol=>Object}) -- .
  • req (Hash{Symbol=>Object}) -- .

Other tags:
    Api: - private
def build_request(req, opts)
q_to_bedrock_specs!(req)
t_input = super
_request = @signer.sign_request(
_method: request_input[:method],
 request_input[:url],
ers: request_input[:headers],
: request_input[:body]
t_input[:headers].merge!(signed_request.headers)
t_input

def fit_req_to_bedrock_specs!(request_components)

Returns:
  • (Hash{Symbol=>Object}) -

Options Hash: (**request_components)
  • :model (Anthropic::Converter, Class, nil) --
  • :page (Class, nil) --
  • :unwrap (Symbol, nil) --
  • :body (Object, nil) --
  • :headers (Hash{String=>String, nil}, nil) --
  • :query (Hash{String=>Array, String, nil}, nil) --
  • :path (String, Array) --
  • :method (Symbol) --

Parameters:
  • request_components (Hash{Symbol=>Object}) -- .

Other tags:
    Private: -
def fit_req_to_bedrock_specs!(request_components)
dy = request_components[:body]).is_a?(Hash)
[:anthropic_version] ||= DEFAULT_VERSION
.transform_keys!("anthropic-beta": :anthropic_beta)
equest_components[:path]
^v1/messages/batches}
age = "The Batch API is not supported in Bedrock yet"
e NotImplementedError.new(message)
v1/messages/count_tokens}
age = "Token counting is not supported in Bedrock yet"
e NotImplementedError.new(message)
v1/models\?beta=true}
bocop:disable Layout/LineLength
age = "Please instead use https://docs.anthropic.com/en/api/claude-on-amazon-bedrock#list-available-models to list available models on Bedrock."
bocop:enable Layout/LineLength
e NotImplementedError.new(message)

omplete
essages
essages?beta=true
ude?(request_components[:path]) && request_components[:method] == :post && body.is_a?(Hash)
l = body.delete(:model)
l = URI.encode_www_form_component(model.to_s)
am = body.delete(:stream) || false
est_components[:path] =
ream ? "model/#{model}/invoke-with-response-stream" : "model/#{model}/invoke"
t_components

def initialize(

Parameters:
  • max_retry_delay (Float) -- The maximum number of seconds to wait before retrying a request
  • initial_retry_delay (Float) -- The number of seconds to wait before retrying a request
  • timeout (Float) -- The number of seconds to wait for a response before timing out
  • max_retries (Integer) -- The maximum number of times to retry a request if it fails
  • base_url (String, nil) -- Override the default base URL for the API, e.g., `"https://api.example.com/v2/"`
  • aws_profile (String, nil) -- Optional AWS profile to use for authentication. Overrides the credential provider chain
  • aws_session_token (String, nil) -- Optional AWS session token to use for authentication. Overrides profile and credential provider chain
  • aws_secret_key (String, nil) -- Optional AWS secret access key to use for authentication. Overrides profile and credential provider chain
  • aws_access_key (String, nil) -- Optional AWS access key to use for authentication. Overrides profile and credential provider chain
  • aws_region (String, nil) -- Enforce the AWS Region to use. If unset, the region is set according to the
def initialize(
  aws_region: nil,
  base_url: nil,
  max_retries: DEFAULT_MAX_RETRIES,
  timeout: DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: DEFAULT_MAX_RETRY_DELAY,
  aws_access_key: nil,
  aws_secret_key: nil,
  aws_session_token: nil,
  aws_profile: nil
)
  begin
    require("aws-sdk-bedrockruntime")
  rescue LoadError
    raise <<~MSG
      In order to access Anthropic models on Bedrock you must require the `aws-sdk-bedrockruntime` gem.
      You can install it by adding the following to your Gemfile:
          gem "aws-sdk-bedrockruntime"
      and then running `bundle install`.
      Alternatively, if you are not using Bundler, simply run:
          gem install aws-sdk-bedrockruntime
    MSG
  end
  @aws_region, @aws_credentials = resolve_region_and_credentials(
    aws_region: aws_region,
    aws_secret_key: aws_secret_key,
    aws_access_key: aws_access_key,
    aws_session_token: aws_session_token,
    aws_profile: aws_profile
  )
  @signer = Aws::Sigv4::Signer.new(
    service: "bedrock",
    region: @aws_region,
    credentials: @aws_credentials
  )
  base_url ||= ENV.fetch(
    "ANTHROPIC_BEDROCK_BASE_URL",
    "https://bedrock-runtime.#{@aws_region}.amazonaws.com"
  )
  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
  )
  @messages = Anthropic::Resources::Messages.new(client: self)
  @completions = Anthropic::Resources::Completions.new(client: self)
  @beta = Anthropic::Resources::Beta.new(client: self)
end

def resolve_region_and_credentials(

Returns:
  • (Array) -

Parameters:
  • aws_profile (String, nil) --
  • aws_session_token (String, nil) --
  • aws_access_key (String, nil) --
  • aws_secret_key (String, nil) --
  • aws_region (String, nil) --
def resolve_region_and_credentials(
gion:,
cret_key:,
cess_key:,
ssion_token:,
ofile:
_options = {
ss_key_id: aws_access_key,
et_access_key: aws_secret_key,
ion_token: aws_session_token,
ile: aws_profile
t_options[:region] = aws_region) unless aws_region.nil?
k_client = Aws::BedrockRuntime::Client.new(client_options)
ck_client.config.region, bedrock_client.config.credentials.credentials]