|
| 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() |
0 commit comments