Skip to content

Commit de1ef9c

Browse files
Adding examples for BigQuery integrations with Anthropic Claude model (GoogleCloudPlatform#1341)
* adding bq integrations with Anthropic Claude model * adding the bqml claude examples to format checker skiplist given the current helper does not support JDK 21 code.
1 parent 879102c commit de1ef9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+19954
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import functions_framework
2+
from anthropic import AnthropicVertex
3+
#from flask import jsonify, Flask, request, Response
4+
import os
5+
import json
6+
import flask
7+
8+
9+
LOCATION="europe-west1" # or "us-east5"
10+
11+
12+
client = AnthropicVertex(region=LOCATION, project_id="[yourprojectid]")
13+
14+
15+
@functions_framework.http
16+
def claude_http(request: flask.Request) -> flask.Response:
17+
"""HTTP Cloud Function.
18+
Args:
19+
request (flask.Request): The request object.
20+
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
21+
Returns:
22+
The response text, or any set of values that can be turned into a
23+
Response object using `make_response`
24+
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
25+
"""
26+
request_json = request.get_json()
27+
calls=request_json['calls']
28+
29+
30+
result = []
31+
for call in calls:
32+
message = client.messages.create(
33+
max_tokens=1024,
34+
messages=[
35+
{
36+
"role": "user",
37+
"content": str(call),
38+
}
39+
],
40+
model="claude-3-5-sonnet@20240620",
41+
)
42+
content_text = message.content[0].text if message.content else ""
43+
result.append(content_text)
44+
45+
# Return a JSON object with all results
46+
return flask.make_response(flask.jsonify({"replies": result}))
47+
#return result
48+
49+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
functions-framework==3.*
2+
anthropic[vertex]
3+
gunicorn==22.0.0
4+
python-dotenv==1.0.1
5+
Flask==3.0.3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)