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



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 = "\n      In order to access Anthropic models on Bedrock you must require the `aws-sdk-bedrockruntime` gem.\n      You can install it by adding the following to your Gemfile:\n\n          gem \"aws-sdk-bedrockruntime\"\n\n      and then running `bundle install`.\n\n      Alternatively, if you are not using Bundler, simply run:\n\n          gem install aws-sdk-bedrockruntime\n    MSG\n\n    raise RuntimeError.new(message)\n  end\n\n  @aws_region, @aws_credentials = resolve_region_and_credentials(\n    aws_region: aws_region,\n    aws_secret_key: aws_secret_key,\n    aws_access_key: aws_access_key,\n    aws_session_token: aws_session_token,\n    aws_profile: aws_profile\n  )\n\n  @signer = Aws::Sigv4::Signer.new(\n    service: \"bedrock\",\n    region: @aws_region,\n    credentials: @aws_credentials\n  )\n\n  base_url ||= ENV.fetch(\n    \"ANTHROPIC_BEDROCK_BASE_URL\",\n    \"https://bedrock-runtime.\#{@aws_region}.amazonaws.com\"\n  )\n\n  super(\n    base_url: base_url,\n    timeout: timeout,\n    max_retries: max_retries,\n    initial_retry_delay: initial_retry_delay,\n    max_retry_delay: max_retry_delay,\n  )\n\n  @messages = Anthropic::Resources::Messages.new(client: self)\n  @completions = Anthropic::Resources::Completions.new(client: self)\n  @beta = Anthropic::Resources::Beta.new(client: self)\nend\n"

Instance Attribute Details

#aws_credentialsAws::Credentials (readonly)



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

def aws_credentials
  @aws_credentials
end

#aws_regionString (readonly)



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