Class: Anthropic::Helpers::Vertex::Client

Inherits:
Client show all
Defined in:
lib/anthropic/helpers/vertex/client.rb,
sig/anthropic/helpers/vertex/client.rbs

Constant Summary

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, #credentials, #models, #organization, #token_cache, #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

Methods inherited from Client

#calculate_nonstreaming_timeout, #retry_request?, #timeout_overridden?

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, #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(region: ENV["CLOUD_ML_REGION"], project_id: ENV["ANTHROPIC_VERTEX_PROJECT_ID"], base_url: nil, max_retries: DEFAULT_MAX_RETRIES, timeout: nil, initial_retry_delay: DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: DEFAULT_MAX_RETRY_DELAY, middleware: nil, open_telemetry: nil) ⇒ Client

Creates and returns a new client for interacting with the GCP Vertex API for Anthropic models.

GCP credentials are resolved according to Application Default Credentials, described at https://cloud.google.com/docs/authentication/provide-credentials-adc

Parameters:

  • region (String, nil) (defaults to: ENV["CLOUD_ML_REGION"]) —

    Enforce the GCP Region to use. If unset, the region may be set with the CLOUD_ML_REGION environment variable.

  • project_id (String, nil) (defaults to: ENV["ANTHROPIC_VERTEX_PROJECT_ID"]) —

    Enforce the GCP Project ID to use. If unset, the project_id may be set with the ANTHROPIC_VERTEX_PROJECT_ID environment variable.

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

    Override the default base URL for the API, e.g., "https://api.example.com/v2/"

  • max_retries (Integer) (defaults to: DEFAULT_MAX_RETRIES) —

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

  • timeout (Float, nil) (defaults to: nil) —

    Request timeout in seconds. Defaults to Anthropic::Client::DEFAULT_TIMEOUT_IN_SECONDS when nil.

  • initial_retry_delay (Float) (defaults to: DEFAULT_INITIAL_RETRY_DELAY) —

    The number of seconds to wait before retrying a request

  • max_retry_delay (Float) (defaults to: DEFAULT_MAX_RETRY_DELAY) —

    The maximum number of seconds to wait before retrying a request

  • middleware (Array<#call>, #call, nil) (defaults to: nil) —

    Per-attempt HTTP around-middleware. See Middleware. Middleware sees the canonical Anthropic request shape; the Vertex URL rewrite and OAuth header happen inside the continuation, per attempt.

  • open_telemetry (Hash{Symbol=>Object}, Boolean, nil) (defaults to: nil) —

    OpenTelemetry tracing of this client's API calls. See Anthropic::Client#initialize.

  • region: (String, nil) (defaults to: ENV["CLOUD_ML_REGION"])
  • project_id: (String, nil) (defaults to: ENV["ANTHROPIC_VERTEX_PROJECT_ID"])
  • base_url: (String, nil) (defaults to: nil)
  • max_retries: (Integer) (defaults to: DEFAULT_MAX_RETRIES)
  • timeout: (Float, nil) (defaults to: nil)
  • initial_retry_delay: (Float) (defaults to: DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay: (Float) (defaults to: DEFAULT_MAX_RETRY_DELAY)
  • middleware: (Anthropic::Middleware::entry_or_array, nil) (defaults to: nil)
  • open_telemetry: (Anthropic::Internal::Tracing::client_option, nil) (defaults to: nil)


47
48
49
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
113
114
115
116
117
118
119
120
121
122
# File 'lib/anthropic/helpers/vertex/client.rb', line 47

def initialize(
  region: ENV["CLOUD_ML_REGION"],
  project_id: ENV["ANTHROPIC_VERTEX_PROJECT_ID"],
  base_url: nil,
  max_retries: DEFAULT_MAX_RETRIES,
  timeout: nil,
  initial_retry_delay: DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: DEFAULT_MAX_RETRY_DELAY,
  middleware: nil,
  open_telemetry: nil
)
  begin
    require("googleauth")
  rescue LoadError
    message = <<~MSG

      In order to access Anthropic models on Vertex you must require the `googleauth` gem.
      You can install it by adding the following to your Gemfile:

          gem "googleauth"

      and then running `bundle install`.

      Alternatively, if you are not using Bundler, simply run:

          gem install googleauth
    MSG

    raise RuntimeError.new(message)
  end

  if region.to_s.empty?
    # rubocop:disable Layout/LineLength
    message = "No region was given. The client should be instantiated with the `region` argument or the `CLOUD_ML_REGION` environment variable should be set."
    # rubocop:enable Layout/LineLength
    raise ArgumentError.new(message)
  end
  @region = region

  if project_id.to_s.empty?
    # rubocop:disable Layout/LineLength
    message = "No project_id was given and it could not be resolved from credentials. The client should be instantiated with the `project_id` argument or the `ANTHROPIC_VERTEX_PROJECT_ID` environment variable should be set."
    # rubocop:enable Layout/LineLength
    raise ArgumentError.new(message)
  end
  @project_id = project_id
  @authorization = nil

  base_url = ENV["ANTHROPIC_VERTEX_BASE_URL"] if base_url.to_s.empty?
  if base_url.to_s.empty?
    base_url =
      case @region.to_s
      when "global"
        "https://aiplatform.googleapis.com/v1"
      when "us"
        "https://aiplatform.us.rep.googleapis.com/v1"
      when "eu"
        "https://aiplatform.eu.rep.googleapis.com/v1"
      else
        "https://#{@region}-aiplatform.googleapis.com/v1"
      end
  end

  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
  )

  @messages = Anthropic::Resources::Messages.new(client: self)
  @beta = Anthropic::Resources::Beta.new(client: self)
end

Instance Attribute Details

#beta ⇒ Anthropic::Resources::Beta (readonly)



17
18
19
# File 'lib/anthropic/helpers/vertex/client.rb', line 17

def beta
  @beta
end

#messages ⇒ Anthropic::Resources::Messages (readonly)



14
15
16
# File 'lib/anthropic/helpers/vertex/client.rb', line 14

def messages
  @messages
end

#project_id ⇒ String (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/anthropic/helpers/vertex/client.rb', line 11

def project_id
  @project_id
end

#region ⇒ String (readonly)

Returns:

  • (String)


8
9
10
# File 'lib/anthropic/helpers/vertex/client.rb', line 8

def region
  @region
end

Instance Method Details

#files ⇒ bot

Returns:

  • (bot)

Raises:

  • (NotImplementedError)


139
140
141
# File 'lib/anthropic/helpers/vertex/client.rb', line 139

def files
  raise NotImplementedError, "Files are not supported on Vertex."
end

#message_threads? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



137
# File 'lib/anthropic/helpers/vertex/client.rb', line 137

def message_threads? = false

#open_telemetry_attributes ⇒ Hash{String=>String}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash{String=>String})


205
206
207
# File 'lib/anthropic/helpers/vertex/client.rb', line 205

def open_telemetry_attributes
  {**super, Anthropic::Internal::Tracing::GEN_AI_PROVIDER_NAME => "gcp.vertex_ai"}
end

#skills ⇒ bot

Returns:

  • (bot)

Raises:

  • (NotImplementedError)


143
144
145
# File 'lib/anthropic/helpers/vertex/client.rb', line 143

def skills
  raise NotImplementedError, "Skills are not supported on Vertex."
end

#user_profiles ⇒ bot

Returns:

  • (bot)

Raises:

  • (NotImplementedError)


147
148
149
# File 'lib/anthropic/helpers/vertex/client.rb', line 147

def user_profiles
  raise NotImplementedError, "Managing user profiles is not supported on Vertex."
end