Skip to content

Commit 4981bd4

Browse files
authored
Merge pull request #1 from getsentry/vg/add-python-genai-scripts
add python-genai test scripts
2 parents 7505a9c + dd59313 commit 4981bd4

File tree

7 files changed

+1099
-0
lines changed

7 files changed

+1099
-0
lines changed

test-google-genai/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Google GenAI / Vertex AI Test
2+
3+
This project demonstrates how to use Google's GenAI API with Vertex AI authentication.
4+
5+
## Prerequisites
6+
7+
1. Python 3.8+
8+
2. Google Cloud Project with Vertex AI API enabled (there is already one in Sentry)
9+
3. Authentication credentials (see setup below)
10+
11+
## Authentication Setup
12+
13+
In shared 1Password there is a shared secret `Vertex API Key JSON` which contains JSON credentials for service account set up.
14+
15+
All the scripts in this directory rely on using that service account as an authentication to Vertex APIs, we don't have an API key.
16+
17+
To set it up, copy the content of the secret to a JSON file, and the path of the file should be store in `GOOGLE_APPLICATION_CREDENTIALS`.
18+
19+
Ask on Slack for the values for `GOOGLE_VERTEX_LOCATION` and `GOOGLE_VERTEX_PROJECT` env variables.
20+
21+
## Configure
22+
23+
Set the following environment variables in `.env` file:
24+
25+
- `SENTRY_DSN`
26+
- `GOOGLE_APPLICATION_CREDENTIALS`
27+
- `GOOGLE_VERTEX_LOCATION`
28+
- `GOOGLE_VERTEX_PROJECT`
29+
30+
# Example scripts
31+
32+
Docs are available at README.md in https://github.com/googleapis/python-genai or at https://googleapis.github.io/python-genai/.
33+
34+
- `client.py` - runs the basic example from the docs
35+
- `client_content_stream.py` - runs the content stream example from the documentation
36+
- `client_async.py` - runs the async example from the documentation
37+
- `chats.py` - runs the chats example from the documentation

test-google-genai/chats.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from dotenv import load_dotenv
2+
from google import genai
3+
from google.genai.types import HttpOptions
4+
import os
5+
6+
import sentry_sdk
7+
from sentry_sdk.integrations.google_genai import GoogleGenAIIntegration
8+
9+
load_dotenv()
10+
11+
sentry_sdk.init(
12+
dsn=os.environ["SENTRY_DSN"],
13+
# Add data like request headers and IP for users,
14+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
15+
send_default_pii=True,
16+
traces_sample_rate=1.0,
17+
integrations=[GoogleGenAIIntegration()],
18+
debug=True,
19+
)
20+
21+
22+
client = genai.Client(
23+
vertexai=True,
24+
project=os.environ["GOOGLE_VERTEX_PROJECT"],
25+
location=os.environ["GOOGLE_VERTEX_LOCATION"],
26+
http_options=HttpOptions(api_version="v1"),
27+
)
28+
29+
30+
def main():
31+
with sentry_sdk.start_transaction(op="test-transaction", name="test-chats"):
32+
chat = client.chats.create(model="gemini-2.0-flash")
33+
response = chat.send_message("What is the weather like in San Francisco, CA?")
34+
35+
print(response.text)
36+
37+
38+
if __name__ == "__main__":
39+
main()

test-google-genai/client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from dotenv import load_dotenv
2+
3+
from google import genai
4+
from google.genai import types
5+
from google.genai.types import HttpOptions
6+
import os
7+
8+
import sentry_sdk
9+
from sentry_sdk.integrations.google_genai import GoogleGenAIIntegration
10+
11+
load_dotenv()
12+
13+
sentry_sdk.init(
14+
dsn=os.environ["SENTRY_DSN"],
15+
# Add data like request headers and IP for users,
16+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
17+
send_default_pii=True,
18+
traces_sample_rate=1.0,
19+
integrations=[GoogleGenAIIntegration()],
20+
debug=True,
21+
)
22+
23+
client = genai.Client(
24+
vertexai=True,
25+
project=os.environ["GOOGLE_VERTEX_PROJECT"],
26+
location=os.environ["GOOGLE_VERTEX_LOCATION"],
27+
http_options=HttpOptions(api_version="v1"),
28+
)
29+
30+
31+
def get_current_weather(location: str) -> str:
32+
"""Returns the current weather.
33+
34+
Args:
35+
location: The city and state, e.g. San Francisco, CA
36+
"""
37+
return "sunny"
38+
39+
40+
google_search_retrieval_tool = {"google_search": {}}
41+
42+
43+
def main():
44+
with sentry_sdk.start_transaction(op="test-transaction", name="test"):
45+
response = client.models.generate_content(
46+
model="gemini-2.5-flash",
47+
contents="What is weather like in Boston, MA?",
48+
config=types.GenerateContentConfig(
49+
tools=[get_current_weather],
50+
system_instruction="You are a helpful assistant that can use tools to help answer questions.",
51+
temperature=0.2,
52+
),
53+
)
54+
55+
print(response.text)
56+
57+
58+
if __name__ == "__main__":
59+
main()

