Class: Anthropic::Helpers::Bedrock::Client

Inherits:
Client show all
Defined in:
lib/anthropic/helpers/bedrock/client.rb

Constant Summary collapse

DEFAULT_VERSION =
"bedrock-2023-05-31"

Constants inherited from Client

Client::DEFAULT_INITIAL_RETRY_DELAY, Client::DEFAULT_MAX_RETRIES, Client::DEFAULT_MAX_RETRY_DELAY, Client::DEFAULT_TIMEOUT_IN_SECONDS, Client::MODEL_NONSTREAMING_TOKENS

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Client

#api_key, #auth_token, #models

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Client

#calculate_nonstreaming_timeout

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(aws_region: nil, base_url: nil, max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, aws_access_key: nil, aws_secret_key: nil, aws_session_token: nil, aws_profile: nil) ⇒ Client

Creates and returns a new client for interacting with the AWS Bedrock API for Anthropic models.

AWS credentials are resolved according to the AWS SDK’s default resolution order, described at

https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html#credchain or https://github.com/aws/aws-sdk-ruby?tab=readme-ov-file#configuration

Parameters:

  • aws_region (String, nil) (defaults to: nil)

    Enforce the AWS Region to use. If unset, the region is set according to the AWS SDK’s default resolution order, described at github.com/aws/aws-sdk-ruby?tab=readme-ov-file#configuration

  • aws_access_key (String, nil) (defaults to: nil)

    Optional AWS access key to use for authentication. Overrides profile and credential provider chain

  • aws_secret_key (String, nil) (defaults to: nil)

    Optional AWS secret access key to use for authentication. Overrides profile and credential provider chain

  • aws_session_token (String, nil) (defaults to: nil)

    Optional AWS session token to use for authentication. Overrides profile and credential provider chain

  • aws_profile (String, nil) (defaults to: nil)

    Optional AWS profile to use for authentication. Overrides the credential provider chain

  • base_url (String, nil) (defaults to: nil)

    Override the default base URL for the API, e.g., ‘“api.example.com/v2/”`

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    The maximum number of times to retry a request if it fails

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)

    The number of seconds to wait for a response before timing out

  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)

    The number of seconds to wait before retrying a request

  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)

    The maximum number of seconds to wait before retrying a request



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
103
104
105
106
107
108
109
110
111
112
# File 'lib/anthropic/helpers/bedrock/client.rb', line 50

def initialize(
  aws_region: nil,
  base_url: nil,
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::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
    message = <<~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

    raise RuntimeError.new(message)
  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

Instance Attribute Details

#aws_credentialsAws::Credentials (readonly)

Returns:

  • (Aws::Credentials)


22
23
24
# File 'lib/anthropic/helpers/bedrock/client.rb', line 22

def aws_credentials
  @aws_credentials
end

#aws_regionString (readonly)

Returns:

  • (String)


19
20
21
# File 'lib/anthropic/helpers/bedrock/client.rb', line 19

def aws_region
  @aws_region
end

#betaAnthropic::Resources::Beta (readonly)



16
17
18
# File 'lib/anthropic/helpers/bedrock/client.rb', line 16

def beta
  @beta
end

#completionsAnthropic::Resources::Completions (readonly)



13
14
15
# File 'lib/anthropic/helpers/bedrock/client.rb', line 13

def completions
  @completions
end

#messagesAnthropic::Resources::Messages (readonly)



10
11
12
# File 'lib/anthropic/helpers/bedrock/client.rb', line 10

def messages
  @messages
end