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



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 = "\n      In order to access Anthropic models on Vertex you must require the `googleauth` gem.\n      You can install it by adding the following to your Gemfile:\n\n          gem \"googleauth\"\n\n      and then running `bundle install`.\n\n      Alternatively, if you are not using Bundler, simply run:\n\n          gem install googleauth\n    MSG\n\n    raise RuntimeError.new(message)\n  end\n\n  if region.to_s.empty?\n    # rubocop:disable Layout/LineLength\n    message = \"No region was given. The client should be instantiated with the `region` argument or the `CLOUD_ML_REGION` environment variable should be set.\"\n    # rubocop:enable Layout/LineLength\n    raise ArgumentError.new(message)\n  end\n  @region = region\n\n  if project_id.to_s.empty?\n    # rubocop:disable Layout/LineLength\n    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.\"\n    # rubocop:enable Layout/LineLength\n    raise ArgumentError.new(message)\n  end\n  @project_id = project_id\n\n  base_url ||= ENV.fetch(\n    \"ANTHROPIC_VERTEX_BASE_URL\",\n    @region.to_s == \"global\" ? \"https://aiplatform.googleapis.com/v1\" : \"https://\#{@region}-aiplatform.googleapis.com/v1\"\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  @beta = Anthropic::Resources::Beta.new(client: self)\nend\n"

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)



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

def project_id
  @project_id
end

#regionString (readonly)



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

def region
  @region
end