test-google-genai/client_async.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from dotenv import load_dotenv
2+
import asyncio
3+
from google import genai
4+
from google.genai import types
5+
from google.genai.types import HttpOptions
6+
import os
7+
8+
import sentry_sdk
9+
from sentry_sdk.integrations.google_genai import GoogleGenAIIntegration
10+
11+
load_dotenv()
12+
13+
sentry_sdk.init(
14+
dsn=os.environ["SENTRY_DSN"],
15+
# Add data like request headers and IP for users,
16+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
17+
send_default_pii=True,
18+
traces_sample_rate=1.0,
19+
integrations=[GoogleGenAIIntegration()],
20+
debug=True,
21+
)
22+
23+
aclient = genai.Client(
24+
vertexai=True,
25+
project=os.environ["GOOGLE_VERTEX_PROJECT"],
26+
location=os.environ["GOOGLE_VERTEX_LOCATION"],
27+
http_options=HttpOptions(api_version="v1"),
28+
).aio
29+
30+
31+
def get_current_weather(location: str) -> str:
32+
"""Returns the current weather.
33+
34+
Args:
35+
location: The city and state, e.g. San Francisco, CA
36+
"""
37+
return "sunny"
38+
39+
40+
async def main():
41+
with sentry_sdk.start_transaction(op="async-test-transaction", name="async-test"):
42+
response = await aclient.models.generate_content(
43+
model="gemini-2.5-flash",
44+
contents="What is weather like in Boston, MA?",
45+
config=types.GenerateContentConfig(
46+
tools=[get_current_weather],
47+
system_instruction="You are a helpful assistant that can use tools to help answer questions.",
48+
temperature=0.2,
49+
),
50+
)
51+
52+
print(response.text)
53+
54+
55+
asyncio.run(main())
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from dotenv import load_dotenv
2+
from google import genai
3+
from google.genai.types import HttpOptions
4+
import os
5+
6+
import sentry_sdk
7+
from sentry_sdk.integrations.google_genai import GoogleGenAIIntegration
8+
9+
load_dotenv()
10+
11+
sentry_sdk.init(
12+
dsn=os.environ["SENTRY_DSN"],
13+
# Add data like request headers and IP for users,
14+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
15+
send_default_pii=True,
16+
traces_sample_rate=1.0,
17+
integrations=[GoogleGenAIIntegration()],
18+
debug=True,
19+
)
20+
21+
22+
client = genai.Client(
23+
vertexai=True,
24+
project=os.environ["GOOGLE_VERTEX_PROJECT"],
25+
location=os.environ["GOOGLE_VERTEX_LOCATION"],
26+
http_options=HttpOptions(api_version="v1"),
27+
)
28+
29+
30+
def get_current_weather(location: str) -> str:
31+
"""Returns the current weather.
32+
33+
Args:
34+
location: The city and state, e.g. San Francisco, CA
35+
"""
36+
return "sunny"
37+
38+
39+
google_search_retrieval_tool = {"google_search": {}}
40+
41+
42+
def main():
43+
with sentry_sdk.start_transaction(op="test-transaction", name="test-streaming"):
44+
for chunk in client.models.generate_content_stream(
45+
model="gemini-2.5-flash",
46+
contents="Why is the sky blue?",
47+
):
48+
print(chunk.text, end="")
49+
50+
51+
if __name__ == "__main__":
52+
main()

test-google-genai/pyproject.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[project]
2+
name = "test-google-genai"
3+
version = "0.1.0"
4+
description = "Google GenAI / Vertex AI API testing with proper authentication"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = [
8+
"google-genai",
9+
"google-auth",
10+
"google-cloud-aiplatform",
11+
"sentry-sdk",
12+
"pip>=25.2",
13+
"python-dotenv>=1.1.1",
14+
]
15+
16+
[tool.uv.sources]
17+
sentry-sdk = { path = "../../sentry-python", editable = true }

0 commit comments

Comments
 (0)