|
| 1 | +# BigQuery Remote Function with Anthropic |
| 2 | + |
| 3 | +This README provides instructions for setting up a BigQuery Remote Function that uses Anthropic's Claude 3.5 Sonnet model via Google Cloud Functions. |
| 4 | + |
| 5 | +## References |
| 6 | +- [BQ Remote function Doc](https://cloud.google.com/bigquery/docs/remote-functions) |
| 7 | +- [Create Cloud Function Doc](https://cloud.google.com/functions/docs/create) |
| 8 | + |
| 9 | +# Required Access |
| 10 | +1. Developer needs to have the following roles: |
| 11 | + - Cloud Run Developer |
| 12 | + - Vertex AI User |
| 13 | + - BigQuery User |
| 14 | +2. Grant Service Accounts IDs access between different services (see Step 2) |
| 15 | + |
| 16 | +## Step 1: Enable Vertex AI API and Enable Claude Models on Vertex AI |
| 17 | +You can follow this documentation: [Link to Claude On Vertex AI Doc](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#before_you_begin_) |
| 18 | + |
| 19 | +## Step 2: Create Cloud Function using the [Create Cloud Function Doc](https://cloud.google.com/functions/docs/create) |
| 20 | +You can use the provided Sample Code below or in the /CloudFunction folder in this folder |
| 21 | +### requirements.txt |
| 22 | +``` |
| 23 | +functions-framework==3.* |
| 24 | +anthropic[vertex] |
| 25 | +gunicorn==22.0.0 |
| 26 | +python-dotenv==1.0.1 |
| 27 | +Flask==3.0.3 |
| 28 | +``` |
| 29 | + |
| 30 | +### main.py |
| 31 | +```python |
| 32 | +import functions_framework |
| 33 | +from anthropic import AnthropicVertex |
| 34 | +import os |
| 35 | +import json |
| 36 | +import flask |
| 37 | + |
| 38 | +LOCATION = "europe-west1" # or "us-east5" |
| 39 | + |
| 40 | +client = AnthropicVertex(region=LOCATION, project_id="[yourprojectid]") |
| 41 | + |
| 42 | +@functions_framework.http |
| 43 | +def claude_http(request: flask.Request) -> flask.Response: |
| 44 | + request_json = request.get_json() |
| 45 | + calls = request_json['calls'] |
| 46 | + |
| 47 | + result = [] |
| 48 | + for call in calls: |
| 49 | + message = client.messages.create( |
| 50 | + max_tokens=1024, |
| 51 | + messages=[ |
| 52 | + { |
| 53 | + "role": "user", |
| 54 | + "content": str(call), |
| 55 | + } |
| 56 | + ], |
| 57 | + model="claude-3-5-sonnet@20240620", |
| 58 | + ) |
| 59 | + content_text = message.content[0].text if message.content else "" |
| 60 | + result.append(content_text) |
| 61 | + |
| 62 | + return flask.make_response(flask.jsonify({"replies": result})) |
| 63 | +``` |
| 64 | + |
| 65 | +### Test the function with sample event |
| 66 | +```json |
| 67 | +{ |
| 68 | + "calls": [ |
| 69 | + "What is the capital of France?", |
| 70 | + "Explain the concept of photosynthesis in simple terms.", |
| 71 | + "Write a haiku about artificial intelligence." |
| 72 | + ] |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## Step 3: Create BQ Remote Function Connection |
| 77 | +Follow the [documentation](https://cloud.google.com/bigquery/docs/remote-functions#create_a_connection) to create a BigQuery Remote Function Connection and set up proper IAM access. Additionally, grant Cloud Run access to the Vertex AI user role. |
| 78 | + |
| 79 | +## Step 4: Create BQ Remote Function |
| 80 | +```sql |
| 81 | +CREATE OR REPLACE FUNCTION |
| 82 | + `[yourproject].[yourdataset].claude35Sonnet`(prompt STRING) RETURNS STRING |
| 83 | +REMOTE WITH CONNECTION `[yourproject].us.llm_connection` |
| 84 | +OPTIONS (endpoint = 'https://[YOUR Function URI....]', max_batching_rows = 1); |
| 85 | +``` |
| 86 | + |
| 87 | +## Step 5: Query the Function |
| 88 | +```sql |
| 89 | +SELECT |
| 90 | + title, |
| 91 | + `[yourproject].[yourdataset]`.claude35Sonnet(CONCAT("translate this into English and only return the translated result:", title)) AS translated_title |
| 92 | +FROM [yourprojectid].[your table] |
| 93 | +WHERE title IS NOT NULL |
| 94 | +LIMIT 1; |
| 95 | +``` |
| 96 | + |
| 97 | +This README provides a step-by-step guide to set up and use a BigQuery Remote Function with Anthropic's Claude model. |
| 98 | + |
0 commit comments