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

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

Constant Summary collapse

DEFAULT_VERSION =
"vertex-2023-10-16"

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, #completions, #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(region: ENV["CLOUD_ML_REGION"], project_id: ENV["ANTHROPIC_VERTEX_PROJECT_ID"], base_url: nil, max_retries: DEFAULT_MAX_RETRIES, timeout: DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: DEFAULT_MAX_RETRY_DELAY) ⇒ 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., ‘“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) (defaults to: DEFAULT_TIMEOUT_IN_SECONDS)

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

  • 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



40
41
42
43
44
45
46
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
# File 'lib/anthropic/helpers/vertex/client.rb', line 40

def initialize(
  region: ENV["CLOUD_ML_REGION"],
  project_id: ENV["ANTHROPIC_VERTEX_PROJECT_ID"],
  base_url: nil,
  max_retries: DEFAULT_MAX_RETRIES,
  timeout: DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: DEFAULT_MAX_RETRY_DELAY
)
  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

  base_url ||= ENV.fetch(
    "ANTHROPIC_VERTEX_BASE_URL",
    @region.to_s == "global" ? "https://aiplatform.googleapis.com/v1" : "https://#{@region}-aiplatform.googleapis.com/v1"
  )

  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)
  @beta = Anthropic::Resources::Beta.new(client: self)
end

Instance Attribute Details

#betaAnthropic::Resources::Beta (readonly)



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

def beta
  @beta
end

#messagesAnthropic::Resources::Messages (readonly)



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

def messages
  @messages
end

#project_idString (readonly)

Returns:

  • (String)


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

def project_id
  @project_id
end

#regionString (readonly)

Returns:

  • (String)


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

def region
  @region
end