This README provides instructions for setting up a BigQuery Remote Function that uses Anthropic's Claude 3.5 Sonnet model via Google Cloud Functions.
- Developer needs to have the following roles:
- Cloud Run Developer
- Vertex AI User
- BigQuery User
- Grant Service Accounts IDs access between different services (see Step 2)
You can follow this documentation: Link to Claude On Vertex AI Doc
Step 2: Create Cloud Function using the Create Cloud Function Doc
You can use the provided Sample Code below or in the /CloudFunction folder in this folder
functions-framework==3.*
anthropic[vertex]
gunicorn==22.0.0
python-dotenv==1.0.1
Flask==3.0.3
import functions_framework
from anthropic import AnthropicVertex
import os
import json
import flask
LOCATION = "europe-west1" # or "us-east5"
client = AnthropicVertex(region=LOCATION, project_id="[yourprojectid]")
@functions_framework.http
def claude_http(request: flask.Request) -> flask.Response:
request_json = request.get_json()
calls = request_json['calls']
result = []
for call in calls:
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": str(call),
}
],
model="claude-3-5-sonnet@20240620",
)
content_text = message.content[0].text if message.content else ""
result.append(content_text)
return flask.make_response(flask.jsonify({"replies": result}))
{
"calls": [
"What is the capital of France?",
"Explain the concept of photosynthesis in simple terms.",
"Write a haiku about artificial intelligence."
]
}
Follow the documentation to create a BigQuery Remote Function Connection and set up proper IAM access. Additionally, grant Cloud Run access to the Vertex AI user role.
CREATE OR REPLACE FUNCTION
`[yourproject].[yourdataset].claude35Sonnet`(prompt STRING) RETURNS STRING
REMOTE WITH CONNECTION `[yourproject].us.llm_connection`
OPTIONS (endpoint = 'https://[YOUR Function URI....]', max_batching_rows = 1);
SELECT
title,
`[yourproject].[yourdataset]`.claude35Sonnet(CONCAT("translate this into English and only return the translated result:", title)) AS translated_title
FROM [yourprojectid].[your table]
WHERE title IS NOT NULL
LIMIT 1;
This README provides a step-by-step guide to set up and use a BigQuery Remote Function with Anthropic's Claude model.