agentops/docs/v2/integrations/google_generative_ai.mdx

115 lines
3.1 KiB
Plaintext

---
title: Google Generative AI
description: "Monitor and analyze your Google Gemini API calls with AgentOps"
---
AgentOps provides seamless integration with [Google's Generative AI API](https://ai.google.dev/), allowing you to monitor and analyze all your Gemini model interactions automatically.
## Installation
<CodeGroup>
```bash pip
pip install agentops google-genai
```
```bash poetry
poetry add agentops google-genai
```
```bash uv
uv pip install agentops google-genai
```
</CodeGroup>
## Setting Up API Keys
Before using Google Gemini with AgentOps, you need to set up your API keys. You can obtain:
- **GOOGLE_API_KEY**: From the [Google AI Studio](https://aistudio.google.com/app/apikey)
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
Then to set them up, you can either export them as environment variables or set them in a `.env` file.
<CodeGroup>
```bash Export to CLI
export GOOGLE_API_KEY="your_google_api_key_here"
export AGENTOPS_API_KEY="your_agentops_api_key_here"
```
```txt Set in .env file
GOOGLE_API_KEY="your_google_api_key_here"
AGENTOPS_API_KEY="your_agentops_api_key_here"
```
</CodeGroup>
Then load the environment variables in your Python code:
```python
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Set up environment variables with fallback values
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
```
## Usage
Initialize AgentOps at the beginning of your application to automatically track all Gemini API calls.
<CodeGroup>
```python Streaming
import agentops
from google import genai
# Initialize AgentOps
agentops.init()
# Create a client
client = genai.Client(api_key="YOUR_GEMINI_API_KEY")
# Generate streaming content
for chunk in client.models.generate_content_stream(
model='gemini-2.0-flash-001',
contents='Explain quantum computing in simple terms.',
):
print(chunk.text, end="", flush=True)
```
```python Simple Chat
import agentops
from google import genai
# Initialize AgentOps
agentops.init()
# Create a client
client = genai.Client(api_key="YOUR_GEMINI_API_KEY")
# Start a chat session
chat = client.chats.create(model='gemini-2.0-flash-001')
# Send messages and get responses
response = chat.send_message('Hello, how can you help me with AI development?')
print(response.text)
# Continue the conversation
response = chat.send_message('What are the best practices for prompt engineering?')
print(response.text)
```
</CodeGroup>
## Examples
<CardGroup cols={2}>
<Card title="Gemini Quickstart Notebook" icon="notebook" href="/v2/examples/google_generative_ai">
Basic Gemini usage with AgentOps
</Card>
</CardGroup>
For more information on using the Google Gen AI SDK, refer to the [official documentation](https://googleapis.github.io/python-genai/).
<script type="module" src="/scripts/github_stars.js"></script>
<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>
<script type="module" src="/scripts/button_heartbeat_animation.js"></script>
<script type="css" src="/styles/styles.css"></script>