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 Models on Google Cloud (Anthropic managed) — 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.
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. "#{LOCATION_URL_TEMPLATE}/workspaces/%<workspace_id>s/invoke".freeze
- ORGANIZATION_URL_TEMPLATE =
Gateway base URL template for organization-level routes, which are served from a base with no workspace in it.
"#{LOCATION_URL_TEMPLATE}/claudeOrganization/invoke".freeze
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
- #location ⇒ String? readonly
- #project ⇒ String? readonly
- #workspace_id ⇒ String? readonly
Attributes inherited from Client
#api_key, #auth_token, #beta, #credentials, #files, #messages, #models, #organization, #skills, #token_cache, #user_profiles, #webhook_key
Attributes inherited from Internal::Transport::BaseClient
#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #middleware, #open_telemetry_max_content_bytes, #requester, #timeout
Instance Method Summary collapse
-
#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: nil, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, middleware: nil, open_telemetry: nil) ⇒ Client
constructor
Creates a new client for the Anthropic API on the Google Cloud gateway.
Methods inherited from Client
#calculate_nonstreaming_timeout, #message_threads?, #retry_request?, #timeout_overridden?
Methods inherited from Internal::Transport::BaseClient
follow_redirect, #inspect, #open_telemetry_attributes, #open_telemetry_content?, #open_telemetry_tracer, 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: nil, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, middleware: nil, open_telemetry: 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
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 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 103 def 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: nil, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, middleware: nil, open_telemetry: 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" @workspace_id = workspace_id base_url = nil if base_url.to_s.empty? unless base_url @organization_base_url = URI.parse(derive_base_url(ORGANIZATION_URL_TEMPLATE)) # Without a workspace ID there is no workspace base to derive, so # `base_url` is the organization base. base_url = @workspace_id.nil? ? @organization_base_url.to_s : derive_base_url(URL_TEMPLATE) end # No first-party credential is ever passed up, and # {#resolve_default_credentials?} keeps the parent from discovering # one; `base_url` is always resolved by this point, so the parent's # `ANTHROPIC_BASE_URL` fallback never applies. super( base_url: base_url, timeout: timeout, max_retries: max_retries, initial_retry_delay: initial_retry_delay, max_retry_delay: max_retry_delay, middleware: middleware, open_telemetry: open_telemetry ) end |
Instance Attribute Details
#location ⇒ String? (readonly)
37 38 39 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 37 def location @location end |
#project ⇒ String? (readonly)
34 35 36 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 34 def project @project end |
#workspace_id ⇒ String? (readonly)
40 41 42 |
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 40 def workspace_id @workspace_id end |