> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentops.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Google GenAI

> Google Generative AI Example with AgentOps

*View Notebook on [Github](https://github.com/AgentOps-AI/agentops/blob/main/examples/google_genai/gemini_example.ipynb)*

# Google Generative AI Example with AgentOps

This notebook demonstrates how to use AgentOps with Google's Generative AI package for observing both synchronous and streaming text generation.

## Installation

```bash pip theme={null}
pip install agentops google-genai
```

```bash poetry theme={null}
poetry add agentops google-genai
```

```bash uv theme={null}
uv pip install agentops google-genai
```

```python theme={null}
from google import genai
import agentops
from dotenv import load_dotenv
import os
```

```python theme={null}
load_dotenv()

os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
os.environ["GEMINI_API_KEY"] = os.getenv("GEMINI_API_KEY", "your_gemini_api_key_here")
```

```python theme={null}
# Initialize AgentOps and Gemini client
agentops.init(tags=["gemini-example", "agentops-example"])
client = genai.Client()
```

```python theme={null}
# Test synchronous generation
print("Testing synchronous generation:")
response = client.models.generate_content(model="gemini-1.5-flash", contents="What are the three laws of robotics?")
print(response.text)
```

```python theme={null}
# Test streaming generation
print("\nTesting streaming generation:")
response_stream = client.models.generate_content_stream(
    model="gemini-1.5-flash", contents="Explain the concept of machine learning in simple terms."
)

for chunk in response_stream:
    print(chunk.text, end="")
print()  # Add newline after streaming output

# Test another synchronous generation
print("\nTesting another synchronous generation:")
response = client.models.generate_content(
    model="gemini-1.5-flash", contents="What is the difference between supervised and unsupervised learning?"
)
print(response.text)
```

```python theme={null}
# Example of token counting
print("\nTesting token counting:")
token_response = client.models.count_tokens(
    model="gemini-1.5-flash", contents="This is a test sentence to count tokens."
)
print(f"Token count: {token_response.total_tokens}")
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.