iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💎
Calling the Anthropic Claude API on Google Cloud Vertex AI using Ruby
Notes on using Anthropic Claude API on Google Cloud Vertex AI from Ruby (on Rails).
Prerequisites
- Ruby 3.3.3
- Vertex AI Claude API must be enabled from the Google Cloud project console.
Calling Claude's API from Ruby
No SDK (probably)
There are Anthropic SDKs available for Python and TypeScript.
Given this, it is likely that an SDK cannot be used when working with Ruby.
Therefore, we will construct the API request directly without using an SDK (fortunately, the link above provides an example for making direct HTTP requests).
Acquiring Credentials
This time, we will use Application Default Credentials (ADC) to obtain an access token.
For local environments, you can set up ADC with:
gcloud auth application-default login
Region
According to
Regions where Anthropic Claude is available
the Tokyo region (asia-northeast1) is not yet available. Therefore, we will use us-central1.
Code
Based on the above, the Claude Haiku API could be called with the following Ruby code.
PROJECT_ID = "your-project-id"
LOCATION = "us-central1".freeze
MODEL = "claude-3-haiku@20240307".freeze
API_URL = "https://#{LOCATION}-aiplatform.googleapis.com/v1/projects/#{PROJECT_ID}/locations/#{LOCATION}/publishers/anthropic/models/#{MODEL}:rawPredict"
TEMPERATURE = 0.0
MAX_TOKENS = 512
# Use ADC (Application Default Credentials) to get the access token
credentials = Google::Auth.get_application_default
access_token = credentials.fetch_access_token!["access_token"]
uri = URI(API_URL)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request["Authorization"] = "Bearer #{access_token}"
request["Content-Type"] = "application/json; charset=utf-8"
request.body = {
anthropic_version: "vertex-2023-10-16",
messages: [
{
role: "user",
content: [
{
type: "text",
text: "こんにちは"
}
],
}
],
temperature: TEMPERATURE,
max_tokens: MAX_TOKENS,
stream: false,
}.to_json
response = http.request(request)
if response.code.to_i >= 400
raise "HTTP Request failed with status code: #{response.code} body: #{response.body}"
end
parsed_overall = JSON.parse(response.body)
result = "{#{parsed_overall['content'][0]['text']}"
puts result
Discussion