Class: Anthropic::Helpers::GoogleCloud::Client
- Inherits:
-
Client
- Object
- Internal::Transport::BaseClient
- Client
- Anthropic::Helpers::GoogleCloud::Client
- Defined in:
- lib/anthropic/helpers/google_cloud/client.rb,
sig/anthropic/helpers/google_cloud/client.rbs
Overview
Client for Claude Platform on Google Cloud — the first-party Anthropic API served through the Google Cloud gateway.
This is distinct from VertexClient, which targets the
:rawPredict publisher-model API (publisher model IDs, messages
only). This client speaks the full first-party Anthropic API: requests
pass through the gateway unchanged — standard /v1/* paths, standard
model names, the complete API surface. The deprecated text Completions
endpoint is intentionally not exposed.
Constant Summary collapse
- SCOPE =
OAuth2 scope required to call the gateway.
"https://www.googleapis.com/auth/cloud-platform"- URL_TEMPLATE =
Gateway base URL template; override via
base_urlorANTHROPIC_GOOGLE_CLOUD_BASE_URL. "https://claude.googleapis.com/v1alpha" \ "/projects/%<project>s/locations/%<location>s/workspaces/%<workspace_id>s/invoke"
- Anthropic =
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
- #beta ⇒ Anthropic::Resources::Beta readonly
- #location ⇒ String? readonly
- #messages ⇒ Anthropic::Resources::Messages readonly
- #models ⇒ Anthropic::Resources::Models readonly
- #project ⇒ String? readonly
- #workspace_id ⇒ String? readonly
Attributes inherited from Client
#api_key, #auth_token, #credentials, #token_cache, #webhook_key
Attributes inherited from Internal::Transport::BaseClient
#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #middleware, #requester, #timeout
Instance Method Summary collapse
-
#completions ⇒ bot
The deprecated text Completions endpoint is not supported on the Google Cloud gateway.
-
#initialize(project: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"], location: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global", workspace_id: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"], base_url: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"], google_credentials: nil, token_provider: nil, skip_auth: false, 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, middleware: nil) ⇒ Client
constructor
Creates a new client for the Anthropic API on the Google Cloud gateway.
Methods inherited from Client
#calculate_nonstreaming_timeout, #retry_request?
Methods inherited from Internal::Transport::BaseClient
follow_redirect, #inspect, reap_connection!, #request, #retry_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(project: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"], location: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global", workspace_id: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"], base_url: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"], google_credentials: nil, token_provider: nil, skip_auth: false, 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, middleware: nil) ⇒ Client
Creates a new client for the Anthropic API on the Google Cloud gateway.
Authentication uses Google credentials by precedence (unless
skip_auth is true):
1. `token_provider` — a callable returning a GCP access token, called
per attempt
2. `google_credentials` — a `googleauth` credentials object (anything
responding to `#apply`), refreshed by the library as needed
3. Application Default Credentials (`Google::Auth.get_application_default`)
with the `cloud-platform` scope
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 98 def initialize( # rubocop:disable Lint/MissingSuper project: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"], location: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global", workspace_id: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"], base_url: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"], google_credentials: nil, token_provider: nil, skip_auth: false, 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, middleware: nil ) if skip_auth && (token_provider || google_credentials) raise ArgumentError.new( "`skip_auth` is mutually exclusive with `token_provider` and `google_credentials`; " \ "`skip_auth` disables authentication entirely." ) end @skip_auth = skip_auth @token_provider = token_provider @authorization = google_credentials # Resolve credentials eagerly so the client is thread-safe (no lazy # ADC race) and so the credentials' project can backfill `project` # before the base URL is derived. if !skip_auth && token_provider.nil? && google_credentials.nil? require_googleauth @authorization = Google::Auth.get_application_default([SCOPE]) end if project.nil? && @authorization.respond_to?(:project_id) project = @authorization.project_id end @project = project # The kwarg default doesn't fire on an explicitly-passed nil — coerce # so nil can't reach the derived URL (matches the other SDKs). @location = location || ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global" if workspace_id.nil? && !skip_auth raise ArgumentError.new( "No workspace ID was given. Set the `workspace_id` argument or the " \ "`ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID` environment variable." ) end @workspace_id = workspace_id base_url = derive_base_url if base_url.nil? # Never inherit first-party Anthropic credentials from the # environment: clear the ivars `Anthropic::Client#auth_headers` reads, # and bind `BaseClient#initialize` directly so the parent's # credential-provider / `ANTHROPIC_BASE_URL` resolution is skipped # entirely. @api_key = nil @auth_token = nil @credentials = nil @token_cache = nil Anthropic::Internal::Transport::BaseClient.instance_method(:initialize).bind(self).call( base_url: base_url, timeout: timeout, max_retries: max_retries, initial_retry_delay: initial_retry_delay, max_retry_delay: max_retry_delay, headers: {"anthropic-version" => "2023-06-01"}, middleware: middleware ) @messages = Anthropic::Resources::Messages.new(client: self) @models = Anthropic::Resources::Models.new(client: self) @beta = Anthropic::Resources::Beta.new(client: self) end |
Instance Attribute Details
#beta ⇒ Anthropic::Resources::Beta (readonly)
41 42 43 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 41 def beta @beta end |
#location ⇒ String? (readonly)
29 30 31 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 29 def location @location end |
#messages ⇒ Anthropic::Resources::Messages (readonly)
35 36 37 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 35 def @messages end |
#models ⇒ Anthropic::Resources::Models (readonly)
38 39 40 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 38 def models @models end |
#project ⇒ String? (readonly)
26 27 28 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 26 def project @project end |
#workspace_id ⇒ String? (readonly)
32 33 34 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 32 def workspace_id @workspace_id end |
Instance Method Details
#completions ⇒ bot
The deprecated text Completions endpoint is not supported on the Google Cloud gateway.
178 179 180 181 182 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 178 def completions raise NotImplementedError.new( "The deprecated text Completions API is not supported on the Google Cloud gateway." ) end |