同步完整源码 - 2026-05-25
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
---
|
||||
title: 'Core Concepts'
|
||||
description: 'Understanding the fundamental concepts of AgentOps'
|
||||
---
|
||||
|
||||
# The AgentOps SDK Architecture
|
||||
|
||||
AgentOps is designed to provide comprehensive monitoring and analytics for AI agent workflows with minimal implementation effort. The SDK follows these key design principles:
|
||||
|
||||
## Automated Instrumentation
|
||||
|
||||
After calling `agentops.init()`, the SDK automatically identifies installed LLM providers and instruments their API calls. This allows AgentOps to capture interactions between your code and the LLM providers to collect data for your dashboard without requiring manual instrumentation for every call.
|
||||
|
||||
## Declarative Tracing with Decorators
|
||||
|
||||
The [decorators](/v2/concepts/decorators) system allows you to add tracing to your existing functions and classes with minimal code changes. Decorators create hierarchical spans that provide a structured view of your agent's operations for monitoring and analysis.
|
||||
|
||||
## OpenTelemetry Foundation
|
||||
|
||||
AgentOps is built on [OpenTelemetry](https://opentelemetry.io/), a widely-adopted standard for observability instrumentation. This provides a robust and standardized approach to collecting, processing, and exporting telemetry data.
|
||||
|
||||
# Sessions
|
||||
|
||||
A [Session](/v2/concepts/sessions) represents a single user interaction with your agent. When you initialize AgentOps using the `init` function, a session is automatically created for you:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps with automatic session creation
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
```
|
||||
|
||||
By default, all events and API calls will be associated with this session. For more advanced use cases, you can control session creation manually:
|
||||
|
||||
```python
|
||||
# Initialize without auto-starting a session
|
||||
agentops.init(api_key="YOUR_API_KEY", auto_start_session=False)
|
||||
|
||||
# Later, manually start a session when needed
|
||||
agentops.start_session(tags=["customer-query"])
|
||||
```
|
||||
|
||||
# Span Hierarchy
|
||||
|
||||
In AgentOps, activities are organized into a hierarchical structure of spans:
|
||||
|
||||
- **SESSION**: The root container for all activities in a single execution of your workflow
|
||||
- **AGENT**: Represents an autonomous entity with specialized capabilities
|
||||
- **WORKFLOW**: A logical grouping of related operations
|
||||
- **OPERATION/TASK**: A specific task or function performed by an agent
|
||||
- **LLM**: An interaction with a language model
|
||||
- **TOOL**: The use of a tool or API by an agent
|
||||
|
||||
This hierarchy creates a complete trace of your agent's execution:
|
||||
|
||||
```
|
||||
SESSION
|
||||
├── AGENT
|
||||
│ ├── OPERATION/TASK
|
||||
│ │ ├── LLM
|
||||
│ │ └── TOOL
|
||||
│ └── WORKFLOW
|
||||
│ └── OPERATION/TASK
|
||||
└── LLM (unattributed to a specific agent)
|
||||
```
|
||||
|
||||
# Agents
|
||||
|
||||
An **Agent** represents a component in your application that performs tasks. You can create and track agents using the `@agent` decorator:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, operation
|
||||
|
||||
@agent(name="customer_service")
|
||||
class CustomerServiceAgent:
|
||||
@operation
|
||||
def answer_query(self, query):
|
||||
# Agent logic here
|
||||
pass
|
||||
```
|
||||
|
||||
# LLM Events
|
||||
|
||||
AgentOps automatically tracks LLM API calls from supported providers, collecting valuable information like:
|
||||
|
||||
- **Model**: The specific model used (e.g., "gpt-4", "claude-3-opus")
|
||||
- **Provider**: The LLM provider (e.g., "OpenAI", "Anthropic")
|
||||
- **Prompt Tokens**: Number of tokens in the input
|
||||
- **Completion Tokens**: Number of tokens in the output
|
||||
- **Cost**: The estimated cost of the interaction
|
||||
- **Messages**: The prompt and completion content
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
# Initialize the OpenAI client
|
||||
client = OpenAI()
|
||||
|
||||
# This LLM call is automatically tracked
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=[{"role": "user", "content": "What's the capital of France?"}]
|
||||
)
|
||||
```
|
||||
|
||||
# Tags
|
||||
|
||||
[Tags](/v2/concepts/tags) help you organize and filter your sessions. You can add tags when initializing AgentOps or when starting a session:
|
||||
|
||||
```python
|
||||
# Add tags when initializing
|
||||
agentops.init(api_key="YOUR_API_KEY", tags=["production", "web-app"])
|
||||
|
||||
# Or when manually starting a session
|
||||
agentops.start_session(tags=["customer-service", "tier-1"])
|
||||
```
|
||||
|
||||
# Host Environment
|
||||
|
||||
AgentOps automatically collects basic [information](/v2/concepts/host-env) about the environment where your agent is running:
|
||||
|
||||
- **Operating System**: The OS type and version
|
||||
- **Python Version**: The version of Python being used
|
||||
- **Hostname**: The name of the host machine (anonymized)
|
||||
- **SDK Version**: The version of the AgentOps SDK being used
|
||||
|
||||
# Dashboard Views
|
||||
|
||||
The AgentOps dashboard provides several ways to visualize and analyze your agent's performance:
|
||||
|
||||
- **Session List**: Overview of all sessions with filtering options
|
||||
- **Timeline View**: Chronological display of spans showing duration and relationships
|
||||
- **Tree View**: Hierarchical representation of spans showing parent-child relationships
|
||||
- **Message View**: Detailed view of LLM interactions with prompt and completion content
|
||||
- **Analytics**: Aggregated metrics across sessions and operations
|
||||
|
||||
# Putting It All Together
|
||||
|
||||
A typical implementation looks like this:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
from agentops.sdk.decorators import agent, operation
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY", tags=["production"])
|
||||
|
||||
# Define an agent
|
||||
@agent(name="assistant")
|
||||
class AssistantAgent:
|
||||
def __init__(self):
|
||||
self.client = OpenAI()
|
||||
|
||||
@operation
|
||||
def answer_question(self, question):
|
||||
# This LLM call will be automatically tracked and associated with this agent
|
||||
response = self.client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=[{"role": "user", "content": question}]
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
|
||||
def workflow():
|
||||
# Use the agent
|
||||
assistant = AssistantAgent()
|
||||
answer = assistant.answer_question("What's the capital of France?")
|
||||
print(answer)
|
||||
|
||||
workflow()
|
||||
# Session is automatically tracked until application terminates
|
||||
```
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,385 @@
|
||||
---
|
||||
title: 'Decorators'
|
||||
description: 'Use decorators to track activities in your agent system'
|
||||
---
|
||||
## Available Decorators
|
||||
|
||||
AgentOps provides the following decorators:
|
||||
|
||||
| Decorator | Purpose | Creates |
|
||||
|-----------|---------|---------|
|
||||
| `@session` | Track an entire user interaction | SESSION span |
|
||||
| `@agent` | Track agent classes and their lifecycle | AGENT span |
|
||||
| `@operation` | Track discrete operations performed by agents | OPERATION span |
|
||||
| `@workflow` | Track a sequence of operations | WORKFLOW span |
|
||||
| `@task` | Track smaller units of work (similar to operations) | TASK span |
|
||||
| `@tool` | Track tool usage and cost in agent operations | TOOL span |
|
||||
| `@guardrail` | Track guardrail input and output | GUARDRAIL span |
|
||||
|
||||
## Decorator Hierarchy
|
||||
|
||||
The decorators create spans that form a hierarchy:
|
||||
|
||||
```
|
||||
SESSION
|
||||
├── AGENT
|
||||
│ ├── OPERATION or TASK
|
||||
│ │ ├── LLM
|
||||
│ │ └── TOOL
|
||||
│ └── WORKFLOW
|
||||
│ └── OPERATION or TASK
|
||||
└── AGENT
|
||||
└── OPERATION or TASK
|
||||
```
|
||||
|
||||
## Using Decorators
|
||||
|
||||
### @session
|
||||
|
||||
The `@session` decorator tracks an entire user interaction from start to finish:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import session
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
@session
|
||||
def answer_question(question):
|
||||
# Create and use agents
|
||||
weather_agent = WeatherAgent()
|
||||
result = weather_agent.get_forecast(question)
|
||||
|
||||
# Return the final result
|
||||
return result
|
||||
```
|
||||
|
||||
Each `@session` function call creates a new session span that contains all the agents, operations, and workflows used during that interaction.
|
||||
|
||||
### @agent
|
||||
|
||||
The `@agent` decorator instruments a class to track its lifecycle and operations:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, operation
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
@agent
|
||||
class WeatherAgent:
|
||||
def __init__(self):
|
||||
self.api_key = "weather_api_key"
|
||||
|
||||
@operation
|
||||
def get_forecast(self, location):
|
||||
# Get weather data
|
||||
return f"The weather in {location} is sunny."
|
||||
|
||||
def check_weather(city):
|
||||
weather_agent = WeatherAgent()
|
||||
forecast = weather_agent.get_forecast(city)
|
||||
return forecast
|
||||
|
||||
weather_info = check_weather("San Francisco")
|
||||
```
|
||||
|
||||
When an agent-decorated class is instantiated within a session, an AGENT span is created automatically.
|
||||
|
||||
### @operation
|
||||
|
||||
The `@operation` decorator tracks discrete functions performed by an agent:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, operation
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
@agent
|
||||
class MathAgent:
|
||||
@operation
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
@operation
|
||||
def multiply(self, a, b):
|
||||
return a * b
|
||||
|
||||
def calculate(x, y):
|
||||
math_agent = MathAgent()
|
||||
sum_result = math_agent.add(x, y)
|
||||
product_result = math_agent.multiply(x, y)
|
||||
return {"sum": sum_result, "product": product_result}
|
||||
|
||||
results = calculate(5, 3)
|
||||
```
|
||||
|
||||
Operations represent the smallest meaningful units of work in your agent system. Each operation creates an OPERATION span with:
|
||||
- Inputs (function arguments)
|
||||
- Output (return value)
|
||||
- Duration
|
||||
- Success/failure status
|
||||
|
||||
### @workflow
|
||||
|
||||
The `@workflow` decorator tracks a sequence of operations that work together:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, operation, workflow
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
@agent
|
||||
class TravelAgent:
|
||||
def __init__(self):
|
||||
self.flight_api = FlightAPI()
|
||||
self.hotel_api = HotelAPI()
|
||||
|
||||
@workflow
|
||||
def plan_trip(self, destination, dates):
|
||||
# This workflow contains multiple operations
|
||||
flights = self.find_flights(destination, dates)
|
||||
hotels = self.find_hotels(destination, dates)
|
||||
|
||||
return {
|
||||
"flights": flights,
|
||||
"hotels": hotels
|
||||
}
|
||||
|
||||
@operation
|
||||
def find_flights(self, destination, dates):
|
||||
return self.flight_api.search(destination, dates)
|
||||
|
||||
@operation
|
||||
def find_hotels(self, destination, dates):
|
||||
return self.hotel_api.search(destination, dates)
|
||||
```
|
||||
|
||||
Workflows help you organize related operations and see their collective performance.
|
||||
|
||||
### @task
|
||||
|
||||
The `@task` decorator is similar to `@operation` but can be used for smaller units of work:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, task
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
@agent
|
||||
class DataProcessor:
|
||||
@task
|
||||
def normalize_data(self, data):
|
||||
# Normalize the data
|
||||
return [x / sum(data) for x in data]
|
||||
|
||||
@task
|
||||
def filter_outliers(self, data, threshold=3):
|
||||
# Filter outliers
|
||||
mean = sum(data) / len(data)
|
||||
std_dev = (sum((x - mean) ** 2 for x in data) / len(data)) ** 0.5
|
||||
|
||||
return [x for x in data if abs(x - mean) <= threshold * std_dev]
|
||||
```
|
||||
|
||||
The `@task` and `@operation` decorators function identically (they are aliases in the codebase), and you can choose the one that best fits your semantic needs.
|
||||
|
||||
### @tool
|
||||
|
||||
The `@tool` decorator tracks tool usage within agent operations and supports cost tracking. It works with all function types: synchronous, asynchronous, generator, and async generator.
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, tool
|
||||
import asyncio
|
||||
|
||||
@agent
|
||||
class ProcessingAgent:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@tool(cost=0.01)
|
||||
def sync_tool(self, item):
|
||||
"""Synchronous tool with cost tracking."""
|
||||
return f"Processed {item}"
|
||||
|
||||
@tool(cost=0.02)
|
||||
async def async_tool(self, item):
|
||||
"""Asynchronous tool with cost tracking."""
|
||||
await asyncio.sleep(0.1)
|
||||
return f"Async processed {item}"
|
||||
|
||||
@tool(cost=0.03)
|
||||
def generator_tool(self, items):
|
||||
"""Generator tool with cost tracking."""
|
||||
for item in items:
|
||||
yield self.sync_tool(item)
|
||||
|
||||
@tool(cost=0.04)
|
||||
async def async_generator_tool(self, items):
|
||||
"""Async generator tool with cost tracking."""
|
||||
for item in items:
|
||||
await asyncio.sleep(0.1)
|
||||
yield await self.async_tool(item)
|
||||
```
|
||||
|
||||
The tool decorator provides:
|
||||
- Cost tracking for each tool call
|
||||
- Proper span creation and nesting
|
||||
- Support for all function types (sync, async, generator, async generator)
|
||||
- Cost accumulation in generator and async generator operations
|
||||
|
||||
### @guardrail
|
||||
|
||||
The `@guardrail` decorator tracks guardrail input and output. You can specify the guardrail type (`"input"` or `"output"`) with the `spec` parameter.
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import guardrail
|
||||
import agentops
|
||||
import re
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
@guardrail(spec="input")
|
||||
def secret_key_guardrail(input):
|
||||
pattern = r'\bsk-[a-zA-Z0-9]{10,}\b'
|
||||
result = True if re.search(pattern, input) else False
|
||||
return {
|
||||
"tripwire_triggered" : result
|
||||
}
|
||||
```
|
||||
|
||||
## Decorator Attributes
|
||||
|
||||
You can pass additional attributes to decorators:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, operation
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
@agent(name="custom_agent_name", attributes={"version": "1.0"})
|
||||
class CustomAgent:
|
||||
@operation(name="custom_operation", attributes={"importance": "high"})
|
||||
def process(self, data):
|
||||
return data
|
||||
```
|
||||
|
||||
Common attributes include:
|
||||
|
||||
| Attribute | Description | Example |
|
||||
|-----------|-------------|---------|
|
||||
| `name` | Custom name for the span | `name="weather_forecast"` |
|
||||
| `attributes` | Dictionary of custom attributes | `attributes={"model": "gpt-4"}` |
|
||||
|
||||
## Complete Example
|
||||
|
||||
Here's a complete example using all the decorators together:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import session, agent, operation, workflow, task
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
@session
|
||||
def assist_user(query):
|
||||
# Create the main assistant
|
||||
assistant = Assistant()
|
||||
|
||||
# Process the query
|
||||
return assistant.process_query(query)
|
||||
|
||||
@agent
|
||||
class Assistant:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@workflow
|
||||
def process_query(self, query):
|
||||
research_agent = ResearchAgent()
|
||||
writing_agent = WritingAgent()
|
||||
|
||||
# Research phase
|
||||
research = research_agent.gather_information(query)
|
||||
|
||||
# Writing phase
|
||||
response = writing_agent.generate_response(query, research)
|
||||
|
||||
return response
|
||||
|
||||
@agent
|
||||
class ResearchAgent:
|
||||
@operation
|
||||
def gather_information(self, query):
|
||||
# Perform web search
|
||||
search_results = self.search(query)
|
||||
|
||||
# Analyze results
|
||||
return self.analyze_results(search_results)
|
||||
|
||||
@task
|
||||
def search(self, query):
|
||||
# Simulate web search
|
||||
return [f"Result for {query}", f"Another result for {query}"]
|
||||
|
||||
@task
|
||||
def analyze_results(self, results):
|
||||
# Analyze search results
|
||||
return {"summary": "Analysis of " + ", ".join(results)}
|
||||
|
||||
@agent
|
||||
class WritingAgent:
|
||||
@operation
|
||||
def generate_response(self, query, research):
|
||||
# Generate a response based on the research
|
||||
return f"Answer to '{query}' based on: {research['summary']}"
|
||||
|
||||
assist_user("What is the capital of France?")
|
||||
```
|
||||
|
||||
In this example:
|
||||
1. The `@session` decorator wraps the entire interaction
|
||||
2. The `@agent` decorator defines multiple agent classes
|
||||
3. The `@workflow` decorator creates a workflow that coordinates agents
|
||||
4. The `@operation` and `@task` decorators track individual operations
|
||||
5. All spans are properly nested in the hierarchy
|
||||
|
||||
Note that LLM and TOOL spans are automatically created when you use compatible LLM libraries or tool integrations.
|
||||
|
||||
## Best Practices
|
||||
|
||||
- **Use @session for top-level functions** that represent complete user interactions
|
||||
- **Apply @agent to classes** that represent distinct components of your system
|
||||
- **Use @operation for significant functions** that represent complete units of work
|
||||
- **Use @task for smaller functions** that are part of larger operations
|
||||
- **Apply @workflow to methods** that coordinate multiple operations
|
||||
- **Keep decorator nesting consistent** with the logical hierarchy of your code
|
||||
- **Add custom attributes** to provide additional context for analysis
|
||||
- **Use meaningful names** for all decorated components
|
||||
|
||||
## Dashboard Visualization
|
||||
|
||||
In the AgentOps dashboard, decorators create spans that appear in:
|
||||
|
||||
1. **Timeline View**: Shows the execution sequence and duration
|
||||
2. **Hierarchy View**: Displays the parent-child relationships
|
||||
3. **Detail Panels**: Shows inputs, outputs, and attributes
|
||||
4. **Performance Metrics**: Tracks execution times and success rates
|
||||
|
||||
This visualization helps you understand the flow and performance of your agent system.
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
title: 'Host Environment'
|
||||
description: 'Automatically collected information about the environment where your agent runs'
|
||||
---
|
||||
## Collected Information
|
||||
|
||||
The following information is automatically collected:
|
||||
|
||||
- **Operating System**: The OS type and version (e.g., "Linux", "Windows", "macOS")
|
||||
- **Python Version**: The version of Python being used
|
||||
- **Hostname**: The name of the host machine (anonymized)
|
||||
- **AgentOps SDK Version**: The version of the AgentOps SDK being used
|
||||
- **Process ID**: The ID of the process running the agent
|
||||
|
||||
## Usage in Analytics
|
||||
|
||||
Host environment information enables:
|
||||
|
||||
- Identifying environment-specific issues
|
||||
- Tracking performance across different platforms
|
||||
- Ensuring compatibility with specific OS versions
|
||||
- Monitoring SDK version adoption
|
||||
|
||||
## Privacy Considerations
|
||||
|
||||
AgentOps is designed with privacy in mind:
|
||||
|
||||
- No personally identifiable information is collected
|
||||
- Hostnames are anonymized
|
||||
- No network scanning or detailed system analysis is performed
|
||||
- Only the minimum necessary information for debugging is gathered
|
||||
|
||||
## Disabling Host Environment Collection
|
||||
|
||||
If needed, you can disable host environment collection when initializing AgentOps:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Disable host environment collection
|
||||
agentops.init(api_key="YOUR_API_KEY", env_data_opt_out=True)
|
||||
```
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,143 @@
|
||||
---
|
||||
title: 'Spans'
|
||||
description: 'Understanding the different types of spans in AgentOps'
|
||||
---
|
||||
## Core Span Types
|
||||
|
||||
AgentOps organizes all spans with specific kinds:
|
||||
|
||||
| Span Kind | Description |
|
||||
|-----------|-------------|
|
||||
| `SESSION` | The root container for all activities in a single execution of your workflow |
|
||||
| `AGENT` | Represents an autonomous entity with specialized capabilities |
|
||||
| `WORKFLOW` | A logical grouping of related operations |
|
||||
| `OPERATION` | A specific task or function performed by an agent |
|
||||
| `TASK` | Alias for OPERATION, used interchangeably |
|
||||
| `LLM` | An interaction with a language model |
|
||||
| `TOOL` | The use of a tool or API by an agent |
|
||||
|
||||
## Span Hierarchy
|
||||
|
||||
Spans in AgentOps are organized hierarchically:
|
||||
|
||||
```
|
||||
SESSION
|
||||
├── AGENT
|
||||
│ ├── OPERATION/TASK
|
||||
│ │ ├── LLM
|
||||
│ │ └── TOOL
|
||||
│ └── WORKFLOW
|
||||
│ └── OPERATION/TASK
|
||||
└── LLM (unattributed to a specific agent)
|
||||
```
|
||||
|
||||
Every span exists within the context of a session, and most spans (other than the session itself) have a parent span that provides context.
|
||||
|
||||
## Span Attributes
|
||||
|
||||
All spans in AgentOps include:
|
||||
|
||||
- **ID**: A unique identifier
|
||||
- **Name**: A descriptive name
|
||||
- **Kind**: The type of span (SESSION, AGENT, etc.)
|
||||
- **Start Time**: When the span began
|
||||
- **End Time**: When the span completed
|
||||
- **Status**: Success or error status
|
||||
- **Attributes**: Key-value pairs with additional metadata
|
||||
|
||||
Different span types have specialized attributes:
|
||||
|
||||
### LLM Spans
|
||||
|
||||
LLM spans track interactions with large language models and include:
|
||||
|
||||
- **Model**: The specific model used (e.g., "gpt-4", "claude-3-opus")
|
||||
- **Provider**: The LLM provider (e.g., "OpenAI", "Anthropic")
|
||||
- **Prompt Tokens**: Number of tokens in the input
|
||||
- **Completion Tokens**: Number of tokens in the output
|
||||
- **Cost**: The estimated cost of the interaction
|
||||
- **Messages**: The prompt and completion content
|
||||
|
||||
### Tool Spans
|
||||
|
||||
Tool spans track the use of tools or APIs and include:
|
||||
|
||||
- **Tool Name**: The name of the tool used
|
||||
- **Input**: The data provided to the tool
|
||||
- **Output**: The result returned by the tool
|
||||
- **Duration**: How long the tool operation took
|
||||
|
||||
### Operation/Task Spans
|
||||
|
||||
Operation spans track specific functions or tasks:
|
||||
|
||||
- **Operation Type**: The kind of operation performed
|
||||
- **Parameters**: Input parameters to the operation
|
||||
- **Result**: The output of the operation
|
||||
- **Duration**: How long the operation took
|
||||
|
||||
## Creating Spans
|
||||
|
||||
There are several ways to create spans in AgentOps:
|
||||
|
||||
### Using Decorators
|
||||
|
||||
The recommended way to create spans is using decorators:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, operation, session, workflow, task
|
||||
|
||||
@session
|
||||
def my_workflow():
|
||||
agent_instance = MyAgent()
|
||||
return agent_instance.perform_task()
|
||||
|
||||
@agent
|
||||
class MyAgent:
|
||||
@operation
|
||||
def perform_task(self):
|
||||
# Perform the task
|
||||
return result
|
||||
```
|
||||
|
||||
### Automatic Instrumentation
|
||||
|
||||
AgentOps automatically instruments LLM API calls from supported providers when `auto_instrument=True` (the default):
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="YOUR_API_KEY")
|
||||
|
||||
# Initialize the OpenAI client
|
||||
client = OpenAI()
|
||||
|
||||
# This LLM call will be automatically tracked
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=[{"role": "user", "content": "Hello!"}]
|
||||
)
|
||||
```
|
||||
|
||||
## Viewing Spans in the Dashboard
|
||||
|
||||
All recorded spans are visible in the AgentOps dashboard:
|
||||
|
||||
1. **Timeline View**: Shows the sequence and duration of spans
|
||||
2. **Tree View**: Displays the hierarchical relationship between spans
|
||||
3. **Details Panel**: Provides in-depth information about each span
|
||||
4. **Analytics**: Aggregates statistics across spans
|
||||
|
||||
## Best Practices
|
||||
|
||||
- Use descriptive names for spans to make them easily identifiable
|
||||
- Create a logical hierarchy with sessions, agents, and operations
|
||||
- Record relevant parameters and results for better debugging
|
||||
- Use consistent naming conventions for span types
|
||||
- Track costs and token usage to monitor resource consumption
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,95 @@
|
||||
---
|
||||
title: 'Tags'
|
||||
description: 'Organize and filter your sessions with customizable tags'
|
||||
---
|
||||
## Adding Tags
|
||||
|
||||
You can add tags when initializing AgentOps, whicreh is the most common approach:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps with tags
|
||||
agentops.init(
|
||||
api_key="YOUR_API_KEY",
|
||||
default_tags=["production", "customer-service", "gpt-4"]
|
||||
)
|
||||
```
|
||||
|
||||
Alternatively, when using manual trace creation:
|
||||
|
||||
```python
|
||||
# Initialize without auto-starting a session
|
||||
agentops.init(api_key="YOUR_API_KEY", auto_start_session=False)
|
||||
|
||||
# Later start a trace with specific tags (modern approach)
|
||||
trace = agentops.start_trace(trace_name="test_workflow", default_tags=["development", "testing", "claude-3"])
|
||||
```
|
||||
|
||||
<Note>
|
||||
Legacy approach using `agentops.start_session(default_tags=["development", "testing", "claude-3"])` is deprecated and will be removed in v4.0. Use `agentops.start_trace()` instead.
|
||||
</Note>
|
||||
|
||||
## Tag Use Cases
|
||||
|
||||
Tags can be used for various purposes:
|
||||
|
||||
### Environment Identification
|
||||
|
||||
Tag sessions based on their environment:
|
||||
|
||||
```python
|
||||
default_tags=["production"] # or ["development", "staging", "testing"]
|
||||
```
|
||||
|
||||
### Feature Tracking
|
||||
|
||||
Tag sessions related to specific features or components:
|
||||
|
||||
```python
|
||||
default_tags=["search-functionality", "user-authentication", "content-generation"]
|
||||
```
|
||||
|
||||
### User Segmentation
|
||||
|
||||
Tag sessions based on user characteristics:
|
||||
|
||||
```python
|
||||
default_tags=["premium-user", "new-user", "enterprise-customer"]
|
||||
```
|
||||
|
||||
### Experiment Tracking
|
||||
|
||||
Tag sessions as part of specific experiments:
|
||||
|
||||
```python
|
||||
default_tags=["experiment-123", "control-group", "variant-A"]
|
||||
```
|
||||
|
||||
### Model Identification
|
||||
|
||||
Tag sessions with the models being used:
|
||||
|
||||
```python
|
||||
default_tags=["gpt-4", "claude-3-opus", "mistral-large"]
|
||||
```
|
||||
|
||||
## Viewing Tagged Sessions
|
||||
|
||||
In the AgentOps dashboard:
|
||||
|
||||
1. Use the tag filter to select specific tags
|
||||
2. Combine multiple tags to refine your view
|
||||
3. Save filtered views for quick access
|
||||
|
||||
## Best Practices
|
||||
|
||||
- Use a consistent naming convention for tags
|
||||
- Include both broad categories and specific identifiers
|
||||
- Avoid using too many tags per session (3-5 is typically sufficient)
|
||||
- Consider using hierarchical tag structures (e.g., "env:production", "model:gpt-4")
|
||||
- Update your tagging strategy as your application evolves
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,275 @@
|
||||
---
|
||||
title: 'Traces'
|
||||
description: 'Effectively manage traces in your agent workflow'
|
||||
---
|
||||
## Automatic Trace Management
|
||||
|
||||
The simplest way to create and manage traces is to use the `init` function with automatic trace creation:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize with automatic trace creation (default)
|
||||
agentops.init(api_key="YOUR_API_KEY", default_tags=["production"])
|
||||
```
|
||||
|
||||
This approach:
|
||||
- Creates a trace automatically when you initialize the SDK
|
||||
- Tracks all events in the context of this trace
|
||||
- Manages the trace throughout the lifecycle of your application
|
||||
|
||||
## Manual Trace Creation
|
||||
For more control, you can disable automatic trace creation and start traces manually:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize without auto-starting a trace
|
||||
agentops.init(api_key="YOUR_API_KEY", auto_start_session=False)
|
||||
|
||||
# Later, manually start a trace when needed
|
||||
trace_context = agentops.start_trace(
|
||||
trace_name="Customer Workflow",
|
||||
tags=["customer-query", "high-priority"]
|
||||
)
|
||||
|
||||
# End the trace when done
|
||||
agentops.end_trace(trace_context, end_state="Success")
|
||||
```
|
||||
|
||||
Manual trace management is useful when:
|
||||
- You want to control exactly when trace tracking begins
|
||||
- You need to associate different traces with different sets of tags
|
||||
- Your application has distinct workflows that should be tracked separately
|
||||
|
||||
## Using the Trace Decorator
|
||||
|
||||
You can use the `@trace` decorator to create a trace for a specific function:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
@agentops.trace
|
||||
def process_customer_data(customer_id):
|
||||
# This entire function execution will be tracked as a trace
|
||||
return analyze_data(customer_id)
|
||||
|
||||
# Or with custom parameters
|
||||
@agentops.trace(name="data_processing", tags=["analytics"])
|
||||
def analyze_user_behavior(user_data):
|
||||
return perform_analysis(user_data)
|
||||
```
|
||||
|
||||
## Trace Context Manager
|
||||
|
||||
TraceContext objects support Python's context manager protocol, making it easy to manage trace lifecycles:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Using trace context as a context manager
|
||||
with agentops.start_trace("user_session", tags=["web"]) as trace:
|
||||
# All operations here are tracked within this trace
|
||||
process_user_request()
|
||||
# Trace automatically ends when exiting the context
|
||||
# Success/Error state is set based on whether exceptions occurred
|
||||
```
|
||||
|
||||
## Trace States
|
||||
|
||||
Every trace has an associated state that indicates its completion status. AgentOps provides multiple ways to specify trace end states for flexibility and backward compatibility.
|
||||
|
||||
### AgentOps TraceState Enum (Recommended)
|
||||
|
||||
The recommended approach is to use the `TraceState` enum from AgentOps:
|
||||
|
||||
```python
|
||||
from agentops import TraceState
|
||||
|
||||
# Available states
|
||||
agentops.end_trace(trace_context, end_state=TraceState.SUCCESS) # Trace completed successfully
|
||||
agentops.end_trace(trace_context, end_state=TraceState.ERROR) # Trace encountered an error
|
||||
agentops.end_trace(trace_context, end_state=TraceState.UNSET) # Trace state is not determined
|
||||
```
|
||||
|
||||
### OpenTelemetry StatusCode
|
||||
|
||||
For advanced users familiar with OpenTelemetry, you can use StatusCode directly:
|
||||
|
||||
```python
|
||||
from opentelemetry.trace.status import StatusCode
|
||||
|
||||
agentops.end_trace(trace_context, end_state=StatusCode.OK) # Same as TraceState.SUCCESS
|
||||
agentops.end_trace(trace_context, end_state=StatusCode.ERROR) # Same as TraceState.ERROR
|
||||
agentops.end_trace(trace_context, end_state=StatusCode.UNSET) # Same as TraceState.UNSET
|
||||
```
|
||||
|
||||
### String Values
|
||||
|
||||
String values are also supported for convenience:
|
||||
|
||||
```python
|
||||
# String representations
|
||||
agentops.end_trace(trace_context, end_state="Success") # Maps to SUCCESS
|
||||
agentops.end_trace(trace_context, end_state="Error") # Maps to ERROR
|
||||
agentops.end_trace(trace_context, end_state="Indeterminate") # Maps to UNSET
|
||||
```
|
||||
|
||||
### State Mapping
|
||||
|
||||
All state representations map to the same underlying OpenTelemetry StatusCode:
|
||||
|
||||
| AgentOps TraceState | OpenTelemetry StatusCode | String Values | Description |
|
||||
|-------------------|-------------------------|---------------|-------------|
|
||||
| `TraceState.SUCCESS` | `StatusCode.OK` | "Success" | Trace completed successfully |
|
||||
| `TraceState.ERROR` | `StatusCode.ERROR` | "Error" | Trace encountered an error |
|
||||
| `TraceState.UNSET` | `StatusCode.UNSET` | "Indeterminate" | Trace state is not determined |
|
||||
|
||||
### Default Behavior
|
||||
|
||||
If no end state is provided, the default is `TraceState.SUCCESS`:
|
||||
|
||||
```python
|
||||
# These are equivalent
|
||||
agentops.end_trace(trace_context)
|
||||
agentops.end_trace(trace_context, end_state=TraceState.SUCCESS)
|
||||
```
|
||||
|
||||
## Trace Attributes
|
||||
|
||||
Every trace collects comprehensive metadata to provide rich context for analysis. Trace attributes are automatically captured by AgentOps and fall into several categories:
|
||||
|
||||
### Core Trace Attributes
|
||||
|
||||
**Identity and Timing:**
|
||||
- **Trace ID**: A unique identifier for the trace
|
||||
- **Span ID**: Identifier for the root span of the trace
|
||||
- **Start Time**: When the trace began
|
||||
- **End Time**: When the trace completed (set automatically)
|
||||
- **Duration**: Total execution time (calculated automatically)
|
||||
|
||||
**User-Defined Attributes:**
|
||||
- **Trace Name**: Custom name provided when starting the trace
|
||||
- **Tags**: Labels for filtering and grouping (list of strings or dictionary)
|
||||
- **End State**: Success, error, or unset status
|
||||
|
||||
```python
|
||||
# Tags can be provided as a list of strings or a dictionary
|
||||
agentops.start_trace("my_trace", tags=["production", "experiment-a"])
|
||||
agentops.start_trace("my_trace", tags={"environment": "prod", "version": "1.2.3"})
|
||||
```
|
||||
|
||||
### Resource Attributes
|
||||
|
||||
AgentOps automatically captures system and environment information:
|
||||
|
||||
**Project and Service:**
|
||||
- **Project ID**: AgentOps project identifier
|
||||
- **Service Name**: Service name (defaults to "agentops")
|
||||
- **Service Version**: Version of your service
|
||||
- **Environment**: Deployment environment (dev, staging, prod)
|
||||
- **SDK Version**: AgentOps SDK version being used
|
||||
|
||||
**Host System Information:**
|
||||
- **Host Name**: Machine hostname
|
||||
- **Host System**: Operating system (Windows, macOS, Linux)
|
||||
- **Host Version**: OS version details
|
||||
- **Host Processor**: CPU architecture information
|
||||
- **Host Machine**: Machine type identifier
|
||||
|
||||
**Performance Metrics:**
|
||||
- **CPU Count**: Number of available CPU cores
|
||||
- **CPU Percent**: CPU utilization at trace start
|
||||
- **Memory Total**: Total system memory
|
||||
- **Memory Available**: Available system memory
|
||||
- **Memory Used**: Currently used memory
|
||||
- **Memory Percent**: Memory utilization percentage
|
||||
|
||||
**Dependencies:**
|
||||
- **Imported Libraries**: List of Python packages imported in your environment
|
||||
|
||||
### Span Hierarchy
|
||||
|
||||
**Nested Operations:**
|
||||
- **Spans**: All spans (operations, agents, tools, workflows) recorded during the trace
|
||||
- **Parent-Child Relationships**: Hierarchical structure of operations
|
||||
- **Span Kinds**: Types of operations (agents, tools, workflows, tasks)
|
||||
|
||||
### Accessing Trace Attributes
|
||||
|
||||
While most attributes are automatically captured, you can access trace information programmatically:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Start a trace and get the context
|
||||
trace_context = agentops.start_trace("my_workflow", tags={"version": "1.0"})
|
||||
|
||||
# Access trace information
|
||||
trace_id = trace_context.span.get_span_context().trace_id
|
||||
span_id = trace_context.span.get_span_context().span_id
|
||||
|
||||
print(f"Trace ID: {trace_id}")
|
||||
print(f"Span ID: {span_id}")
|
||||
|
||||
# End the trace
|
||||
agentops.end_trace(trace_context)
|
||||
```
|
||||
|
||||
### Custom Attributes
|
||||
|
||||
You can add custom attributes to spans within your trace:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
with agentops.start_trace("custom_workflow") as trace:
|
||||
# Add custom attributes to the current span
|
||||
trace.span.set_attribute("custom.workflow.step", "data_processing")
|
||||
trace.span.set_attribute("custom.batch.size", 100)
|
||||
trace.span.set_attribute("custom.user.id", "user_123")
|
||||
|
||||
# Your workflow logic here
|
||||
process_data()
|
||||
```
|
||||
|
||||
### Attribute Naming Conventions
|
||||
|
||||
AgentOps follows OpenTelemetry semantic conventions for attribute naming:
|
||||
|
||||
- **AgentOps Specific**: `agentops.*` (e.g., `agentops.span.kind`)
|
||||
- **GenAI Operations**: `gen_ai.*` (e.g., `gen_ai.request.model`)
|
||||
- **System Resources**: Standard names (e.g., `host.name`, `service.name`)
|
||||
- **Custom Attributes**: Use your own namespace (e.g., `myapp.user.id`)
|
||||
|
||||
## Trace Context
|
||||
|
||||
Traces create a context for all span recording. When a span is recorded:
|
||||
|
||||
1. It's associated with the current active trace
|
||||
2. It's automatically included in the trace's timeline
|
||||
3. It inherits the trace's tags for filtering and analysis
|
||||
|
||||
## Viewing Traces in the Dashboard
|
||||
|
||||
The AgentOps dashboard provides several views for analyzing your traces:
|
||||
|
||||
1. **Trace List**: Overview of all traces with filtering options
|
||||
2. **Trace Details**: In-depth view of a single trace
|
||||
3. **Timeline View**: Chronological display of all spans in a trace
|
||||
4. **Tree View**: Hierarchical representation of agents, operations, and events
|
||||
5. **Analytics**: Aggregated metrics across traces
|
||||
|
||||
## Best Practices
|
||||
|
||||
- **Start traces at logical boundaries** in your application workflow
|
||||
- **Use descriptive trace names** to easily identify them in the dashboard
|
||||
- **Apply consistent tags** to group related traces
|
||||
- **Use fewer, longer traces** rather than many short ones for better analysis
|
||||
- **Use automatic trace management** unless you have specific needs for manual control
|
||||
- **Leverage context managers** for automatic trace lifecycle management
|
||||
- **Set appropriate end states** to track success/failure rates
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,185 @@
|
||||
---
|
||||
title: 'AG2'
|
||||
description: 'AG2 Async Agent Chat'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/ag2/async_human_input.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/ag2/ag2_async_agent.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# AG2 Async Agent Chat with Automated Responses
|
||||
|
||||
This notebook demonstrates how to leverage asynchronous programming with AG2 agents
|
||||
to create automated conversations between AI agents, eliminating the need for human
|
||||
input while maintaining full traceability.
|
||||
|
||||
# Overview
|
||||
This notebook demonstrates a practical example of automated AI-to-AI communication where we:
|
||||
|
||||
1. Initialize AG2 agents with OpenAI's GPT-4o-mini model
|
||||
2. Create custom async agents that simulate human-like responses and processing delays
|
||||
3. Automate the entire conversation flow without requiring manual intervention
|
||||
4. Track all interactions using AgentOps for monitoring and analysis
|
||||
|
||||
By using async operations and automated responses, you can create fully autonomous
|
||||
agent conversations that simulate real-world scenarios. This is particularly useful
|
||||
for testing, prototyping, and creating demos where you want to showcase agent
|
||||
capabilities without manual input.
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install ag2 agentops nest-asyncio
|
||||
```
|
||||
```bash poetry
|
||||
poetry add ag2 agentops nest-asyncio
|
||||
```
|
||||
```bash uv
|
||||
uv add ag2 agentops nest-asyncio
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
```
|
||||
import asyncio
|
||||
from typing import Dict, Optional, Union
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import nest_asyncio
|
||||
import agentops
|
||||
from autogen import AssistantAgent
|
||||
from autogen.agentchat.user_proxy_agent import UserProxyAgent
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
# Load environment variables for API keys
|
||||
load_dotenv()
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")
|
||||
# Initialize AgentOps for tracking and monitoring
|
||||
agentops.init(auto_start_session=False, trace_name="AG2 Async Demo")
|
||||
tracer = agentops.start_trace(trace_name="AG2 Async Agent Demo", tags=["ag2-async-demo", "agentops-example"])
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
# Define an asynchronous function that simulates async processing
|
||||
async def simulate_async_processing(task_name: str, delay: float = 1.0) -> str:
|
||||
"""
|
||||
Simulate some asynchronous processing (e.g., API calls, file operations, etc.)
|
||||
"""
|
||||
print(f"🔄 Starting async task: {task_name}")
|
||||
await asyncio.sleep(delay) # Simulate async work
|
||||
print(f"✅ Completed async task: {task_name}")
|
||||
return f"Processed: {task_name}"
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
# Define a custom UserProxyAgent that simulates automated user responses
|
||||
class AutomatedUserProxyAgent(UserProxyAgent):
|
||||
def __init__(self, name: str, **kwargs):
|
||||
super().__init__(name, **kwargs)
|
||||
self.response_count = 0
|
||||
self.predefined_responses = [
|
||||
"Yes, please generate interview questions for these topics.",
|
||||
"The questions look good. Can you make them more specific to senior-level positions?",
|
||||
"Perfect! These questions are exactly what we need. Thank you!",
|
||||
]
|
||||
|
||||
async def a_get_human_input(self, prompt: str) -> str:
|
||||
# Simulate async processing before responding
|
||||
await simulate_async_processing(f"Processing user input #{self.response_count + 1}")
|
||||
|
||||
if self.response_count < len(self.predefined_responses):
|
||||
response = self.predefined_responses[self.response_count]
|
||||
self.response_count += 1
|
||||
print(f"👤 User: {response}")
|
||||
return response
|
||||
else:
|
||||
print("👤 User: TERMINATE")
|
||||
return "TERMINATE"
|
||||
|
||||
async def a_receive(
|
||||
self,
|
||||
message: Union[Dict, str],
|
||||
sender,
|
||||
request_reply: Optional[bool] = None,
|
||||
silent: Optional[bool] = False,
|
||||
):
|
||||
await super().a_receive(message, sender, request_reply, silent)
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
# Define an AssistantAgent that simulates async processing before responding
|
||||
class AsyncAssistantAgent(AssistantAgent):
|
||||
async def a_receive(
|
||||
self,
|
||||
message: Union[Dict, str],
|
||||
sender,
|
||||
request_reply: Optional[bool] = None,
|
||||
silent: Optional[bool] = False,
|
||||
):
|
||||
# Simulate async processing before responding
|
||||
await simulate_async_processing("Analyzing request and preparing response", 0.5)
|
||||
await super().a_receive(message, sender, request_reply, silent)
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
async def main():
|
||||
print("🚀 Starting AG2 Async Demo")
|
||||
|
||||
# Create agents with automated behavior
|
||||
user_proxy = AutomatedUserProxyAgent(
|
||||
name="hiring_manager",
|
||||
human_input_mode="NEVER", # No human input required
|
||||
max_consecutive_auto_reply=3,
|
||||
code_execution_config=False,
|
||||
is_termination_msg=lambda msg: "TERMINATE" in str(msg.get("content", "")),
|
||||
)
|
||||
|
||||
assistant = AsyncAssistantAgent(
|
||||
name="interview_consultant",
|
||||
system_message="""You are an expert interview consultant. When given interview topics,
|
||||
you create thoughtful, relevant questions. You ask for feedback and incorporate it.
|
||||
When the user is satisfied with the questions, end with 'TERMINATE'.""",
|
||||
llm_config={"config_list": [{"model": "gpt-4o-mini", "api_key": os.environ.get("OPENAI_API_KEY")}]},
|
||||
is_termination_msg=lambda msg: "TERMINATE" in str(msg.get("content", "")),
|
||||
)
|
||||
|
||||
try:
|
||||
print("🤖 Initiating automated conversation...")
|
||||
# Start the automated chat between the user and assistant
|
||||
await user_proxy.a_initiate_chat(
|
||||
assistant,
|
||||
message="""I need help creating interview questions for these topics:
|
||||
- Resume Review
|
||||
- Technical Skills Assessment
|
||||
- Project Discussion
|
||||
- Job Role Expectations
|
||||
- Closing Remarks
|
||||
|
||||
Please create 2-3 questions for each topic.""",
|
||||
max_turns=6,
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error occurred: {e}")
|
||||
finally:
|
||||
agentops.end_trace(tracer, end_state="Success")
|
||||
|
||||
print("\n🎉 Demo completed successfully!")
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
# Run the main async demo
|
||||
nest_asyncio.apply()
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,120 @@
|
||||
---
|
||||
title: 'Agno'
|
||||
description: 'Async Operations with Agno'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/agno/agno_async_operations.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/agno/agno_async_operations.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# Async Operations with Agno
|
||||
|
||||
This notebook demonstrates how to leverage asynchronous programming with Agno agents to execute multiple AI tasks concurrently, significantly improving performance and efficiency.
|
||||
|
||||
## Overview
|
||||
This notebook demonstrates a practical example of concurrent AI operations where we:
|
||||
|
||||
1. **Initialize an Agno agent** with OpenAI's GPT-4o-mini model
|
||||
2. **Create multiple async tasks** that query the AI about different programming languages
|
||||
3. **Compare performance** between concurrent and sequential execution
|
||||
|
||||
By using async operations, you can run multiple AI queries simultaneously instead of waiting for each one to complete sequentially. This is particularly beneficial when dealing with I/O-bound operations like API calls to AI models.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops agno python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops agno python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops agno python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
```python
|
||||
import os
|
||||
import asyncio
|
||||
from dotenv import load_dotenv
|
||||
|
||||
import agentops
|
||||
from agno.agent import Agent
|
||||
from agno.team import Team
|
||||
from agno.models.openai import OpenAIChat
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
load_dotenv()
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_agentops_api_key_here")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
agentops.init(auto_start_session=False, tags=["agno-example", "async-operation"])
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
async def demonstrate_async_operations():
|
||||
"""
|
||||
Demonstrate concurrent execution of multiple AI agent tasks.
|
||||
|
||||
This function creates multiple async tasks that execute concurrently rather than sequentially.
|
||||
Each task makes an independent API call to the AI model, and asyncio.gather()
|
||||
waits for all tasks to complete before returning results.
|
||||
|
||||
Performance benefit: Instead of 3 sequential calls taking ~90 seconds total,
|
||||
concurrent execution typically completes in ~30 seconds.
|
||||
"""
|
||||
tracer = agentops.start_trace(trace_name="Agno Async Operations Example",)
|
||||
|
||||
try:
|
||||
# Initialize AI agent with specified model
|
||||
agent = Agent(model=OpenAIChat(id="gpt-4o-mini"))
|
||||
|
||||
async def task1():
|
||||
"""Query AI about Python programming language."""
|
||||
response = await agent.arun("Explain Python programming language in one paragraph")
|
||||
return f"Python: {response.content}"
|
||||
|
||||
async def task2():
|
||||
"""Query AI about JavaScript programming language."""
|
||||
response = await agent.arun("Explain JavaScript programming language in one paragraph")
|
||||
return f"JavaScript: {response.content}"
|
||||
|
||||
async def task3():
|
||||
"""Query AI for comparison between programming languages."""
|
||||
response = await agent.arun("Compare Python and JavaScript briefly")
|
||||
return f"Comparison: {response.content}"
|
||||
|
||||
# Execute all tasks concurrently using asyncio.gather()
|
||||
results = await asyncio.gather(task1(), task2(), task3())
|
||||
|
||||
for i, result in enumerate(results, 1):
|
||||
print(f"\nTask {i} Result:")
|
||||
print(result)
|
||||
print("-" * 50)
|
||||
|
||||
agentops.end_trace(tracer, end_state="Success")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
agentops.end_trace(tracer, end_state="Error")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
await demonstrate_async_operations()
|
||||
```
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,455 @@
|
||||
---
|
||||
title: 'Anthropic Tools Example'
|
||||
description: 'Understanding and using tools with the Anthropic SDK and AgentOps'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/anthropic/agentops-anthropic-understanding-tools.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/anthropic/agentops-anthropic-understanding-tools.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# Anthropic Example for understanding Tools
|
||||
|
||||
Anthropic's tool returns are not as simple as getting a few strings! While this system is more complex than those before it, it's also simple enough to be used without problem once you understand how it works!
|
||||
|
||||
To get started, we will import Agentops and Anthropic.
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops anthropic python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops anthropic python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops anthropic python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setup
|
||||
|
||||
Setup our generic default statements:
|
||||
```python
|
||||
import agentops
|
||||
from anthropic import Anthropic
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
import random
|
||||
```
|
||||
|
||||
And set our API keys.
|
||||
```python
|
||||
load_dotenv()
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_agentops_api_key_here")
|
||||
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY", "your_anthropic_api_key_here")
|
||||
```
|
||||
|
||||
Now let's set the client as Anthropic and make an AgentOps trace.
|
||||
```python
|
||||
agentops.init(tags=["anthropic-example-tool-tutorials", "agentops-example"])
|
||||
client = Anthropic()
|
||||
```
|
||||
|
||||
## Creating a Dummy Tool
|
||||
|
||||
Now to create a simple dummy tool! We are going to make a tool that will tell us about the demon infestation levels for 3 areas. From there, we will have VEGA, our AI determine the best place for the Doom Slayer to attack.
|
||||
|
||||
```python
|
||||
locations = [
|
||||
{
|
||||
"Name": "Super Gore Nest",
|
||||
"Description": "A grotesque mass of demonic growth and organic structures infesting the ruins of an urban area on Earth. The Super Gore Nest serves as a massive, pulsating hive for Hell’s forces, complete with rivers of blood, twisted tendrils, and a dark, organic design that shows how deeply Hell has taken root in the city.",
|
||||
},
|
||||
{
|
||||
"Name": "Exultia",
|
||||
"Description": "An ancient, mystical world that holds the ruins of the Night Sentinels' kingdom, with gothic structures and arcane symbols throughout. This realm is filled with epic landscapes, medieval architecture, and hints of the powerful civilization that once defended against Hell’s forces.",
|
||||
},
|
||||
{
|
||||
"Name": "Cultist Base",
|
||||
"Description": "A grim fortress hidden within the icy mountains, where a fanatical cult worships demons. Filled with chilling sacrificial chambers, traps, and rituals, the Cultist Base is a hostile stronghold where Doom Slayer must confront the cult leaders aiding Hell's invasion of Earth.",
|
||||
},
|
||||
{
|
||||
"Name": "Taras Nabad",
|
||||
"Description": "A war-ravaged city on the homeworld of the Night Sentinels, showcasing grandiose, ancient architecture in the midst of destruction. Taras Nabad's sprawling structures and historical significance reveal glimpses into the Doom Slayer’s past and the once-thriving Sentinel civilization.",
|
||||
},
|
||||
{
|
||||
"Name": "Nekravol",
|
||||
"Description": "A hellish, industrial fortress where souls are processed into Argent energy. With conveyor belts moving the damned and a skyline dominated by fire and darkness, Nekravol is a nightmarish facility that powers Hell's armies and embodies the horrific machinery of Hell's cruelty.",
|
||||
},
|
||||
{
|
||||
"Name": "Urdak",
|
||||
"Description": "A surreal, high-tech realm that serves as the home of the angelic Maykrs. Urdak’s sleek, pristine architecture and ethereal ambiance sharply contrast with Hell’s brutal landscapes, yet this realm holds its own dark secrets and a critical role in Hell's invasion of Earth.",
|
||||
},
|
||||
{
|
||||
"Name": "UAC Base",
|
||||
"Description": "A futuristic military base on Earth controlled by the Union Aerospace Corporation (UAC), filled with high-tech weaponry and security systems. The UAC Base serves as a human foothold in the fight against Hell, though some within its ranks may have darker intentions.",
|
||||
},
|
||||
]
|
||||
|
||||
combat_casualties = ["Nonexistent", "Low", "Medium", "High", "Extinction"]
|
||||
|
||||
missions = [
|
||||
"Locate and confront a key leader of Hell’s invasion forces.",
|
||||
"Clear out demonic infestations to secure a strategic foothold.",
|
||||
"Disrupt Hell's control over the area by eliminating critical targets.",
|
||||
"Enter a critical demonic stronghold to disrupt enemy operations.",
|
||||
"Locate and destroy the central power source to weaken enemy forces.",
|
||||
"Collect essential resources before the area becomes unstable.",
|
||||
]
|
||||
```
|
||||
|
||||
Now that that's done, we can make a function! We will generate three random missions and pass it off to the AI.
|
||||
```python
|
||||
def generate_missions():
|
||||
selectedmissions = []
|
||||
loop = 0
|
||||
|
||||
while loop < 3:
|
||||
location = random.choice(locations)
|
||||
casualties = random.choice(combat_casualties)
|
||||
mission = random.choice(missions)
|
||||
final = (
|
||||
f"LocationName: {location['Name']}, "
|
||||
f"LocationInfo: {location['Description']}, "
|
||||
f"HumanCombatCasualties: {casualties}, "
|
||||
f"Mission: {mission}"
|
||||
)
|
||||
|
||||
selectedmissions.append(final)
|
||||
loop += 1
|
||||
|
||||
# Combine all mission strings into a single string with a separator (e.g., newline or comma)
|
||||
# Notebook had 'missions_string = "\n".join(missions)', which is a bug.
|
||||
# Using 'selectedmissions' as it's the correct variable.
|
||||
missions_string = "\n".join(selectedmissions)
|
||||
print(missions_string)
|
||||
return missions_string
|
||||
```
|
||||
|
||||
```python
|
||||
generate_missions()
|
||||
```
|
||||
|
||||
## Constructing the Message Stream
|
||||
|
||||
Now to the real core of this; making our message stream! We create this as a function we can call later! I create examples since the LLM's context size can handle it (and it's generally good practice)!
|
||||
|
||||
We are also going to take several steps here; we must create an example of the tool being used as context. Next, we must add the generated lines to the messages list once done being generated. Finally, we will parse the text for the format we want and request another line.
|
||||
|
||||
Now we make a message! This time around we will skip making an initial message that has too much context, unlike in the past!
|
||||
```python
|
||||
# We make our history a separate block to be easier to add to later on! This is essentially our history
|
||||
initial_messages = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "You are VEGA, the assistant to the DOOMGUY. Get three missions from the ship's API and tell me which mission is most to least important for quellng the forces of hell. ",
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
Now to construct a request!
|
||||
```python
|
||||
response = client.messages.create(
|
||||
max_tokens=5000,
|
||||
model="claude-3-7-sonnet-20250219", # Matched notebook
|
||||
tools=[
|
||||
{
|
||||
"name": "generate_missions",
|
||||
"description": "Retrieve three missions for the DoomSlayer",
|
||||
"input_schema": {"type": "object", "properties": {}, "required": []},
|
||||
}
|
||||
],
|
||||
messages=initial_messages,
|
||||
)
|
||||
|
||||
print(response.content)
|
||||
```
|
||||
Having trouble understanding this? The first block given is always AI dialogue! You can use `response.content[0].text` to get the AI's text! Let's try it below.
|
||||
```python
|
||||
message = response.content[0].text
|
||||
print(message)
|
||||
```
|
||||
The code below finds the tool used!
|
||||
```python
|
||||
gen_mission_result = ""
|
||||
|
||||
# Print response content to see the data
|
||||
# print(response.content) # Already printed in the cell above in notebook
|
||||
|
||||
# Assuming ToolUseBlock is at index 1 (matches notebook's direct access)
|
||||
tool_use_block = response.content[1]
|
||||
|
||||
# Get the tool name and input
|
||||
tool_name = tool_use_block.name
|
||||
tool_input = tool_use_block.input
|
||||
|
||||
# We don't need to look to extract any inputs since we don't use any
|
||||
|
||||
# Check if the tool name is "generate_missions"
|
||||
if tool_name == "generate_missions":
|
||||
# Call the function with the tool creator as an argument
|
||||
gen_mission_result = generate_missions()
|
||||
```
|
||||
Now we add these as context to the LLM through `initial_messages`!
|
||||
```python
|
||||
# Matches notebook cell 25 logic
|
||||
initial_messages.append({"role": "assistant", "content": gen_mission_result})
|
||||
|
||||
initial_messages.append(
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Based on these, which location should take priority and why?",
|
||||
}
|
||||
)
|
||||
```
|
||||
And now to get a response!
|
||||
```python
|
||||
response = client.messages.create(
|
||||
max_tokens=5000,
|
||||
model="claude-3-7-sonnet-20250219", # Matched notebook
|
||||
tools=[
|
||||
{
|
||||
"name": "generate_missions",
|
||||
"description": "Retrieve three missions for the DoomSlayer",
|
||||
"input_schema": {"type": "object", "properties": {}, "required": []},
|
||||
}
|
||||
],
|
||||
messages=initial_messages,
|
||||
)
|
||||
|
||||
print(response)
|
||||
```
|
||||
Isolate again!
|
||||
```python
|
||||
message = response.content[0].text
|
||||
print(message)
|
||||
```
|
||||
Hmmm, what if we wanted to include more tools and add inputs? Let's create two new functions to display this!
|
||||
|
||||
One will show the kind of demon we are facing, whereas another one will take our weapon input to determine what the best weapon chain to use is (You heard that right, we believe in quick weapon switches around these parts)
|
||||
```python
|
||||
demons = [
|
||||
{
|
||||
"Name": "Imp",
|
||||
"Description": "A fast, agile demon that hurls fireballs and uses its claws to tear apart its prey. Imps are commonly found in Hell’s army, notorious for their quickness and ability to climb walls, making them dangerous adversaries in any environment.",
|
||||
},
|
||||
{
|
||||
"Name": "Cacodemon",
|
||||
"Description": "A floating, spherical demon with a large mouth full of teeth and an ability to launch explosive projectiles. Cacodemons are often encountered in open areas, where their aerial agility and relentless attacks pose a constant threat.",
|
||||
},
|
||||
{
|
||||
"Name": "Hell Knight",
|
||||
"Description": "A towering, brutish demon with immense strength and durability. The Hell Knight is capable of charging at the Doom Slayer and delivering devastating melee attacks. Its tough hide makes it resistant to most forms of damage.",
|
||||
},
|
||||
{
|
||||
"Name": "Mancubus",
|
||||
"Description": "A grotesque, overweight demon that releases powerful fireballs from its massive arm cannons. Mancubus demons are slow-moving but dangerous due to their firepower and the ability to overwhelm enemies with their fiery onslaughts.",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
weapons = [
|
||||
{
|
||||
"Name": "Super Shotgun",
|
||||
"Description": "A powerful, double-barreled shotgun that delivers devastating close-range damage. Known for its sheer stopping power, the Super Shotgun can tear through enemies with ease, especially when equipped with the Meat Hook attachment, allowing for rapid mobility and devastating hits.",
|
||||
},
|
||||
{
|
||||
"Name": "Rocket Launcher",
|
||||
"Description": "A high-powered weapon that fires explosive rockets capable of dealing massive area damage. The Rocket Launcher is invaluable for taking down groups of enemies or dealing significant damage to larger demons, especially when upgraded with the Lock-On Burst mod.",
|
||||
},
|
||||
{
|
||||
"Name": "Chaingun",
|
||||
"Description": "A rapid-fire weapon that can unleash a torrent of bullets at a high rate of speed. The Chaingun is perfect for mowing down enemies and can be equipped with the Heat Blast mod, allowing for explosive energy rounds that can clear multiple enemies at once.",
|
||||
},
|
||||
{
|
||||
"Name": "BFG 9000",
|
||||
"Description": "One of the most iconic weapons in the *Doom* franchise, the BFG 9000 fires a massive energy beam that obliterates anything in its path. With its massive damage potential, the BFG 9000 is a game-changer, especially in dealing with large groups of enemies or the toughest foes.",
|
||||
},
|
||||
{
|
||||
"Name": "Ice Bomb",
|
||||
"Description": "A special grenade that freezes enemies in a wide area, giving the Doom Slayer a chance to deal with multiple foes at once. The Ice Bomb is effective for crowd control, allowing for easy Glory Kills or creating distance from overwhelming enemies.",
|
||||
},
|
||||
]
|
||||
```
|
||||
Now we can keep the `initial_messages` from before actually! However let's change the context.
|
||||
```python
|
||||
# Matches notebook cell 33 (no conditional append of previous AI message)
|
||||
initial_messages.append(
|
||||
{
|
||||
"role": "user",
|
||||
"content": "The first priority mission was selected. At the same time, scan for enemies and check inventory to determine the best combat strategy. You should use both tools at once.",
|
||||
}
|
||||
)
|
||||
```
|
||||
And we of course make functions.
|
||||
```python
|
||||
def enemyscan(amount): # Matched notebook (no type hint)
|
||||
enemiesonscene = []
|
||||
loop = 0
|
||||
|
||||
while loop < amount + 1: # Matched notebook
|
||||
scannedenemy = random.choice(demons)
|
||||
|
||||
# Append name and description separately (Matches notebook)
|
||||
enemiesonscene.append(scannedenemy["Name"])
|
||||
enemiesonscene.append(scannedenemy["Description"])
|
||||
loop += 1
|
||||
|
||||
# Combine all mission strings into a single string with a separator (e.g., newline or comma)
|
||||
enemies_string = "\n".join(enemiesonscene)
|
||||
print(enemies_string)
|
||||
return enemies_string
|
||||
```
|
||||
```python
|
||||
enemyscan(5)
|
||||
```
|
||||
And now inventory.
|
||||
```python
|
||||
def inventoryscan():
|
||||
weapons_at_hand = []
|
||||
loop = 0
|
||||
|
||||
while loop < 5: # Matched notebook (loop < 5)
|
||||
weapon = random.choice(weapons)
|
||||
|
||||
# Append name and description separately (Matches notebook)
|
||||
weapons_at_hand.append(weapon["Name"])
|
||||
weapons_at_hand.append(weapon["Description"])
|
||||
loop += 1
|
||||
|
||||
# Combine all mission strings into a single string with a separator (e.g., newline or comma)
|
||||
weapons_string = "\n".join(weapons_at_hand)
|
||||
print(weapons_string)
|
||||
return weapons_string
|
||||
```
|
||||
```python
|
||||
inventoryscan()
|
||||
```
|
||||
With that, let's construct our new tools and run this!!
|
||||
```python
|
||||
response = client.messages.create(
|
||||
max_tokens=5000,
|
||||
model="claude-3-7-sonnet-20250219", # Matched notebook
|
||||
tools=[
|
||||
{
|
||||
"name": "enemyscan_tool",
|
||||
"description": "Retrieve a list of demons currently present in the area.",
|
||||
"input_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"amount": {
|
||||
"type": "integer",
|
||||
"description": "Number of enemies to scan.",
|
||||
}
|
||||
},
|
||||
"required": ["amount"],
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "inventoryscan_tool",
|
||||
"description": "Retrieve a list of weapons the Doom Slayer has at hand.",
|
||||
"input_schema": {"type": "object", "properties": {}, "required": []},
|
||||
},
|
||||
],
|
||||
messages=initial_messages,
|
||||
)
|
||||
|
||||
print(response)
|
||||
```
|
||||
Display just the text.
|
||||
```python
|
||||
# Matches notebook cell 43
|
||||
message = response.content[0].text
|
||||
print(message)
|
||||
```
|
||||
```python
|
||||
# Matches notebook cell 44
|
||||
initial_messages.append({"role": "assistant", "content": f"{str(response.content[0].text)}"})
|
||||
```
|
||||
And now to get the information and put it all together! PLEASE read the comments!
|
||||
```python
|
||||
# Matches notebook cell 46 logic
|
||||
inv_scan_res = ""
|
||||
enemy_scan_res = ""
|
||||
|
||||
response_str = str(response) # As in notebook
|
||||
tool_use_count = response_str.count(
|
||||
"ToolUseBlock"
|
||||
) # We know the ToolUseBlock will appear once for each tool request so we check how many time it appears
|
||||
|
||||
# You can use print(tool_use_count)to validate the ToolBlocks here if you wish
|
||||
|
||||
loop = 0
|
||||
|
||||
# We do this instead of a (foreach) because we need to skip the first block! This contains the message from the AI, not the tool! This way allows us to reference the item we want as easily as possible without complex logic needed!
|
||||
while loop < tool_use_count: # We will get the tools now
|
||||
tool_use_block = response.content[loop + 1] # We start at 1 since 0 holds the AI mesage
|
||||
tool_name = tool_use_block.name
|
||||
tool_input = tool_use_block.input
|
||||
|
||||
if tool_name == "inventoryscan_tool":
|
||||
# Call the inventoryscan function for inventoryscan_tool
|
||||
inv_scan_res = inventoryscan()
|
||||
elif tool_name == "enemyscan_tool":
|
||||
# Get the amount for enemyscan_tool
|
||||
amount = tool_input["amount"] # Direct access as in notebook
|
||||
# Call the enemyscan function with the amount
|
||||
enemy_scan_res = enemyscan(amount)
|
||||
|
||||
loop = loop + 1 # Matches notebook
|
||||
print(inv_scan_res)
|
||||
print(enemy_scan_res)
|
||||
```
|
||||
And now we are basically done! We can give this to the AI and see what we get.
|
||||
```python
|
||||
# Matches notebook cell 48
|
||||
initial_messages.append(
|
||||
{
|
||||
"role": "assistant", # Notebook uses assistant role here for tool results
|
||||
"content": f"Weapons Inventory Scan Result: {inv_scan_res}\nEnemy Scans Result: {enemy_scan_res}",
|
||||
}
|
||||
)
|
||||
|
||||
initial_messages.append(
|
||||
{
|
||||
"role": "user",
|
||||
"content": "What is the combat plan for killing these demons? Based on the last message, tell me which demons to kill first, in which order and using which weapons as well as any sweakpoints.", # Typo 'sweakpoints' from notebook
|
||||
}
|
||||
)
|
||||
```
|
||||
```python
|
||||
# Matches notebook cell 49
|
||||
response = client.messages.create(
|
||||
max_tokens=5000,
|
||||
model="claude-3-7-sonnet-20250219", # Matched notebook
|
||||
tools=[
|
||||
{
|
||||
"name": "enemyscan_tool",
|
||||
"description": "Retrieve a list of demons currently present in the area.",
|
||||
"input_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"amount": {
|
||||
"type": "integer",
|
||||
"description": "Number of enemies to scan.",
|
||||
}
|
||||
},
|
||||
"required": ["amount"],
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "inventoryscan_tool",
|
||||
"description": "Retrieve a list of weapons the Doom Slayer has at hand.",
|
||||
"input_schema": {"type": "object", "properties": {}, "required": []},
|
||||
},
|
||||
],
|
||||
messages=initial_messages,
|
||||
)
|
||||
|
||||
message = response.content[0].text # Matches notebook
|
||||
print(message)
|
||||
```
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,155 @@
|
||||
---
|
||||
title: 'AutoGen'
|
||||
description: 'Microsoft Autogen Chat Example'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/autogen/AgentChat.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/autogen/AgentChat.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# Microsoft Autogen Chat Example
|
||||
|
||||
AgentOps automatically configures itself when it's initialized meaning your agent run data will be tracked and logged to your AgentOps dashboard right away.
|
||||
|
||||
First let's install the required packages
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install "autogen-ext[openai]" -U agentops autogen-agentchat python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add "autogen-ext[openai]" -U agentops autogen-agentchat python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install "autogen-ext[openai]" -U agentops autogen-agentchat python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then import them
|
||||
|
||||
|
||||
```python
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from IPython.core.error import (
|
||||
StdinNotImplementedError,
|
||||
)
|
||||
import asyncio
|
||||
|
||||
import agentops
|
||||
|
||||
from autogen_agentchat.agents import AssistantAgent, UserProxyAgent
|
||||
from autogen_ext.models.openai import OpenAIChatCompletionClient
|
||||
|
||||
from autogen_agentchat.teams import RoundRobinGroupChat
|
||||
from autogen_agentchat.conditions import MaxMessageTermination
|
||||
from autogen_agentchat.ui import Console
|
||||
```
|
||||
|
||||
Next, we'll set our API keys. There are several ways to do this, the code below is just the most foolproof way for the purposes of this notebook. It accounts for both users who use environment variables and those who just want to set the API Key here in this notebook.
|
||||
|
||||
[Get an AgentOps API key](https://agentops.ai/settings/projects)
|
||||
|
||||
1. Create an environment variable in a .env file or other method. By default, the AgentOps `init()` function will look for an environment variable named `AGENTOPS_API_KEY`. Or...
|
||||
|
||||
2. Replace `<your_agentops_key>` below and pass in the optional `api_key` parameter to the AgentOps `init(api_key=...)` function. Remember not to commit your API key to a public repo!
|
||||
|
||||
|
||||
```python
|
||||
load_dotenv()
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# When initializing AgentOps, you can pass in optional tags to help filter sessions
|
||||
agentops.init(auto_start_session=False)
|
||||
tracer = agentops.start_trace(
|
||||
trace_name="Microsoft Agent Chat Example", tags=["autogen-chat", "microsoft-autogen", "agentops-example"]
|
||||
)
|
||||
```
|
||||
|
||||
AutoGen will now start automatically tracking
|
||||
|
||||
* LLM prompts and completions
|
||||
* Token usage and costs
|
||||
* Agent names and actions
|
||||
* Correspondence between agents
|
||||
* Tool usage
|
||||
* Errors
|
||||
|
||||
# Simple Chat Example
|
||||
|
||||
|
||||
```python
|
||||
# Define model and API key
|
||||
model_name = "gpt-4-turbo" # Or "gpt-4o" / "gpt-4o-mini" as per migration guide examples
|
||||
api_key = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
# Create the model client
|
||||
model_client = OpenAIChatCompletionClient(model=model_name, api_key=api_key)
|
||||
|
||||
# Create the agent that uses the LLM.
|
||||
assistant = AssistantAgent(
|
||||
name="assistant",
|
||||
system_message="You are a helpful assistant.", # Added system message for clarity
|
||||
model_client=model_client,
|
||||
)
|
||||
|
||||
user_proxy_initiator = UserProxyAgent("user_initiator")
|
||||
|
||||
|
||||
async def main():
|
||||
termination = MaxMessageTermination(max_messages=2)
|
||||
|
||||
group_chat = RoundRobinGroupChat(
|
||||
[user_proxy_initiator, assistant], # Corrected: agents as positional argument
|
||||
termination_condition=termination,
|
||||
)
|
||||
|
||||
chat_task = "How can I help you today?"
|
||||
print(f"User Initiator: {chat_task}")
|
||||
|
||||
try:
|
||||
stream = group_chat.run_stream(task=chat_task)
|
||||
await Console().run(stream)
|
||||
agentops.end_trace(tracer, end_state="Success")
|
||||
|
||||
except StdinNotImplementedError:
|
||||
print("StdinNotImplementedError: This typically happens in non-interactive environments.")
|
||||
print("Skipping interactive part of chat for automation.")
|
||||
agentops.end_trace(tracer, end_state="Indeterminate")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
agentops.end_trace(tracer, end_state="Error")
|
||||
finally:
|
||||
await model_client.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
loop = None
|
||||
|
||||
if loop and loop.is_running():
|
||||
import nest_asyncio
|
||||
|
||||
nest_asyncio.apply()
|
||||
asyncio.run(main())
|
||||
else:
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
You can view data on this run at [app.agentops.ai](https://app.agentops.ai).
|
||||
|
||||
The dashboard will display LLM events for each message sent by each agent, including those made by the human user.
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,402 @@
|
||||
---
|
||||
title: 'CrewAI Example'
|
||||
description: 'Build a job posting generator with CrewAI agents and AgentOps tracking'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/crewai/job_posting.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/crewai/job_posting.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# CrewAI Job Posting Generator with AgentOps
|
||||
|
||||
This example shows you how to build a complete job posting generator using CrewAI's official project structure with comprehensive tracking via AgentOps.
|
||||
|
||||
## What We're Building
|
||||
|
||||
A **3-agent system** that collaborates to create job postings:
|
||||
- 🔍 **Research Agent**: Analyzes company culture and industry trends
|
||||
- ✍️ **Writer Agent**: Drafts compelling job descriptions
|
||||
- 📝 **Editor Agent**: Reviews and polishes the final posting
|
||||
|
||||
**With AgentOps**, you'll get complete visibility into agent conversations, tool usage, costs, and performance.
|
||||
|
||||
## Step-by-Step Implementation
|
||||
|
||||
### Step 1: Install Dependencies
|
||||
|
||||
Install CrewAI with tools support and AgentOps for tracking:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install -U 'crewai[tools]' agentops
|
||||
```
|
||||
```bash poetry
|
||||
poetry add 'crewai[tools]' agentops
|
||||
```
|
||||
```bash uv
|
||||
uv pip install 'crewai[tools]' agentops
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
**What AgentOps adds:**
|
||||
- 🔍 **Automatic tracking** of all LLM calls and agent interactions
|
||||
- 💰 **Cost monitoring** with token usage breakdown
|
||||
- 🛠️ **Tool usage analytics** for web searches and file operations
|
||||
- 📊 **Performance metrics** and execution timelines
|
||||
- 🐛 **Session replay** for debugging and optimization
|
||||
|
||||
### Step 2: Create CrewAI Project
|
||||
|
||||
Use CrewAI's CLI to create the proper project structure:
|
||||
|
||||
```bash
|
||||
crewai create crew job_posting_agent
|
||||
```
|
||||
|
||||
**Select your provider and model:**
|
||||
- Choose **1. openai**
|
||||
- Choose **1. gpt-4** (or your preferred model)
|
||||
- Enter your OpenAI API key when prompted
|
||||
|
||||
**This creates the complete project structure:**
|
||||
```
|
||||
job_posting_agent/
|
||||
├── .env # API keys
|
||||
├── .gitignore
|
||||
├── pyproject.toml # Dependencies
|
||||
├── README.md
|
||||
├── knowledge/
|
||||
│ └── user_preference.txt
|
||||
└── src/job_posting_agent/
|
||||
├── __init__.py
|
||||
├── main.py # Main execution
|
||||
├── crew.py # Crew definition
|
||||
├── config/
|
||||
│ ├── agents.yaml # Agent configurations
|
||||
│ └── tasks.yaml # Task definitions
|
||||
└── tools/
|
||||
├── __init__.py
|
||||
└── custom_tool.py # Custom tools
|
||||
```
|
||||
|
||||
### Step 3: Add Additional API Keys
|
||||
|
||||
Edit the generated `.env` file to add AgentOps and Serper keys:
|
||||
|
||||
```bash
|
||||
# .env (already has OPENAI_API_KEY from crewai create)
|
||||
OPENAI_API_KEY=your_openai_key_here
|
||||
AGENTOPS_API_KEY=your_agentops_key_here
|
||||
SERPER_API_KEY=your_serper_key_here
|
||||
```
|
||||
|
||||
**Get your additional API keys:**
|
||||
- **AgentOps**: [Get one here](https://agentops.ai/settings/projects)
|
||||
- **Serper**: [Serper Dev](https://serper.dev/api-key) (for web search)
|
||||
|
||||
### Step 4: Configure Agents
|
||||
|
||||
Edit `src/job_posting_agent/config/agents.yaml` to define your 3 agents:
|
||||
|
||||
```yaml
|
||||
researcher:
|
||||
role: "Research Analyst"
|
||||
goal: "Analyze the company website and provided description to extract insights on culture, values, and specific needs."
|
||||
backstory: "Expert in analyzing company cultures and identifying key values and needs from various sources, including websites and brief descriptions."
|
||||
verbose: true
|
||||
|
||||
writer:
|
||||
role: "Job Description Writer"
|
||||
goal: "Use insights from the Research Analyst to create a detailed, engaging, and enticing job posting."
|
||||
backstory: "Skilled in crafting compelling job descriptions that resonate with the company's values and attract the right candidates."
|
||||
verbose: true
|
||||
|
||||
editor:
|
||||
role: "Review and Editing Specialist"
|
||||
goal: "Review the job posting for clarity, engagement, grammatical accuracy, and alignment with company values and refine it to ensure perfection."
|
||||
backstory: "A meticulous editor with an eye for detail, ensuring every piece of content is clear, engaging, and grammatically perfect."
|
||||
verbose: true
|
||||
```
|
||||
|
||||
### Step 5: Configure Tasks
|
||||
|
||||
Edit `src/job_posting_agent/config/tasks.yaml` to define the workflow:
|
||||
|
||||
```yaml
|
||||
research_company_culture:
|
||||
description: >
|
||||
Analyze the provided company website and description: "{company_description}"
|
||||
at domain {company_domain}. Focus on understanding the company's culture,
|
||||
values, and mission. Identify unique selling points and specific projects
|
||||
or achievements highlighted on the site. Compile a report summarizing these
|
||||
insights, specifically how they can be leveraged in a job posting to attract
|
||||
the right candidates.
|
||||
expected_output: >
|
||||
A comprehensive report detailing the company's culture, values, and mission,
|
||||
along with specific selling points relevant to the job role. Suggestions on
|
||||
incorporating these insights into the job posting should be included.
|
||||
agent: researcher
|
||||
|
||||
research_role_requirements:
|
||||
description: >
|
||||
Based on the hiring manager's needs: "{hiring_needs}", identify the key skills,
|
||||
experiences, and qualities the ideal candidate should possess for the role.
|
||||
Consider the company's current projects, its competitive landscape, and industry
|
||||
trends. Prepare a list of recommended job requirements and qualifications that
|
||||
align with the company's needs and values.
|
||||
expected_output: >
|
||||
A list of recommended skills, experiences, and qualities for the ideal candidate,
|
||||
aligned with the company's culture, ongoing projects, and the specific role's requirements.
|
||||
agent: researcher
|
||||
|
||||
draft_job_posting:
|
||||
description: >
|
||||
Draft a job posting for the role described by the hiring manager: "{hiring_needs}".
|
||||
Use the insights on "{company_description}" to start with a compelling introduction,
|
||||
followed by a detailed role description, responsibilities, and required skills and
|
||||
qualifications. Ensure the tone aligns with the company's culture and incorporate
|
||||
any unique benefits or opportunities offered by the company.
|
||||
Specific benefits: "{specific_benefits}"
|
||||
expected_output: >
|
||||
A detailed, engaging job posting that includes an introduction, role description,
|
||||
responsibilities, requirements, and unique company benefits. The tone should
|
||||
resonate with the company's culture and values, aimed at attracting the right candidates.
|
||||
agent: writer
|
||||
context:
|
||||
- research_company_culture
|
||||
- research_role_requirements
|
||||
|
||||
review_and_edit_job_posting:
|
||||
description: >
|
||||
Review the draft job posting for the role: "{hiring_needs}". Check for clarity,
|
||||
engagement, grammatical accuracy, and alignment with the company's culture and
|
||||
values. Edit and refine the content, ensuring it speaks directly to the desired
|
||||
candidates and accurately reflects the role's unique benefits and opportunities.
|
||||
Provide feedback for any necessary revisions.
|
||||
expected_output: >
|
||||
A polished, error-free job posting that is clear, engaging, and perfectly
|
||||
aligned with the company's culture and values. Feedback on potential improvements
|
||||
and final approval for publishing. Formatted in markdown.
|
||||
agent: editor
|
||||
context:
|
||||
- draft_job_posting
|
||||
output_file: "job_posting.md"
|
||||
```
|
||||
|
||||
### Step 6: Update the Crew Definition
|
||||
|
||||
Edit `src/job_posting_agent/crew.py` to add tools and AgentOps:
|
||||
|
||||
```python
|
||||
from crewai import Agent, Crew, Process, Task
|
||||
from crewai.project import CrewBase, agent, crew, task
|
||||
from crewai_tools import SerperDevTool, WebsiteSearchTool
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
@CrewBase
|
||||
class JobPostingAgentCrew():
|
||||
"""JobPostingAgent crew"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
# Initialize tools
|
||||
self.serper_tool = SerperDevTool()
|
||||
self.website_tool = WebsiteSearchTool()
|
||||
|
||||
@agent
|
||||
def researcher(self) -> Agent:
|
||||
return Agent(
|
||||
config=self.agents_config['researcher'],
|
||||
tools=[self.serper_tool, self.website_tool]
|
||||
)
|
||||
|
||||
@agent
|
||||
def writer(self) -> Agent:
|
||||
return Agent(
|
||||
config=self.agents_config['writer'],
|
||||
tools=[self.serper_tool, self.website_tool]
|
||||
)
|
||||
|
||||
@agent
|
||||
def editor(self) -> Agent:
|
||||
return Agent(
|
||||
config=self.agents_config['editor']
|
||||
)
|
||||
|
||||
@task
|
||||
def research_company_culture(self) -> Task:
|
||||
return Task(
|
||||
config=self.tasks_config['research_company_culture']
|
||||
)
|
||||
|
||||
@task
|
||||
def research_role_requirements(self) -> Task:
|
||||
return Task(
|
||||
config=self.tasks_config['research_role_requirements']
|
||||
)
|
||||
|
||||
@task
|
||||
def draft_job_posting(self) -> Task:
|
||||
return Task(
|
||||
config=self.tasks_config['draft_job_posting']
|
||||
)
|
||||
|
||||
@task
|
||||
def review_and_edit_job_posting(self) -> Task:
|
||||
return Task(
|
||||
config=self.tasks_config['review_and_edit_job_posting']
|
||||
)
|
||||
|
||||
@crew
|
||||
def crew(self) -> Crew:
|
||||
"""Creates the JobPostingAgent crew"""
|
||||
return Crew(
|
||||
agents=self.agents,
|
||||
tasks=self.tasks,
|
||||
process=Process.sequential,
|
||||
verbose=True
|
||||
)
|
||||
```
|
||||
|
||||
### Step 7: Update the Main Execution File
|
||||
|
||||
Edit `src/job_posting_agent/main.py` to add AgentOps session management:
|
||||
|
||||
```python
|
||||
#!/usr/bin/env python
|
||||
import sys
|
||||
import warnings
|
||||
from job_posting_agent.crew import JobPostingAgentCrew
|
||||
import agentops
|
||||
|
||||
warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd")
|
||||
|
||||
def run():
|
||||
"""
|
||||
Run the crew with AgentOps tracking.
|
||||
"""
|
||||
# Start AgentOps session
|
||||
session = agentops.start_session(tags=["crewai", "job-posting"])
|
||||
|
||||
try:
|
||||
# Define the inputs for the crew
|
||||
inputs = {
|
||||
'company_description': 'A fast-paced tech startup focused on AI-driven solutions for e-commerce.',
|
||||
'company_domain': 'https://agentops.ai',
|
||||
'hiring_needs': 'Senior Software Engineer with experience in Python, AI, and cloud platforms.',
|
||||
'specific_benefits': 'Competitive salary, stock options, remote work flexibility, and great team culture.'
|
||||
}
|
||||
|
||||
# Initialize and run the crew
|
||||
crew = JobPostingAgentCrew().crew()
|
||||
result = crew.kickoff(inputs=inputs)
|
||||
|
||||
print("\n" + "="*50)
|
||||
print("Job Posting Creation Completed!")
|
||||
print("Result:")
|
||||
print(result)
|
||||
print("\nCheck 'job_posting.md' for the generated job posting.")
|
||||
|
||||
# End session successfully
|
||||
agentops.end_session("Success")
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
agentops.end_session("Failed", end_state_reason=str(e))
|
||||
raise
|
||||
|
||||
def train():
|
||||
"""
|
||||
Train the crew for a given number of iterations.
|
||||
"""
|
||||
inputs = {
|
||||
'company_description': 'A fast-paced tech startup focused on AI-driven solutions for e-commerce.',
|
||||
'company_domain': 'https://agentops.ai',
|
||||
'hiring_needs': 'Senior Software Engineer with experience in Python, AI, and cloud platforms.',
|
||||
'specific_benefits': 'Competitive salary, stock options, remote work flexibility, and great team culture.'
|
||||
}
|
||||
try:
|
||||
JobPostingAgentCrew().crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)
|
||||
except Exception as e:
|
||||
raise Exception(f"An error occurred while training the crew: {e}")
|
||||
|
||||
def replay():
|
||||
"""
|
||||
Replay the crew execution from a specific task.
|
||||
"""
|
||||
try:
|
||||
JobPostingAgentCrew().crew().replay(task_id=sys.argv[1])
|
||||
except Exception as e:
|
||||
raise Exception(f"An error occurred while replaying the crew: {e}")
|
||||
|
||||
def test():
|
||||
"""
|
||||
Test the crew execution and returns the results.
|
||||
"""
|
||||
inputs = {
|
||||
'company_description': 'A fast-paced tech startup focused on AI-driven solutions for e-commerce.',
|
||||
'company_domain': 'https://agentops.ai',
|
||||
'hiring_needs': 'Senior Software Engineer with experience in Python, AI, and cloud platforms.',
|
||||
'specific_benefits': 'Competitive salary, stock options, remote work flexibility, and great team culture.'
|
||||
}
|
||||
try:
|
||||
JobPostingAgentCrew().crew().test(n_iterations=int(sys.argv[1]), openai_model_name=sys.argv[2], inputs=inputs)
|
||||
except Exception as e:
|
||||
raise Exception(f"An error occurred while testing the crew: {e}")
|
||||
```
|
||||
|
||||
### Step 8: Run Your Job Posting Generator
|
||||
|
||||
Navigate to your project and run:
|
||||
|
||||
```bash
|
||||
cd job_posting_agent
|
||||
crewai run
|
||||
```
|
||||
|
||||
**What happens:**
|
||||
1. AgentOps session starts automatically
|
||||
2. Research agent analyzes the company and industry trends
|
||||
3. Writer agent drafts the job posting using research insights
|
||||
4. Editor agent reviews and polishes the final version
|
||||
5. A `job_posting.md` file is created with the result
|
||||
6. AgentOps captures all agent interactions, tool usage, and costs
|
||||
7. Session ends with success/failure status
|
||||
|
||||
## View Results in AgentOps Dashboard
|
||||
|
||||
After running your job posting generator, visit your [AgentOps Dashboard](https://app.agentops.ai) to see:
|
||||
|
||||
1. **Session Overview**: Complete timeline of the 3-agent workflow
|
||||
2. **Agent Conversations**: Every LLM call with prompts and responses
|
||||
3. **Tool Execution**: Web searches and their results
|
||||
4. **Cost Breakdown**: Token usage and costs per agent and task
|
||||
5. **Performance Analytics**: Execution times and success rates
|
||||
6. **Session Replay**: Step-by-step playback for debugging
|
||||
|
||||
## Key Files Modified
|
||||
|
||||
**Configuration files you edited:**
|
||||
- `config/agents.yaml` - Agent definitions with roles and backstories
|
||||
- `config/tasks.yaml` - Task workflow with context dependencies
|
||||
- `crew.py` - Added tools and AgentOps initialization
|
||||
- `main.py` - Added session management
|
||||
- `.env` - Added AgentOps and Serper API keys
|
||||
|
||||
**Generated output:**
|
||||
- `job_posting.md` - Final job posting created by the crew
|
||||
|
||||
**AgentOps Integration Points:**
|
||||
- `agentops.init()` in `crew.py` - Enables automatic instrumentation
|
||||
- `agentops.start_session()` in `main.py` - Begins tracking
|
||||
- `agentops.end_session()` in `main.py` - Completes the session
|
||||
|
||||
## Next Steps
|
||||
|
||||
- Customize the input parameters in `main.py`
|
||||
- Modify agent configurations in `agents.yaml`
|
||||
- Add more tasks or change the workflow in `tasks.yaml`
|
||||
- Add custom tools in the `tools/` directory
|
||||
- Use AgentOps analytics to optimize agent performance
|
||||
@@ -0,0 +1,88 @@
|
||||
---
|
||||
title: 'Examples'
|
||||
description: 'Examples of AgentOps with various integrations'
|
||||
---
|
||||
|
||||
## Explore our examples to see AgentOps in action!
|
||||
|
||||
### LLM Integrations
|
||||
|
||||
<CardGroup cols={3}>
|
||||
|
||||
<Card title="Anthropic" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/anthropic/anthropic_icon_slate.png?raw=true" alt="Anthropic" />} iconType="image" href="/v2/examples/anthropic">
|
||||
Claude integration with tool usage and advanced features
|
||||
</Card>
|
||||
|
||||
<Card title="Google Generative AI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/deepmind/gemini-logo.png?raw=true" alt="Gemini" />} iconType="image" href="/v2/examples/google_genai">
|
||||
Google Gemini models and their examples
|
||||
</Card>
|
||||
|
||||
<Card title="LiteLLM" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/litellm/litellm.png?raw=true" alt="LiteLLM" />} iconType="image" href="/v2/examples/litellm">
|
||||
Unified LLM interface monitoring example
|
||||
</Card>
|
||||
|
||||
<Card title="OpenAI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/openai/openai-logomark.png?raw=true" alt="OpenAI" />} iconType="image" href="/v2/examples/openai">
|
||||
Advanced multi-tool orchestration with GPT models
|
||||
</Card>
|
||||
|
||||
<Card title="Watsonx" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/ibm/ibm-logo.svg?raw=true" alt="IBM" />} iconType="image" href="/v2/examples/watsonx">
|
||||
Watsonx text chat integration example
|
||||
</Card>
|
||||
|
||||
<Card title="x.AI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/xai/xai-logo.png?raw=true" alt="x.AI" />} iconType="image" href="/v2/examples/xai">
|
||||
Grok LLM basic usage patterns
|
||||
</Card>
|
||||
|
||||
</CardGroup>
|
||||
|
||||
|
||||
### Agent Integrations
|
||||
|
||||
<CardGroup>
|
||||
|
||||
<Card title="AG2" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/ag2/ag2-logo.svg?raw=true" alt="AG2" />} iconType="image" href="/v2/examples/ag2">
|
||||
Multi-agent conversations with memory capabilities
|
||||
</Card>
|
||||
|
||||
<Card title="Agno" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/agno/agno.png?raw=true" alt="Agno" />} iconType="image" href="/v2/examples/agno">
|
||||
Modern AI agent framework with teams, workflows, and tool integration
|
||||
</Card>
|
||||
|
||||
<Card title="Autogen" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/autogen/autogen-logo.svg?raw=true" alt="Autogen" />} iconType="image" href="/v2/examples/autogen">
|
||||
AG2 multi-agent workflow demonstration
|
||||
</Card>
|
||||
|
||||
<Card title="CrewAI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/crewai/crewai-logo.png?raw=true" alt="CrewAI" />} iconType="image" href="/v2/examples/crewai">
|
||||
CrewAI multi-agent framework example
|
||||
</Card>
|
||||
|
||||
<Card title="Google ADK" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/google-adk/google-adk-logo.png?raw=true" alt="Google ADK" />} iconType="image" href="/v2/examples/google_adk">
|
||||
Google Agent Development Kit integration
|
||||
</Card>
|
||||
|
||||
<Card title="Haystack" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/haystack/haystack-logo.png?raw=true" alt="Haystack" />} iconType="image" href="/v2/integrations/haystack">
|
||||
Monitor your Haystack agents with AgentOps
|
||||
</Card>
|
||||
|
||||
<Card title="OpenAI Agents SDK" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/openai/openai-logomark.png?raw=true" alt="OpenAI Agents SDK" />} iconType="image" href="/v2/examples/openai_agents">
|
||||
OpenAI Agents SDK workflow walkthrough
|
||||
</Card>
|
||||
|
||||
<Card title="LangChain" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/langchain/langchain-logo.svg?raw=true" alt="LangChain" />} iconType="image" href="/v2/examples/langchain">
|
||||
LangChain callback handler integration
|
||||
</Card>
|
||||
|
||||
<Card title="Mem0" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/mem0/mem0.png?raw=true" alt="Mem0" />} iconType="image" href="/v2/examples/mem0">
|
||||
Comprehensive memory operations with Mem0ai
|
||||
</Card>
|
||||
|
||||
<Card title="smolagents" icon={<img src="https://github.com/AgentOps-AI/agentops/blob/main/docs/images/external/huggingface/hf-logo.png?raw=true" alt="smolagents" />} iconType="image" href="/v2/integrations/smolagents">
|
||||
Track HuggingFace's smolagents with AgentOps seamlessly
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
|
||||
<script type="module" src="/scripts/github_stars.js" />
|
||||
<script type="module" src="/scripts/scroll-img-fadein-animation.js" />
|
||||
<script type="module" src="/scripts/button_heartbeat_animation.js" />
|
||||
<script type="module" src="/scripts/adjust_api_dynamically.js" />
|
||||
@@ -0,0 +1,386 @@
|
||||
---
|
||||
title: 'Google ADK'
|
||||
description: 'Build a human approval workflow with Google ADK and AgentOps tracking'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/google_adk/human_approval.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/google_adk/human_approval.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# Google ADK Human Approval Workflow with AgentOps
|
||||
|
||||
This example shows you how to build a complete human approval workflow using Google's Agent Development Kit (ADK) with comprehensive tracking via AgentOps.
|
||||
|
||||
## What We're Building
|
||||
|
||||
A **3-agent sequential workflow** for processing approval requests:
|
||||
- 🔍 **Prepare Agent**: Extracts and validates approval request details
|
||||
- 👤 **Approval Agent**: Handles human approval via external tool
|
||||
- ✅ **Decision Agent**: Processes approval decisions and provides final response
|
||||
|
||||
**With AgentOps**, you'll get complete visibility into agent conversations, tool usage, costs, and performance.
|
||||
|
||||
## Step-by-Step Implementation
|
||||
|
||||
|
||||
|
||||
### Step 1: Install Dependencies
|
||||
|
||||
Install Google ADK with required packages and AgentOps for tracking:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install google-adk agentops nest_asyncio python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add google-adk agentops nest_asyncio python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install google-adk agentops nest_asyncio python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
**What AgentOps adds:**
|
||||
- 🔍 **Automatic tracking** of all LLM calls and agent interactions
|
||||
- 💰 **Cost monitoring** with token usage breakdown
|
||||
- 🛠️ **Tool usage analytics** for approval workflow steps
|
||||
- 📊 **Performance metrics** and execution timelines
|
||||
- 🐛 **Session replay** for debugging and optimization
|
||||
|
||||
|
||||
### Step 2: Create Google ADK Project
|
||||
|
||||
Create the proper project structure for your approval workflow:
|
||||
|
||||
```bash
|
||||
# Create project directory
|
||||
mkdir human_approval_workflow
|
||||
cd human_approval_workflow
|
||||
|
||||
# Create agent module
|
||||
mkdir approval_agent
|
||||
touch approval_agent/__init__.py
|
||||
touch approval_agent/agent.py
|
||||
touch approval_agent/.env
|
||||
```
|
||||
|
||||
**This creates the standard Google ADK structure:**
|
||||
```
|
||||
human_approval_workflow/
|
||||
└── approval_agent/
|
||||
├── __init__.py # Module initialization
|
||||
├── agent.py # Agent definitions
|
||||
└── .env # API keys
|
||||
```
|
||||
|
||||
### Step 3: Set Up Environment Variables
|
||||
|
||||
Create the `.env` file in `approval_agent/` directory:
|
||||
|
||||
```bash
|
||||
# approval_agent/.env
|
||||
GOOGLE_GENAI_USE_VERTEXAI=FALSE
|
||||
GOOGLE_API_KEY=your_google_api_key_here
|
||||
AGENTOPS_API_KEY=your_agentops_api_key_here
|
||||
```
|
||||
|
||||
**Get your API keys:**
|
||||
- **Google API Key**: [Google AI Studio](https://aistudio.google.com/app/apikey)
|
||||
- **AgentOps API Key**: [AgentOps Settings](https://agentops.ai/settings/projects)
|
||||
|
||||
**Note**: For Vertex AI, set `GOOGLE_GENAI_USE_VERTEXAI=TRUE` and run `gcloud auth application-default login`
|
||||
|
||||
### Step 4: Create Module Initialization
|
||||
|
||||
Edit `approval_agent/__init__.py`:
|
||||
|
||||
```python
|
||||
# approval_agent/__init__.py
|
||||
from . import agent
|
||||
```
|
||||
|
||||
### Step 5: Define the Agent Workflow
|
||||
|
||||
Edit `approval_agent/agent.py` to create your approval workflow:
|
||||
|
||||
```python
|
||||
# approval_agent/agent.py
|
||||
import json
|
||||
import os
|
||||
import asyncio
|
||||
from google.adk.agents import LlmAgent, SequentialAgent
|
||||
from google.adk.tools import FunctionTool
|
||||
from google.adk.runners import Runner
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.genai import types
|
||||
from pydantic import BaseModel, Field
|
||||
import nest_asyncio
|
||||
import agentops
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment and initialize AgentOps
|
||||
load_dotenv()
|
||||
nest_asyncio.apply()
|
||||
agentops.init(auto_start_session=False)
|
||||
|
||||
# Constants
|
||||
APP_NAME = "human_approval_workflow"
|
||||
USER_ID = "approval_user"
|
||||
MODEL_NAME = "gemini-2.0-flash"
|
||||
|
||||
# Data models
|
||||
class ApprovalRequest(BaseModel):
|
||||
amount: float = Field(description="The amount requiring approval")
|
||||
reason: str = Field(description="The reason for the request")
|
||||
|
||||
class ApprovalDecision(BaseModel):
|
||||
decision: str = Field(description="The approval decision: 'approved' or 'rejected'")
|
||||
comments: str = Field(description="Additional comments from the approver")
|
||||
```
|
||||
|
||||
Add the approval tool function:
|
||||
|
||||
```python
|
||||
# External approval tool with human interaction
|
||||
async def external_approval_tool(amount: float, reason: str) -> str:
|
||||
"""
|
||||
Prompts for human approval and returns the decision as a JSON string.
|
||||
"""
|
||||
print("🔔 HUMAN APPROVAL REQUIRED:")
|
||||
print(f" Amount: ${amount:,.2f}")
|
||||
print(f" Reason: {reason}")
|
||||
decision = ""
|
||||
while decision.lower() not in ["approved", "rejected"]:
|
||||
decision = input(" Enter decision (approved/rejected): ").strip().lower()
|
||||
if decision.lower() not in ["approved", "rejected"]:
|
||||
print(" Invalid input. Please enter 'approved' or 'rejected'.")
|
||||
comments = input(" Enter comments (optional): ").strip()
|
||||
print(f" Decision: {decision.upper()}")
|
||||
print(f" Comments: {comments if comments else 'N/A'}")
|
||||
return json.dumps({"decision": decision, "comments": comments, "amount": amount, "reason": reason})
|
||||
|
||||
# Create the approval tool instance
|
||||
approval_tool = FunctionTool(func=external_approval_tool)
|
||||
```
|
||||
|
||||
### Step 6: Define the Three-Agent Workflow
|
||||
|
||||
Add the agent definitions to create your sequential workflow:
|
||||
|
||||
```python
|
||||
# Agent 1: Prepare the approval request
|
||||
prepare_request = LlmAgent(
|
||||
model=MODEL_NAME,
|
||||
name="PrepareApprovalAgent",
|
||||
description="Extracts and prepares approval request details from user input",
|
||||
instruction="""You are an approval request preparation agent.
|
||||
Your task:
|
||||
1. Extract the amount and reason from the user's request
|
||||
2. Store these values in the session state with keys 'approval_amount' and 'approval_reason'
|
||||
3. Validate that both amount and reason are provided
|
||||
4. Respond with a summary of what will be submitted for approval
|
||||
If the user input is missing amount or reason, ask for clarification.
|
||||
""",
|
||||
output_key="request_prepared",
|
||||
)
|
||||
|
||||
# Agent 2: Request human approval using the tool
|
||||
request_approval = LlmAgent(
|
||||
model=MODEL_NAME,
|
||||
name="RequestHumanApprovalAgent",
|
||||
description="Calls the external approval system with prepared request details",
|
||||
instruction="""You are a human approval request agent.
|
||||
Your task:
|
||||
1. Get the 'approval_amount' and 'approval_reason' from the session state
|
||||
2. Use the external_approval_tool with these values
|
||||
3. Store the approval decision in session state with key 'human_decision'
|
||||
4. Respond with the approval status
|
||||
Always use the exact values from the session state for the tool call.
|
||||
""",
|
||||
tools=[approval_tool],
|
||||
output_key="approval_requested",
|
||||
)
|
||||
|
||||
# Agent 3: Process the approval decision
|
||||
process_decision = LlmAgent(
|
||||
model=MODEL_NAME,
|
||||
name="ProcessDecisionAgent",
|
||||
description="Processes the human approval decision and provides final response",
|
||||
instruction="""You are a decision processing agent.
|
||||
Your task:
|
||||
1. Check the 'human_decision' from session state
|
||||
2. Parse the approval decision JSON
|
||||
3. If approved: congratulate and provide next steps
|
||||
4. If rejected: explain the rejection and suggest alternatives
|
||||
5. Provide a clear, helpful final response to the user
|
||||
|
||||
Be professional and helpful in your response.
|
||||
""",
|
||||
output_key="final_decision",
|
||||
)
|
||||
```
|
||||
|
||||
### Step 7: Create the Sequential Workflow and Runner
|
||||
|
||||
Combine agents into a workflow with session management:
|
||||
|
||||
```python
|
||||
# Create sequential workflow
|
||||
approval_workflow = SequentialAgent(
|
||||
name="HumanApprovalWorkflow",
|
||||
description="Complete workflow for processing approval requests with human oversight",
|
||||
sub_agents=[prepare_request, request_approval, process_decision],
|
||||
)
|
||||
|
||||
# Set up session service and runner
|
||||
session_service = InMemorySessionService()
|
||||
workflow_runner = Runner(agent=approval_workflow, app_name=APP_NAME, session_service=session_service)
|
||||
```
|
||||
|
||||
### Step 8: Add the Main Execution Function
|
||||
|
||||
Create the function to run the approval workflow with AgentOps tracking:
|
||||
|
||||
```python
|
||||
async def run_approval_workflow(user_request: str, session_id: str):
|
||||
"""Run the complete approval workflow with AgentOps tracking"""
|
||||
# Start AgentOps session
|
||||
session = agentops.start_session(tags=["google-adk", "approval-workflow"])
|
||||
|
||||
try:
|
||||
print(f"{'=' * 60}")
|
||||
print(f" Starting Approval Workflow for Session: {session_id}")
|
||||
print(f"{'=' * 60}")
|
||||
print(f"User Request: {user_request}")
|
||||
|
||||
# Create user message
|
||||
user_content = types.Content(role="user", parts=[types.Part(text=user_request)])
|
||||
step_count = 0
|
||||
final_response = "No response received"
|
||||
|
||||
# Run the workflow
|
||||
async for event in workflow_runner.run_async(
|
||||
user_id=USER_ID,
|
||||
session_id=session_id,
|
||||
new_message=user_content,
|
||||
):
|
||||
if event.author and event.content:
|
||||
step_count += 1
|
||||
print(f"📋 Step {step_count} - {event.author}:")
|
||||
if event.content.parts:
|
||||
response_text = event.content.parts[0].text
|
||||
print(f" {response_text}")
|
||||
if event.is_final_response():
|
||||
final_response = response_text
|
||||
|
||||
# Display session state
|
||||
session_data = await session_service.get_session(
|
||||
app_name=APP_NAME,
|
||||
user_id=USER_ID,
|
||||
session_id=session_id,
|
||||
)
|
||||
print(f"{'=' * 60}")
|
||||
print(f"📊 Workflow Complete - Session State ({session_id}):")
|
||||
print(f"{'=' * 60}")
|
||||
for key, value in session_data.state.items():
|
||||
print(f" {key}: {value}")
|
||||
print(f"🎯 Final Response: {final_response}")
|
||||
|
||||
# End AgentOps session successfully
|
||||
agentops.end_session("Success")
|
||||
return final_response
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error occurred: {e}")
|
||||
agentops.end_session("Failed", end_state_reason=str(e))
|
||||
raise
|
||||
```
|
||||
|
||||
Finally, add the main execution logic:
|
||||
|
||||
```python
|
||||
# Main execution function
|
||||
async def main():
|
||||
test_requests = [
|
||||
"I need approval for $750 for team lunch and celebrations",
|
||||
"Please approve $3,000 for a conference ticket and travel expenses",
|
||||
"I need $12,000 approved for critical software licenses renewal",
|
||||
]
|
||||
|
||||
for i, request in enumerate(test_requests, 1):
|
||||
current_session_id = f"approval_session_{456 + i - 1}"
|
||||
# Create the session before running the workflow
|
||||
await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=current_session_id)
|
||||
print(f"Created session: {current_session_id}")
|
||||
await run_approval_workflow(request, current_session_id)
|
||||
|
||||
# Export the workflow for ADK
|
||||
root_agent = approval_workflow
|
||||
```
|
||||
|
||||
### Step 9: Run Your Approval Workflow
|
||||
|
||||
Navigate to your project directory and run the workflow:
|
||||
|
||||
**Option 1: Interactive Development UI**
|
||||
```bash
|
||||
cd human_approval_workflow
|
||||
adk web
|
||||
```
|
||||
Then open http://localhost:8000, select "approval_agent" from the dropdown, and interact with your workflow.
|
||||
|
||||
**Option 2: Terminal Interface**
|
||||
```bash
|
||||
cd human_approval_workflow
|
||||
adk run approval_agent
|
||||
```
|
||||
|
||||
**Option 3: Run Programmatically**
|
||||
```python
|
||||
# Run this in a separate script or notebook
|
||||
import asyncio
|
||||
from approval_agent.agent import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
```
|
||||
|
||||
**What happens:**
|
||||
1. AgentOps session starts automatically
|
||||
2. Prepare agent extracts amount and reason from requests
|
||||
3. Approval agent prompts for human input via terminal
|
||||
4. Decision agent processes the approval and provides final response
|
||||
5. AgentOps captures all interactions, tool usage, and costs
|
||||
6. Session ends with success/failure status
|
||||
## View Results in AgentOps Dashboard
|
||||
|
||||
After running your approval workflow, visit your [AgentOps Dashboard](https://app.agentops.ai) to see:
|
||||
|
||||
1. **Session Overview**: Complete timeline of the 3-agent workflow
|
||||
2. **Agent Conversations**: Every LLM call with prompts and responses
|
||||
3. **Tool Execution**: Human approval interactions and decisions
|
||||
4. **Cost Breakdown**: Token usage and costs per agent and step
|
||||
5. **Performance Analytics**: Execution times and success rates
|
||||
6. **Session Replay**: Step-by-step playback for debugging
|
||||
|
||||
## Key Files Created
|
||||
|
||||
**Project structure you built:**
|
||||
- `approval_agent/__init__.py` - Module initialization with agent import
|
||||
- `approval_agent/agent.py` - Complete workflow with 3 agents and AgentOps integration
|
||||
- `approval_agent/.env` - API keys for Google AI and AgentOps
|
||||
|
||||
**AgentOps Integration Points:**
|
||||
- `agentops.init()` - Enables automatic instrumentation
|
||||
- `agentops.start_session()` - Begins tracking each workflow run
|
||||
- `agentops.end_session()` - Completes the session with status
|
||||
|
||||
## Next Steps
|
||||
|
||||
- Customize approval thresholds and business logic
|
||||
- Add more sophisticated approval routing
|
||||
- Integrate with external approval systems (Slack, email, etc.)
|
||||
- Use Google ADK's web UI for better user experience
|
||||
- Use AgentOps analytics to optimize approval times
|
||||
@@ -0,0 +1,93 @@
|
||||
---
|
||||
title: 'Google GenAI'
|
||||
description: 'Google Generative AI Example with AgentOps'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/google_genai/gemini_example.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/google_genai/gemini_example.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# 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
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops google-genai
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops google-genai
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops google-genai
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
```python
|
||||
from google import genai
|
||||
import agentops
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
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
|
||||
# Initialize AgentOps and Gemini client
|
||||
agentops.init(tags=["gemini-example", "agentops-example"])
|
||||
client = genai.Client()
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# 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
|
||||
# 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
|
||||
# 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}")
|
||||
```
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,141 @@
|
||||
---
|
||||
title: 'LangChain'
|
||||
description: 'AgentOps Langchain Agent Implementation'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/langchain/langchain_examples.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/langchain/langchain_examples.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# AgentOps Langchain Agent Implementation
|
||||
|
||||
Using AgentOps monitoring with Langchain is simple. We've created a LangchainCallbackHandler that will do all of the heavy lifting!
|
||||
|
||||
First let's install the required packages
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops langchain langchain_openai python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops langchain langchain_openai python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops langchain langchain_openai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then import them
|
||||
|
||||
|
||||
```python
|
||||
import os
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain.agents import tool, AgentExecutor, create_openai_tools_agent
|
||||
from dotenv import load_dotenv
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
```
|
||||
|
||||
The only difference with using AgentOps is that we'll also import this special Callback Handler
|
||||
|
||||
|
||||
```python
|
||||
from agentops.integration.callbacks.langchain import (
|
||||
LangchainCallbackHandler as AgentOpsLangchainCallbackHandler,
|
||||
)
|
||||
```
|
||||
|
||||
Next, we'll set our API keys. There are several ways to do this, the code below is just the most foolproof way for the purposes of this notebook. It accounts for both users who use environment variables and those who just want to set the API Key here in this notebook.
|
||||
|
||||
[Get an AgentOps API key](https://agentops.ai/settings/projects)
|
||||
|
||||
1. Create an environment variable in a .env file or other method. By default, the AgentOps `init()` function will look for an environment variable named `AGENTOPS_API_KEY`. Or...
|
||||
|
||||
2. Replace `<your_agentops_key>` below and pass in the optional `api_key` parameter to the AgentOps `init(api_key=...)` function. Remember not to commit your API key to a public repo!
|
||||
|
||||
|
||||
```python
|
||||
load_dotenv()
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")
|
||||
```
|
||||
|
||||
This is where AgentOps comes into play. Before creating our LLM instance via Langchain, first we'll create an instance of the AO LangchainCallbackHandler. After the handler is initialized, a session will be recorded automatically.
|
||||
|
||||
Pass in your API key, and optionally any tags to describe this session for easier lookup in the AO dashboard.
|
||||
|
||||
|
||||
|
||||
```python
|
||||
agentops_handler = AgentOpsLangchainCallbackHandler(tags=["Langchain Example", "agentops-example"])
|
||||
|
||||
llm = ChatOpenAI(callbacks=[agentops_handler], model="gpt-3.5-turbo")
|
||||
|
||||
# You must pass in a callback handler to record your agent
|
||||
llm.callbacks = [agentops_handler]
|
||||
|
||||
prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
("system", "You are a helpful assistant. Respond only in Spanish."),
|
||||
("human", "{input}"),
|
||||
# Placeholders fill up a **list** of messages
|
||||
("placeholder", "{agent_scratchpad}"),
|
||||
# ("tool_names", "find_movie")
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
Agents generally use tools. Let's define a simple tool here. Tool usage is also recorded.
|
||||
|
||||
|
||||
```python
|
||||
@tool
|
||||
def find_movie(genre: str) -> str:
|
||||
"""Find available movies"""
|
||||
if genre == "drama":
|
||||
return "Dune 2"
|
||||
else:
|
||||
return "Pineapple Express"
|
||||
|
||||
|
||||
tools = [find_movie]
|
||||
```
|
||||
|
||||
For each tool, you need to also add the callback handler
|
||||
|
||||
|
||||
```python
|
||||
for t in tools:
|
||||
t.callbacks = [agentops_handler]
|
||||
```
|
||||
|
||||
Add the tools to our LLM
|
||||
|
||||
|
||||
```python
|
||||
llm_with_tools = llm.bind_tools([find_movie])
|
||||
```
|
||||
|
||||
Finally, let's create our agent! Pass in the callback handler to the agent, and all the actions will be recorded in the AO Dashboard
|
||||
|
||||
|
||||
```python
|
||||
agent = create_openai_tools_agent(llm, tools, prompt)
|
||||
agent_executor = AgentExecutor(agent=agent, tools=tools)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
agent_executor.invoke({"input": "What comedies are playing?"}, config={"callback": [agentops_handler]})
|
||||
```
|
||||
|
||||
## Check your session
|
||||
Finally, check your run on [AgentOps](https://app.agentops.ai). You will see a session recorded with the LLM calls and tool usage.
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,260 @@
|
||||
---
|
||||
title: 'Langgraph'
|
||||
description: 'Build a basic chatbot with LangGraph and AgentOps tracking'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/langgraph/langgraph_example.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/langgraph/langgraph_example.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# LangGraph Basic Chatbot with AgentOps
|
||||
|
||||
This example shows you how to build a basic chatbot using LangGraph's StateGraph with comprehensive tracking via AgentOps.
|
||||
|
||||
## What We're Building
|
||||
|
||||
A **stateful chatbot** using LangGraph fundamentals:
|
||||
- 🗃️ **StateGraph**: Core LangGraph structure for managing conversation state
|
||||
- 💬 **Chat Model**: LLM integration for generating responses
|
||||
- 🔄 **State Management**: Automatic message history tracking with `add_messages`
|
||||
- 🎯 **Graph Flow**: START → chatbot node → END pattern
|
||||
|
||||
**With AgentOps**, you'll get complete visibility into graph execution, state transitions, and LLM interactions.
|
||||
|
||||
## Step-by-Step Implementation
|
||||
|
||||
|
||||
|
||||
### Step 1: Install Dependencies
|
||||
|
||||
Install LangGraph with your preferred chat model and AgentOps for tracking:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install langgraph langchain agentops python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add langgraph langchain agentops python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install langgraph langchain agentops python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
**What AgentOps adds:**
|
||||
- 📊 **Graph execution tracking** with node transitions and timing
|
||||
- 💰 **LLM cost monitoring** with token usage breakdown
|
||||
- 🔄 **State change visualization** showing message flow
|
||||
- 📈 **Performance metrics** for each graph execution
|
||||
- 🐛 **Execution replay** for debugging graph flows
|
||||
|
||||
### Step 2: Create Your Project Structure
|
||||
|
||||
Create a simple Python project for your chatbot:
|
||||
|
||||
```bash
|
||||
# Create project directory
|
||||
mkdir langgraph_chatbot
|
||||
cd langgraph_chatbot
|
||||
|
||||
# Create main chatbot file
|
||||
touch chatbot.py
|
||||
touch .env
|
||||
```
|
||||
|
||||
**This creates the basic structure:**
|
||||
```
|
||||
langgraph_chatbot/
|
||||
├── chatbot.py # Main chatbot implementation
|
||||
└── .env # API keys
|
||||
```
|
||||
|
||||
### Step 3: Set Up Environment Variables
|
||||
|
||||
Create your `.env` file with the necessary API keys:
|
||||
|
||||
```bash
|
||||
# .env
|
||||
OPENAI_API_KEY=your_openai_api_key_here
|
||||
AGENTOPS_API_KEY=your_agentops_api_key_here
|
||||
```
|
||||
|
||||
**Get your API keys:**
|
||||
- **OpenAI API Key**: [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
- **AgentOps API Key**: [AgentOps Settings](https://agentops.ai/settings/projects)
|
||||
|
||||
**Note**: You can use any LangChain-compatible model (Anthropic, Google, etc.) by adjusting the imports and model initialization.
|
||||
|
||||
### Step 4: Build Your Basic Chatbot
|
||||
|
||||
Edit `chatbot.py` to create your LangGraph chatbot:
|
||||
|
||||
```python
|
||||
# chatbot.py
|
||||
import os
|
||||
from typing import Annotated
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
from langchain.chat_models import init_chat_model
|
||||
from langgraph.graph import StateGraph, START, END
|
||||
from langgraph.graph.message import add_messages
|
||||
import agentops
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables and initialize AgentOps
|
||||
load_dotenv()
|
||||
agentops.init(auto_start_session=False)
|
||||
|
||||
# Define the State schema
|
||||
class State(TypedDict):
|
||||
# Messages have the type "list". The `add_messages` function
|
||||
# in the annotation defines how this state key should be updated
|
||||
# (in this case, it appends messages to the list, rather than overwriting them)
|
||||
messages: Annotated[list, add_messages]
|
||||
```
|
||||
|
||||
### Step 5: Initialize the Chat Model and Create the Chatbot Node
|
||||
|
||||
Add the model and node function:
|
||||
|
||||
```python
|
||||
# Initialize the chat model (you can change to any provider)
|
||||
llm = init_chat_model("openai:gpt-4o-mini")
|
||||
|
||||
# Create the chatbot node function
|
||||
def chatbot(state: State):
|
||||
"""Main chatbot function that processes messages and returns responses."""
|
||||
return {"messages": [llm.invoke(state["messages"])]}
|
||||
```
|
||||
|
||||
### Step 6: Build and Compile the StateGraph
|
||||
|
||||
Construct your LangGraph workflow:
|
||||
|
||||
```python
|
||||
# Create the StateGraph
|
||||
graph_builder = StateGraph(State)
|
||||
|
||||
# Add the chatbot node
|
||||
# The first argument is the unique node name
|
||||
# The second argument is the function that will be called
|
||||
graph_builder.add_node("chatbot", chatbot)
|
||||
|
||||
# Add entry point (where to start)
|
||||
graph_builder.add_edge(START, "chatbot")
|
||||
|
||||
# Add exit point (where to end)
|
||||
graph_builder.add_edge("chatbot", END)
|
||||
|
||||
# Compile the graph
|
||||
graph = graph_builder.compile()
|
||||
```
|
||||
|
||||
### Step 7: Add the Streaming Chat Function
|
||||
|
||||
Create the interactive chat interface with AgentOps tracking:
|
||||
|
||||
```python
|
||||
def stream_graph_updates(user_input: str):
|
||||
"""Stream graph updates for the given user input."""
|
||||
for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}):
|
||||
for value in event.values():
|
||||
print("Assistant:", value["messages"][-1].content)
|
||||
|
||||
def run_chatbot():
|
||||
"""Main function to run the chatbot with AgentOps tracking."""
|
||||
# Start AgentOps session
|
||||
session = agentops.start_session(tags=["langgraph", "chatbot"])
|
||||
|
||||
try:
|
||||
print("🤖 LangGraph Chatbot Started!")
|
||||
print("Type 'quit', 'exit', or 'q' to stop.\n")
|
||||
|
||||
while True:
|
||||
try:
|
||||
user_input = input("User: ")
|
||||
if user_input.lower() in ["quit", "exit", "q"]:
|
||||
print("Goodbye!")
|
||||
break
|
||||
stream_graph_updates(user_input)
|
||||
print() # Add blank line for readability
|
||||
except KeyboardInterrupt:
|
||||
print("\nGoodbye!")
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
break
|
||||
|
||||
# End session successfully
|
||||
agentops.end_session("Success")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error occurred: {e}")
|
||||
agentops.end_session("Failed", end_state_reason=str(e))
|
||||
raise
|
||||
|
||||
# Main execution
|
||||
if __name__ == "__main__":
|
||||
run_chatbot()
|
||||
```
|
||||
|
||||
### Step 8: Run Your Chatbot
|
||||
|
||||
Execute your chatbot:
|
||||
|
||||
```bash
|
||||
cd langgraph_chatbot
|
||||
python chatbot.py
|
||||
```
|
||||
|
||||
**What happens:**
|
||||
1. AgentOps session starts automatically
|
||||
2. Interactive chat loop begins
|
||||
3. Each user message flows through: START → chatbot node → END
|
||||
4. LLM generates responses based on conversation history
|
||||
5. AgentOps captures all state transitions and LLM interactions
|
||||
6. Session ends when you type 'quit'
|
||||
|
||||
**Example conversation:**
|
||||
```
|
||||
🤖 LangGraph Chatbot Started!
|
||||
Type 'quit', 'exit', or 'q' to stop.
|
||||
|
||||
User: Hello! What can you help me with?
|
||||
Assistant: Hello! I'm a helpful AI assistant. I can help you with a wide variety of tasks...
|
||||
|
||||
User: Tell me a joke
|
||||
Assistant: Why don't scientists trust atoms? Because they make up everything!
|
||||
|
||||
User: quit
|
||||
Goodbye!
|
||||
```
|
||||
|
||||
## View Results in AgentOps Dashboard
|
||||
|
||||
After running your chatbot, visit your [AgentOps Dashboard](https://app.agentops.ai) to see:
|
||||
|
||||
1. **Graph Structure**: Visual representation of your StateGraph (START → chatbot → END)
|
||||
2. **State Transitions**: How messages flow through the graph
|
||||
3. **LLM Interactions**: Every conversation turn with prompts and responses
|
||||
4. **Execution Timing**: How long each node takes to process
|
||||
5. **Session Analytics**: Conversation length, token usage, and costs
|
||||
6. **Message History**: Complete conversation flow with state management
|
||||
|
||||
## Key Files Created
|
||||
|
||||
**Project structure you built:**
|
||||
- `chatbot.py` - Complete LangGraph chatbot with AgentOps integration
|
||||
- `.env` - API keys for OpenAI and AgentOps
|
||||
|
||||
**AgentOps Integration Points:**
|
||||
- `agentops.init()` - Enables automatic LangGraph instrumentation
|
||||
- `agentops.start_session()` - Begins tracking each chat session
|
||||
- `agentops.end_session()` - Completes the session with status
|
||||
|
||||
## Next Steps
|
||||
|
||||
- Add tools to your chatbot (web search, calculators, etc.)
|
||||
- Implement more complex graph structures with conditional edges
|
||||
- Add memory persistence across sessions
|
||||
- Create multi-agent workflows with LangGraph
|
||||
- Use AgentOps analytics to optimize conversation flows
|
||||
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: 'LiteLLM'
|
||||
description: 'AgentOps for observing LiteLLM'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/litellm/litellm_example.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/litellm/litellm_example.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# AgentOps for observing LiteLLM
|
||||
|
||||
We can use AgentOps to observe LiteLLM, a lightweight library for working with large language models. This integration allows you to monitor and log the performance of your LiteLLM applications, providing insights into their behavior and efficiency.
|
||||
LiteLLM integration extends observability to the different agent libraries which rely on LiteLLM and hence make it possible to observe the agents built using these libraries.
|
||||
|
||||
[See our LiteLLM docs](https://docs.agentops.ai/v1/integrations/litellm)
|
||||
|
||||
First let's install the required packages
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install -U agentops litellm python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add -U agentops litellm python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install -U agentops litellm python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then import them
|
||||
|
||||
|
||||
```python
|
||||
import litellm
|
||||
import agentops
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
```
|
||||
|
||||
Next, we'll set our API keys. There are several ways to do this, the code below is just the most foolproof way for the purposes of this notebook. It accounts for both users who use environment variables and those who just want to set the API Key here in this notebook.
|
||||
|
||||
[Get an AgentOps API key](https://agentops.ai/settings/projects)
|
||||
|
||||
1. Create an environment variable in a .env file or other method. By default, the AgentOps `init()` function will look for an environment variable named `AGENTOPS_API_KEY`. Or...
|
||||
|
||||
2. Replace `<your_agentops_key>` below and pass in the optional `api_key` parameter to the AgentOps `init(api_key=...)` function. Remember not to commit your API key to a public repo!
|
||||
|
||||
LiteLLM allows you to use several models including from OpenAI, Llama, Mistral, Claude, Gemini, Gemma, Dall-E, Whisper, and more all using the OpenAI format. To use a different model all you need to change are the API KEY and model (litellm.completion(model="...")).
|
||||
|
||||
|
||||
```python
|
||||
load_dotenv()
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv(
|
||||
"OPENAI_API_KEY", "your_openai_api_key_here"
|
||||
) # or the provider of your choosing
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
agentops.init(auto_start_session=False)
|
||||
tracer = agentops.start_trace(trace_name="LiteLLM Example", tags=["litellm-example", "agentops-example"])
|
||||
```
|
||||
|
||||
Note: AgentOps requires that you call LiteLLM completions differently than the LiteLLM's docs mention
|
||||
Instead of doing this -
|
||||
|
||||
```python
|
||||
from litellm import completion
|
||||
completion()
|
||||
```
|
||||
|
||||
You should do this -
|
||||
|
||||
```python
|
||||
import litellm
|
||||
litellm.completion()
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
messages = [{"role": "user", "content": "Write a 12 word poem about secret agents."}]
|
||||
response = litellm.completion(model="gpt-4", messages=messages) # or the model of your choosing
|
||||
print(response.choices[0].message.content)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
agentops.end_trace(tracer, end_state="Success")
|
||||
```
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,136 @@
|
||||
---
|
||||
title: 'Llamaindex Examples Example'
|
||||
description: 'LlamaIndex AgentOps Integration Example'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/llamaindex_examples/llamaindex_example.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/llamaindex_examples/llamaindex_example.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# LlamaIndex AgentOps Integration Example
|
||||
|
||||
This notebook demonstrates how to use AgentOps with LlamaIndex for observability and monitoring of your context-augmented generative AI applications.
|
||||
|
||||
## Setup
|
||||
|
||||
First, install the required packages:
|
||||
|
||||
|
||||
```
|
||||
# Install required packages
|
||||
!pip install agentops llama-index-instrumentation-agentops llama-index-embeddings-huggingface llama-index-llms-huggingface python-dotenv
|
||||
```
|
||||
|
||||
## Initialize AgentOps Handler
|
||||
|
||||
Set up the AgentOps handler for LlamaIndex instrumentation:
|
||||
|
||||
|
||||
```
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from llama_index.core import VectorStoreIndex, Document, Settings
|
||||
from llama_index.instrumentation.agentops import AgentOpsHandler
|
||||
|
||||
# Initialize AgentOps handler
|
||||
handler = AgentOpsHandler()
|
||||
handler.init()
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Set API keys (replace with your actual keys)
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_agentops_api_key_here")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")
|
||||
```
|
||||
|
||||
## Configure Local Models (Optional)
|
||||
|
||||
For this example, we'll use local HuggingFace models to avoid requiring external API keys:
|
||||
|
||||
|
||||
```
|
||||
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
||||
from llama_index.llms.huggingface import HuggingFaceLLM
|
||||
|
||||
# Configure local embeddings and LLM
|
||||
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
||||
Settings.llm = HuggingFaceLLM(model_name="microsoft/DialoGPT-medium")
|
||||
print("Using local HuggingFace embeddings and LLM")
|
||||
```
|
||||
|
||||
## Create Sample Documents and Index
|
||||
|
||||
Create some sample documents and build a vector index:
|
||||
|
||||
|
||||
```
|
||||
print("🚀 Starting LlamaIndex AgentOps Integration Example")
|
||||
print("=" * 50)
|
||||
|
||||
# Create sample documents
|
||||
documents = [
|
||||
Document(text="LlamaIndex is a framework for building context-augmented generative AI applications with LLMs."),
|
||||
Document(text="AgentOps provides observability into your AI applications, tracking LLM calls, performance metrics, and more."),
|
||||
Document(text="The integration between LlamaIndex and AgentOps allows you to monitor your RAG applications seamlessly."),
|
||||
Document(text="Vector databases are used to store and retrieve embeddings for similarity search in RAG applications."),
|
||||
Document(text="Context-augmented generation combines retrieval and generation to provide more accurate and relevant responses.")
|
||||
]
|
||||
|
||||
print("📚 Creating vector index from sample documents...")
|
||||
index = VectorStoreIndex.from_documents(documents)
|
||||
print("✅ Vector index created successfully")
|
||||
```
|
||||
|
||||
## Perform Queries
|
||||
|
||||
Now let's perform some queries to demonstrate the AgentOps integration:
|
||||
|
||||
|
||||
```
|
||||
# Create query engine
|
||||
query_engine = index.as_query_engine()
|
||||
|
||||
print("🔍 Performing queries...")
|
||||
|
||||
# Sample queries
|
||||
queries = [
|
||||
"What is LlamaIndex?",
|
||||
"How does AgentOps help with AI applications?",
|
||||
"What are the benefits of using vector databases in RAG?"
|
||||
]
|
||||
|
||||
for i, query in enumerate(queries, 1):
|
||||
print(f"\n📝 Query {i}: {query}")
|
||||
response = query_engine.query(query)
|
||||
print(f"💬 Response: {response}")
|
||||
```
|
||||
|
||||
## Results
|
||||
|
||||
After running this notebook, you should see:
|
||||
|
||||
1. **AgentOps Session Link**: A URL to view the session in your AgentOps dashboard
|
||||
2. **Cost Tracking**: Information about the cost of LLM calls (if using paid APIs)
|
||||
3. **Operation Tracking**: All LlamaIndex operations are automatically tracked
|
||||
|
||||
Check your AgentOps dashboard to see detailed information about:
|
||||
- LLM calls and responses
|
||||
- Performance metrics
|
||||
- Cost analysis
|
||||
- Session replay
|
||||
|
||||
The session link will be printed in the output above by AgentOps.
|
||||
|
||||
|
||||
```
|
||||
print("\n" + "=" * 50)
|
||||
print("🎉 Example completed successfully!")
|
||||
print("📊 Check your AgentOps dashboard to see the recorded session with LLM calls and operations.")
|
||||
print("🔗 The session link should be printed above by AgentOps.")
|
||||
```
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,239 @@
|
||||
---
|
||||
title: 'Mem0'
|
||||
description: 'Memory Operations with Mem0'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/mem0/mem0_memory_example.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/mem0/mem0_memory_example.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# Memory Operations with Mem0
|
||||
|
||||
This example demonstrates how to use Mem0's memory management capabilities with both synchronous and asynchronous operations to store, search, and manage conversational context and user preferences.
|
||||
|
||||
## Overview
|
||||
|
||||
This example showcases practical memory management operations where we:
|
||||
|
||||
1. **Initialize Mem0 Memory instances** for both sync and async operations
|
||||
2. **Store conversation history** and user preferences with metadata
|
||||
3. **Search memories** using natural language queries
|
||||
4. **Compare performance** between synchronous and asynchronous memory operations
|
||||
|
||||
By using async operations, you can perform multiple memory operations simultaneously instead of waiting for each one to complete sequentially. This is particularly beneficial when dealing with multiple memory additions or searches.
|
||||
|
||||
|
||||
|
||||
|
||||
## Setup and Imports
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops mem0ai python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops mem0ai python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops mem0ai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
|
||||
Import the required libraries for local memory management with Mem0. We'll use both Memory and AsyncMemory classes to demonstrate different execution patterns for memory operations.
|
||||
|
||||
|
||||
|
||||
```python
|
||||
|
||||
from mem0 import Memory, AsyncMemory
|
||||
import os
|
||||
import asyncio
|
||||
import logging
|
||||
from dotenv import load_dotenv
|
||||
import agentops
|
||||
```
|
||||
|
||||
## Environment Configuration
|
||||
|
||||
Set up environment variables for API keys. These are essential for authenticating with AgentOps for tracing and OpenAI for the language model used in memory operations.
|
||||
|
||||
|
||||
|
||||
```python
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
||||
```
|
||||
|
||||
## Configuration and Sample Data
|
||||
|
||||
Set up the configuration for local memory storage and define sample user data. The configuration specifies the LLM provider and model settings for processing memories.
|
||||
|
||||
|
||||
|
||||
```python
|
||||
local_config = {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4o-mini",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
},
|
||||
}
|
||||
}
|
||||
user_id = "alice_demo"
|
||||
agent_id = "assistant_demo"
|
||||
run_id = "session_001"
|
||||
|
||||
sample_messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future.",
|
||||
},
|
||||
]
|
||||
|
||||
sample_preferences = [
|
||||
"I prefer dark roast coffee over light roast",
|
||||
"I exercise every morning at 6 AM",
|
||||
"I'm vegetarian and avoid all meat products",
|
||||
"I love reading science fiction novels",
|
||||
"I work in software engineering",
|
||||
]
|
||||
```
|
||||
|
||||
## Synchronous Memory Operations
|
||||
|
||||
This function demonstrates sequential memory operations using the synchronous Memory class. While straightforward to implement, each operation must complete before the next begins, which can impact performance.
|
||||
|
||||
|
||||
|
||||
```python
|
||||
def demonstrate_sync_memory(local_config, sample_messages, sample_preferences, user_id):
|
||||
"""
|
||||
Demonstrate synchronous Memory class operations.
|
||||
"""
|
||||
|
||||
agentops.start_trace("mem0_memory_example", tags=["mem0_memory_example"])
|
||||
try:
|
||||
|
||||
memory = Memory.from_config(local_config)
|
||||
|
||||
result = memory.add(
|
||||
sample_messages, user_id=user_id, metadata={"category": "movie_preferences", "session": "demo"}
|
||||
)
|
||||
|
||||
for i, preference in enumerate(sample_preferences):
|
||||
result = memory.add(preference, user_id=user_id, metadata={"type": "preference", "index": i})
|
||||
|
||||
search_queries = [
|
||||
"What movies does the user like?",
|
||||
"What are the user's food preferences?",
|
||||
"When does the user exercise?",
|
||||
]
|
||||
|
||||
for query in search_queries:
|
||||
results = memory.search(query, user_id=user_id)
|
||||
|
||||
if results and "results" in results:
|
||||
for j, result in enumerate(results):
|
||||
print(f"Result {j+1}: {result.get('memory', 'N/A')}")
|
||||
else:
|
||||
print("No results found")
|
||||
|
||||
all_memories = memory.get_all(user_id=user_id)
|
||||
if all_memories and "results" in all_memories:
|
||||
print(f"Total memories: {len(all_memories['results'])}")
|
||||
|
||||
delete_all_result = memory.delete_all(user_id=user_id)
|
||||
print(f"Delete all result: {delete_all_result}")
|
||||
|
||||
agentops.end_trace(end_state="success")
|
||||
except Exception as e:
|
||||
agentops.end_trace(end_state="error")
|
||||
```
|
||||
|
||||
## Asynchronous Memory Operations
|
||||
|
||||
This function showcases concurrent memory operations using AsyncMemory. By leveraging asyncio.gather(), multiple operations execute simultaneously, significantly reducing total execution time for I/O-bound tasks.
|
||||
|
||||
|
||||
|
||||
```python
|
||||
async def demonstrate_async_memory(local_config, sample_messages, sample_preferences, user_id):
|
||||
"""
|
||||
Demonstrate asynchronous Memory class operations with concurrent execution.
|
||||
"""
|
||||
|
||||
agentops.start_trace("mem0_memory_async_example", tags=["mem0_memory_async_example"])
|
||||
try:
|
||||
|
||||
async_memory = await AsyncMemory.from_config(local_config)
|
||||
|
||||
result = await async_memory.add(
|
||||
sample_messages, user_id=user_id, metadata={"category": "async_movie_preferences", "session": "async_demo"}
|
||||
)
|
||||
|
||||
async def add_preference(preference, index):
|
||||
"""Helper function to add a single preference asynchronously."""
|
||||
return await async_memory.add(
|
||||
preference, user_id=user_id, metadata={"type": "async_preference", "index": index}
|
||||
)
|
||||
|
||||
tasks = [add_preference(pref, i) for i, pref in enumerate(sample_preferences)]
|
||||
results = await asyncio.gather(*tasks)
|
||||
for i, result in enumerate(results):
|
||||
print(f"Added async preference {i+1}: {result}")
|
||||
|
||||
search_queries = [
|
||||
"What movies does the user like?",
|
||||
"What are the user's dietary restrictions?",
|
||||
"What does the user do for work?",
|
||||
]
|
||||
|
||||
async def search_memory(query):
|
||||
"""Helper function to perform async memory search."""
|
||||
return await async_memory.search(query, user_id=user_id), query
|
||||
|
||||
search_tasks = [search_memory(query) for query in search_queries]
|
||||
search_results = await asyncio.gather(*search_tasks)
|
||||
|
||||
for result, query in search_results:
|
||||
if result and "results" in result:
|
||||
for j, res in enumerate(result["results"]):
|
||||
print(f"Result {j+1}: {res.get('memory', 'N/A')}")
|
||||
else:
|
||||
print("No results found")
|
||||
|
||||
all_memories = await async_memory.get_all(user_id=user_id)
|
||||
if all_memories and "results" in all_memories:
|
||||
print(f"Total async memories: {len(all_memories['results'])}")
|
||||
|
||||
delete_all_result = await async_memory.delete_all(user_id=user_id)
|
||||
print(f"Delete all result: {delete_all_result}")
|
||||
|
||||
agentops.end_trace(end_state="success")
|
||||
|
||||
except Exception as e:
|
||||
agentops.end_trace(end_state="error")
|
||||
```
|
||||
|
||||
## Execute Demonstrations
|
||||
|
||||
Run both synchronous and asynchronous demonstrations to compare their execution patterns and performance. The async version demonstrates the benefits of concurrent execution for multiple memory operations.
|
||||
|
||||
|
||||
|
||||
```python
|
||||
# Execute both sync and async demonstrations
|
||||
demonstrate_sync_memory(local_config, sample_messages, sample_preferences, user_id)
|
||||
await demonstrate_async_memory(local_config, sample_messages, sample_preferences, user_id)
|
||||
```
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,436 @@
|
||||
---
|
||||
title: 'OpenAI'
|
||||
description: 'Multi-tool orchestration with OpenAI and AgentOps instrumentation'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/openai/multi_tool_orchestration.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/openai/multi_tool_orchestration.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
### Multi-Tool Orchestration with RAG approach using OpenAI's Responses API
|
||||
|
||||
|
||||
This cookbook guides you through building dynamic, multi-tool workflows using OpenAI's Responses API. It demonstrates how to implement a Retrieval-Augmented Generation (RAG) approach that intelligently routes user queries to the appropriate in-built or external tools. Whether your query calls for general knowledge or requires accessing specific internal context from a vector database (like Pinecone), this guide shows you how to integrate function calls, web searches in-built tool, and leverage document retrieval to generate accurate, context-aware responses.
|
||||
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops datasets openai pandas pinecone python-dotenv tqdm
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops datasets openai pandas pinecone python-dotenv tqdm
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops datasets openai pandas pinecone python-dotenv tqdm
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
```python
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import time
|
||||
from tqdm.auto import tqdm
|
||||
from pandas import DataFrame
|
||||
from datasets import load_dataset
|
||||
import random
|
||||
import string
|
||||
from openai import OpenAI
|
||||
import agentops
|
||||
from pinecone import Pinecone
|
||||
from pinecone import ServerlessSpec
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
load_dotenv()
|
||||
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")
|
||||
os.environ["PINECONE_API_KEY"] = os.getenv("PINECONE_API_KEY", "your_pinecone_api_key_here")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
agentops.init(auto_start_session=True)
|
||||
tracer = agentops.start_trace(
|
||||
trace_name="Multi-Tool Orchestration with RAG",
|
||||
tags=["multi-tool-orchestration-rag-demo", "openai-responses", "agentops-example"],
|
||||
)
|
||||
client = OpenAI()
|
||||
```
|
||||
|
||||
In this example we use a sample medical reasoning dataset from Hugging Face. We convert the dataset into a Pandas DataFrame and merge the “Question” and “Response” columns into a single string. This merged text is used for embedding and later stored as metadata.
|
||||
|
||||
|
||||
```python
|
||||
# Load the dataset (ensure you're logged in with huggingface-cli if needed)
|
||||
ds = load_dataset("FreedomIntelligence/medical-o1-reasoning-SFT", "en", split="train[:100]", trust_remote_code=True)
|
||||
ds_dataframe = DataFrame(ds)
|
||||
|
||||
# Merge the Question and Response columns into a single string.
|
||||
ds_dataframe["merged"] = ds_dataframe.apply(
|
||||
lambda row: f"Question: {row['Question']} Answer: {row['Response']}", axis=1
|
||||
)
|
||||
print("Example merged text:", ds_dataframe["merged"].iloc[0])
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
ds_dataframe
|
||||
```
|
||||
|
||||
### Create a Pinecone Index Based on the Dataset
|
||||
Use the dataset itself to determine the embedding dimensionality. For example, compute one embedding from the merged column and then create the index accordingly.
|
||||
|
||||
|
||||
```python
|
||||
MODEL = "text-embedding-3-small" # Replace with your production embedding model if needed
|
||||
# Compute an embedding for the first document to obtain the embedding dimension.
|
||||
sample_embedding_resp = client.embeddings.create(input=[ds_dataframe["merged"].iloc[0]], model=MODEL)
|
||||
embed_dim = len(sample_embedding_resp.data[0].embedding)
|
||||
print(f"Embedding dimension: {embed_dim}")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Initialize Pinecone using your API key.
|
||||
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
|
||||
|
||||
# Define the Pinecone serverless specification.
|
||||
AWS_REGION = "us-east-1"
|
||||
spec = ServerlessSpec(cloud="aws", region=AWS_REGION)
|
||||
|
||||
# Create a random index name with lower case alphanumeric characters and '-'
|
||||
index_name = "pinecone-index-" + "".join(random.choices(string.ascii_lowercase + string.digits, k=10))
|
||||
|
||||
# Create the index if it doesn't already exist.
|
||||
if index_name not in pc.list_indexes().names():
|
||||
pc.create_index(index_name, dimension=embed_dim, metric="dotproduct", spec=spec)
|
||||
|
||||
# Connect to the index.
|
||||
index = pc.Index(index_name)
|
||||
time.sleep(1)
|
||||
print("Index stats:", index.describe_index_stats())
|
||||
```
|
||||
|
||||
#### Upsert the Dataset into Pinecone index
|
||||
|
||||
Process the dataset in batches, generate embeddings for each merged text, prepare metadata (including separate Question and Answer fields), and upsert each batch into the index. You may also update metadata for specific entries if needed.
|
||||
|
||||
|
||||
```python
|
||||
batch_size = 32
|
||||
for i in tqdm(range(0, len(ds_dataframe["merged"]), batch_size), desc="Upserting to Pinecone"):
|
||||
i_end = min(i + batch_size, len(ds_dataframe["merged"]))
|
||||
lines_batch = ds_dataframe["merged"][i:i_end]
|
||||
ids_batch = [str(n) for n in range(i, i_end)]
|
||||
|
||||
# Create embeddings for the current batch.
|
||||
res = client.embeddings.create(input=[line for line in lines_batch], model=MODEL)
|
||||
embeds = [record.embedding for record in res.data]
|
||||
|
||||
# Prepare metadata by extracting original Question and Answer.
|
||||
meta = []
|
||||
for record in ds_dataframe.iloc[i:i_end].to_dict("records"):
|
||||
q_text = record["Question"]
|
||||
a_text = record["Response"]
|
||||
# Optionally update metadata for specific entries.
|
||||
meta.append({"Question": q_text, "Answer": a_text})
|
||||
|
||||
# Upsert the batch into Pinecone.
|
||||
vectors = list(zip(ids_batch, embeds, meta))
|
||||
index.upsert(vectors=vectors)
|
||||
```
|
||||
|
||||
### Query the Pinecone Index
|
||||
|
||||
Create a natural language query, compute its embedding, and perform a similarity search on the Pinecone index. The returned results include metadata that provides context for generating answers.
|
||||
|
||||
|
||||
```python
|
||||
def query_pinecone_index(client, index, model, query_text):
|
||||
# Generate an embedding for the query.
|
||||
query_embedding = client.embeddings.create(input=query_text, model=model).data[0].embedding
|
||||
|
||||
# Query the index and return top 5 matches.
|
||||
res = index.query(vector=[query_embedding], top_k=5, include_metadata=True)
|
||||
print("Query Results:")
|
||||
for match in res["matches"]:
|
||||
print(
|
||||
f"{match['score']:.2f}: {match['metadata'].get('Question', 'N/A')} - {match['metadata'].get('Answer', 'N/A')}"
|
||||
)
|
||||
return res
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Example usage with a different query from the train/test set
|
||||
query = (
|
||||
"A 45-year-old man with a history of alcohol use presents with symptoms including confusion, ataxia, and ophthalmoplegia. "
|
||||
"What is the most likely diagnosis and the recommended treatment?"
|
||||
)
|
||||
query_pinecone_index(client, index, MODEL, query)
|
||||
```
|
||||
|
||||
### Generate a Response Using the Retrieved Context
|
||||
|
||||
Select the best matching result from your query results and use the OpenAI Responses API to generate a final answer by combining the retrieved context with the original question.
|
||||
|
||||
|
||||
```python
|
||||
# Retrieve and concatenate top 3 match contexts.
|
||||
matches = index.query(
|
||||
vector=[client.embeddings.create(input=query, model=MODEL).data[0].embedding], top_k=3, include_metadata=True
|
||||
)["matches"]
|
||||
|
||||
context = "\n\n".join(
|
||||
f"Question: {m['metadata'].get('Question', '')}\nAnswer: {m['metadata'].get('Answer', '')}" for m in matches
|
||||
)
|
||||
# Use the context to generate a final answer.
|
||||
response = client.responses.create(
|
||||
model="gpt-4o",
|
||||
input=f"Provide the answer based on the context: {context} and the question: {query} as per the internal knowledge base",
|
||||
)
|
||||
print("\nFinal Answer:")
|
||||
print(response.output_text)
|
||||
```
|
||||
|
||||
### Orchestrate Multi-Tool Calls
|
||||
|
||||
Now, we'll define the built-in function available through the Responses API, including the ability to invoke the external Vector Store - Pinecone as an example.
|
||||
|
||||
*Web Search Preview Tool*: Enables the model to perform live web searches and preview the results. This is ideal for retrieving real-time or up-to-date information from the internet.
|
||||
|
||||
*Pinecone Search Tool*: Allows the model to query a vector database using semantic search. This is especially useful for retrieving relevant documents—such as medical literature or other domain-specific content—that have been stored in a vectorized format.
|
||||
|
||||
|
||||
```python
|
||||
# Tools definition: The list of tools includes:
|
||||
# - A web search preview tool.
|
||||
# - A Pinecone search tool for retrieving medical documents.
|
||||
|
||||
# Define available tools.
|
||||
tools = [
|
||||
{
|
||||
"type": "web_search_preview",
|
||||
"user_location": {"type": "approximate", "country": "US", "region": "California", "city": "SF"},
|
||||
"search_context_size": "medium",
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"name": "PineconeSearchDocuments",
|
||||
"description": "Search for relevant documents based on the medical question asked by the user that is stored within the vector database using a semantic query.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {"type": "string", "description": "The natural language query to search the vector database."},
|
||||
"top_k": {"type": "integer", "description": "Number of top results to return.", "default": 3},
|
||||
},
|
||||
"required": ["query"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
},
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Example queries that the model should route appropriately.
|
||||
queries = [
|
||||
{"query": "Who won the cricket world cup in 1983?"},
|
||||
{"query": "What is the most common cause of death in the United States according to the internet?"},
|
||||
{
|
||||
"query": (
|
||||
"A 7-year-old boy with sickle cell disease is experiencing knee and hip pain, "
|
||||
"has been admitted for pain crises in the past, and now walks with a limp. "
|
||||
"His exam shows a normal, cool hip with decreased range of motion and pain with ambulation. "
|
||||
"What is the most appropriate next step in management according to the internal knowledge base?"
|
||||
)
|
||||
},
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Process each query dynamically.
|
||||
for item in queries:
|
||||
input_messages = [{"role": "user", "content": item["query"]}]
|
||||
print("\n🌟--- Processing Query ---🌟")
|
||||
print(f"🔍 **User Query:** {item['query']}")
|
||||
|
||||
# Call the Responses API with tools enabled and allow parallel tool calls.
|
||||
response = client.responses.create(
|
||||
model="gpt-4o",
|
||||
input=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": "When prompted with a question, select the right tool to use based on the question.",
|
||||
},
|
||||
{"role": "user", "content": item["query"]},
|
||||
],
|
||||
tools=tools,
|
||||
parallel_tool_calls=True,
|
||||
)
|
||||
|
||||
print("\n✨ **Initial Response Output:**")
|
||||
print(response.output)
|
||||
|
||||
# Determine if a tool call is needed and process accordingly.
|
||||
if response.output:
|
||||
tool_call = response.output[0]
|
||||
if tool_call.type in ["web_search_preview", "function_call"]:
|
||||
tool_name = tool_call.name if tool_call.type == "function_call" else "web_search_preview"
|
||||
print(f"\n🔧 **Model triggered a tool call:** {tool_name}")
|
||||
|
||||
if tool_name == "PineconeSearchDocuments":
|
||||
print("🔍 **Invoking PineconeSearchDocuments tool...**")
|
||||
res = query_pinecone_index(client, index, MODEL, item["query"])
|
||||
if res["matches"]:
|
||||
best_match = res["matches"][0]["metadata"]
|
||||
result = f"**Question:** {best_match.get('Question', 'N/A')}\n**Answer:** {best_match.get('Answer', 'N/A')}"
|
||||
else:
|
||||
result = "**No matching documents found in the index.**"
|
||||
print("✅ **PineconeSearchDocuments tool invoked successfully.**")
|
||||
else:
|
||||
print("🔍 **Invoking simulated web search tool...**")
|
||||
result = "**Simulated web search result.**"
|
||||
print("✅ **Simulated web search tool invoked successfully.**")
|
||||
|
||||
# Append the tool call and its output back into the conversation.
|
||||
input_messages.append(tool_call)
|
||||
input_messages.append({"type": "function_call_output", "call_id": tool_call.call_id, "output": str(result)})
|
||||
|
||||
# Get the final answer incorporating the tool's result.
|
||||
final_response = client.responses.create(
|
||||
model="gpt-4o", input=input_messages, tools=tools, parallel_tool_calls=True
|
||||
)
|
||||
print("\n💡 **Final Answer:**")
|
||||
print(final_response.output_text)
|
||||
else:
|
||||
# If no tool call is triggered, print the response directly.
|
||||
print("💡 **Final Answer:**")
|
||||
print(response.output_text)
|
||||
```
|
||||
|
||||
As shown above, depending on the query, appropriate tool is invoked in order to determine the optimal response.
|
||||
|
||||
For instance, looking at the third example, when the model triggers the tool named "PineconeSearchDocuments", the code calls `query_pinecone_index` with the current query and then extracts the best match (or an appropriate context) as the result. For non health related inqueries or queries where explicit internet search is asked, the code calls the web_search_call function and for other queries, it may choose to not call any tool and rather provide a response based on the question under consideration.
|
||||
|
||||
Finally, the tool call and its output are appended to the conversation, and the final answer is generated by the Responses API.
|
||||
|
||||
### Multi-tool orchestration flow
|
||||
|
||||
Now let us try to modify the input query and the system instructions to the responses API in order to follow a tool calling sequence and generate the output.
|
||||
|
||||
|
||||
```python
|
||||
# Process one query as an example to understand the tool calls and function calls as part of the response output
|
||||
item = "What is the most common cause of death in the United States"
|
||||
|
||||
# Initialize input messages with the user's query.
|
||||
input_messages = [{"role": "user", "content": item}]
|
||||
print("\n🌟--- Processing Query ---🌟")
|
||||
print(f"🔍 **User Query:** {item}")
|
||||
|
||||
# Call the Responses API with tools enabled and allow parallel tool calls.
|
||||
print("\n🔧 **Calling Responses API with Tools Enabled**")
|
||||
print("\n🕵️♂️ **Step 1: Web Search Call**")
|
||||
print(" - Initiating web search to gather initial information.")
|
||||
print("\n📚 **Step 2: Pinecone Search Call**")
|
||||
print(" - Querying Pinecone to find relevant examples from the internal knowledge base.")
|
||||
|
||||
response = client.responses.create(
|
||||
model="gpt-4o",
|
||||
input=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": "Every time it's prompted with a question, first call the web search tool for results, then call `PineconeSearchDocuments` to find real examples in the internal knowledge base.",
|
||||
},
|
||||
{"role": "user", "content": item},
|
||||
],
|
||||
tools=tools,
|
||||
parallel_tool_calls=True,
|
||||
)
|
||||
|
||||
# Print the initial response output.
|
||||
print("input_messages", input_messages)
|
||||
|
||||
print("\n✨ **Initial Response Output:**")
|
||||
print(response.output)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Understand the tool calls and function calls as part of the response output
|
||||
|
||||
import pandas as pd
|
||||
|
||||
# Create a list to store the tool call and function call details
|
||||
tool_calls = []
|
||||
|
||||
# Iterate through the response output and collect the details
|
||||
for i in response.output:
|
||||
tool_calls.append(
|
||||
{
|
||||
"Type": i.type,
|
||||
"Call ID": i.call_id if hasattr(i, "call_id") else i.id if hasattr(i, "id") else "N/A",
|
||||
"Output": str(i.output) if hasattr(i, "output") else "N/A",
|
||||
"Name": i.name if hasattr(i, "name") else "N/A",
|
||||
}
|
||||
)
|
||||
|
||||
# Convert the list to a DataFrame for tabular display
|
||||
df_tool_calls = pd.DataFrame(tool_calls)
|
||||
|
||||
# Display the DataFrame
|
||||
df_tool_calls
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
tool_call_1 = response.output[0]
|
||||
print(tool_call_1)
|
||||
print(tool_call_1.id)
|
||||
|
||||
tool_call_2 = response.output[2]
|
||||
print(tool_call_2)
|
||||
print(tool_call_2.call_id)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# append the tool call and its output back into the conversation.
|
||||
input_messages.append(response.output[2])
|
||||
input_messages.append({"type": "function_call_output", "call_id": tool_call_2.call_id, "output": str(result)})
|
||||
print(input_messages)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Get the final answer incorporating the tool's result.
|
||||
print("\n🔧 **Calling Responses API for Final Answer**")
|
||||
|
||||
response_2 = client.responses.create(
|
||||
model="gpt-4o",
|
||||
input=input_messages,
|
||||
)
|
||||
print(response_2)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# print the final answer
|
||||
print(response_2.output_text)
|
||||
agentops.end_trace(tracer, end_state="Success")
|
||||
```
|
||||
|
||||
|
||||
Here, we have seen how to utilize OpenAI's Responses API to implement a Retrieval-Augmented Generation (RAG) approach with multi-tool calling capabilities. It showcases an example where the model selects the appropriate tool based on the input query: general questions may be handled by built-in tools such as web-search, while specific medical inquiries related to internal knowledge are addressed by retrieving context from a vector database (such as Pinecone) via function calls. Additonally, we have showcased how multiple tool calls can be sequentially combined to generate a final response based on our instructions provided to responses API. Happy coding!
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,250 @@
|
||||
---
|
||||
title: 'OpenAI Agents'
|
||||
description: 'Airline Customer Service Agent'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/openai_agents/customer_service_agent.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/openai_agents/customer_service_agent.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# Airline Customer Service Agent
|
||||
|
||||
This is a simple chatbot designed to assist airline customers with common queries. Here the agents are also used as tools to help the bot answer questions more effectively.
|
||||
|
||||
Using AgentOps we can track the flow of the conversation and the agents used. This is useful for debugging and understanding how the bot is performing.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before running this notebook, you'll need:
|
||||
|
||||
1. **AgentOps Account**: Create a free account at [app.agentops.ai](https://app.agentops.ai)
|
||||
2. **AgentOps API Key**: Obtain your API key from your AgentOps dashboard
|
||||
3. **OpenAI API Key**: Get your API key from [platform.openai.com](https://platform.openai.com)
|
||||
|
||||
Make sure to set these as environment variables or create a `.env` file in your project root with:
|
||||
|
||||
```
|
||||
AGENTOPS_API_KEY=your_agentops_api_key_here
|
||||
OPENAI_API_KEY=your_openai_api_key_here
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install -q agentops openai-agents pydotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add -q agentops openai-agents pydotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install -q agentops openai-agents pydotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
```python
|
||||
# Set the API keys for your AgentOps and OpenAI accounts.
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
from __future__ import annotations as _annotations # noqa: F404
|
||||
|
||||
import random
|
||||
import uuid
|
||||
|
||||
from pydantic import BaseModel
|
||||
import agentops
|
||||
|
||||
from agents import ( # noqa: E402
|
||||
Agent,
|
||||
HandoffOutputItem,
|
||||
ItemHelpers,
|
||||
MessageOutputItem,
|
||||
RunContextWrapper,
|
||||
Runner,
|
||||
ToolCallItem,
|
||||
ToolCallOutputItem,
|
||||
TResponseInputItem,
|
||||
function_tool,
|
||||
handoff,
|
||||
trace,
|
||||
)
|
||||
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX # noqa: E402
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
agentops.init(tags=["customer-service-agent", "openai-agents", "agentops-example"])
|
||||
tracer = agentops.start_trace(trace_name="Customer Service Agent")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Context model for the airline agent
|
||||
class AirlineAgentContext(BaseModel):
|
||||
passenger_name: str | None = None
|
||||
confirmation_number: str | None = None
|
||||
seat_number: str | None = None
|
||||
flight_number: str | None = None
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Tools for the airline agent
|
||||
@function_tool(name_override="faq_lookup_tool", description_override="Lookup frequently asked questions.")
|
||||
async def faq_lookup_tool(question: str) -> str:
|
||||
if "bag" in question or "baggage" in question:
|
||||
return (
|
||||
"You are allowed to bring one bag on the plane. "
|
||||
"It must be under 50 pounds and 22 inches x 14 inches x 9 inches."
|
||||
)
|
||||
elif "seats" in question or "plane" in question:
|
||||
return (
|
||||
"There are 120 seats on the plane. "
|
||||
"There are 22 business class seats and 98 economy seats. "
|
||||
"Exit rows are rows 4 and 16. "
|
||||
"Rows 5-8 are Economy Plus, with extra legroom. "
|
||||
)
|
||||
elif "wifi" in question:
|
||||
return "We have free wifi on the plane, join Airline-Wifi"
|
||||
return "I'm sorry, I don't know the answer to that question."
|
||||
|
||||
|
||||
@function_tool
|
||||
async def update_seat(context: RunContextWrapper[AirlineAgentContext], confirmation_number: str, new_seat: str) -> str:
|
||||
"""
|
||||
Update the seat for a given confirmation number.
|
||||
|
||||
Args:
|
||||
confirmation_number: The confirmation number for the flight.
|
||||
new_seat: The new seat to update to.
|
||||
"""
|
||||
# Update the context based on the customer's input
|
||||
context.context.confirmation_number = confirmation_number
|
||||
context.context.seat_number = new_seat
|
||||
# Ensure that the flight number has been set by the incoming handoff
|
||||
assert context.context.flight_number is not None, "Flight number is required"
|
||||
return f"Updated seat to {new_seat} for confirmation number {confirmation_number}"
|
||||
|
||||
|
||||
### HOOKS
|
||||
|
||||
|
||||
async def on_seat_booking_handoff(context: RunContextWrapper[AirlineAgentContext]) -> None:
|
||||
flight_number = f"FLT-{random.randint(100, 999)}"
|
||||
context.context.flight_number = flight_number
|
||||
|
||||
|
||||
### AGENTS
|
||||
|
||||
faq_agent = Agent[AirlineAgentContext](
|
||||
name="FAQ Agent",
|
||||
handoff_description="A helpful agent that can answer questions about the airline.",
|
||||
instructions=f"""{RECOMMENDED_PROMPT_PREFIX}
|
||||
You are an FAQ agent. If you are speaking to a customer, you probably were transferred to from the triage agent.
|
||||
Use the following routine to support the customer.
|
||||
# Routine
|
||||
1. Identify the last question asked by the customer.
|
||||
2. Use the faq lookup tool to answer the question. Do not rely on your own knowledge.
|
||||
3. If you cannot answer the question, transfer back to the triage agent.""",
|
||||
tools=[faq_lookup_tool],
|
||||
)
|
||||
|
||||
seat_booking_agent = Agent[AirlineAgentContext](
|
||||
name="Seat Booking Agent",
|
||||
handoff_description="A helpful agent that can update a seat on a flight.",
|
||||
instructions=f"""{RECOMMENDED_PROMPT_PREFIX}
|
||||
You are a seat booking agent. If you are speaking to a customer, you probably were transferred to from the triage agent.
|
||||
Use the following routine to support the customer.
|
||||
# Routine
|
||||
1. Ask for their confirmation number.
|
||||
2. Ask the customer what their desired seat number is.
|
||||
3. Use the update seat tool to update the seat on the flight.
|
||||
If the customer asks a question that is not related to the routine, transfer back to the triage agent. """,
|
||||
tools=[update_seat],
|
||||
)
|
||||
|
||||
triage_agent = Agent[AirlineAgentContext](
|
||||
name="Triage Agent",
|
||||
handoff_description="A triage agent that can delegate a customer's request to the appropriate agent.",
|
||||
instructions=(
|
||||
f"{RECOMMENDED_PROMPT_PREFIX} "
|
||||
"You are a helpful triaging agent. You can use your tools to delegate questions to other appropriate agents."
|
||||
),
|
||||
handoffs=[
|
||||
faq_agent,
|
||||
handoff(agent=seat_booking_agent, on_handoff=on_seat_booking_handoff),
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
faq_agent.handoffs.append(triage_agent)
|
||||
seat_booking_agent.handoffs.append(triage_agent)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
async def main():
|
||||
current_agent: Agent[AirlineAgentContext] = triage_agent
|
||||
input_items: list[TResponseInputItem] = []
|
||||
context = AirlineAgentContext()
|
||||
|
||||
# Normally, each input from the user would be an API request to your app, and you can wrap the request in a trace()
|
||||
# Here, we'll just use a random UUID for the conversation ID
|
||||
conversation_id = uuid.uuid4().hex[:16]
|
||||
|
||||
while True:
|
||||
user_input = input("Enter your message: ")
|
||||
with trace("Customer service", group_id=conversation_id):
|
||||
input_items.append({"content": user_input, "role": "user"})
|
||||
result = await Runner.run(current_agent, input_items, context=context)
|
||||
|
||||
for new_item in result.new_items:
|
||||
agent_name = new_item.agent.name
|
||||
if isinstance(new_item, MessageOutputItem):
|
||||
print(f"{agent_name}: {ItemHelpers.text_message_output(new_item)}")
|
||||
elif isinstance(new_item, HandoffOutputItem):
|
||||
print(f"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}")
|
||||
elif isinstance(new_item, ToolCallItem):
|
||||
print(f"{agent_name}: Calling a tool")
|
||||
elif isinstance(new_item, ToolCallOutputItem):
|
||||
print(f"{agent_name}: Tool call output: {new_item.output}")
|
||||
else:
|
||||
print(f"{agent_name}: Skipping item: {new_item.__class__.__name__}")
|
||||
input_items = result.to_input_list()
|
||||
current_agent = result.last_agent
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
await main()
|
||||
agentops.end_trace(tracer, end_state="Success")
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
|
||||
**AgentOps makes observability effortless** - simply import the library and all your interactions are automatically tracked, visualized, and analyzed. This enables you to:
|
||||
|
||||
- Monitor tool performance across different use cases
|
||||
- Optimize costs by understanding tool usage patterns
|
||||
- Debug tool integration issues quickly
|
||||
- Scale your AI applications with confidence in tool reliability
|
||||
|
||||
Visit [app.agentops.ai](https://app.agentops.ai) to explore your tool usage sessions and gain deeper insights into your AI application's tool interactions.
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,168 @@
|
||||
---
|
||||
title: 'Smolagents'
|
||||
description: 'Orchestrate a Multi-Agent System'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/smolagents/multi_smolagents_system.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/smolagents/multi_smolagents_system.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# Orchestrate a Multi-Agent System
|
||||
|
||||
In this notebook, we will make a multi-agent web browser: an agentic system with several agents collaborating to solve problems using the web!
|
||||
|
||||
It will be a simple hierarchy, using a `ManagedAgent` object to wrap the managed web search agent:
|
||||
|
||||
```
|
||||
+----------------+
|
||||
| Manager agent |
|
||||
+----------------+
|
||||
|
|
||||
_________|______________
|
||||
| |
|
||||
Code interpreter +--------------------------------+
|
||||
tool | Managed agent |
|
||||
| +------------------+ |
|
||||
| | Web Search agent | |
|
||||
| +------------------+ |
|
||||
| | | |
|
||||
| Web Search tool | |
|
||||
| Visit webpage tool |
|
||||
+--------------------------------+
|
||||
```
|
||||
Let’s set up this system.
|
||||
|
||||
Run the line below to install the required dependencies:
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops duckduckgo-search markdownify smolagents
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops duckduckgo-search markdownify smolagents
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops duckduckgo-search markdownify smolagents
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
🖇️ Now we initialize the AgentOps client and load the environment variables to use the API keys.
|
||||
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
import re
|
||||
import requests
|
||||
from markdownify import markdownify
|
||||
from requests.exceptions import RequestException
|
||||
from smolagents import LiteLLMModel, tool, CodeAgent, ToolCallingAgent, DuckDuckGoSearchTool
|
||||
|
||||
load_dotenv()
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")
|
||||
```
|
||||
|
||||
⚡️ Our agent will be powered by `openai/gpt-4o-mini` using the `LiteLLMModel` class.
|
||||
|
||||
|
||||
```python
|
||||
from smolagents import LiteLLMModel, tool ,CodeAgent, ToolCallingAgent, DuckDuckGoSearchTool
|
||||
agentops.init(auto_start_session=False)
|
||||
tracer = agentops.start_trace(
|
||||
trace_name="Orchestrate a Multi-Agent System", tags=["smolagents", "example", "multi-agent", "agentops-example"]
|
||||
)
|
||||
model = LiteLLMModel("openai/gpt-4o-mini")
|
||||
```
|
||||
|
||||
## Create a Web Search Tool
|
||||
|
||||
For web browsing, we can already use our pre-existing `DuckDuckGoSearchTool`. However, we will also create a `VisitWebpageTool` from scratch using `markdownify`. Here’s how:
|
||||
|
||||
|
||||
```python
|
||||
@tool
|
||||
def visit_webpage(url: str) -> str:
|
||||
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
||||
|
||||
Args:
|
||||
url: The URL of the webpage to visit.
|
||||
|
||||
Returns:
|
||||
The content of the webpage converted to Markdown, or an error message if the request fails.
|
||||
"""
|
||||
try:
|
||||
# Send a GET request to the URL
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # Raise an exception for bad status codes
|
||||
|
||||
# Convert the HTML content to Markdown
|
||||
markdown_content = markdownify(response.text).strip()
|
||||
|
||||
# Remove multiple line breaks
|
||||
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
||||
|
||||
return markdown_content
|
||||
|
||||
except RequestException as e:
|
||||
return f"Error fetching the webpage: {str(e)}"
|
||||
except Exception as e:
|
||||
return f"An unexpected error occurred: {str(e)}"
|
||||
```
|
||||
|
||||
Let’s test our tool:
|
||||
|
||||
|
||||
```python
|
||||
print(visit_webpage("https://en.wikipedia.org/wiki/Hugging_Face")[:500])
|
||||
```
|
||||
|
||||
## Build Our Multi-Agent System
|
||||
|
||||
We will now use the tools `search` and `visit_webpage` to create the web agent.
|
||||
|
||||
|
||||
```python
|
||||
web_agent = ToolCallingAgent(
|
||||
tools=[DuckDuckGoSearchTool(), visit_webpage],
|
||||
model=model,
|
||||
name="search",
|
||||
description="Runs web searches for you. Give it your query as an argument.",
|
||||
)
|
||||
|
||||
manager_agent = CodeAgent(
|
||||
tools=[],
|
||||
model=model,
|
||||
managed_agents=[web_agent],
|
||||
additional_authorized_imports=["time", "numpy", "pandas"],
|
||||
)
|
||||
```
|
||||
|
||||
Let’s run our system with the following query:
|
||||
|
||||
|
||||
```python
|
||||
answer = manager_agent.run(
|
||||
"If LLM trainings continue to scale up at the current rhythm until 2030, what would be the electric power in GW required to power the biggest training runs by 2030? What does that correspond to, compared to some countries? Please provide a source for any number used."
|
||||
)
|
||||
|
||||
print(answer)
|
||||
```
|
||||
|
||||
Awesome! We've successfully run a multi-agent system. Let's end the agentops session with a "Success" state. You can also end the session with a "Failure" or "Indeterminate" state, which is set as default.
|
||||
|
||||
|
||||
```python
|
||||
agentops.end_trace(tracer, end_state="Success")
|
||||
```
|
||||
|
||||
You can view the session in the [AgentOps dashboard](https://app.agentops.ai/sessions) by clicking the link provided after ending the session.
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,137 @@
|
||||
---
|
||||
title: 'IBM Watsonx.ai Example'
|
||||
description: 'Using IBM Watsonx.ai for text generation and chat with AgentOps'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/watsonx/watsonx-text-chat.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/watsonx/watsonx-text-chat.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# IBM Watsonx.ai Text Generation and Chat with AgentOps
|
||||
|
||||
This notebook demonstrates how to use IBM Watsonx.ai for basic text generation and chat completion tasks with AgentOps instrumentation.
|
||||
|
||||
## Installation
|
||||
Install the required packages:
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops ibm-watsonx-ai python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops ibm-watsonx-ai python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops ibm-watsonx-ai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setup
|
||||
|
||||
First, let's import the necessary libraries and initialize AgentOps:
|
||||
```python
|
||||
import agentops
|
||||
from ibm_watsonx_ai import Credentials
|
||||
from ibm_watsonx_ai.foundation_models import ModelInference
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_agentops_api_key_here")
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(tags=["watsonx-text-chat", "agentops-example"])
|
||||
```
|
||||
|
||||
## Initialize IBM Watsonx.ai Credentials
|
||||
|
||||
To use IBM Watsonx.ai, you need to set up your credentials and project ID.
|
||||
```python
|
||||
# Initialize credentials - replace with your own API key
|
||||
# Best practice: Store API keys in environment variables
|
||||
# Ensure WATSONX_API_KEY, WATSONX_URL, and WATSONX_PROJECT_ID are set in your .env file or environment
|
||||
os.environ["WATSONX_API_KEY"] = os.getenv("WATSONX_API_KEY", "your_watsonx_api_key_here")
|
||||
os.environ["WATSONX_URL"] = os.getenv("WATSONX_URL", "https://eu-de.ml.cloud.ibm.com") # Example URL, ensure it's correct for your region
|
||||
os.environ["WATSONX_PROJECT_ID"] = os.getenv("WATSONX_PROJECT_ID", "your-project-id-here")
|
||||
|
||||
|
||||
credentials = Credentials(
|
||||
url=os.environ["WATSONX_URL"],
|
||||
api_key=os.environ["WATSONX_API_KEY"],
|
||||
)
|
||||
|
||||
# Project ID for your IBM Watsonx project
|
||||
project_id = os.environ["WATSONX_PROJECT_ID"]
|
||||
```
|
||||
|
||||
## Text Generation
|
||||
|
||||
Let's use IBM Watsonx.ai to generate text based on a prompt:
|
||||
```python
|
||||
# Initialize text generation model
|
||||
gen_model = ModelInference(model_id="google/flan-ul2", credentials=credentials, project_id=project_id)
|
||||
|
||||
# Generate text with a prompt
|
||||
prompt = "Write a short poem about artificial intelligence:"
|
||||
response = gen_model.generate_text(prompt)
|
||||
print(f"Generated Text:\n{response}")
|
||||
```
|
||||
|
||||
## Chat Completion
|
||||
|
||||
Now, let's use a different model for chat completion:
|
||||
```python
|
||||
# Initialize chat model
|
||||
chat_model = ModelInference(
|
||||
model_id="meta-llama/llama-3-8b-instruct", # Using the model ID from the MDX as it might be more current/available
|
||||
credentials=credentials,
|
||||
project_id=project_id
|
||||
)
|
||||
|
||||
# Format messages for chat
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a helpful AI assistant."},
|
||||
{"role": "user", "content": "What are the three laws of robotics?"},
|
||||
]
|
||||
|
||||
# Get chat response
|
||||
chat_response = chat_model.chat(messages)
|
||||
# Accessing response based on typical ibm-watsonx-ai SDK structure
|
||||
print(f"Chat Response:\n{chat_response['results'][0]['generated_text']}")
|
||||
```
|
||||
|
||||
## Another Chat Example
|
||||
|
||||
Let's try a different type of query:
|
||||
```python
|
||||
# New chat messages
|
||||
messages = [
|
||||
{"role": "system", "content": "You are an expert in machine learning."},
|
||||
{"role": "user", "content": "Explain the difference between supervised and unsupervised learning in simple terms."},
|
||||
]
|
||||
|
||||
# Get chat response
|
||||
chat_response = chat_model.chat(messages)
|
||||
print(f"Chat Response:\n{chat_response['results'][0]['generated_text']}")
|
||||
```
|
||||
|
||||
## Clean Up
|
||||
|
||||
Finally, let's close the persistent connection with the models if they were established and end the AgentOps session.
|
||||
```python
|
||||
# Close connections if persistent connections were used.
|
||||
# This is good practice if the SDK version/usage implies persistent connections.
|
||||
try:
|
||||
gen_model.close_persistent_connection()
|
||||
chat_model.close_persistent_connection()
|
||||
except AttributeError:
|
||||
# Handle cases where this method might not exist (e.g. newer SDK versions or stateless calls)
|
||||
print("Note: close_persistent_connection not available or needed for one or more models.")
|
||||
pass
|
||||
|
||||
agentops.end_session("Success") # Manually end session
|
||||
```
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,142 @@
|
||||
---
|
||||
title: 'xAI'
|
||||
description: 'XAI Example'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/xai/grok_examples.ipynb */}
|
||||
|
||||
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/xai/grok_examples.ipynb'} target={'_blank'}>Github</a>_
|
||||
|
||||
# XAI Example
|
||||
This notebook demonstrates how to use XAI with AgentOps via the OpenAI python client.
|
||||
|
||||
We are going to use the latest Grok model from XAI to create a transliteration chatbot that can understand the major languages of the world and translate them to a user's native language! We will use AgentOps to track the chatbot's performance.
|
||||
|
||||
First let's install the required packages
|
||||
|
||||
|
||||
|
||||
## Installation
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install -U agentops openai
|
||||
```
|
||||
```bash poetry
|
||||
poetry add -U agentops openai
|
||||
```
|
||||
```bash uv
|
||||
uv pip install -U agentops openai
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then import them
|
||||
|
||||
|
||||
```python
|
||||
from openai import OpenAI
|
||||
import agentops
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
```
|
||||
|
||||
Next, we'll grab our API keys. You can use dotenv like below or however else you like to load environment variables
|
||||
|
||||
|
||||
```python
|
||||
load_dotenv()
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")
|
||||
```
|
||||
|
||||
Next we initialize the AgentOps client.
|
||||
|
||||
|
||||
```python
|
||||
agentops.init(auto_start_session=False)
|
||||
tracer = agentops.start_trace(trace_name="XAI Example", tags=["xai-example", "grok", "agentops-example"])
|
||||
```
|
||||
|
||||
And we are all set! Note the session URL above. We will use it to track the chatbot.
|
||||
|
||||
Let's initialize the OpenAI client with the XAI API key and base url.
|
||||
|
||||
|
||||
```python
|
||||
client = OpenAI(
|
||||
base_url="https://api.x.ai/v1",
|
||||
api_key=os.getenv("XAI_API_KEY", "your_xai_api_key_here"),
|
||||
)
|
||||
```
|
||||
|
||||
Now we will set the system and instruction prompts for the chatbot. We will set the native language to Spanish and the user prompt to transliterate an excerpt from Haruki Murakami's "Kafka On The Shore".
|
||||
|
||||
|
||||
```python
|
||||
SYSTEM_PROMPT = """
|
||||
You are a highly intelligent, multilingual assistant designed to understand user prompts in English and respond in the user's specified native language.
|
||||
Your key responsibilities include:
|
||||
1. Translating and generating meaningful, contextually appropriate responses in the user's native language.
|
||||
2. Ensuring the output is accurate, coherent, and in Unicode format for proper display in the specified language.
|
||||
3. Adhering to the nuances of the specified language's grammar, tone, and cultural context.
|
||||
|
||||
When asked to respond in a language, generate the response entirely in that language without using English unless explicitly requested.
|
||||
|
||||
If the specified language is unfamiliar or ambiguous, politely ask for clarification in English.
|
||||
"""
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
native_language = "Spanish"
|
||||
|
||||
USER_PROMPT = """
|
||||
Sometimes fate is like a small sandstorm that keeps changing directions. You change direction but the sandstorm chases you.
|
||||
You turn again, but the storm adjusts. Over and over you play this out, like some ominous dance with death just before dawn. Why?
|
||||
Because this storm isn’t something that blew in from far away, something that has nothing to do with you. This storm is you.
|
||||
Something inside of you. So all you can do is give in to it, step right inside the storm, closing your eyes and plugging up your ears so the sand doesn’t get in, and walk through it, step by step.
|
||||
There’s no sun there, no moon, no direction, no sense of time. Just fine white sand swirling up into the sky like pulverized bones.
|
||||
That’s the kind of sandstorm you need to imagine.
|
||||
|
||||
And you really will have to make it through that violent, metaphysical, symbolic storm.
|
||||
No matter how metaphysical or symbolic it might be, make no mistake about it: it will cut through flesh like a thousand razor blades. People will bleed there, and you will bleed too.
|
||||
Hot, red blood. You’ll catch that blood in your hands, your own blood and the blood of others.
|
||||
|
||||
And once the storm is over you won’t remember how you made it through, how you managed to survive. You won’t even be sure, in fact, whether the storm is really over.
|
||||
But one thing is certain. When you come out of the storm you won’t be the same person who walked in. That’s what this storm’s all about.
|
||||
"""
|
||||
|
||||
INSTRUCTION_PROMPT = f"""
|
||||
You are a multilingual chatbot. Take the user's prompt: "{USER_PROMPT}" and respond naturally in {native_language}.
|
||||
Ensure that the response is in Unicode characters appropriate for {native_language}.
|
||||
"""
|
||||
```
|
||||
|
||||
Now we will use the OpenAI client to generate the response by passing in the system and instruction prompts.
|
||||
|
||||
|
||||
```python
|
||||
response = client.chat.completions.create(
|
||||
model="grok-3-mini",
|
||||
messages=[{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": INSTRUCTION_PROMPT}],
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
print(f"Original Prompt:\n{USER_PROMPT}")
|
||||
generated_response = response.choices[0].message.content
|
||||
print(f"Response in {native_language}:\n{generated_response}")
|
||||
```
|
||||
|
||||
Awesome! We can now transliterate from English to any language! And all of this can be tracked with AgentOps by going to the session url above.
|
||||
|
||||
|
||||
```python
|
||||
agentops.end_trace(tracer, end_state="Success")
|
||||
```
|
||||
|
||||
We end the session with a success state and a success reason. This is useful if you want to track the success or failure of the chatbot. In that case you can set the end state to failure and provide a reason. By default the session will have an indeterminate end state.
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: 'Xpander'
|
||||
description: 'Xpander coding agent example with AgentOps'
|
||||
---
|
||||
{/* SOURCE_FILE: examples/xpander/coding_agent.py */}
|
||||
|
||||
_View Python Example on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/xpander/coding_agent.py'} target={'_blank'}>Github</a>_
|
||||
|
||||
This example demonstrates a complete Xpander coding agent implementation with AgentOps instrumentation using callback handlers. The example shows how to build a single-file agent that combines the MyAgent class and XpanderEventListener for comprehensive monitoring.
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Single-file implementation** combining agent logic and event handling
|
||||
- **Callback-based instrumentation** using XpanderEventListener
|
||||
- **Async tool execution** with proper error handling and logging
|
||||
- **AgentOps integration** with custom trace names and tags
|
||||
|
||||
## Running the Example
|
||||
|
||||
1. Set up your environment variables in a `.env` file or export them:
|
||||
```bash
|
||||
AGENTOPS_API_KEY=your_agentops_api_key
|
||||
XPANDER_API_KEY=your_xpander_api_key
|
||||
XPANDER_AGENT_ID=your_agent_id
|
||||
OPENAI_API_KEY=your_openai_api_key
|
||||
```
|
||||
|
||||
2. Run the agent:
|
||||
```bash
|
||||
python examples/xpander/coding_agent.py
|
||||
```
|
||||
|
||||
The agent will start an interactive session where you can ask coding questions and see the results tracked in your AgentOps dashboard.
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 6.2 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 84 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 215 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 45 KiB |
@@ -0,0 +1,135 @@
|
||||
---
|
||||
title: AG2
|
||||
description: "Track and analyze your AG2 agents with AgentOps"
|
||||
---
|
||||
|
||||
[AG2](https://ag2.ai/) (formerly AutoGen) is a framework for building multi-agent conversational AI systems. AgentOps provides seamless, automatic instrumentation for AG2 — just call `agentops.init()` and all agent interactions are tracked.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops pyautogen
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops pyautogen
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops pyautogen
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using AG2 with AgentOps, you need to set up your API keys. You can obtain:
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
- **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 OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
OPENAI_API_KEY="your_openai_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["OPENAI_API_KEY"] = os.getenv("OPENAI_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 AG2 agent interactions:
|
||||
|
||||
<CodeGroup>
|
||||
```python Single Agent Conversation
|
||||
import agentops
|
||||
import autogen
|
||||
import os
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Configure your AG2 agents
|
||||
config_list = [
|
||||
{
|
||||
"model": "gpt-4",
|
||||
"api_key": os.getenv("OPENAI_API_KEY"),
|
||||
}
|
||||
]
|
||||
|
||||
llm_config = {
|
||||
"config_list": config_list,
|
||||
"timeout": 60,
|
||||
}
|
||||
|
||||
# Create a single agent
|
||||
assistant = autogen.AssistantAgent(
|
||||
name="assistant",
|
||||
llm_config=llm_config,
|
||||
system_message="You are a helpful AI assistant."
|
||||
)
|
||||
|
||||
user_proxy = autogen.UserProxyAgent(
|
||||
name="user_proxy",
|
||||
human_input_mode="TERMINATE",
|
||||
max_consecutive_auto_reply=10,
|
||||
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
|
||||
code_execution_config={"last_n_messages": 3, "work_dir": "coding"},
|
||||
)
|
||||
|
||||
# Initiate a conversation
|
||||
user_proxy.initiate_chat(
|
||||
assistant,
|
||||
message="How can I implement a basic web scraper in Python?"
|
||||
)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="AG2 Async Agent Chat" icon="notebook" href="/v2/examples/ag2">
|
||||
AG2 Async Agent Chat with Automated Responses
|
||||
</Card>
|
||||
<Card title="Async Human Input" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/ag2/ag2_async_agent.ipynb" newTab={true}>
|
||||
Demonstrates asynchronous human input with AG2 agents.
|
||||
</Card>
|
||||
<Card title="Wikipedia Search Tool" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/ag2/tools_wikipedia_search.ipynb" newTab={true}>
|
||||
Example of AG2 agents using a Wikipedia search tool.
|
||||
</Card>
|
||||
<Card title="Multi-Agent Group Chat" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/ag2/groupchat.py" newTab={true}>
|
||||
Orchestrate a team of specialized agents (researcher, coder, critic) with full AgentOps tracing.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
## Resources
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="AG2 - AgentOps Ecosystem Docs" icon="book" href="https://docs.ag2.ai/latest/docs/ecosystem/agentops/" newTab={true}>
|
||||
Official AG2 documentation on integrating with AgentOps.
|
||||
</Card>
|
||||
<Card title="AG2 OpenTelemetry Tracing" icon="blog" href="https://docs.ag2.ai/latest/docs/blog/2026/02/08/AG2-OpenTelemetry-Tracing/" newTab={true}>
|
||||
Full observability for multi-agent systems with AG2's built-in tracing.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,167 @@
|
||||
---
|
||||
title: Agno
|
||||
description: "Track your Agno agents, teams, and workflows with AgentOps"
|
||||
---
|
||||
|
||||
## Video Tutorial
|
||||
|
||||
<iframe
|
||||
width="560"
|
||||
height="315"
|
||||
src="https://www.youtube.com/embed/M4e1Ybkn_K0"
|
||||
title="AgentOps + Agno Integration Tutorial"
|
||||
frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
||||
allowfullscreen
|
||||
></iframe>
|
||||
|
||||
[Agno](https://docs.agno.com) is a modern AI agent framework for building intelligent agents, teams, and workflows. AgentOps provides automatic instrumentation to track all Agno operations including agent interactions, team coordination, tool usage, and workflow execution.
|
||||
|
||||
## Installation
|
||||
|
||||
Install AgentOps and Agno:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops agno
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops agno
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops agno
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
You'll need API keys for AgentOps and your chosen LLM provider:
|
||||
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys) (if using OpenAI)
|
||||
- **ANTHROPIC_API_KEY**: From [Anthropic Console](https://console.anthropic.com/) (if using Claude)
|
||||
|
||||
Set these as environment variables or in a `.env` file.
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export ANTHROPIC_API_KEY="your_anthropic_api_key_here" # Optional
|
||||
```
|
||||
```txt Set in .env file
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
ANTHROPIC_API_KEY="your_anthropic_api_key_here" # Optional
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Quick Start
|
||||
|
||||
```python
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
|
||||
from agno.agent import Agent
|
||||
from agno.team import Team
|
||||
from agno.models.openai import OpenAIChat
|
||||
|
||||
# Initialize AgentOps
|
||||
import agentops
|
||||
agentops.init(api_key=os.getenv("AGENTOPS_API_KEY"))
|
||||
|
||||
# Create and run an agent
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
role="Helpful AI assistant",
|
||||
model=OpenAIChat(id="gpt-4o-mini")
|
||||
)
|
||||
|
||||
response = agent.run("What are the key benefits of AI agents?")
|
||||
print(response.content)
|
||||
```
|
||||
|
||||
## AgentOps Integration
|
||||
|
||||
### Basic Agent Tracking
|
||||
|
||||
AgentOps automatically instruments Agno agents and teams:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agno.agent import Agent
|
||||
from agno.team import Team
|
||||
from agno.models.openai import OpenAIChat
|
||||
|
||||
# Initialize AgentOps - this enables automatic tracking
|
||||
agentops.init(api_key=os.getenv("AGENTOPS_API_KEY"))
|
||||
|
||||
# Create agents - automatically tracked by AgentOps
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
role="Helpful AI assistant",
|
||||
model=OpenAIChat(id="gpt-4o-mini")
|
||||
)
|
||||
|
||||
# Create teams - coordination automatically tracked
|
||||
team = Team(
|
||||
name="Research Team",
|
||||
mode="coordinate",
|
||||
members=[agent]
|
||||
)
|
||||
|
||||
# All operations are automatically logged to AgentOps
|
||||
response = team.run("Analyze the current AI market trends")
|
||||
print(response.content)
|
||||
```
|
||||
|
||||
## What Gets Tracked
|
||||
|
||||
AgentOps automatically captures:
|
||||
|
||||
- **Agent Interactions**: All agent inputs, outputs, and configurations
|
||||
- **Team Coordination**: Multi-agent collaboration patterns and results
|
||||
- **Tool Executions**: Function calls, parameters, and return values
|
||||
- **Workflow Steps**: Session states, caching, and performance metrics
|
||||
- **Token Usage**: Costs and resource consumption across all operations
|
||||
- **Timing Metrics**: Response times and concurrent operation performance
|
||||
- **Error Tracking**: Failures and debugging information
|
||||
|
||||
|
||||
## Dashboard and Monitoring
|
||||
|
||||
Once your Agno agents are running with AgentOps, you can monitor them in the [AgentOps Dashboard](https://app.agentops.ai/):
|
||||
|
||||
- **Real-time Monitoring**: Live agent status and performance
|
||||
- **Execution Traces**: Detailed logs of agent interactions
|
||||
- **Performance Analytics**: Token usage, costs, and timing metrics
|
||||
- **Team Collaboration**: Visual representation of multi-agent workflows
|
||||
- **Error Tracking**: Comprehensive error logs and debugging information
|
||||
|
||||
## Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Basic Agents and Teams" icon="users" href="/v2/examples/agno">
|
||||
Learn the fundamentals of creating AI agents and organizing them into collaborative teams
|
||||
</Card>
|
||||
|
||||
<Card title="Async Operations" icon="bolt" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/agno/agno_async_operations.ipynb">
|
||||
Execute multiple AI tasks concurrently for improved performance using asyncio
|
||||
</Card>
|
||||
|
||||
<Card title="Research Team Collaboration" icon="magnifying-glass" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/agno/agno_research_team.ipynb">
|
||||
Build sophisticated multi-agent teams with specialized tools for comprehensive research
|
||||
</Card>
|
||||
|
||||
<Card title="RAG Tool Integration" icon="database" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/agno/agno_tool_integrations.ipynb">
|
||||
Implement Retrieval-Augmented Generation with vector databases and knowledge bases
|
||||
</Card>
|
||||
|
||||
<Card title="Workflow Setup with Caching" icon="diagram-project" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/agno/agno_workflow_setup.ipynb">
|
||||
Create custom workflows with intelligent caching for optimized agent performance
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
---
|
||||
title: Anthropic
|
||||
description: "Track and analyze your Anthropic API calls with AgentOps"
|
||||
---
|
||||
|
||||
AgentOps provides seamless integration with [Anthropic's Python SDK](https://github.com/anthropics/anthropic-sdk-python), allowing you to track and analyze all your Claude model interactions automatically.
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops anthropic
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops anthropic
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops anthropic
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using Anthropic with AgentOps, you need to set up your API keys. You can obtain:
|
||||
- **ANTHROPIC_API_KEY**: From the [Anthropic Console](https://console.anthropic.com/)
|
||||
- **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 ANTHROPIC_API_KEY="your_anthropic_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
ANTHROPIC_API_KEY="your_anthropic_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["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_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 Anthropic API calls:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
import anthropic
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create Anthropic client
|
||||
client = anthropic.Anthropic()
|
||||
|
||||
# Make a completion request - AgentOps will track it automatically
|
||||
message = client.messages.create(
|
||||
model="claude-sonnet-4-20250514",
|
||||
messages=[
|
||||
{"role": "user", "content": "What is artificial intelligence?"}
|
||||
]
|
||||
)
|
||||
|
||||
# Print the response received
|
||||
print(message.content[0].text)
|
||||
```
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
<CodeGroup>
|
||||
```python Streaming
|
||||
import agentops
|
||||
import anthropic
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create Anthropic client
|
||||
client = anthropic.Anthropic()
|
||||
|
||||
# Make a streaming request
|
||||
with client.messages.stream(
|
||||
model="claude-sonnet-4-20250514",
|
||||
messages=[
|
||||
{"role": "user", "content": "Write a short poem about artificial intelligence."}
|
||||
]
|
||||
) as stream:
|
||||
for text in stream.text_stream:
|
||||
print(text, end="", flush=True)
|
||||
print()
|
||||
```
|
||||
|
||||
```python Tool Use
|
||||
import agentops
|
||||
import anthropic
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create Anthropic client
|
||||
client = anthropic.Anthropic()
|
||||
|
||||
# Define tools
|
||||
tools = [
|
||||
{
|
||||
"type": "custom",
|
||||
"name": "get_current_time",
|
||||
"description": "Get the current date and time",
|
||||
"input_schema": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
def get_current_time():
|
||||
return {"current_time": datetime.now().isoformat()}
|
||||
|
||||
# Make a request with tools
|
||||
message = client.messages.create(
|
||||
model="claude-opus-4-20250514",
|
||||
tools=tools,
|
||||
messages=[
|
||||
{"role": "user", "content": "What time is it now?"}
|
||||
]
|
||||
)
|
||||
|
||||
# Handle tool use
|
||||
if message.content[0].type == "tool_calls":
|
||||
tool_call = message.content[0].tool_calls[0]
|
||||
tool_name = tool_call.name
|
||||
|
||||
if tool_name == "get_current_time":
|
||||
tool_response = get_current_time()
|
||||
|
||||
# Continue the conversation with the tool response
|
||||
second_message = client.messages.create(
|
||||
model="claude-opus-4-20250514",
|
||||
messages=[
|
||||
{"role": "user", "content": "What time is it now?"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_calls",
|
||||
"tool_calls": [
|
||||
{
|
||||
"type": "custom",
|
||||
"name": "get_current_time",
|
||||
"input": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"role": "tool",
|
||||
"content": json.dumps(tool_response),
|
||||
"tool_call_id": tool_call.id
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
print(second_message.content[0].text)
|
||||
else:
|
||||
print(message.content[0].text)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## More Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Understanding Tools" icon="notebook" href="/v2/examples/anthropic">
|
||||
Claude integration with tool usage and advanced features
|
||||
</Card>
|
||||
<Card title="Sync Example" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/anthropic/anthropic-example-sync.ipynb" newTab={true}>
|
||||
Shows synchronous calls with the Anthropic SDK.
|
||||
</Card>
|
||||
<Card title="Async Example" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/anthropic/anthropic-example-async.ipynb" newTab={true}>
|
||||
Demonstrates asynchronous calls with the Anthropic SDK.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,432 @@
|
||||
---
|
||||
title: "AutoGen"
|
||||
description: "Integrate AgentOps with Microsoft AutoGen for multi-agent workflow tracking"
|
||||
---
|
||||
|
||||
[AutoGen](https://microsoft.github.io/autogen/stable/) is Microsoft's framework for building multi-agent conversational AI systems. AgentOps provides seamless integration with AutoGen to track and monitor your multi-agent workflows.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops autogen-core python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops autogen-core python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops autogen-core python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using AutoGen with AgentOps, you need to set up your API keys. You can obtain:
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
- **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 OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
OPENAI_API_KEY="your_openai_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["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
AgentOps automatically instruments AutoGen agents and tracks their interactions. Simply initialize AgentOps before creating your AutoGen agents!
|
||||
|
||||
<CodeGroup>
|
||||
```python Countdown
|
||||
import asyncio
|
||||
from dataclasses import dataclass
|
||||
from typing import Callable
|
||||
import agentops
|
||||
|
||||
from autogen_core import (
|
||||
DefaultTopicId,
|
||||
MessageContext,
|
||||
RoutedAgent,
|
||||
default_subscription,
|
||||
message_handler,
|
||||
AgentId,
|
||||
SingleThreadedAgentRuntime
|
||||
)
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
@dataclass
|
||||
class CountdownMessage:
|
||||
"""Message containing a number for countdown operations"""
|
||||
content: int
|
||||
|
||||
@default_subscription
|
||||
class ModifierAgent(RoutedAgent):
|
||||
"""Agent that modifies numbers by applying a transformation function"""
|
||||
|
||||
def __init__(self, modify_val: Callable[[int], int]) -> None:
|
||||
super().__init__("A modifier agent that transforms numbers.")
|
||||
self._modify_val = modify_val
|
||||
|
||||
@message_handler
|
||||
async def handle_message(self, message: CountdownMessage, ctx: MessageContext) -> None:
|
||||
"""Handle incoming messages and apply modification"""
|
||||
original_val = message.content
|
||||
modified_val = self._modify_val(original_val)
|
||||
|
||||
print(f"🔧 ModifierAgent: Transformed {original_val} → {modified_val}")
|
||||
|
||||
# Publish the modified value to continue the workflow
|
||||
await self.publish_message(
|
||||
CountdownMessage(content=modified_val),
|
||||
DefaultTopicId()
|
||||
)
|
||||
|
||||
@default_subscription
|
||||
class CheckerAgent(RoutedAgent):
|
||||
"""Agent that checks if a condition is met and decides whether to continue"""
|
||||
|
||||
def __init__(self, stop_condition: Callable[[int], bool]) -> None:
|
||||
super().__init__("A checker agent that validates conditions.")
|
||||
self._stop_condition = stop_condition
|
||||
|
||||
@message_handler
|
||||
async def handle_message(self, message: CountdownMessage, ctx: MessageContext) -> None:
|
||||
"""Handle incoming messages and check stopping condition"""
|
||||
value = message.content
|
||||
|
||||
if not self._stop_condition(value):
|
||||
print(f"✅ CheckerAgent: {value} passed validation, continuing workflow")
|
||||
# Continue the workflow by publishing the message
|
||||
await self.publish_message(
|
||||
CountdownMessage(content=value),
|
||||
DefaultTopicId()
|
||||
)
|
||||
else:
|
||||
print(f"🛑 CheckerAgent: {value} failed validation, stopping workflow")
|
||||
print("🎉 Countdown completed successfully!")
|
||||
|
||||
async def run_countdown_workflow():
|
||||
"""Run a countdown workflow from 10 to 1 using AutoGen agents"""
|
||||
|
||||
print("🚀 Starting AutoGen Countdown Workflow")
|
||||
print("=" * 50)
|
||||
|
||||
# Create the AutoGen runtime
|
||||
runtime = SingleThreadedAgentRuntime()
|
||||
|
||||
# Register the modifier agent (subtracts 1 from each number)
|
||||
await ModifierAgent.register(
|
||||
runtime,
|
||||
"modifier",
|
||||
lambda: ModifierAgent(modify_val=lambda x: x - 1),
|
||||
)
|
||||
|
||||
# Register the checker agent (stops when value <= 1)
|
||||
await CheckerAgent.register(
|
||||
runtime,
|
||||
"checker",
|
||||
lambda: CheckerAgent(stop_condition=lambda x: x <= 1),
|
||||
)
|
||||
|
||||
# Start the runtime
|
||||
runtime.start()
|
||||
print("🤖 AutoGen runtime started")
|
||||
print("📨 Sending initial message with value: 10")
|
||||
|
||||
# Send initial message to start the countdown
|
||||
await runtime.send_message(
|
||||
CountdownMessage(10),
|
||||
AgentId("checker", "default")
|
||||
)
|
||||
|
||||
# Wait for the workflow to complete
|
||||
await runtime.stop_when_idle()
|
||||
|
||||
print("=" * 50)
|
||||
print("✨ Workflow completed! Check your AgentOps dashboard for detailed traces.")
|
||||
|
||||
# Run the workflow
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run_countdown_workflow())
|
||||
```
|
||||
|
||||
```python Multi-Agent
|
||||
import asyncio
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Dict, Any
|
||||
import agentops
|
||||
|
||||
from autogen_core import (
|
||||
DefaultTopicId,
|
||||
MessageContext,
|
||||
RoutedAgent,
|
||||
default_subscription,
|
||||
message_handler,
|
||||
AgentId,
|
||||
SingleThreadedAgentRuntime
|
||||
)
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
@dataclass
|
||||
class DataMessage:
|
||||
"""Message containing data to be processed"""
|
||||
data: List[Dict[str, Any]]
|
||||
stage: str
|
||||
metadata: Dict[str, Any]
|
||||
|
||||
@default_subscription
|
||||
class DataCollectorAgent(RoutedAgent):
|
||||
"""Agent responsible for collecting and preparing initial data"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Data collector agent that gathers initial dataset.")
|
||||
|
||||
@message_handler
|
||||
async def handle_message(self, message: DataMessage, ctx: MessageContext) -> None:
|
||||
print(f"📊 DataCollector: Collecting data for {message.metadata.get('source', 'unknown')}")
|
||||
|
||||
# Simulate data collection
|
||||
collected_data = [
|
||||
{"id": 1, "value": 100, "category": "A"},
|
||||
{"id": 2, "value": 200, "category": "B"},
|
||||
{"id": 3, "value": 150, "category": "A"},
|
||||
{"id": 4, "value": 300, "category": "C"},
|
||||
]
|
||||
|
||||
print(f"✅ DataCollector: Collected {len(collected_data)} records")
|
||||
|
||||
# Send to processor
|
||||
await self.publish_message(
|
||||
DataMessage(
|
||||
data=collected_data,
|
||||
stage="processing",
|
||||
metadata={**message.metadata, "collected_count": len(collected_data)}
|
||||
),
|
||||
DefaultTopicId()
|
||||
)
|
||||
|
||||
@default_subscription
|
||||
class DataProcessorAgent(RoutedAgent):
|
||||
"""Agent that processes and transforms data"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Data processor agent that transforms collected data.")
|
||||
|
||||
@message_handler
|
||||
async def handle_message(self, message: DataMessage, ctx: MessageContext) -> None:
|
||||
if message.stage != "processing":
|
||||
return
|
||||
|
||||
print(f"⚙️ DataProcessor: Processing {len(message.data)} records")
|
||||
|
||||
# Process data - add calculated fields
|
||||
processed_data = []
|
||||
for item in message.data:
|
||||
processed_item = {
|
||||
**item,
|
||||
"processed_value": item["value"] * 1.1, # 10% increase
|
||||
"status": "processed"
|
||||
}
|
||||
processed_data.append(processed_item)
|
||||
|
||||
print(f"✅ DataProcessor: Processed {len(processed_data)} records")
|
||||
|
||||
# Send to analyzer
|
||||
await self.publish_message(
|
||||
DataMessage(
|
||||
data=processed_data,
|
||||
stage="analysis",
|
||||
metadata={**message.metadata, "processed_count": len(processed_data)}
|
||||
),
|
||||
DefaultTopicId()
|
||||
)
|
||||
|
||||
@default_subscription
|
||||
class DataAnalyzerAgent(RoutedAgent):
|
||||
"""Agent that analyzes processed data and generates insights"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Data analyzer agent that generates insights.")
|
||||
|
||||
@message_handler
|
||||
async def handle_message(self, message: DataMessage, ctx: MessageContext) -> None:
|
||||
if message.stage != "analysis":
|
||||
return
|
||||
|
||||
print(f"🧠 DataAnalyzer: Analyzing {len(message.data)} records")
|
||||
|
||||
# Perform analysis
|
||||
total_value = sum(item["processed_value"] for item in message.data)
|
||||
avg_value = total_value / len(message.data)
|
||||
categories = set(item["category"] for item in message.data)
|
||||
|
||||
analysis_results = {
|
||||
"total_records": len(message.data),
|
||||
"total_value": total_value,
|
||||
"average_value": avg_value,
|
||||
"unique_categories": len(categories),
|
||||
"categories": list(categories)
|
||||
}
|
||||
|
||||
print(f"📈 DataAnalyzer: Analysis complete")
|
||||
print(f" • Total records: {analysis_results['total_records']}")
|
||||
print(f" • Average value: {analysis_results['average_value']:.2f}")
|
||||
print(f" • Categories: {', '.join(analysis_results['categories'])}")
|
||||
|
||||
# Send to reporter
|
||||
await self.publish_message(
|
||||
DataMessage(
|
||||
data=message.data,
|
||||
stage="reporting",
|
||||
metadata={
|
||||
**message.metadata,
|
||||
"analysis": analysis_results
|
||||
}
|
||||
),
|
||||
DefaultTopicId()
|
||||
)
|
||||
|
||||
@default_subscription
|
||||
class ReportGeneratorAgent(RoutedAgent):
|
||||
"""Agent that generates final reports"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Report generator agent that creates final output.")
|
||||
|
||||
@message_handler
|
||||
async def handle_message(self, message: DataMessage, ctx: MessageContext) -> None:
|
||||
if message.stage != "reporting":
|
||||
return
|
||||
|
||||
print(f"📝 ReportGenerator: Generating final report")
|
||||
|
||||
analysis = message.metadata.get("analysis", {})
|
||||
|
||||
report = f"""
|
||||
🎯 DATA PROCESSING REPORT
|
||||
========================
|
||||
Source: {message.metadata.get('source', 'Unknown')}
|
||||
Processing Date: {message.metadata.get('timestamp', 'Unknown')}
|
||||
|
||||
📊 SUMMARY STATISTICS:
|
||||
• Total Records Processed: {analysis.get('total_records', 0)}
|
||||
• Total Value: ${analysis.get('total_value', 0):,.2f}
|
||||
• Average Value: ${analysis.get('average_value', 0):,.2f}
|
||||
• Unique Categories: {analysis.get('unique_categories', 0)}
|
||||
• Categories Found: {', '.join(analysis.get('categories', []))}
|
||||
|
||||
✅ Processing pipeline completed successfully!
|
||||
"""
|
||||
|
||||
print(report)
|
||||
print("🎉 Multi-agent data processing workflow completed!")
|
||||
|
||||
async def run_data_processing_pipeline():
|
||||
"""Run a complete data processing pipeline using multiple AutoGen agents"""
|
||||
|
||||
print("🚀 Starting AutoGen Data Processing Pipeline")
|
||||
print("=" * 60)
|
||||
|
||||
# Create runtime
|
||||
runtime = SingleThreadedAgentRuntime()
|
||||
|
||||
# Register all agents
|
||||
await DataCollectorAgent.register(
|
||||
runtime,
|
||||
"collector",
|
||||
lambda: DataCollectorAgent(),
|
||||
)
|
||||
|
||||
await DataProcessorAgent.register(
|
||||
runtime,
|
||||
"processor",
|
||||
lambda: DataProcessorAgent(),
|
||||
)
|
||||
|
||||
await DataAnalyzerAgent.register(
|
||||
runtime,
|
||||
"analyzer",
|
||||
lambda: DataAnalyzerAgent(),
|
||||
)
|
||||
|
||||
await ReportGeneratorAgent.register(
|
||||
runtime,
|
||||
"reporter",
|
||||
lambda: ReportGeneratorAgent(),
|
||||
)
|
||||
|
||||
# Start runtime
|
||||
runtime.start()
|
||||
print("🤖 AutoGen runtime with 4 agents started")
|
||||
|
||||
# Trigger the pipeline
|
||||
initial_message = DataMessage(
|
||||
data=[],
|
||||
stage="collection",
|
||||
metadata={
|
||||
"source": "customer_database",
|
||||
"timestamp": "2024-01-15T10:30:00Z",
|
||||
"pipeline_id": "data_proc_001"
|
||||
}
|
||||
)
|
||||
|
||||
print("📨 Triggering data processing pipeline...")
|
||||
await runtime.send_message(
|
||||
initial_message,
|
||||
AgentId("collector", "default")
|
||||
)
|
||||
|
||||
# Wait for completion
|
||||
await runtime.stop_when_idle()
|
||||
|
||||
print("=" * 60)
|
||||
print("✨ Pipeline completed! Check AgentOps dashboard for detailed agent traces.")
|
||||
|
||||
# Run the pipeline
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run_data_processing_pipeline())
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Agent Chat Example" icon="notebook" href="/v2/examples/autogen">
|
||||
Basic multi-agent chat functionality
|
||||
</Card>
|
||||
<Card title="Math Agent Example" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/autogen/MathAgent.ipynb" newTab={true}>
|
||||
Demonstrates an agent specialized for mathematical problem-solving.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
Visit your [AgentOps Dashboard](https://app.agentops.ai) to see detailed traces of your AutoGen agent interactions, performance metrics, and workflow analytics.
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,129 @@
|
||||
---
|
||||
title: 'CrewAI'
|
||||
description: 'AgentOps and CrewAI teamed up to make monitoring Crew agents dead simple.'
|
||||
---
|
||||
|
||||
## Video Tutorial
|
||||
|
||||
<iframe
|
||||
width="560"
|
||||
height="315"
|
||||
src="https://www.youtube.com/embed/t9YRRd2-wus"
|
||||
title="AgentOps + CrewAI Integration Tutorial"
|
||||
frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
||||
allowfullscreen
|
||||
></iframe>
|
||||
|
||||
[CrewAI](https://www.crewai.com/) is a framework for easily building multi-agent applications. AgentOps integrates with CrewAI to provide observability into your agent workflows. Crew has comprehensive [documentation](https://docs.crewai.com) available as well as a great [quickstart](https://docs.crewai.com/how-to/Creating-a-Crew-and-kick-it-off/) guide.
|
||||
|
||||
## Installation
|
||||
Install AgentOps and CrewAI, along with `python-dotenv` for managing API keys:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops crewai python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops crewai python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops crewai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
You'll need API keys for AgentOps and OpenAI (since CrewAI's built-in `LLM` uses OpenAI models by default):
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
|
||||
|
||||
Set these as environment variables or in a `.env` file.
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then load them in your Python code:
|
||||
```python
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Simply initialize AgentOps at the beginning of your CrewAI application. AgentOps automatically instruments CrewAI components—including its `LLM`—to track your agent interactions.
|
||||
|
||||
Here's how to set up a basic CrewAI application with AgentOps:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from crewai import Agent, Task, Crew, LLM
|
||||
|
||||
# Initialize AgentOps client
|
||||
agentops.init()
|
||||
|
||||
# Define the LLM to use with CrewAI
|
||||
llm = LLM(
|
||||
model="openai/gpt-4o", # Or your preferred model
|
||||
temperature=0.7,
|
||||
)
|
||||
|
||||
# Create an agent
|
||||
researcher = Agent(
|
||||
role='Researcher',
|
||||
goal='Research and provide accurate information about cities and their history',
|
||||
backstory='You are an expert researcher with vast knowledge of world geography and history.',
|
||||
llm=llm,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Create a task
|
||||
research_task = Task(
|
||||
description='What is the capital of France? Provide a detailed answer about its history, culture, and significance.',
|
||||
expected_output='A comprehensive response about Paris, including its status as the capital of France, historical significance, cultural importance, and key landmarks.',
|
||||
agent=researcher
|
||||
)
|
||||
|
||||
# Create a crew with the researcher
|
||||
crew = Crew(
|
||||
agents=[researcher],
|
||||
tasks=[research_task],
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Execute the task
|
||||
result = crew.kickoff()
|
||||
|
||||
print("\nCrew Research Results:")
|
||||
print(result)
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Job Posting" icon="notebook" href="/v2/examples/crewai" newTab={true}>
|
||||
Create job postings with a crew of specialized agents
|
||||
</Card>
|
||||
<Card title="Markdown Validator" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/crewai/markdown_validator.ipynb" newTab={true}>
|
||||
Validate and improve markdown content using CrewAI agents
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,222 @@
|
||||
---
|
||||
title: Google ADK
|
||||
description: "Track and analyze your Google Agent Development Kit (ADK) AI agents with AgentOps"
|
||||
---
|
||||
|
||||
AgentOps provides seamless integration with [Google Agent Development Kit (ADK)](https://google.github.io/adk-docs/), allowing you to track and analyze all your ADK agent interactions automatically.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops google-adk
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops google-adk
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops google-adk
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using Google ADK 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 Google ADK agent interactions:
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
import json
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
import agentops
|
||||
from google.adk.agents import LlmAgent
|
||||
from google.adk.runners import Runner
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.genai import types
|
||||
|
||||
agentops.init()
|
||||
|
||||
# --- 1. Define Constants ---
|
||||
APP_NAME = "agent_comparison_app"
|
||||
USER_ID = "test_user_456"
|
||||
SESSION_ID_TOOL_AGENT = "session_tool_agent_xyz"
|
||||
SESSION_ID_SCHEMA_AGENT = "session_schema_agent_xyz"
|
||||
MODEL_NAME = "gemini-2.0-flash"
|
||||
|
||||
# --- 2. Define Schemas ---
|
||||
|
||||
# Input schema used by both agents
|
||||
class CountryInput(BaseModel):
|
||||
country: str = Field(description="The country to get information about.")
|
||||
|
||||
# Output schema ONLY for the second agent
|
||||
class CapitalInfoOutput(BaseModel):
|
||||
capital: str = Field(description="The capital city of the country.")
|
||||
# Note: Population is illustrative; the LLM will infer or estimate this
|
||||
# as it cannot use tools when output_schema is set.
|
||||
population_estimate: str = Field(description="An estimated population of the capital city.")
|
||||
|
||||
# --- 3. Define the Tool (Only for the first agent) ---
|
||||
def get_capital_city(country: str) -> str:
|
||||
"""Retrieves the capital city of a given country."""
|
||||
print(f"\n-- Tool Call: get_capital_city(country='{country}') --")
|
||||
country_capitals = {
|
||||
"united states": "Washington, D.C.",
|
||||
"canada": "Ottawa",
|
||||
"france": "Paris",
|
||||
"japan": "Tokyo",
|
||||
}
|
||||
result = country_capitals.get(country.lower(), f"Sorry, I couldn't find the capital for {country}.")
|
||||
print(f"-- Tool Result: '{result}' --")
|
||||
return result
|
||||
|
||||
# --- 4. Configure Agents ---
|
||||
|
||||
# Agent 1: Uses a tool and output_key
|
||||
capital_agent_with_tool = LlmAgent(
|
||||
model=MODEL_NAME,
|
||||
name="capital_agent_tool",
|
||||
description="Retrieves the capital city using a specific tool.",
|
||||
instruction="""You are a helpful agent that provides the capital city of a country using a tool.
|
||||
The user will provide the country name in a JSON format like {"country": "country_name"}.
|
||||
1. Extract the country name.
|
||||
2. Use the `get_capital_city` tool to find the capital.
|
||||
3. Respond clearly to the user, stating the capital city found by the tool.
|
||||
""",
|
||||
tools=[get_capital_city],
|
||||
input_schema=CountryInput,
|
||||
output_key="capital_tool_result", # Store final text response
|
||||
)
|
||||
|
||||
# Agent 2: Uses output_schema (NO tools possible)
|
||||
structured_info_agent_schema = LlmAgent(
|
||||
model=MODEL_NAME,
|
||||
name="structured_info_agent_schema",
|
||||
description="Provides capital and estimated population in a specific JSON format.",
|
||||
instruction=f"""You are an agent that provides country information.
|
||||
The user will provide the country name in a JSON format like {{"country": "country_name"}}.
|
||||
Respond ONLY with a JSON object matching this exact schema:
|
||||
{json.dumps(CapitalInfoOutput.model_json_schema(), indent=2)}
|
||||
Use your knowledge to determine the capital and estimate the population. Do not use any tools.
|
||||
""",
|
||||
# *** NO tools parameter here - using output_schema prevents tool use ***
|
||||
input_schema=CountryInput,
|
||||
output_schema=CapitalInfoOutput, # Enforce JSON output structure
|
||||
output_key="structured_info_result", # Store final JSON response
|
||||
)
|
||||
|
||||
# --- 5. Set up Session Management and Runners ---
|
||||
session_service = InMemorySessionService()
|
||||
|
||||
# Create a runner for EACH agent
|
||||
capital_runner = Runner(
|
||||
agent=capital_agent_with_tool,
|
||||
app_name=APP_NAME,
|
||||
session_service=session_service
|
||||
)
|
||||
structured_runner = Runner(
|
||||
agent=structured_info_agent_schema,
|
||||
app_name=APP_NAME,
|
||||
session_service=session_service
|
||||
)
|
||||
|
||||
# --- 6. Define Agent Interaction Logic ---
|
||||
async def call_agent_and_print(
|
||||
runner_instance: Runner,
|
||||
agent_instance: LlmAgent,
|
||||
session_id: str,
|
||||
query_json: str
|
||||
):
|
||||
"""Sends a query to the specified agent/runner and prints results."""
|
||||
print(f"\n>>> Calling Agent: '{agent_instance.name}' | Query: {query_json}")
|
||||
|
||||
user_content = types.Content(role='user', parts=[types.Part(text=query_json)])
|
||||
|
||||
final_response_content = "No final response received."
|
||||
async for event in runner_instance.run_async(user_id=USER_ID, session_id=session_id, new_message=user_content):
|
||||
# print(f"Event: {event.type}, Author: {event.author}") # Uncomment for detailed logging
|
||||
if event.is_final_response() and event.content and event.content.parts:
|
||||
# For output_schema, the content is the JSON string itself
|
||||
final_response_content = event.content.parts[0].text
|
||||
|
||||
print(f"<<< Agent '{agent_instance.name}' Response: {final_response_content}")
|
||||
|
||||
current_session = await session_service.get_session(app_name=APP_NAME,
|
||||
user_id=USER_ID,
|
||||
session_id=session_id)
|
||||
stored_output = current_session.state.get(agent_instance.output_key)
|
||||
|
||||
# Pretty print if the stored output looks like JSON (likely from output_schema)
|
||||
print(f"--- Session State ['{agent_instance.output_key}']: ", end="")
|
||||
try:
|
||||
# Attempt to parse and pretty print if it's JSON
|
||||
parsed_output = json.loads(stored_output)
|
||||
print(json.dumps(parsed_output, indent=2))
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
# Otherwise, print as string
|
||||
print(stored_output)
|
||||
print("-" * 30)
|
||||
|
||||
|
||||
# --- 7. Run Interactions ---
|
||||
async def main():
|
||||
# Create sessions
|
||||
await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID_TOOL_AGENT)
|
||||
await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID_SCHEMA_AGENT)
|
||||
|
||||
print("--- Testing Agent with Tool ---")
|
||||
await call_agent_and_print(capital_runner, capital_agent_with_tool, SESSION_ID_TOOL_AGENT, '{"country": "France"}')
|
||||
|
||||
print("\n\n--- Testing Agent with Output Schema (No Tool Use) ---")
|
||||
await call_agent_and_print(structured_runner, structured_info_agent_schema, SESSION_ID_SCHEMA_AGENT, '{"country": "Japan"}')
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Human Approval Example" icon="notebook" href="/v2/examples/google_adk" newTab={true}>
|
||||
Implement human-in-the-loop approval workflows with Google ADK agents
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
Visit your [AgentOps Dashboard](https://app.agentops.ai) to see detailed traces of your Google ADK agent interactions, tool usage, and session management.
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,114 @@
|
||||
---
|
||||
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>
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
title: Haystack
|
||||
description: "Monitor your Haystack agents with AgentOps"
|
||||
---
|
||||
|
||||
[Haystack](https://docs.haystack.deepset.ai/docs/installation) is a flexible framework for building production-ready AI agents. AgentOps makes monitoring your Haystack agents seamless.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops haystack-ai python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops haystack-ai python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops haystack-ai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
<Warning>
|
||||
We currently only support Haystack 2.x.
|
||||
</Warning>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
You'll need API keys for AgentOps and whatever model provider you use with Haystack (e.g. OpenAI):
|
||||
|
||||
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys) *(if using OpenAI models)*
|
||||
|
||||
Set these as environment variables or in a `.env` file.
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then load them in your Python code:
|
||||
|
||||
```python
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Integrating AgentOps with Haystack only takes a few lines:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from haystack.components.generators.openai import OpenAIGenerator
|
||||
|
||||
agentops.init(AGENTOPS_API_KEY)
|
||||
|
||||
generator = OpenAIGenerator(model="gpt-4o-mini", api_key=OPENAI_API_KEY)
|
||||
result = generator.run(prompt="In one sentence, what is AgentOps?")
|
||||
print(result["replies"][0])
|
||||
```
|
||||
|
||||
Run your script and visit the [AgentOps Dashboard](https://app.agentops.ai/drilldown) to monitor the trace.
|
||||
|
||||
## Examples
|
||||
|
||||
- [Simple Haystack example (OpenAI)](https://github.com/AgentOps-AI/agentops/blob/main/examples/haystack/haystack_example.py)
|
||||
- [Haystack Azure OpenAI Chat example](https://github.com/AgentOps-AI/agentops/blob/main/examples/haystack/azure_haystack_example.py)
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
---
|
||||
title: IBM Watsonx.ai
|
||||
description: "Track and analyze your IBM Watsonx.ai API calls with AgentOps"
|
||||
---
|
||||
|
||||
AgentOps provides seamless integration with [IBM Watsonx.ai Python SDK](https://ibm.github.io/watsonx-ai-python-sdk/), allowing you to track and analyze all your Watsonx.ai model interactions automatically.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops ibm-watsonx-ai
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops ibm-watsonx-ai
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops ibm-watsonx-ai
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using IBM Watsonx.ai with AgentOps, you need to set up your API keys. You can obtain:
|
||||
- **IBM_WATSONX_API_KEY**: From your [IBM Cloud account](https://cloud.ibm.com/)
|
||||
- **IBM_WATSONX_URL**: The URL for your Watsonx.ai instance, typically found in your IBM Cloud dashboard.
|
||||
- **IBM_WATSONX_PROJECT_ID**: The project ID for your Watsonx.ai project, which you can find in the Watsonx.ai console.
|
||||
- **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 IBM_WATSONX_API_KEY="your_ibm_api_key_here"
|
||||
export IBM_WATSONX_URL="your_ibm_url_here"
|
||||
export IBM_WATSONX_PROJECT_ID="your_project_id_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
IBM_WATSONX_API_KEY="your_ibm_api_key_here"
|
||||
IBM_WATSONX_URL="your_ibm_url_here"
|
||||
IBM_WATSONX_PROJECT_ID="your_project_id_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["IBM_WATSONX_API_KEY"] = os.getenv("IBM_WATSONX_API_KEY")
|
||||
os.environ["IBM_WATSONX_URL"] = os.getenv("IBM_WATSONX_URL")
|
||||
os.environ["IBM_WATSONX_PROJECT_ID"] = os.getenv("IBM_WATSONX_PROJECT_ID")
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Initialize AgentOps at the beginning of your application to automatically track all IBM Watsonx.ai API calls:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from ibm_watsonx_ai import Credentials
|
||||
from ibm_watsonx_ai.foundation_models import ModelInference
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="")
|
||||
|
||||
# Initialize credentials
|
||||
credentials = Credentials(
|
||||
url=os.getenv("IBM_WATSONX_URL"),
|
||||
api_key=os.getenv("IBM_WATSONX_API_KEY"),
|
||||
)
|
||||
|
||||
# Project ID
|
||||
project_id = os.getenv("IBM_WATSONX_PROJECT_ID")
|
||||
|
||||
# Create a model instance
|
||||
model = ModelInference(
|
||||
model_id="meta-llama/llama-3-3-70b-instruct",
|
||||
credentials=credentials,
|
||||
project_id=project_id
|
||||
)
|
||||
|
||||
# Make a completion request
|
||||
response = model.generate_text("What is artificial intelligence?")
|
||||
print(f"Generated Text:\n{response}")
|
||||
|
||||
# Don't forget to close connection when done
|
||||
model.close_persistent_connection()
|
||||
```
|
||||
|
||||
## Examples
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Watsonx Text Chat" icon="notebook" href="/v2/examples/watsonx">
|
||||
Basic text generation and chat
|
||||
</Card>
|
||||
<Card title="Watsonx Streaming" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/watsonx/watsonx-streaming.ipynb" newTab={true}>
|
||||
Demonstrates streaming responses with Watsonx.ai.
|
||||
</Card>
|
||||
<Card title="Watsonx Tokenization" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/watsonx/watsonx-tokeniation-model.ipynb" newTab={true}>
|
||||
Example of text tokenization with Watsonx.ai models.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
## Additional Resources
|
||||
|
||||
- [IBM Watsonx.ai Python SDK Documentation](https://ibm.github.io/watsonx-ai-python-sdk/)
|
||||
- [IBM Watsonx.ai Models](http://ibm.com/products/watsonx-ai/foundation-models)
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,185 @@
|
||||
---
|
||||
title: IO Intelligence
|
||||
description: "Track and analyze your IO Intelligence API calls with AgentOps"
|
||||
---
|
||||
|
||||
AgentOps seamlessly integrates with IO Intelligence's OpenAI‑compatible API, allowing you to track and analyze every request without changing your workflow.
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install openai
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Basic Usage
|
||||
|
||||
Initialize AgentOps at the beginning of your application. Then create an **OpenAI** client that points at IO Intelligence's endpoint:
|
||||
|
||||
<CodeGroup>
|
||||
```python Basic Usage
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialise AgentOps (tracks every request automatically)
|
||||
agentops.init("<YOUR_AGENTOPS_API_KEY>")
|
||||
|
||||
# Create IO Intelligence client (just add base_url)
|
||||
client = OpenAI(
|
||||
api_key="<YOUR_IO_INTELLIGENCE_API_KEY>",
|
||||
base_url="https://api.intelligence.io.solutions/api/v1/"
|
||||
)
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="meta-llama/Llama-3.3-70B-Instruct",
|
||||
messages=[{"role": "user", "content": "Say this is a test!"}]
|
||||
)
|
||||
|
||||
print(response.choices[0].message.content)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Model Limits & Daily Quotas
|
||||
|
||||
Below are the free daily limits for each model (tokens are counted across input *and* output):
|
||||
|
||||
| LLM Model Name | Daily Chat quote | Daily API quote | Daily Embeddings quote | Context Length |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| deepseek-ai/DeepSeek-R1 | 1,000,000 tk | 500,000 tk | N/A | 128,
|
||||
| deepseek-ai/DeepSeek-R1-Distill-Llama-70B | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| meta-llama/Llama-3.3-70B-Instruct | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| deepseek-ai/DeepSeek-R1-Distill-Qwen-32B | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| Qwen/QwQ-32B-Preview | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| databricks/dbrx-instruct | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| deepseek-ai/DeepSeek-R1-Distill-Llama-8B | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| deepseek-ai/DeepSeek-R1-Distill-Qwen-14B | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| deepseek-ai/DeepSeek-R1-Distill-Qwen-7B | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| microsoft/phi-4 | 1,000,000 tk | 500,000 tk | N/A | 16,000 tk |
|
||||
| mistralai/Mistral-Large-Instruct-2411 | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| neuralmagic/Llama-3.1-Nemotron-70B-Instruct-HF-FP8-dynamic | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| google/gemma-2-9b-it | 1,000,000 tk | 500,000 tk | N/A | 8,000 tk |
|
||||
| nvidia/AceMath-7B-Instruct | 1,000,000 tk | 500,000 tk | N/A | 4,000 tk |
|
||||
| CohereForAI/aya-expanse-32b | 1,000,000 tk | 500,000 tk | N/A | 8,000 tk |
|
||||
| Qwen/Qwen2.5-Coder-32B-Instruct | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| THUDM/glm-4-9b-chat | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| CohereForAI/c4ai-command-r-plus-08-2024 | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| tiiuae/Falcon3-10B-Instruct | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| NovaSky-AI/Sky-T1-32B-Preview | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| bespokelabs/Bespoke-Stratos-32B | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| netease-youdao/Confucius-o1-14B | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| Qwen/Qwen2.5-1.5B-Instruct | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| mistralai/Ministral-8B-Instruct-2410 | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| openbmb/MiniCPM3-4B | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| jinaai/ReaderLM-v2 | 1,000,000 tk | 500,000 tk | N/A | 512,000 tk |
|
||||
| ibm-granite/granite-3.1-8b-instruct | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| microsoft/Phi-3.5-mini-instruct | 1,000,000 tk | 500,000 tk | N/A | 128,000 tk |
|
||||
| ozone-ai/0x-lite | 1,000,000 tk | 500,000 tk | N/A | 32,000 tk |
|
||||
| mixedbread-ai/mxbai-embed-large-v1 | N/A | N/A | 500,000 tk | 512 tk |
|
||||
| meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8 | 1,000,000 tk | 500,000 tk | N/A | 430,000 tk |
|
||||
|
||||
## Quick Test (cURL)
|
||||
|
||||
<CodeGroup>
|
||||
```bash cURL
|
||||
curl https://api.intelligence.io.solutions/api/v1/chat/completions \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer <YOUR_IO_INTELLIGENCE_API_KEY>" \
|
||||
-d '{
|
||||
"model": "meta-llama/Llama-3.3-70B-Instruct",
|
||||
"messages": [{"role": "user", "content": "Say this is a test!"}],
|
||||
"temperature": 0.7
|
||||
}'
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Streaming Completions
|
||||
|
||||
AgentOps also captures streaming responses:
|
||||
|
||||
<CodeGroup>
|
||||
```python Streaming Example
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
agentops.init("<YOUR_AGENTOPS_API_KEY>")
|
||||
|
||||
client = OpenAI(
|
||||
api_key="<YOUR_IO_INTELLIGENCE_API_KEY>",
|
||||
base_url="https://api.intelligence.io.solutions/api/v1/"
|
||||
)
|
||||
|
||||
stream = client.chat.completions.create(
|
||||
model="meta-llama/Llama-3.3-70B-Instruct",
|
||||
messages=[{"role": "user", "content": "Stream please"}],
|
||||
stream=True
|
||||
)
|
||||
for chunk in stream:
|
||||
print(chunk.choices[0].delta.content, end="", flush=True)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Advanced: Tool Calls
|
||||
|
||||
All OpenAI‑style tool calls work the same way—AgentOps will record parameters and results automatically.
|
||||
|
||||
<CodeGroup>
|
||||
```python Tool Calls
|
||||
import agentops, json
|
||||
from openai import OpenAI
|
||||
|
||||
agentops.init("<YOUR_AGENTOPS_API_KEY>")
|
||||
client = OpenAI(
|
||||
api_key="<YOUR_IO_INTELLIGENCE_API_KEY>",
|
||||
base_url="https://api.intelligence.io.solutions/api/v1/"
|
||||
)
|
||||
|
||||
def get_weather(location):
|
||||
return f"The weather in {location} is sunny."
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a weather bot."},
|
||||
{"role": "user", "content": "What's the weather like in Boston?"}
|
||||
]
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="meta-llama/Llama-3.3-70B-Instruct",
|
||||
messages=messages,
|
||||
tools=[{
|
||||
"type":"function",
|
||||
"function":{
|
||||
"name":"get_weather",
|
||||
"description":"Get weather for a city",
|
||||
"parameters":{
|
||||
"type":"object",
|
||||
"properties":{
|
||||
"location":{"type":"string"}
|
||||
},
|
||||
"required":["location"]
|
||||
}
|
||||
}
|
||||
}]
|
||||
)
|
||||
|
||||
tool_calls = response.choices[0].message.tool_calls
|
||||
for call in tool_calls:
|
||||
if call.function.name == "get_weather":
|
||||
function_response = get_weather(json.loads(call.function.arguments)["location"])
|
||||
messages.append({"role":"tool","tool_call_id":call.id,"content":function_response})
|
||||
|
||||
follow_up = client.chat.completions.create(
|
||||
model="meta-llama/Llama-3.3-70B-Instruct",
|
||||
messages=messages
|
||||
)
|
||||
print(follow_up.choices[0].message.content)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,122 @@
|
||||
---
|
||||
title: LangChain
|
||||
description: "Track your LangChain agents with AgentOps"
|
||||
---
|
||||
|
||||
[LangChain](https://python.langchain.com/docs/tutorials/) is a framework for developing applications powered by language models. AgentOps automatically tracks your LangChain agents by integrating its callback handler.
|
||||
|
||||
## Installation
|
||||
|
||||
Install AgentOps and the necessary LangChain dependencies:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops langchain langchain-community langchain-openai python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops langchain langchain-community langchain-openai python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops langchain langchain-community langchain-openai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
You'll need API keys for AgentOps and OpenAI (as `ChatOpenAI` is commonly used with LangChain):
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
|
||||
|
||||
Set these as environment variables or in a `.env` file.
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then load them in your Python code:
|
||||
```python
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Integrating AgentOps with LangChain involves using the `LangchainCallbackHandler`.
|
||||
<Tip>
|
||||
You don't need a separate `agentops.init()` call; the `LangchainCallbackHandler` initializes the AgentOps client automatically if an API key is provided to it or found in the environment.
|
||||
</Tip>
|
||||
|
||||
Here's a basic example:
|
||||
|
||||
```python
|
||||
from langchain_community.chat_models import ChatOpenAI
|
||||
from langchain.agents import initialize_agent, AgentType, Tool # Corrected Tool import
|
||||
from langchain.tools import DuckDuckGoSearchRun # Example tool
|
||||
from agentops.integration.callbacks.langchain import LangchainCallbackHandler
|
||||
|
||||
|
||||
# 1. Initialize LangchainCallbackHandler
|
||||
# AGENTOPS_API_KEY can be passed here or loaded from environment
|
||||
handler = LangchainCallbackHandler(api_key=AGENTOPS_API_KEY, tags=['LangChain Example'])
|
||||
|
||||
# 2. Define tools for the agent
|
||||
search_tool = DuckDuckGoSearchRun()
|
||||
tools = [
|
||||
Tool( # Wrap DuckDuckGoSearchRun in a Tool object
|
||||
name="DuckDuckGo Search",
|
||||
func=search_tool.run,
|
||||
description="Useful for when you need to answer questions about current events or the current state of the world."
|
||||
)
|
||||
]
|
||||
|
||||
# 3. Configure LLM with the AgentOps handler
|
||||
# OPENAI_API_KEY can be passed here or loaded from environment
|
||||
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY,
|
||||
callbacks=[handler],
|
||||
model='gpt-3.5-turbo',
|
||||
temperature=0) # Added temperature for reproducibility
|
||||
|
||||
# 4. Initialize your agent, passing the handler to callbacks
|
||||
agent = initialize_agent(
|
||||
tools,
|
||||
llm,
|
||||
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
|
||||
verbose=True,
|
||||
callbacks=[handler],
|
||||
handle_parsing_errors=True
|
||||
)
|
||||
|
||||
# 5. Run your agent
|
||||
try:
|
||||
response = agent.run("Who is the current CEO of OpenAI and what is his most recent public statement?")
|
||||
print(response)
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
```
|
||||
|
||||
Visit the [AgentOps Dashboard](https://app.agentops.ai/) to see your session.
|
||||
|
||||
## Examples
|
||||
<CardGroup cols={1}>
|
||||
<Card title="LangChain Example" icon="notebook" href="/v2/examples/langchain">
|
||||
A detailed notebook demonstrating the LangChain callback handler integration.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<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>
|
||||
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,237 @@
|
||||
---
|
||||
title: LangGraph
|
||||
description: "Track and analyze your LangGraph workflows with AgentOps"
|
||||
---
|
||||
|
||||
[LangGraph](https://github.com/langchain-ai/langgraph) is a framework for building stateful, multi-step applications with LLMs as graphs. AgentOps automatically instruments LangGraph to provide comprehensive observability into your graph-based agent workflows.
|
||||
|
||||
## Core Concepts
|
||||
|
||||
LangGraph enables you to build complex agentic workflows as graphs with:
|
||||
- **Nodes**: Individual steps in your workflow (agents, tools, functions)
|
||||
- **Edges**: Connections between nodes that define flow
|
||||
- **State**: Shared data that flows through the graph
|
||||
- **Conditional Edges**: Dynamic routing based on state or outputs
|
||||
- **Cycles**: Support for iterative workflows and feedback loops
|
||||
|
||||
## Installation
|
||||
|
||||
Install AgentOps and LangGraph along with LangChain dependencies:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops langgraph langchain-openai python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops langgraph langchain-openai python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops langgraph langchain-openai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
You'll need API keys for AgentOps and your LLM provider:
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
|
||||
|
||||
Set these as environment variables or in a `.env` file.
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then load them in your Python code:
|
||||
```python
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Initialize AgentOps at the beginning of your application to automatically track all LangGraph operations:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from typing import Annotated, Literal, TypedDict
|
||||
from langgraph.graph import StateGraph, END
|
||||
from langgraph.graph.message import add_messages
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Define your graph state
|
||||
class AgentState(TypedDict):
|
||||
messages: Annotated[list, add_messages]
|
||||
|
||||
# Create your LLM
|
||||
model = ChatOpenAI(temperature=0)
|
||||
|
||||
# Define nodes
|
||||
def agent_node(state: AgentState):
|
||||
messages = state["messages"]
|
||||
response = model.invoke(messages)
|
||||
return {"messages": [response]}
|
||||
|
||||
# Build the graph
|
||||
workflow = StateGraph(AgentState)
|
||||
workflow.add_node("agent", agent_node)
|
||||
workflow.set_entry_point("agent")
|
||||
workflow.add_edge("agent", END)
|
||||
|
||||
# Compile and run
|
||||
app = workflow.compile()
|
||||
result = app.invoke({"messages": [{"role": "user", "content": "Hello!"}]})
|
||||
```
|
||||
|
||||
## What Gets Tracked
|
||||
|
||||
AgentOps automatically captures:
|
||||
|
||||
- **Graph Structure**: Nodes, edges, and entry points during compilation
|
||||
- **Execution Flow**: The path taken through your graph
|
||||
- **Node Executions**: Each node execution with inputs and outputs
|
||||
- **LLM Calls**: All language model interactions within nodes
|
||||
- **Tool Usage**: Any tools called within your graph
|
||||
- **State Changes**: How state evolves through the workflow
|
||||
- **Timing Information**: Duration of each node and total execution time
|
||||
|
||||
## Advanced Example
|
||||
|
||||
Here's a more complex example with conditional routing and tools:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from typing import Annotated, Literal, TypedDict
|
||||
from langgraph.graph import StateGraph, END
|
||||
from langgraph.graph.message import add_messages
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_core.tools import tool
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Define tools
|
||||
@tool
|
||||
def search(query: str) -> str:
|
||||
"""Search for information."""
|
||||
return f"Search results for: {query}"
|
||||
|
||||
@tool
|
||||
def calculate(expression: str) -> str:
|
||||
"""Evaluate a mathematical expression."""
|
||||
try:
|
||||
return str(eval(expression))
|
||||
except:
|
||||
return "Error in calculation"
|
||||
|
||||
# Configure model with tools
|
||||
tools = [search, calculate]
|
||||
model = ChatOpenAI(temperature=0).bind_tools(tools)
|
||||
|
||||
# Define state
|
||||
class AgentState(TypedDict):
|
||||
messages: Annotated[list, add_messages]
|
||||
|
||||
# Define conditional logic
|
||||
def should_continue(state: AgentState) -> Literal["tools", "end"]:
|
||||
messages = state["messages"]
|
||||
last_message = messages[-1]
|
||||
|
||||
if hasattr(last_message, "tool_calls") and last_message.tool_calls:
|
||||
return "tools"
|
||||
return "end"
|
||||
|
||||
# Define nodes
|
||||
def call_model(state: AgentState):
|
||||
messages = state["messages"]
|
||||
response = model.invoke(messages)
|
||||
return {"messages": [response]}
|
||||
|
||||
def call_tools(state: AgentState):
|
||||
messages = state["messages"]
|
||||
last_message = messages[-1]
|
||||
|
||||
tool_responses = []
|
||||
for tool_call in last_message.tool_calls:
|
||||
# Execute the appropriate tool
|
||||
if tool_call["name"] == "search":
|
||||
result = search.invoke(tool_call["args"])
|
||||
elif tool_call["name"] == "calculate":
|
||||
result = calculate.invoke(tool_call["args"])
|
||||
|
||||
tool_responses.append({
|
||||
"role": "tool",
|
||||
"content": result,
|
||||
"tool_call_id": tool_call["id"]
|
||||
})
|
||||
|
||||
return {"messages": tool_responses}
|
||||
|
||||
# Build the graph
|
||||
workflow = StateGraph(AgentState)
|
||||
workflow.add_node("agent", call_model)
|
||||
workflow.add_node("tools", call_tools)
|
||||
workflow.set_entry_point("agent")
|
||||
workflow.add_conditional_edges(
|
||||
"agent",
|
||||
should_continue,
|
||||
{
|
||||
"tools": "tools",
|
||||
"end": END
|
||||
}
|
||||
)
|
||||
workflow.add_edge("tools", "agent")
|
||||
|
||||
# Compile and run
|
||||
app = workflow.compile()
|
||||
result = app.invoke({
|
||||
"messages": [{"role": "user", "content": "Search for AI news and calculate 25*4"}]
|
||||
})
|
||||
```
|
||||
|
||||
## Dashboard Insights
|
||||
|
||||
In your AgentOps dashboard, you'll see:
|
||||
|
||||
1. **Graph Visualization**: Visual representation of your compiled graph
|
||||
2. **Execution Trace**: Step-by-step flow through nodes
|
||||
3. **Node Metrics**: Performance data for each node
|
||||
4. **LLM Analytics**: Token usage and costs across all model calls
|
||||
5. **Tool Usage**: Which tools were called and their results
|
||||
6. **Error Tracking**: Any failures in node execution
|
||||
|
||||
## Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="LangGraph Example" icon="notebook" href="/v2/examples/langgraph">
|
||||
Complete example showing agent workflows with tools
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Initialize Early**: Call `agentops.init()` before creating your graph
|
||||
2. **Use Descriptive Names**: Name your nodes clearly for better traces
|
||||
3. **Handle Errors**: Implement error handling in your nodes
|
||||
4. **Monitor State Size**: Large states can impact performance
|
||||
5. **Leverage Conditional Edges**: Use them for dynamic workflows
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,140 @@
|
||||
---
|
||||
title: LiteLLM
|
||||
description: "Track and analyze your LiteLLM calls across multiple providers with AgentOps"
|
||||
---
|
||||
|
||||
AgentOps provides seamless integration with [LiteLLM](https://github.com/BerriAI/litellm), allowing you to automatically track all your LLM API calls across different providers through a unified interface.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops litellm
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops litellm
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops litellm
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using LiteLLM with AgentOps, you need to set up your API keys. You can obtain:
|
||||
- **Provider API Keys**: From your chosen LLM provider (OpenAI, Anthropic, Google, etc.)
|
||||
- **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 OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export ANTHROPIC_API_KEY="your_anthropic_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
ANTHROPIC_API_KEY="your_anthropic_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["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
||||
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
The simplest way to integrate AgentOps with LiteLLM is to set up the success_callback.
|
||||
|
||||
```python
|
||||
import litellm
|
||||
from litellm import completion
|
||||
|
||||
# Configure LiteLLM to use AgentOps
|
||||
litellm.success_callback = ["agentops"]
|
||||
|
||||
# Make completion requests with LiteLLM
|
||||
response = completion(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=[{"role": "user", "content": "Hello, how are you?"}]
|
||||
)
|
||||
|
||||
print(response.choices[0].message.content)
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
<CodeGroup>
|
||||
```python Streaming
|
||||
import litellm
|
||||
from litellm import completion
|
||||
|
||||
# Configure LiteLLM to use AgentOps
|
||||
litellm.success_callback = ["agentops"]
|
||||
|
||||
# Make a streaming completion request
|
||||
response = completion(
|
||||
model="gpt-4",
|
||||
messages=[{"role": "user", "content": "Write a short poem about AI."}],
|
||||
stream=True
|
||||
)
|
||||
|
||||
# Process the streaming response
|
||||
for chunk in response:
|
||||
if chunk.choices[0].delta.content:
|
||||
print(chunk.choices[0].delta.content, end="", flush=True)
|
||||
print() # Add a newline at the end
|
||||
```
|
||||
|
||||
```python Multi-Provider
|
||||
import litellm
|
||||
from litellm import completion
|
||||
|
||||
# Configure LiteLLM to use AgentOps
|
||||
litellm.success_callback = ["agentops"]
|
||||
|
||||
# OpenAI request
|
||||
openai_response = completion(
|
||||
model="gpt-4",
|
||||
messages=[{"role": "user", "content": "What are the advantages of GPT-4?"}]
|
||||
)
|
||||
|
||||
print("OpenAI Response:", openai_response.choices[0].message.content)
|
||||
|
||||
# Anthropic request using the same interface
|
||||
anthropic_response = completion(
|
||||
model="anthropic/claude-3-opus-20240229",
|
||||
messages=[{"role": "user", "content": "What are the advantages of Claude?"}]
|
||||
)
|
||||
|
||||
print("Anthropic Response:", anthropic_response.choices[0].message.content)
|
||||
|
||||
# All requests across different providers are automatically tracked by AgentOps
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
|
||||
## More Examples
|
||||
<CardGroup cols={2}>
|
||||
<Card title="LiteLLM Quickstart Notebook" icon="notebook" href="/v2/examples/litellm" />
|
||||
</CardGroup>
|
||||
|
||||
For more information on integrating AgentOps with LiteLLM, refer to the [LiteLLM documentation on AgentOps integration](https://docs.litellm.ai/docs/observability/agentops_integration).
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,79 @@
|
||||
---
|
||||
title: 'LlamaIndex'
|
||||
description: 'AgentOps works seamlessly with LlamaIndex, a framework for building context-augmented generative AI applications with LLMs.'
|
||||
---
|
||||
|
||||
[LlamaIndex](https://www.llamaindex.ai/) is a framework for building context-augmented generative AI applications with LLMs. AgentOps provides comprehensive observability into your LlamaIndex applications through automatic instrumentation, allowing you to monitor LLM calls, track performance, and analyze your application's behavior.
|
||||
|
||||
## Installation
|
||||
|
||||
Install AgentOps and the LlamaIndex AgentOps instrumentation package:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops llama-index-instrumentation-agentops
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops llama-index-instrumentation-agentops
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops llama-index-instrumentation-agentops
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
You'll need an AgentOps API key from your [AgentOps Dashboard](https://app.agentops.ai/):
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Usage
|
||||
|
||||
Simply set the global handler to "agentops" at the beginning of your LlamaIndex application. AgentOps will automatically instrument LlamaIndex to track your LLM interactions and application performance.
|
||||
|
||||
```python
|
||||
from llama_index.core import set_global_handler
|
||||
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
||||
|
||||
# Set the global handler to AgentOps
|
||||
# NOTE: Feel free to set your AgentOps environment variables (e.g., 'AGENTOPS_API_KEY')
|
||||
# as outlined in the AgentOps documentation, or pass the equivalent keyword arguments
|
||||
# anticipated by AgentOps' AOClient as **eval_params in set_global_handler.
|
||||
set_global_handler("agentops")
|
||||
|
||||
# Your LlamaIndex application code here
|
||||
documents = SimpleDirectoryReader("data").load_data()
|
||||
index = VectorStoreIndex.from_documents(documents)
|
||||
|
||||
# Create a query engine
|
||||
query_engine = index.as_query_engine()
|
||||
|
||||
# Query your data - AgentOps will automatically track this
|
||||
response = query_engine.query("What is the main topic of these documents?")
|
||||
print(response)
|
||||
```
|
||||
|
||||
## What Gets Tracked
|
||||
|
||||
When you use AgentOps with LlamaIndex, the following operations are automatically tracked:
|
||||
|
||||
- **LLM Calls**: All interactions with language models including prompts, completions, and token usage
|
||||
- **Embeddings**: Vector embedding generation and retrieval operations
|
||||
- **Query Operations**: Search and retrieval operations on your indexes
|
||||
- **Performance Metrics**: Response times, token costs, and success/failure rates
|
||||
|
||||
## Additional Resources
|
||||
|
||||
For more detailed information about LlamaIndex's observability features and AgentOps integration, check out the [LlamaIndex documentation](https://docs.llamaindex.ai/en/stable/module_guides/observability/#agentops).
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,153 @@
|
||||
---
|
||||
title: 'Mem0'
|
||||
description: 'Track and monitor Mem0 memory operations with AgentOps'
|
||||
---
|
||||
|
||||
[Mem0](https://mem0.ai/) provides a smart memory layer for AI applications, enabling personalized interactions by remembering user preferences, conversation history, and context across sessions.
|
||||
|
||||
## Why Track Mem0 with AgentOps?
|
||||
|
||||
When building memory-powered AI applications, you need visibility into:
|
||||
- **Memory Operations**: Track when memories are created, updated, or retrieved
|
||||
- **Search Performance**: Monitor how effectively your AI finds relevant memories
|
||||
- **Memory Usage Patterns**: Understand what information is being stored and accessed
|
||||
- **Error Tracking**: Identify issues with memory storage or retrieval
|
||||
- **Cost Analysis**: Track API calls to both Mem0 and your LLM provider
|
||||
|
||||
AgentOps automatically instruments Mem0 to provide complete observability of your memory operations.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops mem0ai python-dotenv
|
||||
```
|
||||
|
||||
```bash poetry
|
||||
poetry add agentops mem0ai python-dotenv
|
||||
```
|
||||
|
||||
```bash uv
|
||||
uv pip install agentops mem0ai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Environment Configuration
|
||||
|
||||
Load environment variables and set up API keys. The MEM0_API_KEY is only required if you're using the cloud-based MemoryClient.
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Tracking Memory Operations
|
||||
|
||||
<CodeGroup>
|
||||
```python Local Memory
|
||||
import agentops
|
||||
from mem0 import Memory
|
||||
|
||||
# Start a trace to group related operations
|
||||
agentops.start_trace("user_preference_learning",tags=["mem0_memory_example"])
|
||||
|
||||
try:
|
||||
# Initialize Memory - AgentOps tracks the configuration
|
||||
memory = Memory.from_config({
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4o-mini",
|
||||
"temperature": 0.1
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
# Add memories - AgentOps tracks each operation
|
||||
memory.add(
|
||||
"I prefer morning meetings and dark roast coffee",
|
||||
user_id="user_123",
|
||||
metadata={"category": "preferences"}
|
||||
)
|
||||
|
||||
# Search memories - AgentOps tracks search queries and results
|
||||
results = memory.search(
|
||||
"What are the user's meeting preferences?",
|
||||
user_id="user_123"
|
||||
)
|
||||
|
||||
# End trace - AgentOps aggregates all operations
|
||||
agentops.end_trace(end_state="success")
|
||||
|
||||
except Exception as e:
|
||||
agentops.end_trace(end_state="error")
|
||||
```
|
||||
|
||||
```python Cloud Memory
|
||||
import agentops
|
||||
from mem0 import MemoryClient
|
||||
|
||||
# Start trace for cloud operations
|
||||
agentops.start_trace("cloud_memory_sync",tags=["mem0_memoryclient_example"])
|
||||
|
||||
try:
|
||||
# Initialize MemoryClient - AgentOps tracks API authentication
|
||||
client = MemoryClient(api_key="your_mem0_api_key")
|
||||
|
||||
# Batch add memories - AgentOps tracks bulk operations
|
||||
messages = [
|
||||
{"role": "user", "content": "I work in software engineering"},
|
||||
{"role": "user", "content": "I prefer Python over Java"},
|
||||
]
|
||||
|
||||
client.add(messages, user_id="user_123")
|
||||
|
||||
# Search with filters - AgentOps tracks complex queries
|
||||
filters = {"AND": [{"user_id": "user_123"}]}
|
||||
results = client.search(
|
||||
query="What programming languages does the user know?",
|
||||
filters=filters,
|
||||
version="v2"
|
||||
)
|
||||
|
||||
# End trace - AgentOps aggregates all operations
|
||||
agentops.end_trace(end_state="success")
|
||||
|
||||
except Exception as e:
|
||||
agentops.end_trace(end_state="error")
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## What You'll See in AgentOps
|
||||
|
||||
When using Mem0 with AgentOps, your dashboard will show:
|
||||
|
||||
1. **Memory Operation Timeline**: Visual flow of all memory operations
|
||||
2. **Search Analytics**: Query patterns and retrieval effectiveness
|
||||
3. **Memory Growth**: Track how user memories accumulate over time
|
||||
4. **Performance Metrics**: Latency for adds, searches, and retrievals
|
||||
5. **Error Tracking**: Failed operations with full error context
|
||||
6. **Cost Attribution**: Token usage for memory extraction and searches
|
||||
|
||||
## Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Memory Operations" icon="book" href="/v2/examples/mem0">
|
||||
Simple example showing memory storage and retrieval with AgentOps tracking
|
||||
</Card>
|
||||
|
||||
<Card title="MemoryClient Operations" icon="cloud" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/mem0/mem0_memoryclient_example.ipynb">
|
||||
Track concurrent memory operations with async/await patterns
|
||||
</Card>
|
||||
|
||||
</CardGroup>
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,125 @@
|
||||
---
|
||||
title: 'Memori'
|
||||
description: 'Track and monitor Memori memory operations with AgentOps'
|
||||
---
|
||||
|
||||
[Memori](https://github.com/GibsonAI/memori) provides automatic short-term and long-term memory for AI applications and agents, seamlessly recording conversations and adding context to LLM interactions without requiring explicit memory management.
|
||||
|
||||
## Why Track Memori with AgentOps?
|
||||
|
||||
- **Memory Recording**: Track when conversations are automatically captured and stored
|
||||
- **Context Injection**: Monitor how memory is automatically added to LLM context
|
||||
- **Conversation Flow**: Understand the complete dialogue history across sessions
|
||||
- **Memory Effectiveness**: Analyze how historical context improves response quality
|
||||
- **Performance Impact**: Track latency and token usage from memory operations
|
||||
- **Error Tracking**: Identify issues with memory recording or context retrieval
|
||||
|
||||
AgentOps automatically instruments Memori to provide complete observability of your memory operations.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops memorisdk openai python-dotenv
|
||||
```
|
||||
|
||||
```bash poetry
|
||||
poetry add agentops memorisdk openai python-dotenv
|
||||
```
|
||||
|
||||
```bash uv
|
||||
uv pip install agentops memorisdk openai python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Environment Configuration
|
||||
|
||||
Load environment variables and set up API keys.
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Tracking Automatic Memory Operations
|
||||
|
||||
<CodeGroup>
|
||||
```python Basic Memory Tracking
|
||||
import agentops
|
||||
from memori import Memori
|
||||
from openai import OpenAI
|
||||
|
||||
# Start a trace to group related operations
|
||||
agentops.start_trace("memori_conversation_flow", tags=["memori_memory_example"])
|
||||
|
||||
try:
|
||||
# Initialize OpenAI client
|
||||
openai_client = OpenAI()
|
||||
|
||||
# Initialize Memori with conscious ingestion enabled
|
||||
# AgentOps tracks the memory configuration
|
||||
memori = Memori(
|
||||
database_connect="sqlite:///agentops_example.db",
|
||||
conscious_ingest=True,
|
||||
auto_ingest=True,
|
||||
)
|
||||
|
||||
memori.enable()
|
||||
|
||||
# First conversation - AgentOps tracks LLM call and memory recording
|
||||
response1 = openai_client.chat.completions.create(
|
||||
model="gpt-4o-mini",
|
||||
messages=[
|
||||
{"role": "user", "content": "I'm working on a Python FastAPI project"}
|
||||
],
|
||||
)
|
||||
|
||||
print("Assistant:", response1.choices[0].message.content)
|
||||
|
||||
# Second conversation - AgentOps tracks memory retrieval and context injection
|
||||
response2 = openai_client.chat.completions.create(
|
||||
model="gpt-4o-mini",
|
||||
messages=[{"role": "user", "content": "Help me add user authentication"}],
|
||||
)
|
||||
|
||||
print("Assistant:", response2.choices[0].message.content)
|
||||
print("💡 Notice: Memori automatically provided FastAPI project context!")
|
||||
|
||||
# End trace - AgentOps aggregates all operations
|
||||
agentops.end_trace(end_state="success")
|
||||
|
||||
except Exception as e:
|
||||
agentops.end_trace(end_state="error")
|
||||
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## What You'll See in AgentOps
|
||||
|
||||
When using Memori with AgentOps, your dashboard will show:
|
||||
|
||||
1. **Conversation Timeline**: Complete flow of all conversations with memory context
|
||||
2. **Memory Injection Analytics**: Track when and how much context is automatically added
|
||||
3. **Context Relevance**: Monitor the effectiveness of automatic memory retrieval
|
||||
4. **Performance Metrics**: Latency impact of memory operations on LLM calls
|
||||
5. **Token Usage**: Track additional tokens consumed by memory context
|
||||
6. **Memory Growth**: Visualize how conversation history accumulates over time
|
||||
7. **Error Tracking**: Failed memory operations with full error context
|
||||
|
||||
## Key Benefits of Memori + AgentOps
|
||||
|
||||
- **Zero-Effort Memory**: Memori automatically handles conversation recording
|
||||
- **Intelligent Context**: Only relevant memory is injected into LLM context
|
||||
- **Complete Visibility**: AgentOps tracks all automatic memory operations
|
||||
- **Performance Monitoring**: Understand the cost/benefit of automatic memory
|
||||
- **Debugging Support**: Full traceability of memory decisions and context injection
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,214 @@
|
||||
---
|
||||
title: OpenAI
|
||||
description: "Track and analyze your OpenAI API calls with AgentOps"
|
||||
---
|
||||
|
||||
AgentOps seamlessly integrates with [OpenAI's Python SDK](https://github.com/openai/openai-python), allowing you to track and analyze all your OpenAI API calls automatically.
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops openai
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops openai
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops openai
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using OpenAI with AgentOps, you need to set up your API keys. You can obtain:
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
- **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 OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
OPENAI_API_KEY="your_openai_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["OPENAI_API_KEY"] = os.getenv("OPENAI_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 OpenAI API calls:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create OpenAI client
|
||||
client = OpenAI()
|
||||
|
||||
# Make API calls as usual - AgentOps will track them automatically
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful assistant."},
|
||||
{"role": "user", "content": "What is the capital of France?"}
|
||||
]
|
||||
)
|
||||
|
||||
print(response.choices[0].message.content)
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
<CodeGroup>
|
||||
```python Streaming
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create OpenAI client
|
||||
client = OpenAI()
|
||||
|
||||
# Make a streaming API call
|
||||
stream = client.chat.completions.create(
|
||||
model="gpt-4o-mini",
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful assistant."},
|
||||
{"role": "user", "content": "Write a short poem about AI."}
|
||||
],
|
||||
stream=True
|
||||
)
|
||||
|
||||
# Process the streaming response
|
||||
for chunk in stream:
|
||||
if chunk.choices[0].delta.content is not None:
|
||||
print(chunk.choices[0].delta.content, end="")
|
||||
```
|
||||
|
||||
```python Function Calling
|
||||
import json
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create OpenAI client
|
||||
client = OpenAI()
|
||||
|
||||
# Define tools
|
||||
tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"description": "Get the current weather in a given location",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {
|
||||
"type": "string",
|
||||
"description": "The city and state, e.g. San Francisco, CA",
|
||||
}
|
||||
},
|
||||
"required": ["location"],
|
||||
},
|
||||
},
|
||||
}
|
||||
]
|
||||
|
||||
# Function implementation
|
||||
def get_weather(location):
|
||||
return json.dumps({"location": location, "temperature": "72", "unit": "fahrenheit", "forecast": ["sunny", "windy"]})
|
||||
|
||||
# Make a function call API request
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a helpful weather assistant."},
|
||||
{"role": "user", "content": "What's the weather like in Boston?"}
|
||||
]
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=messages,
|
||||
tools=tools,
|
||||
tool_choice="auto",
|
||||
)
|
||||
|
||||
# Process response
|
||||
response_message = response.choices[0].message
|
||||
messages.append({"role": "assistant", "content": response_message.content, "tool_calls": response_message.tool_calls})
|
||||
|
||||
if response_message.tool_calls:
|
||||
# Process each tool call
|
||||
for tool_call in response_message.tool_calls:
|
||||
function_name = tool_call.function.name
|
||||
function_args = json.loads(tool_call.function.arguments)
|
||||
|
||||
if function_name == "get_weather":
|
||||
function_response = get_weather(function_args.get("location"))
|
||||
|
||||
# Add tool response to messages
|
||||
messages.append(
|
||||
{
|
||||
"role": "tool",
|
||||
"tool_call_id": tool_call.id,
|
||||
"name": function_name,
|
||||
"content": function_response,
|
||||
}
|
||||
)
|
||||
|
||||
# Get a new response from the model
|
||||
second_response = client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
print(second_response.choices[0].message.content)
|
||||
else:
|
||||
print(response_message.content)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## More Examples
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Multi-Tool Orchestration" icon="notebook" href="/v2/examples/openai">
|
||||
Advanced multi-tool RAG example
|
||||
</Card>
|
||||
<Card title="Async OpenAI Example" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/openai/openai_example_async.ipynb" newTab={true}>
|
||||
Demonstrates asynchronous calls with the OpenAI SDK.
|
||||
</Card>
|
||||
<Card title="Sync OpenAI Example" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/openai/openai_example_sync.ipynb" newTab={true}>
|
||||
Shows synchronous calls with the OpenAI SDK.
|
||||
</Card>
|
||||
<Card title="Web Search Example" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/openai/web_search.ipynb" newTab={true}>
|
||||
Example of integrating web search capabilities.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: OpenAI Agents JS
|
||||
description: 'AgentOps integration with the OpenAI Agents SDK for TypeScript/JavaScript.'
|
||||
---
|
||||
|
||||
[OpenAI Agents JS](https://github.com/openai/openai-agents-js) is a lightweight yet powerful SDK for building multi-agent workflows in TypeScript. AgentOps seamlessly integrates to provide observability into these workflows.
|
||||
|
||||
- [OpenAI Agents JS documentation](https://openai.github.io/openai-agents-js)
|
||||
- [Python guide](/v2/integrations/openai_agents_python)
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install agentops @openai/agents
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```typescript
|
||||
import { agentops } from 'agentops';
|
||||
import { Agent, run } from '@openai/agents';
|
||||
|
||||
await agentops.init();
|
||||
|
||||
const agent = new Agent({
|
||||
name: 'Assistant',
|
||||
instructions: 'You are a helpful assistant.'
|
||||
});
|
||||
|
||||
const result = await run(agent, 'Hello, world!');
|
||||
console.log(result.finalOutput);
|
||||
```
|
||||
|
||||
<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>
|
||||
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,181 @@
|
||||
---
|
||||
title: OpenAI Agents SDK
|
||||
description: 'AgentOps and OpenAI Agents SDK integration for powerful multi-agent workflow monitoring.'
|
||||
---
|
||||
|
||||
## Video Tutorial
|
||||
|
||||
<iframe
|
||||
width="560"
|
||||
height="315"
|
||||
src="https://www.youtube.com/embed/9FH-BYqYYrQ"
|
||||
title="AgentOps + OpenAI Agents SDK Integration Tutorial"
|
||||
frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
||||
allowfullscreen
|
||||
></iframe>
|
||||
|
||||
[OpenAI Agents Python](https://github.com/openai/openai-agents-python) is a lightweight yet powerful SDK for building multi-agent workflows in Python. AgentOps seamlessly integrates to provide observability into these workflows.
|
||||
|
||||
- [OpenAI Agents Python documentation](https://openai.github.io/openai-agents-python/)
|
||||
- [TypeScript guide](/v2/integrations/openai_agents_js)
|
||||
|
||||
## Core Concepts
|
||||
|
||||
- **Agents**: LLMs configured with instructions, tools, guardrails, and handoffs
|
||||
- **Handoffs**: Allow agents to transfer control to other agents for specific tasks
|
||||
- **Guardrails**: Configurable safety checks for input and output validation
|
||||
- **Tracing**: Built-in tracking of agent runs, allowing you to view, debug and optimize your workflows
|
||||
|
||||
## Python
|
||||
|
||||
### Installation
|
||||
|
||||
Install AgentOps, the OpenAI Agents SDK, and `python-dotenv` for managing API keys:
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops openai-agents python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops openai-agents python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops openai-agents python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Setting Up API Keys
|
||||
|
||||
Before using the OpenAI Agents SDK with AgentOps, you need to set up your API keys:
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
|
||||
|
||||
You can set these as environment variables or in a `.env` file.
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then load them in your Python code:
|
||||
```python
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
AgentOps will automatically instrument the OpenAI Agents SDK after being initialized. You can then create agents, run them, and track their interactions.
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agents import Agent, Runner
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create an agent with instructions
|
||||
agent = Agent(name="Assistant", instructions="You are a helpful assistant")
|
||||
|
||||
result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
|
||||
print(result.final_output)
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
<CodeGroup>
|
||||
```python Handoffs
|
||||
from agents import Agent, Runner
|
||||
import asyncio
|
||||
import agentops
|
||||
import os
|
||||
|
||||
agentops.init()
|
||||
|
||||
spanish_agent = Agent(
|
||||
name="Spanish agent",
|
||||
instructions="You only speak Spanish.",
|
||||
)
|
||||
|
||||
english_agent = Agent(
|
||||
name="English agent",
|
||||
instructions="You only speak English",
|
||||
)
|
||||
|
||||
triage_agent = Agent(
|
||||
name="Triage agent",
|
||||
instructions="Handoff to the appropriate agent based on the language of the request.",
|
||||
handoffs=[spanish_agent, english_agent],
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
|
||||
print(result.final_output)
|
||||
# Expected Output: ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
```python Function Calling
|
||||
import asyncio
|
||||
from agents import Agent, Runner, function_tool
|
||||
import agentops
|
||||
import os
|
||||
|
||||
agentops.init()
|
||||
|
||||
@function_tool
|
||||
def get_weather(city: str) -> str:
|
||||
return f"The weather in {city} is sunny."
|
||||
|
||||
|
||||
agent = Agent(
|
||||
name="Weather Agent",
|
||||
instructions="You are a helpful agent that can get weather information.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
result = await Runner.run(agent, input="What's the weather in Tokyo?")
|
||||
print(result.final_output)
|
||||
# Expected Output: The weather in Tokyo is sunny.
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## More Examples
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Customer Service Agent" icon="notebook" href="/v2/examples/openai_agents">
|
||||
Demonstrates a customer service workflow
|
||||
</Card>
|
||||
<Card title="Agent Patterns" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/openai_agents/agent_patterns.ipynb" newTab={true}>
|
||||
Illustrates various agent interaction patterns.
|
||||
</Card>
|
||||
<Card title="Agents with Tools" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/openai_agents/agents_tools.ipynb" newTab={true}>
|
||||
Showcases agents utilizing different tools.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<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>
|
||||
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,238 @@
|
||||
---
|
||||
title: Smolagents
|
||||
description: "Track and analyze your Smolagents AI agents with AgentOps"
|
||||
---
|
||||
|
||||
AgentOps provides seamless integration with [Smolagents](https://github.com/huggingface/smolagents), HuggingFace's lightweight framework for building AI agents. Monitor your agent workflows, tool usage, and execution traces automatically.
|
||||
|
||||
## Core Concepts
|
||||
|
||||
Smolagents is designed around several key concepts:
|
||||
|
||||
- **Agents**: AI assistants that can use tools and reason through problems
|
||||
- **Tools**: Functions that agents can call to interact with external systems
|
||||
- **Models**: LLM backends that power agent reasoning (supports various providers via LiteLLM)
|
||||
- **Code Execution**: Agents can write and execute Python code in sandboxed environments
|
||||
- **Multi-Agent Systems**: Orchestrate multiple specialized agents working together
|
||||
|
||||
## Installation
|
||||
|
||||
Install AgentOps and Smolagents, along with any additional dependencies:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops smolagents python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops smolagents python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops smolagents python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using Smolagents with AgentOps, you need to set up your API keys:
|
||||
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
|
||||
- **LLM API Keys**: Depending on your chosen model provider (e.g., OPENAI_API_KEY, ANTHROPIC_API_KEY)
|
||||
|
||||
Set these as environment variables or in a `.env` file.
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Then load them in your Python code:
|
||||
```python
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Initialize AgentOps before creating your Smolagents to automatically track all agent interactions:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from smolagents import LiteLLMModel, ToolCallingAgent, DuckDuckGoSearchTool
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create a model (supports various providers via LiteLLM)
|
||||
model = LiteLLMModel("openai/gpt-4o-mini")
|
||||
|
||||
# Create an agent with tools
|
||||
agent = ToolCallingAgent(
|
||||
tools=[DuckDuckGoSearchTool()],
|
||||
model=model,
|
||||
)
|
||||
|
||||
# Run the agent
|
||||
result = agent.run("What are the latest developments in AI safety research?")
|
||||
print(result)
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
<CodeGroup>
|
||||
```python Simple Math Agent
|
||||
import agentops
|
||||
from smolagents import LiteLLMModel, CodeAgent
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create a model
|
||||
model = LiteLLMModel("openai/gpt-4o-mini")
|
||||
|
||||
# Create a code agent that can perform calculations
|
||||
agent = CodeAgent(
|
||||
tools=[], # No external tools needed for math
|
||||
model=model,
|
||||
additional_authorized_imports=["math", "numpy"],
|
||||
)
|
||||
|
||||
# Ask the agent to solve a math problem
|
||||
result = agent.run(
|
||||
"Calculate the compound interest on $10,000 invested at 5% annual rate "
|
||||
"for 10 years, compounded monthly. Show your work."
|
||||
)
|
||||
|
||||
print(result)
|
||||
```
|
||||
|
||||
```python Research Agent with Tools
|
||||
import agentops
|
||||
from smolagents import (
|
||||
LiteLLMModel,
|
||||
ToolCallingAgent,
|
||||
DuckDuckGoSearchTool,
|
||||
tool
|
||||
)
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create a custom tool
|
||||
@tool
|
||||
def word_counter(text: str) -> str:
|
||||
"""
|
||||
Counts the number of words in a given text.
|
||||
|
||||
Args:
|
||||
text: The text to count words in.
|
||||
|
||||
Returns:
|
||||
A string with the word count.
|
||||
"""
|
||||
word_count = len(text.split())
|
||||
return f"The text contains {word_count} words."
|
||||
|
||||
# Create model and agent
|
||||
model = LiteLLMModel("openai/gpt-4o-mini")
|
||||
|
||||
agent = ToolCallingAgent(
|
||||
tools=[DuckDuckGoSearchTool(), word_counter],
|
||||
model=model,
|
||||
)
|
||||
|
||||
# Run a research task
|
||||
result = agent.run(
|
||||
"Search for information about the James Webb Space Telescope's latest discoveries. "
|
||||
"Then count how many words are in your summary."
|
||||
)
|
||||
|
||||
print(result)
|
||||
```
|
||||
|
||||
```python Multi-Step Task Agent
|
||||
import agentops
|
||||
from smolagents import LiteLLMModel, CodeAgent, tool
|
||||
import json
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create tools for data processing
|
||||
@tool
|
||||
def save_json(data: dict, filename: str) -> str:
|
||||
"""
|
||||
Saves data to a JSON file.
|
||||
|
||||
Args:
|
||||
data: Dictionary to save
|
||||
filename: Name of the file to save to
|
||||
|
||||
Returns:
|
||||
Success message
|
||||
"""
|
||||
with open(filename, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
return f"Data saved to {filename}"
|
||||
|
||||
@tool
|
||||
def load_json(filename: str) -> dict:
|
||||
"""
|
||||
Loads data from a JSON file.
|
||||
|
||||
Args:
|
||||
filename: Name of the file to load from
|
||||
|
||||
Returns:
|
||||
The loaded data as a dictionary
|
||||
"""
|
||||
with open(filename, 'r') as f:
|
||||
return json.load(f)
|
||||
|
||||
# Create agent
|
||||
model = LiteLLMModel("openai/gpt-4o-mini")
|
||||
|
||||
agent = CodeAgent(
|
||||
tools=[save_json, load_json],
|
||||
model=model,
|
||||
additional_authorized_imports=["pandas", "datetime"],
|
||||
)
|
||||
|
||||
# Run a multi-step data processing task
|
||||
result = agent.run("""
|
||||
1. Create a dataset of 5 fictional employees with names, departments, and salaries
|
||||
2. Save this data to 'employees.json'
|
||||
3. Load the data back and calculate the average salary
|
||||
4. Find the highest paid employee
|
||||
5. Return a summary of your findings
|
||||
""")
|
||||
|
||||
print(result)
|
||||
```
|
||||
</CodeGroup>
|
||||
## More Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Multi-Agent System" icon="notebook" href="/v2/examples/smolagents" newTab={true}>
|
||||
Complex multi-agent web browsing system
|
||||
</Card>
|
||||
<Card title="Text to SQL Agent" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/smolagents/text_to_sql.ipynb" newTab={true}>
|
||||
Convert natural language queries to SQL
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
Visit your [AgentOps Dashboard](https://app.agentops.ai) to see detailed traces of your Smolagents executions, tool usage, and agent reasoning steps.
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,129 @@
|
||||
---
|
||||
title: xAI (Grok)
|
||||
description: "Track and analyze your xAI Grok API calls with AgentOps"
|
||||
---
|
||||
|
||||
AgentOps can track Grok. Grok is this true?
|
||||
|
||||
## Installation
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops openai
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops openai
|
||||
``
|
||||
```bash uv
|
||||
uv pip install agentops openai
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
Before using xAI with AgentOps, you need to set up your API keys. You can obtain:
|
||||
- **XAI_API_KEY**: From the [xAI Developer Platform](https://console.x.ai/)
|
||||
- **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 XAI_API_KEY="your_xai_api_key_here"
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
XAI_API_KEY="your_xai_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["XAI_API_KEY"] = os.getenv("XAI_API_KEY")
|
||||
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Initialize AgentOps at the beginning of your application. Then, use the OpenAI SDK with xAI's base URL to interact with Grok. AgentOps will automatically track all API calls.
|
||||
|
||||
<CodeGroup>
|
||||
```python Simple Chat
|
||||
import os
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create OpenAI client configured for xAI
|
||||
client = OpenAI(
|
||||
api_key=os.getenv("XAI_API_KEY"),
|
||||
base_url="https://api.x.ai/v1",
|
||||
)
|
||||
|
||||
# Basic chat completion
|
||||
completion = client.chat.completions.create(
|
||||
model="grok-3-latest",
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful AI assistant."},
|
||||
{"role": "user", "content": "Explain the concept of AI observability in simple terms."},
|
||||
],
|
||||
)
|
||||
|
||||
print(completion.choices[0].message.content)
|
||||
```
|
||||
|
||||
```python Streaming Chat
|
||||
import os
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init()
|
||||
|
||||
# Create OpenAI client configured for xAI
|
||||
client = OpenAI(
|
||||
api_key=os.getenv("XAI_API_KEY"),
|
||||
base_url="https://api.x.ai/v1",
|
||||
)
|
||||
|
||||
# Streaming chat completion
|
||||
stream = client.chat.completions.create(
|
||||
model="grok-3-latest",
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful AI assistant."},
|
||||
{"role": "user", "content": "Tell me about the latest developments in AI."},
|
||||
],
|
||||
stream=True,
|
||||
)
|
||||
|
||||
for chunk in stream:
|
||||
if chunk.choices[0].delta.content is not None:
|
||||
print(chunk.choices[0].delta.content, end="")
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Examples
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Grok Simple Example" icon="notebook" href="/v2/examples/xai">
|
||||
Basic usage patterns for Grok LLM
|
||||
</Card>
|
||||
<Card title="Grok Vision Example" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/xai/grok_vision_examples.ipynb" newTab={true}>
|
||||
Demonstrates using Grok with vision capabilities.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,266 @@
|
||||
---
|
||||
title: 'Xpander'
|
||||
description: 'Monitor and analyze your Xpander agent workflows with automatic AgentOps instrumentation'
|
||||
---
|
||||
|
||||
[Xpander](https://xpander.ai/) is a powerful platform for building and deploying AI agents with sophisticated workflow management capabilities. AgentOps provides seamless integration with the Xpander SDK, automatically instrumenting all agent activities, tool executions, and LLM interactions without any manual setup.
|
||||
|
||||
## Installation
|
||||
|
||||
Install AgentOps and the Xpander SDK, along with the required dependencies:
|
||||
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops xpander-sdk xpander-utils openai python-dotenv loguru
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops xpander-sdk xpander-utils openai python-dotenv loguru
|
||||
```
|
||||
```bash uv
|
||||
uv add agentops xpander-sdk xpander-utils openai python-dotenv loguru
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Setting Up API Keys
|
||||
|
||||
You'll need API keys for AgentOps, Xpander, and OpenAI:
|
||||
- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)
|
||||
- **XPANDER_API_KEY**: From your [Xpander Dashboard](https://app.xpander.ai/)
|
||||
- **XPANDER_AGENT_ID**: The ID of your Xpander agent
|
||||
- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
|
||||
|
||||
Set these as environment variables or in a `.env` file:
|
||||
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
export XPANDER_API_KEY="your_xpander_api_key_here"
|
||||
export XPANDER_AGENT_ID="your_xpander_agent_id_here"
|
||||
export OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
XPANDER_API_KEY="your_xpander_api_key_here"
|
||||
XPANDER_AGENT_ID="your_xpander_agent_id_here"
|
||||
OPENAI_API_KEY="your_openai_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
You can also store your configuration in a `xpander_config.json` file:
|
||||
|
||||
```json
|
||||
{
|
||||
"api_key": "your_xpander_api_key_here",
|
||||
"agent_id": "your_xpander_agent_id_here"
|
||||
}
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
The key to AgentOps + Xpander integration is **initialization order**: Initialize AgentOps **before** importing the Xpander SDK to enable automatic instrumentation.
|
||||
|
||||
<Note>
|
||||
The following example shows the callback-based integration pattern. For a complete working example, see our [Xpander example](/v2/examples/xpander).
|
||||
</Note>
|
||||
|
||||
```python
|
||||
# ruff: noqa: E402
|
||||
import os
|
||||
import json
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables first
|
||||
load_dotenv()
|
||||
|
||||
# 1. Initialize AgentOps FIRST (this enables auto-instrumentation)
|
||||
import agentops
|
||||
agentops.init(
|
||||
api_key=os.getenv("AGENTOPS_API_KEY"),
|
||||
trace_name="my-xpander-coding-agent-callbacks",
|
||||
default_tags=["xpander", "coding-agent", "callbacks"],
|
||||
)
|
||||
|
||||
# 2. Now import Xpander SDK (instrumentation will automatically activate)
|
||||
from xpander_sdk import XpanderClient, LLMProvider, LLMTokens, Tokens, Agent, ExecutionStatus
|
||||
from xpander_utils.events import XpanderEventListener, AgentExecutionResult, AgentExecution
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
class MyAgent:
|
||||
def __init__(self):
|
||||
# Load config
|
||||
config_path = Path(__file__).parent / "xpander_config.json"
|
||||
config = json.loads(config_path.read_text())
|
||||
|
||||
# Get API keys
|
||||
xpander_key = config.get("api_key") or os.getenv("XPANDER_API_KEY")
|
||||
agent_id = config.get("agent_id") or os.getenv("XPANDER_AGENT_ID")
|
||||
openai_key = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
# Initialize clients
|
||||
self.openai = AsyncOpenAI(api_key=openai_key)
|
||||
xpander_client = XpanderClient(api_key=xpander_key)
|
||||
self.agent_backend: Agent = xpander_client.agents.get(agent_id=agent_id)
|
||||
self.agent_backend.select_llm_provider(LLMProvider.OPEN_AI)
|
||||
|
||||
async def run(self, user_input: str) -> dict:
|
||||
tokens = Tokens(worker=LLMTokens(0, 0, 0))
|
||||
|
||||
while not self.agent_backend.is_finished():
|
||||
# Call LLM
|
||||
response = await self.openai.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=self.agent_backend.messages,
|
||||
tools=self.agent_backend.get_tools(),
|
||||
tool_choice=self.agent_backend.tool_choice,
|
||||
temperature=0,
|
||||
)
|
||||
|
||||
# Track tokens
|
||||
if hasattr(response, "usage"):
|
||||
tokens.worker.prompt_tokens += response.usage.prompt_tokens
|
||||
tokens.worker.completion_tokens += response.usage.completion_tokens
|
||||
tokens.worker.total_tokens += response.usage.total_tokens
|
||||
|
||||
# Add response to agent context
|
||||
self.agent_backend.add_messages(response.model_dump())
|
||||
self.agent_backend.report_execution_metrics(llm_tokens=tokens, ai_model="gpt-4")
|
||||
|
||||
# Execute any tool calls
|
||||
tool_calls = self.agent_backend.extract_tool_calls(response.model_dump())
|
||||
if tool_calls:
|
||||
tool_results = await asyncio.to_thread(self.agent_backend.run_tools, tool_calls)
|
||||
|
||||
result = self.agent_backend.retrieve_execution_result()
|
||||
return {"result": result.result, "thread_id": result.memory_thread_id}
|
||||
|
||||
# Set up event listener with callback handlers
|
||||
listener = XpanderEventListener(
|
||||
api_key=os.getenv("XPANDER_API_KEY"),
|
||||
agent_id=os.getenv("XPANDER_AGENT_ID")
|
||||
)
|
||||
|
||||
async def on_execution_request(execution_task: AgentExecution) -> AgentExecutionResult:
|
||||
agent = MyAgent()
|
||||
agent.agent_backend.init_task(execution=execution_task.model_dump())
|
||||
|
||||
try:
|
||||
await agent.run(execution_task.input.text)
|
||||
execution_result = agent.agent_backend.retrieve_execution_result()
|
||||
return AgentExecutionResult(
|
||||
result=execution_result.result,
|
||||
is_success=execution_result.status == ExecutionStatus.COMPLETED,
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
raise
|
||||
|
||||
# Register the callback
|
||||
listener.register(on_execution_request=on_execution_request)
|
||||
```
|
||||
|
||||
## What's Automatically Tracked
|
||||
|
||||
AgentOps automatically captures comprehensive telemetry from your Xpander agents:
|
||||
|
||||
### 🤖 Agent Activities
|
||||
- Agent initialization and configuration
|
||||
- Task lifecycle (start, execution steps, completion)
|
||||
- Workflow phase transitions (planning → executing → finished)
|
||||
- Session management and context persistence
|
||||
|
||||
### 🧠 LLM Interactions
|
||||
- All OpenAI API calls with full request/response data
|
||||
- Token usage and cost tracking across models
|
||||
- Conversation history and context management
|
||||
- Model parameters and settings
|
||||
|
||||
### 🛠️ Tool Executions
|
||||
- Tool call detection with parameters and arguments
|
||||
- Tool execution results and success/failure status
|
||||
- Tool performance metrics and timing
|
||||
- Tool call hierarchies and dependencies
|
||||
|
||||
### 📊 Performance Metrics
|
||||
- End-to-end execution duration and timing
|
||||
- Step-by-step workflow progression
|
||||
- Resource utilization and efficiency metrics
|
||||
- Error handling and exception tracking
|
||||
|
||||
## Key Features
|
||||
|
||||
### ✅ Zero-Configuration Setup
|
||||
No manual trace creation or span management required. Simply initialize AgentOps before importing Xpander SDK.
|
||||
|
||||
### ✅ Complete Workflow Visibility
|
||||
Track the entire agent execution flow from task initiation to completion, including all intermediate steps.
|
||||
|
||||
### ✅ Real-time Monitoring
|
||||
View your agent activities in real-time on the AgentOps dashboard as they execute.
|
||||
|
||||
### ✅ Tool Execution Insights
|
||||
Monitor which tools are being called, their parameters, execution time, and results.
|
||||
|
||||
### ✅ Cost Tracking
|
||||
Automatic token usage tracking for all LLM interactions with cost analysis.
|
||||
|
||||
## Callback Handler Pattern
|
||||
|
||||
The Xpander integration supports two main patterns:
|
||||
|
||||
1. **Direct Integration**: Directly instrument your agent code (shown above)
|
||||
2. **Callback Handler**: Use XpanderEventListener for webhook-style integration
|
||||
|
||||
The callback handler pattern is particularly useful for:
|
||||
- Production deployments with centralized monitoring
|
||||
- Multi-agent orchestration systems
|
||||
- Event-driven architectures
|
||||
|
||||
## Runtime-Specific Instrumentation
|
||||
|
||||
Xpander SDK uses JSII to create methods at runtime, which requires specialized instrumentation. AgentOps handles this automatically by:
|
||||
|
||||
- **Method Wrapping**: Dynamically wrapping agent methods as they're created
|
||||
- **Context Persistence**: Maintaining session context across runtime object lifecycle
|
||||
- **Agent Detection**: Automatically detecting and instrumenting new agent instances
|
||||
- **Tool Result Extraction**: Properly extracting results from JSII object references
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Import Order Issues
|
||||
If you're not seeing traces, ensure AgentOps is initialized before importing Xpander SDK:
|
||||
|
||||
```python
|
||||
# ✅ Correct order
|
||||
import agentops
|
||||
agentops.init()
|
||||
from xpander_sdk import XpanderClient
|
||||
|
||||
# ❌ Incorrect order
|
||||
from xpander_sdk import XpanderClient
|
||||
import agentops
|
||||
agentops.init() # Too late - instrumentation won't activate
|
||||
```
|
||||
|
||||
### Missing Tool Results
|
||||
If tool results show `{"__jsii_ref__": "..."}` instead of actual content, ensure you're using the latest version of AgentOps, which includes improved JSII object handling.
|
||||
|
||||
### Import Errors (E402)
|
||||
If you see linting errors about imports not being at the top of the file, this is expected for Xpander integration. Add `# ruff: noqa: E402` at the top of your file to suppress these warnings, as the import order is required for proper instrumentation.
|
||||
|
||||
## Examples
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Coding Agent Example" icon="robot" href="/v2/examples/xpander">
|
||||
Complete single-file implementation with callback handlers
|
||||
</Card>
|
||||
<Card title="GitHub Repository" icon="github" href="https://github.com/AgentOps-AI/agentops/tree/main/examples/xpander" newTab={true}>
|
||||
View the complete source code and configuration files
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<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>
|
||||
@@ -0,0 +1,136 @@
|
||||
---
|
||||
title: "Introduction"
|
||||
description: "AgentOps is the developer favorite platform for testing, debugging, and deploying AI agents and LLM apps."
|
||||
---
|
||||
<iframe
|
||||
width="100%"
|
||||
height="300"
|
||||
src="https://www.youtube.com/embed/EU_A3_ux09s?si=I05b3Jf5eufCy3R-"
|
||||
title="AgentOps Introduction"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
||||
allowFullScreen
|
||||
></iframe>
|
||||
|
||||
<Note title="Chat with the docs in your IDE">
|
||||
Prefer asking your IDE? Install the Mintlify <a href="/v2/usage/mcp-docs"><strong>MCP Docs Server</strong></a> for AgentOps to chat with the docs while you code:
|
||||
`npx mint-mcp add agentops`
|
||||
</Note>
|
||||
|
||||
|
||||
<Note title="Open Source">
|
||||
The AgentOps app is open source. Browse the code or contribute in our <a href="https://github.com/AgentOps-AI/agentops/tree/main/app">GitHub app directory</a>.
|
||||
</Note>
|
||||
|
||||
|
||||
## Integrate with developer favorite LLM providers and agent frameworks
|
||||
|
||||
### Agent Frameworks
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="AG2" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/ag2/ag2-logo.svg?raw=true" alt="AG2"/>} iconType="image" href="/v2/integrations/ag2" />
|
||||
<Card title="Agno" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/agno/agno.png?raw=true" alt="Agno" />} iconType="image" href="/v2/integrations/agno" />
|
||||
<Card title="AutoGen" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/autogen/autogen-logo.svg?raw=true" alt="AutoGen" />} iconType="image" href="/v2/integrations/autogen" />
|
||||
<Card title="CrewAI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/crewai/crewai-logo.png?raw=true" alt="CrewAI" />} iconType="image" href="/v2/integrations/crewai" />
|
||||
<Card title="Google ADK" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/google-adk/google-adk-logo.png?raw=true" alt="Google ADK" />} iconType="image" href="/v2/integrations/google_adk" />
|
||||
<Card title="Haystack" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/haystack/haystack-logo.png?raw=true" alt="Haystack" />} iconType="image" href="/v2/integrations/haystack" />
|
||||
<Card title="LangChain" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/langchain/langchain-logo.svg?raw=true" alt="LangChain" />} iconType="image" href="/v2/integrations/langchain" />
|
||||
<Card title="OpenAI Agents (Python)" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/openai/openai-logomark.png?raw=true" alt="OpenAI Agents Python" />} iconType="image" href="/v2/integrations/openai_agents_python" />
|
||||
<Card title="OpenAI Agents (TypeScript)" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/openai/openai-logomark.png?raw=true" alt="OpenAI Agents JS" />} iconType="image" href="/v2/integrations/openai_agents_js" />
|
||||
<Card title="Smolagents" icon={<img src="https://github.com/AgentOps-AI/agentops/blob/main/docs/images/external/huggingface/hf-logo.png?raw=true" alt="smolagents" />} iconType="image" href="/v2/integrations/smolagents" />
|
||||
</CardGroup>
|
||||
|
||||
### LLM Providers
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Anthropic" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/anthropic/anthropic_icon_slate.png?raw=true" alt="Anthropic" />} iconType="image" href="/v2/integrations/anthropic" />
|
||||
<Card title="Google Generative AI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/deepmind/gemini-logo.png?raw=true" alt="Gemini" />} iconType="image" href="/v2/integrations/google_generative_ai" />
|
||||
<Card title="OpenAI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/openai/openai-logomark.png?raw=true" alt="OpenAI" />} iconType="image" href="/v2/integrations/openai" />
|
||||
<Card title="LiteLLM" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/litellm/litellm.png?raw=true" alt="LiteLLM" />} iconType="image" href="/v2/integrations/litellm" />
|
||||
<Card title="Watsonx" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/ibm/ibm-logo.svg?raw=true" alt="IBM" />} iconType="image" href="/v2/integrations/ibm_watsonx_ai" />
|
||||
<Card title="x.AI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/xai/xai-logo.png?raw=true" alt="x.AI" />} iconType="image" href="/v2/integrations/xai" />
|
||||
<Card title="Mem0" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/mem0/mem0.png?raw=true" alt="Mem0" />} iconType="image" href="/v2/integrations/mem0" />
|
||||
<Card title="Memori" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/gibsonai/gibsonai.png?raw=true" alt="Memori" />} iconType="image" href="/v2/integrations/memori" />
|
||||
</CardGroup>
|
||||
|
||||
Observability and monitoring for your AI agents and LLM apps. And we do it all in just two lines of code...
|
||||
<CodeGroup>
|
||||
```python python
|
||||
import agentops
|
||||
agentops.init(<INSERT YOUR API KEY HERE>)
|
||||
```
|
||||
</CodeGroup>
|
||||
... that logs everything back to your AgentOps Dashboard.
|
||||
|
||||
<Note>
|
||||
AgentOps is also available for TypeScript/JavaScript applications. Check out our [TypeScript SDK guide](/v2/usage/typescript-sdk) for Node.js projects.
|
||||
</Note>
|
||||
|
||||
That's it! AgentOps will automatically instrument your code and start tracking traces.
|
||||
|
||||
Need more control? You can create custom traces using the `@trace` decorator (recommended) or manage traces manually for advanced use cases:
|
||||
|
||||
<CodeGroup>
|
||||
```python python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import trace
|
||||
|
||||
agentops.init(<INSERT YOUR API KEY HERE>, auto_start_session=False)
|
||||
|
||||
@trace(name="my-workflow", tags=["production"])
|
||||
def my_workflow():
|
||||
# Your code here
|
||||
return "Workflow completed"
|
||||
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
You can also set a custom trace name during initialization:
|
||||
|
||||
<CodeGroup>
|
||||
```python python
|
||||
import agentops
|
||||
agentops.init(<INSERT YOUR API KEY HERE>, trace_name="custom-trace-name")
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## The AgentOps Dashboard
|
||||
|
||||
<Check>[Give us a star](https://github.com/AgentOps-AI/agentops) to bookmark on GitHub, save for later 🖇️)</Check>
|
||||
|
||||
With just two lines of code, you can free yourself from the chains of the terminal and, instead, visualize your agents' behavior
|
||||
in your AgentOps Dashboard. After setting up AgentOps, each execution of your program is recorded as a session and the above
|
||||
data is automatically recorded for you.
|
||||
|
||||
The examples below were captured with two lines of code.
|
||||
|
||||
### Session Drilldown
|
||||
Here you will find a list of all of your previously recorded sessions and useful data about each such as total execution time.
|
||||
You also get helpful debugging info such as any SDK versions you were on if you're building on a supported agent framework like Crew or AutoGen.
|
||||
LLM calls are presented as a familiar chat history view, and charts give you a breakdown of the types of events that were called and how long they took.
|
||||
|
||||
<Frame type="glass" caption="Session Summary">
|
||||
<img height="200" src="/images/session-summary.gif" />
|
||||
</Frame>
|
||||
|
||||
Find any past sessions from your Session Drawer.
|
||||
<Frame type="glass" caption="Session Drawer">
|
||||
<img height="250" src="/images/session-drawer.gif" />
|
||||
</Frame>
|
||||
|
||||
Most powerful of all is the Session Waterfall. On the left, a time visualization of all your LLM calls, Action events, Tool calls, and Errors.
|
||||
On the right, specific details about the event you've selected on the waterfall. For instance the exact prompt and completion for a given LLM call.
|
||||
Most of which has been automatically recorded for you.
|
||||
|
||||
<Frame type="glass" caption="Session Waterfall">
|
||||
<img height="200" src="/images/session-waterfall.gif" />
|
||||
</Frame>
|
||||
|
||||
|
||||
### Session Overview
|
||||
View a meta-analysis of all of your sessions in a single view.
|
||||
<Frame type="glass" caption="Session Overview">
|
||||
<img height="200" src="/images/overview.png" />
|
||||
</Frame>
|
||||
|
||||
<script type="module" src="/scripts/github_stars.js"></script>
|
||||
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,283 @@
|
||||
---
|
||||
title: "Quickstart"
|
||||
description: "Get started with AgentOps in minutes with just 2 lines of code for basic monitoring, and explore powerful decorators for custom tracing."
|
||||
---
|
||||
|
||||
AgentOps is designed for easy integration into your AI agent projects, providing powerful observability with minimal setup. This guide will get you started quickly.
|
||||
|
||||
<Check>[Give us a star on GitHub!](https://github.com/AgentOps-AI/agentops) Your support helps us grow. ⭐</Check>
|
||||
|
||||
<Note title="Open Source">
|
||||
The AgentOps app is open source—explore the code in our <a href="https://github.com/AgentOps-AI/agentops/tree/main/app">GitHub app directory</a>.
|
||||
</Note>
|
||||
|
||||
<Note title="Chat with the docs in your IDE">
|
||||
Prefer asking your IDE? Install the Mintlify <a href="/v2/usage/mcp-docs"><strong>MCP Docs Server</strong></a> for AgentOps to chat with the docs while you code:
|
||||
`npx mint-mcp add agentops`
|
||||
</Note>
|
||||
|
||||
## Installation
|
||||
First, install the AgentOps SDK. We recommend including `python-dotenv` for easy API key management.
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops python-dotenv
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops python-dotenv
|
||||
```
|
||||
```bash uv
|
||||
uv pip install agentops python-dotenv
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Initial Setup (2 Lines of Code)
|
||||
|
||||
At its simplest, AgentOps can start monitoring your supported LLM and agent framework calls with just two lines of Python code.
|
||||
|
||||
1. **Import AgentOps**: Add `import agentops` to your script.
|
||||
2. **Initialize AgentOps**: Call `agentops.init()` with your API key.
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import agentops
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables (recommended for API keys)
|
||||
load_dotenv()
|
||||
|
||||
# Initialize AgentOps
|
||||
# The API key can be passed directly or set as an environment variable AGENTOPS_API_KEY
|
||||
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
|
||||
agentops.init(AGENTOPS_API_KEY)
|
||||
|
||||
# That's it for basic auto-instrumentation!
|
||||
# If you're using a supported library (like OpenAI, LangChain, CrewAI, etc.),
|
||||
# AgentOps will now automatically track LLM calls and agent actions.
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Setting Your AgentOps API Key
|
||||
You need an AgentOps API key to send data to your dashboard.
|
||||
- Get your API key from the [AgentOps Dashboard](https://app.agentops.ai/settings/projects).
|
||||
|
||||
It's best practice to set your API key as an environment variable.
|
||||
<CodeGroup>
|
||||
```bash Export to CLI
|
||||
export AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
```txt Set in .env file
|
||||
AGENTOPS_API_KEY="your_agentops_api_key_here"
|
||||
```
|
||||
</CodeGroup>
|
||||
If you use a `.env` file, make sure `load_dotenv()` is called before `agentops.init()`.
|
||||
|
||||
## Running Your Agent & Viewing Traces
|
||||
|
||||
After adding the two lines and ensuring your API key is set up:
|
||||
1. Run your agent application as you normally would.
|
||||
2. AgentOps will automatically instrument supported libraries and send trace data.
|
||||
3. Visit your [AgentOps Dashboard](https://app.agentops.ai/traces) to observe your agent's operations!
|
||||
|
||||
|
||||
## Beyond Automatic Instrumentation: Decorators
|
||||
|
||||
While AgentOps automatically instruments many popular libraries, you can gain finer-grained control and track custom parts of your code using our powerful decorators. This allows you to define specific operations, group logic under named agents, track tool usage with costs, and create custom traces.
|
||||
|
||||
### Tracking Custom Operations with `@operation`
|
||||
Instrument any function in your code to create spans that track its execution, parameters, and return values. These operations will appear in your session visualization alongside LLM calls.
|
||||
```python
|
||||
from agentops.sdk.decorators import operation
|
||||
|
||||
@operation
|
||||
def process_data(data):
|
||||
# Your function logic here
|
||||
processed_result = data.upper()
|
||||
# agentops.record(Events("Processed Data", result=processed_result)) # Optional: record specific events
|
||||
return processed_result
|
||||
|
||||
# Example usage:
|
||||
# my_data = "example input"
|
||||
# output = process_data(my_data)
|
||||
```
|
||||
|
||||
### Tracking Agent Logic with `@agent`
|
||||
If you structure your system with specific named agents (e.g., classes), use the `@agent` decorator on the class and `@operation` on its methods to group all downstream operations under that agent's context.
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, operation
|
||||
|
||||
@agent(name="MyCustomAgent") # You can provide a name for the agent
|
||||
class MyAgent:
|
||||
def __init__(self, agent_id):
|
||||
self.agent_id = agent_id # agent_id is a reserved parameter for AgentOps
|
||||
|
||||
@operation
|
||||
def perform_task(self, task_description):
|
||||
# Agent task logic here
|
||||
# This could include LLM calls or calls to other @operation decorated functions
|
||||
return f"Agent {self.agent_id} completed: {task_description}"
|
||||
|
||||
# Example usage:
|
||||
# research_agent = MyAgent(agent_id="researcher-001")
|
||||
# result = research_agent.perform_task("Analyze market trends")
|
||||
```
|
||||
|
||||
### Tracking Tools with `@tool`
|
||||
Track the usage of specific tools or functions, and optionally associate costs with them. This data will be aggregated in your dashboard.
|
||||
```python
|
||||
from agentops.sdk.decorators import tool
|
||||
|
||||
@tool(name="WebSearchTool", cost=0.05) # Cost is optional
|
||||
def web_search(query: str) -> str:
|
||||
# Tool logic here
|
||||
return f"Search results for: {query}"
|
||||
|
||||
@tool # No cost specified
|
||||
def calculator(expression: str) -> str:
|
||||
try:
|
||||
return str(eval(expression))
|
||||
except Exception as e:
|
||||
return f"Error: {e}"
|
||||
|
||||
# Example usage:
|
||||
# search_result = web_search("AgentOps features")
|
||||
# calculation = calculator("2 + 2")
|
||||
```
|
||||
|
||||
### Grouping with Traces (`@trace` or manual)
|
||||
Create custom traces to group a sequence of operations or define logical units of work. You can use the `@trace` decorator or manage traces manually for more complex scenarios.
|
||||
<Note>If `auto_start_session=False` in `agentops.init()`, you must use `@trace` or `agentops.start_trace()` for any data to be recorded.</Note>
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import trace
|
||||
# Assuming MyAgent and web_search are defined as above
|
||||
|
||||
# Option 1: Using the @trace decorator
|
||||
@trace(name="MyMainWorkflow", tags=["main-flow"])
|
||||
def my_workflow_decorated(task_to_perform):
|
||||
# Your workflow code here
|
||||
main_agent = MyAgent(agent_id="workflow-agent") # Assuming MyAgent is defined
|
||||
result = main_agent.perform_task(task_to_perform)
|
||||
# Example of using a tool within the trace
|
||||
tool_result = web_search(f"details for {task_to_perform}") # Assuming web_search is defined
|
||||
return result, tool_result
|
||||
|
||||
# result_decorated = my_workflow_decorated("complex data processing")
|
||||
|
||||
# Option 2: Managing traces manually
|
||||
# import agentops # Already imported
|
||||
|
||||
# custom_trace = agentops.start_trace(name="MyManualWorkflow", tags=["manual-flow"])
|
||||
# try:
|
||||
# # Your code here
|
||||
# main_agent = MyAgent(agent_id="manual-workflow-agent") # Assuming MyAgent is defined
|
||||
# result = main_agent.perform_task("another complex task")
|
||||
# tool_result = web_search(f"info for {result}") # Assuming web_search is defined
|
||||
# agentops.end_trace(custom_trace, end_state="Success", end_prompt=f"Completed: {result}")
|
||||
# except Exception as e:
|
||||
# if custom_trace: # Ensure trace was started before trying to end it
|
||||
# agentops.end_trace(custom_trace, end_state="Fail", error_message=str(e))
|
||||
# raise
|
||||
```
|
||||
|
||||
### Updating Trace Metadata
|
||||
|
||||
You can also update metadata on running traces to add context or track progress:
|
||||
|
||||
```python
|
||||
from agentops import update_trace_metadata
|
||||
|
||||
# Update metadata during trace execution
|
||||
update_trace_metadata({
|
||||
"operation_name": "AI Agent Processing",
|
||||
"processing_stage": "data_validation",
|
||||
"records_processed": 1500,
|
||||
"user_id": "user_123",
|
||||
"tags": ["validation", "production"]
|
||||
})
|
||||
```
|
||||
|
||||
## Complete Example with Decorators
|
||||
|
||||
Here's a consolidated example showcasing how these decorators can work together:
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import agent, operation, tool, trace
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
|
||||
|
||||
# Initialize AgentOps.
|
||||
# Set auto_start_session=False because @trace will manage the session.
|
||||
agentops.init(AGENTOPS_API_KEY, auto_start_session=False, tags=["quickstart-complete-example"])
|
||||
|
||||
# Define a tool
|
||||
@tool(name="AdvancedSearch", cost=0.02)
|
||||
def advanced_web_search(query: str) -> str:
|
||||
# Simulate a more advanced search
|
||||
return f"Advanced search results for '{query}': [Details...]"
|
||||
|
||||
# Define an agent class
|
||||
@agent(name="ResearchSpecialistAgent")
|
||||
class ResearchAgent:
|
||||
def __init__(self, agent_id: str):
|
||||
self.agent_id = agent_id # This will be used as the agent_id in AgentOps
|
||||
|
||||
@operation(name="ConductResearch")
|
||||
def conduct_research(self, research_topic: str) -> str:
|
||||
# Use the tool within the agent's operation
|
||||
search_results = advanced_web_search(f"Deep dive into {research_topic}")
|
||||
# Simulate further processing
|
||||
analysis = f"Analysis of '{research_topic}': Based on '{search_results}', the key findings are..."
|
||||
return analysis
|
||||
|
||||
# Define a workflow using the @trace decorator
|
||||
@trace(name="FullResearchWorkflow", tags=["research", "analysis", "example"])
|
||||
def run_full_research_workflow(topic: str) -> str:
|
||||
specialist_agent = ResearchAgent(agent_id="researcher-alpha-007")
|
||||
research_findings = specialist_agent.conduct_research(topic)
|
||||
|
||||
final_report = f"Research Report for '{topic}':\n{research_findings}"
|
||||
# agentops.record(Events("ReportGenerated", details=final_report)) # Optional: record a custom event
|
||||
return final_report
|
||||
|
||||
# Execute the workflow
|
||||
final_output = run_full_research_workflow("AI in healthcare")
|
||||
print(final_output)
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
You've seen how to get started with AgentOps! Explore further to leverage its full potential:
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card
|
||||
title="Integrations"
|
||||
icon="plug"
|
||||
href="/v2/integrations"
|
||||
>
|
||||
See how AgentOps automatically instruments popular LLM and agent frameworks.
|
||||
</Card>
|
||||
<Card
|
||||
title="Examples"
|
||||
icon="square-code"
|
||||
href="/v2/examples"
|
||||
>
|
||||
Explore detailed examples for various use cases and integrations.
|
||||
</Card>
|
||||
<Card title="SDK Reference" icon="book-open" href="/v2/sdk-reference">
|
||||
Dive deeper into the AgentOps SDK capabilities and API.
|
||||
</Card>
|
||||
<Card title="Trace Decorator" icon="code" href="/v2/usage/trace-decorator">
|
||||
Learn how to group operations and create custom traces using the @trace decorator.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<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>
|
||||
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,294 @@
|
||||
---
|
||||
title: "Backend Setup Guide"
|
||||
description: "Complete guide for setting up and running AgentOps backend services"
|
||||
---
|
||||
|
||||
# Backend Setup Guide
|
||||
|
||||
This guide covers how to set up and run the AgentOps backend services from the `/app` directory. The backend includes the API server, dashboard, database services, and observability infrastructure.
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
The AgentOps backend consists of several interconnected services:
|
||||
|
||||
- **API Server** (`api/`) - FastAPI backend with authentication, billing, and data processing
|
||||
- **Dashboard** (`dashboard/`) - Next.js frontend for visualization and management
|
||||
- **Supabase** - Authentication and primary PostgreSQL database
|
||||
- **ClickHouse** - Analytics database for traces and metrics
|
||||
- **OpenTelemetry Collector** - Observability and trace collection
|
||||
- **Redis** (optional) - Caching and session storage
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before setting up the backend, ensure you have the following installed:
|
||||
|
||||
### Required Software
|
||||
- **Node.js** 18+ ([Download](https://nodejs.org/))
|
||||
- **Python** 3.12+ ([Download](https://www.python.org/downloads/))
|
||||
- **Docker & Docker Compose** ([Download](https://www.docker.com/get-started))
|
||||
- **Bun** (recommended) or npm ([Install Bun](https://bun.sh/))
|
||||
- **uv** (recommended for Python) ([Install uv](https://github.com/astral-sh/uv))
|
||||
- **Just** (optional, for convenience commands) ([Install Just](https://github.com/casey/just))
|
||||
|
||||
### External Services
|
||||
You'll need accounts and setup for these external services:
|
||||
|
||||
- **Supabase** - Database and authentication ([supabase.com](https://supabase.com))
|
||||
- **ClickHouse Cloud** - Analytics database ([clickhouse.com/cloud](https://clickhouse.com/cloud))
|
||||
- **Stripe** (optional) - Payment processing ([stripe.com](https://stripe.com))
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 1. Clone and Navigate
|
||||
```bash
|
||||
git clone https://github.com/AgentOps-AI/AgentOps.Next.git
|
||||
cd AgentOps.Next/app
|
||||
```
|
||||
|
||||
### 2. Environment Setup
|
||||
Copy and configure environment files:
|
||||
|
||||
```bash
|
||||
# Root environment (for Docker Compose)
|
||||
cp .env.example .env
|
||||
|
||||
# API environment
|
||||
cp api/.env.example api/.env
|
||||
|
||||
# Dashboard environment
|
||||
cp dashboard/.env.example dashboard/.env.local
|
||||
```
|
||||
|
||||
### 3. Install Dependencies
|
||||
```bash
|
||||
# Using Just (recommended)
|
||||
just install
|
||||
|
||||
# Or manually:
|
||||
bun install # Root dependencies
|
||||
uv pip install -r requirements-dev.txt # Python dev tools
|
||||
cd api && uv pip install -e . && cd .. # API dependencies
|
||||
cd dashboard && bun install && cd .. # Dashboard dependencies
|
||||
```
|
||||
|
||||
### 4. Configure External Services
|
||||
Update your `.env` files with your service credentials. See [External Services Configuration](#external-services-configuration) below.
|
||||
|
||||
### 5. Start Services
|
||||
```bash
|
||||
# Option 1: Using Docker Compose (recommended)
|
||||
docker-compose up -d
|
||||
|
||||
# Option 2: Using Just commands
|
||||
just api-run # Start API server
|
||||
just fe-run # Start dashboard (in another terminal)
|
||||
|
||||
# Option 3: Native development
|
||||
cd api && uv run python run.py # API server
|
||||
cd dashboard && bun dev # Dashboard (in another terminal)
|
||||
```
|
||||
|
||||
### 6. Verify Setup
|
||||
- **Dashboard**: http://localhost:3000
|
||||
- **API Documentation**: http://localhost:8000/redoc
|
||||
- **API Health Check**: http://localhost:8000/health
|
||||
|
||||
## External Services Configuration
|
||||
|
||||
### Supabase Setup
|
||||
1. Create a new project at [supabase.com](https://supabase.com)
|
||||
2. Go to Settings → API to get your keys
|
||||
3. Run the database migrations:
|
||||
```bash
|
||||
cd supabase
|
||||
npx supabase db push
|
||||
```
|
||||
4. Update your `.env` files with:
|
||||
```env
|
||||
NEXT_PUBLIC_SUPABASE_URL=https://your-project-id.supabase.co
|
||||
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
|
||||
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key
|
||||
SUPABASE_PROJECT_ID=your-project-id
|
||||
```
|
||||
|
||||
### ClickHouse Setup
|
||||
1. Sign up for [ClickHouse Cloud](https://clickhouse.com/cloud) or self-host
|
||||
2. Create a database and get connection details
|
||||
3. Run the ClickHouse migrations:
|
||||
```bash
|
||||
# Apply schema from clickhouse/schema_dump.sql
|
||||
```
|
||||
4. Update your `.env` files with:
|
||||
```env
|
||||
CLICKHOUSE_HOST=your-host.clickhouse.cloud
|
||||
CLICKHOUSE_PORT=8123
|
||||
CLICKHOUSE_USER=default
|
||||
CLICKHOUSE_PASSWORD=your-password
|
||||
CLICKHOUSE_DATABASE=your-database
|
||||
CLICKHOUSE_SECURE=true
|
||||
```
|
||||
|
||||
### Stripe Setup (Optional)
|
||||
For billing functionality:
|
||||
1. Create a [Stripe](https://stripe.com) account
|
||||
2. Get your API keys from the dashboard
|
||||
3. Update your `.env` files with:
|
||||
```env
|
||||
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_...
|
||||
STRIPE_SECRET_KEY=sk_test_...
|
||||
STRIPE_WEBHOOK_SECRET=whsec_...
|
||||
```
|
||||
|
||||
### Additional Services (Optional)
|
||||
- **Sentry**: Error monitoring
|
||||
```env
|
||||
SENTRY_DSN=https://your-dsn@sentry.io/project-id
|
||||
SENTRY_ENVIRONMENT=development
|
||||
```
|
||||
- **PostHog**: Analytics
|
||||
```env
|
||||
NEXT_PUBLIC_POSTHOG_KEY=phc_your-key
|
||||
NEXT_PUBLIC_POSTHOG_HOST=https://app.posthog.com
|
||||
```
|
||||
|
||||
## Development Workflow
|
||||
|
||||
### Using Just Commands (Recommended)
|
||||
|
||||
The `justfile` provides convenient commands for development:
|
||||
|
||||
```bash
|
||||
# Setup and installation
|
||||
just setup # Complete development setup
|
||||
just install # Install all dependencies
|
||||
|
||||
# API Development
|
||||
just api-native # Run API natively (fastest)
|
||||
just api-build # Build API Docker image
|
||||
just api-run # Run API in Docker
|
||||
just api-test # Run API tests
|
||||
|
||||
# Frontend Development
|
||||
just fe-run # Run dashboard development server
|
||||
just fe-build # Build dashboard for production
|
||||
just fe-test # Run frontend tests
|
||||
|
||||
# Code Quality
|
||||
just lint # Run all linting checks
|
||||
just format # Format all code
|
||||
just test # Run all tests
|
||||
|
||||
# Docker Management
|
||||
just up # Start all services
|
||||
just down # Stop all services
|
||||
just logs # View service logs
|
||||
just clean # Clean up Docker resources
|
||||
```
|
||||
|
||||
### Manual Development
|
||||
|
||||
If you prefer running services manually:
|
||||
|
||||
```bash
|
||||
# Start API server
|
||||
cd api && uv run python run.py
|
||||
|
||||
# Start dashboard (in another terminal)
|
||||
cd dashboard && bun dev
|
||||
|
||||
# Start landing page (in another terminal)
|
||||
cd landing && bun dev
|
||||
```
|
||||
|
||||
## Service Configuration
|
||||
|
||||
### API Server Configuration
|
||||
Key environment variables for the API server (`api/.env`):
|
||||
|
||||
```env
|
||||
# Database connections
|
||||
SUPABASE_URL=https://your-project.supabase.co
|
||||
SUPABASE_KEY=your-service-role-key
|
||||
CLICKHOUSE_HOST=your-clickhouse-host
|
||||
|
||||
# Application settings
|
||||
APP_URL=http://localhost:3000
|
||||
LOGGING_LEVEL=INFO
|
||||
JWT_SECRET_KEY=your-jwt-secret
|
||||
|
||||
# External integrations
|
||||
SENTRY_DSN=your-sentry-dsn
|
||||
```
|
||||
|
||||
### Dashboard Configuration
|
||||
Key environment variables for the dashboard (`dashboard/.env.local`):
|
||||
|
||||
```env
|
||||
# Supabase
|
||||
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
|
||||
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
|
||||
|
||||
# Application URLs
|
||||
NEXT_PUBLIC_APP_URL=http://localhost:8000
|
||||
NEXT_PUBLIC_SITE_URL=http://localhost:3000
|
||||
|
||||
# Features
|
||||
NEXT_PUBLIC_ENVIRONMENT_TYPE=development
|
||||
NEXT_PUBLIC_PLAYGROUND=true
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
**Port conflicts:**
|
||||
```bash
|
||||
# Check what's running on ports 3000 and 8000
|
||||
lsof -i :3000
|
||||
lsof -i :8000
|
||||
```
|
||||
|
||||
**Database connection issues:**
|
||||
- Verify your Supabase and ClickHouse credentials
|
||||
- Check network connectivity to external services
|
||||
- Ensure database migrations have been applied
|
||||
|
||||
**Docker issues:**
|
||||
```bash
|
||||
# Reset Docker environment
|
||||
just clean
|
||||
docker system prune -f
|
||||
just up
|
||||
```
|
||||
|
||||
**Dependency issues:**
|
||||
```bash
|
||||
# Clean and reinstall
|
||||
rm -rf node_modules api/.venv dashboard/node_modules
|
||||
just install
|
||||
```
|
||||
|
||||
### Logs and Debugging
|
||||
|
||||
```bash
|
||||
# View service logs
|
||||
just logs
|
||||
|
||||
# View specific service logs
|
||||
docker-compose logs api
|
||||
docker-compose logs dashboard
|
||||
|
||||
# Run with debug logging
|
||||
LOGGING_LEVEL=DEBUG just api-run
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
Once your backend is running:
|
||||
|
||||
1. **Create an account** at http://localhost:3000
|
||||
2. **Generate an API key** in the dashboard
|
||||
3. **Install the AgentOps SDK** and start tracking your AI agents
|
||||
4. **Explore the dashboard** to view traces and analytics
|
||||
|
||||
For production deployment, see our [Deployment Guide](/v2/self-hosting/deployment).
|
||||
@@ -0,0 +1,427 @@
|
||||
---
|
||||
title: "Docker Guide"
|
||||
description: "Complete guide for running AgentOps with Docker and Docker Compose"
|
||||
---
|
||||
|
||||
# Docker Guide
|
||||
|
||||
This guide covers how to run AgentOps backend services using Docker and Docker Compose. This is the recommended approach for both development and production deployments.
|
||||
|
||||
## Overview
|
||||
|
||||
The AgentOps Docker setup includes:
|
||||
|
||||
- **API Server** - FastAPI backend service
|
||||
- **Dashboard** - Next.js frontend application
|
||||
- **OpenTelemetry Collector** - Observability and trace collection
|
||||
- **External Services** - Supabase, ClickHouse (configured separately)
|
||||
|
||||
## Docker Compose Configuration
|
||||
|
||||
The main `compose.yaml` file in the `/app` directory defines the service architecture:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: ./api
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- '8000:8000'
|
||||
environment:
|
||||
# Database connections
|
||||
SUPABASE_URL: ${NEXT_PUBLIC_SUPABASE_URL}
|
||||
SUPABASE_KEY: ${SUPABASE_SERVICE_ROLE_KEY}
|
||||
CLICKHOUSE_HOST: ${CLICKHOUSE_HOST}
|
||||
# ... other environment variables
|
||||
network_mode: 'host'
|
||||
volumes:
|
||||
- ./api:/app/api
|
||||
|
||||
dashboard:
|
||||
profiles: ['dashboard']
|
||||
build:
|
||||
context: ./dashboard
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- '3000:3000'
|
||||
environment:
|
||||
# Frontend configuration
|
||||
NEXT_PUBLIC_SUPABASE_URL: ${NEXT_PUBLIC_SUPABASE_URL}
|
||||
NEXT_PUBLIC_SUPABASE_ANON_KEY: ${NEXT_PUBLIC_SUPABASE_ANON_KEY}
|
||||
# ... other environment variables
|
||||
network_mode: 'host'
|
||||
depends_on:
|
||||
- api
|
||||
volumes:
|
||||
- ./dashboard:/app/
|
||||
```
|
||||
|
||||
## Quick Start with Docker
|
||||
|
||||
### 1. Prerequisites
|
||||
- Docker Engine 20.10+
|
||||
- Docker Compose 2.0+
|
||||
- Git
|
||||
|
||||
### 2. Clone and Setup
|
||||
```bash
|
||||
git clone https://github.com/AgentOps-AI/AgentOps.Next.git
|
||||
cd AgentOps.Next/app
|
||||
|
||||
# Copy environment files
|
||||
cp .env.example .env
|
||||
cp api/.env.example api/.env
|
||||
cp dashboard/.env.example dashboard/.env.local
|
||||
```
|
||||
|
||||
### 3. Configure Environment Variables
|
||||
Update your `.env` files with your external service credentials:
|
||||
|
||||
```env
|
||||
# .env (root)
|
||||
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
|
||||
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key
|
||||
CLICKHOUSE_HOST=your-clickhouse-host
|
||||
CLICKHOUSE_PASSWORD=your-password
|
||||
# ... other variables
|
||||
```
|
||||
|
||||
### 4. Start Services
|
||||
```bash
|
||||
# Start all services
|
||||
docker-compose up -d
|
||||
|
||||
# Or start with dashboard profile
|
||||
docker-compose --profile dashboard up -d
|
||||
|
||||
# View logs
|
||||
docker-compose logs -f
|
||||
```
|
||||
|
||||
### 5. Verify Services
|
||||
- **API Health**: http://localhost:8000/health
|
||||
- **API Docs**: http://localhost:8000/redoc
|
||||
- **Dashboard**: http://localhost:3000
|
||||
|
||||
## Docker Commands Reference
|
||||
|
||||
### Basic Operations
|
||||
|
||||
```bash
|
||||
# Start all services in detached mode
|
||||
docker-compose up -d
|
||||
|
||||
# Start services with dashboard
|
||||
docker-compose --profile dashboard up -d
|
||||
|
||||
# Stop all services
|
||||
docker-compose down
|
||||
|
||||
# Stop and remove volumes
|
||||
docker-compose down -v
|
||||
|
||||
# View service status
|
||||
docker-compose ps
|
||||
|
||||
# View logs for all services
|
||||
docker-compose logs -f
|
||||
|
||||
# View logs for specific service
|
||||
docker-compose logs -f api
|
||||
docker-compose logs -f dashboard
|
||||
```
|
||||
|
||||
### Development Commands
|
||||
|
||||
```bash
|
||||
# Rebuild services after code changes
|
||||
docker-compose build
|
||||
|
||||
# Rebuild specific service
|
||||
docker-compose build api
|
||||
docker-compose build dashboard
|
||||
|
||||
# Force recreate containers
|
||||
docker-compose up -d --force-recreate
|
||||
|
||||
# Scale services (if needed)
|
||||
docker-compose up -d --scale api=2
|
||||
```
|
||||
|
||||
### Debugging Commands
|
||||
|
||||
```bash
|
||||
# Execute commands in running containers
|
||||
docker-compose exec api bash
|
||||
docker-compose exec dashboard sh
|
||||
|
||||
# View container resource usage
|
||||
docker stats
|
||||
|
||||
# Inspect service configuration
|
||||
docker-compose config
|
||||
|
||||
# View service networks
|
||||
docker network ls
|
||||
docker network inspect app_default
|
||||
```
|
||||
|
||||
## Using Just Commands
|
||||
|
||||
The project includes a `justfile` with convenient Docker commands:
|
||||
|
||||
```bash
|
||||
# Start all services
|
||||
just up
|
||||
|
||||
# Stop all services
|
||||
just down
|
||||
|
||||
# View logs
|
||||
just logs
|
||||
|
||||
# Clean up Docker resources
|
||||
just clean
|
||||
|
||||
# Build and run API
|
||||
just api-build
|
||||
just api-run
|
||||
```
|
||||
|
||||
## Service-Specific Configuration
|
||||
|
||||
### API Service
|
||||
|
||||
The API service runs a FastAPI application with the following configuration:
|
||||
|
||||
**Dockerfile highlights:**
|
||||
```dockerfile
|
||||
FROM python:3.12-slim
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install -r requirements.txt
|
||||
COPY . .
|
||||
EXPOSE 8000
|
||||
CMD ["python", "run.py"]
|
||||
```
|
||||
|
||||
**Key environment variables:**
|
||||
- `SUPABASE_URL`, `SUPABASE_KEY` - Database connection
|
||||
- `CLICKHOUSE_HOST`, `CLICKHOUSE_PASSWORD` - Analytics database
|
||||
- `LOGGING_LEVEL` - Log verbosity (DEBUG, INFO, WARNING, ERROR)
|
||||
- `SENTRY_DSN` - Error tracking
|
||||
|
||||
### Dashboard Service
|
||||
|
||||
The Dashboard service runs a Next.js application:
|
||||
|
||||
**Dockerfile highlights:**
|
||||
```dockerfile
|
||||
FROM node:18-alpine
|
||||
WORKDIR /app
|
||||
COPY package*.json ./
|
||||
RUN npm ci --only=production
|
||||
COPY . .
|
||||
RUN npm run build
|
||||
EXPOSE 3000
|
||||
CMD ["npm", "start"]
|
||||
```
|
||||
|
||||
**Key environment variables:**
|
||||
- `NEXT_PUBLIC_SUPABASE_URL`, `NEXT_PUBLIC_SUPABASE_ANON_KEY` - Frontend auth
|
||||
- `NEXT_PUBLIC_APP_URL` - API server URL
|
||||
- `NEXT_PUBLIC_ENVIRONMENT_TYPE` - Environment (development/production)
|
||||
|
||||
## OpenTelemetry Collector
|
||||
|
||||
The OpenTelemetry Collector is included via a separate compose file:
|
||||
|
||||
```yaml
|
||||
# opentelemetry-collector/compose.yaml
|
||||
services:
|
||||
otel-collector:
|
||||
image: otel/opentelemetry-collector-contrib:latest
|
||||
command: ["--config=/etc/otel-collector-config.yaml"]
|
||||
volumes:
|
||||
- ./config/otel-collector-config.yaml:/etc/otel-collector-config.yaml
|
||||
ports:
|
||||
- "4317:4317" # OTLP gRPC receiver
|
||||
- "4318:4318" # OTLP HTTP receiver
|
||||
- "8889:8889" # Prometheus metrics
|
||||
```
|
||||
|
||||
## Production Configuration
|
||||
|
||||
### Environment Variables for Production
|
||||
|
||||
```env
|
||||
# Security
|
||||
DEBUG=false
|
||||
LOGGING_LEVEL=WARNING
|
||||
JWT_SECRET_KEY=your-secure-jwt-secret
|
||||
|
||||
# URLs
|
||||
PROTOCOL=https
|
||||
API_DOMAIN=api.yourdomain.com
|
||||
APP_DOMAIN=yourdomain.com
|
||||
|
||||
# Database
|
||||
CLICKHOUSE_SECURE=true
|
||||
SUPABASE_URL=https://your-prod-project.supabase.co
|
||||
|
||||
# Monitoring
|
||||
SENTRY_ENVIRONMENT=production
|
||||
NEXT_PUBLIC_ENVIRONMENT_TYPE=production
|
||||
```
|
||||
|
||||
### Production Docker Compose
|
||||
|
||||
For production, you may want to:
|
||||
|
||||
1. **Use specific image tags** instead of building locally
|
||||
2. **Configure resource limits**
|
||||
3. **Set up health checks**
|
||||
4. **Use external networks**
|
||||
|
||||
Example production overrides (`compose.prod.yaml`):
|
||||
|
||||
```yaml
|
||||
services:
|
||||
api:
|
||||
image: agentops/api:v1.0.0
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '1.0'
|
||||
memory: 1G
|
||||
reservations:
|
||||
cpus: '0.5'
|
||||
memory: 512M
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
restart: unless-stopped
|
||||
|
||||
dashboard:
|
||||
image: agentops/dashboard:v1.0.0
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.5'
|
||||
memory: 512M
|
||||
restart: unless-stopped
|
||||
```
|
||||
|
||||
Run with production config:
|
||||
```bash
|
||||
docker-compose -f compose.yaml -f compose.prod.yaml up -d
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
**Services won't start:**
|
||||
```bash
|
||||
# Check logs for errors
|
||||
docker-compose logs api
|
||||
docker-compose logs dashboard
|
||||
|
||||
# Verify environment variables
|
||||
docker-compose config
|
||||
```
|
||||
|
||||
**Port conflicts:**
|
||||
```bash
|
||||
# Check what's using ports
|
||||
lsof -i :3000
|
||||
lsof -i :8000
|
||||
|
||||
# Use different ports
|
||||
docker-compose up -d -p 3001:3000 -p 8001:8000
|
||||
```
|
||||
|
||||
**Database connection issues:**
|
||||
- Verify external service credentials in `.env` files
|
||||
- Check network connectivity from containers
|
||||
- Ensure services are accessible from Docker network
|
||||
|
||||
**Build failures:**
|
||||
```bash
|
||||
# Clean build cache
|
||||
docker system prune -f
|
||||
docker-compose build --no-cache
|
||||
|
||||
# Check Dockerfile syntax
|
||||
docker-compose config
|
||||
```
|
||||
|
||||
### Performance Optimization
|
||||
|
||||
**Resource monitoring:**
|
||||
```bash
|
||||
# Monitor container resources
|
||||
docker stats
|
||||
|
||||
# View container processes
|
||||
docker-compose exec api top
|
||||
```
|
||||
|
||||
**Volume optimization:**
|
||||
```bash
|
||||
# Use named volumes for better performance
|
||||
volumes:
|
||||
- api_data:/app/data
|
||||
- dashboard_cache:/app/.next
|
||||
```
|
||||
|
||||
**Network optimization:**
|
||||
```bash
|
||||
# Create custom network for better isolation
|
||||
networks:
|
||||
agentops:
|
||||
driver: bridge
|
||||
```
|
||||
|
||||
## Maintenance
|
||||
|
||||
### Regular Maintenance Tasks
|
||||
|
||||
```bash
|
||||
# Update images
|
||||
docker-compose pull
|
||||
docker-compose up -d
|
||||
|
||||
# Clean up unused resources
|
||||
docker system prune -f
|
||||
|
||||
# Backup volumes
|
||||
docker run --rm -v app_api_data:/data -v $(pwd):/backup alpine tar czf /backup/api_data.tar.gz -C /data .
|
||||
|
||||
# View disk usage
|
||||
docker system df
|
||||
```
|
||||
|
||||
### Monitoring
|
||||
|
||||
```bash
|
||||
# Service health checks
|
||||
curl http://localhost:8000/health
|
||||
curl http://localhost:3000/api/health
|
||||
|
||||
# Container logs
|
||||
docker-compose logs --tail=100 -f api
|
||||
|
||||
# Resource usage
|
||||
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
- Set up [monitoring and observability](/v2/self-hosting/monitoring)
|
||||
- Configure [production deployment](/v2/self-hosting/deployment)
|
||||
- Set up [backup and recovery](/v2/self-hosting/backup)
|
||||
- Configure [SSL/TLS certificates](/v2/self-hosting/ssl)
|
||||
@@ -0,0 +1,467 @@
|
||||
---
|
||||
title: "Just Commands Reference"
|
||||
description: "Complete reference for all available Just commands in AgentOps development"
|
||||
---
|
||||
|
||||
# Just Commands Reference
|
||||
|
||||
[Just](https://github.com/casey/just) is a command runner that provides convenient shortcuts for common development tasks. The AgentOps project includes a comprehensive `justfile` with commands for setup, development, testing, and deployment.
|
||||
|
||||
## Installation
|
||||
|
||||
First, install Just if you haven't already:
|
||||
|
||||
```bash
|
||||
# macOS
|
||||
brew install just
|
||||
|
||||
# Linux (using cargo)
|
||||
cargo install just
|
||||
|
||||
# Or download from GitHub releases
|
||||
curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash -s -- --to ~/bin
|
||||
```
|
||||
|
||||
## Quick Reference
|
||||
|
||||
View all available commands:
|
||||
```bash
|
||||
just
|
||||
# or
|
||||
just --list
|
||||
```
|
||||
|
||||
## Setup Commands
|
||||
|
||||
### `just setup`
|
||||
Complete development environment setup - runs the full initialization process.
|
||||
|
||||
```bash
|
||||
just setup
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Copies environment files (`.env.example` → `.env`, etc.)
|
||||
- Installs all dependencies (root, API, dashboard)
|
||||
- Sets up development environment
|
||||
|
||||
**Use when:**
|
||||
- First time setting up the project
|
||||
- After a fresh clone
|
||||
- When you want to reset your development environment
|
||||
|
||||
### `just install`
|
||||
Install all project dependencies across all services.
|
||||
|
||||
```bash
|
||||
just install
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Installs root Node.js dependencies (`bun install`)
|
||||
- Installs Python development dependencies (`uv pip install -r requirements-dev.txt`)
|
||||
- Installs API dependencies (`cd api && uv pip install -e .`)
|
||||
- Installs dashboard dependencies (`cd dashboard && bun install`)
|
||||
|
||||
**Use when:**
|
||||
- After pulling changes that modify dependencies
|
||||
- When `package.json`, `pyproject.toml`, or requirements files change
|
||||
|
||||
## API Development Commands
|
||||
|
||||
### `just api-native`
|
||||
Run the API server natively (fastest for development).
|
||||
|
||||
```bash
|
||||
just api-native
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Starts the FastAPI server using `uv run python run.py`
|
||||
- Runs on `http://localhost:8000`
|
||||
- Provides fastest reload times for development
|
||||
|
||||
**Use when:**
|
||||
- Active API development
|
||||
- You need fastest iteration cycles
|
||||
- Debugging API code
|
||||
|
||||
### `just api-build`
|
||||
Build the API Docker image.
|
||||
|
||||
```bash
|
||||
just api-build
|
||||
|
||||
# With Stripe support
|
||||
just api-build stripe
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Builds Docker image for the API service
|
||||
- Uses `./scripts/just-api-build.sh`
|
||||
- Optional Stripe integration support
|
||||
|
||||
**Use when:**
|
||||
- Preparing for Docker-based deployment
|
||||
- Testing Docker build process
|
||||
- Before running `just api-run`
|
||||
|
||||
### `just api-run`
|
||||
Run the API server in a Docker container.
|
||||
|
||||
```bash
|
||||
just api-run
|
||||
|
||||
# With Stripe support
|
||||
just api-run stripe
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Runs the API service using Docker
|
||||
- Uses `./scripts/just-api-run.sh`
|
||||
- Includes all necessary environment variables
|
||||
- Optional Stripe integration
|
||||
|
||||
**Use when:**
|
||||
- Testing Docker deployment locally
|
||||
- You need isolated environment
|
||||
- Production-like testing
|
||||
|
||||
### `just api-test`
|
||||
Run API tests using pytest.
|
||||
|
||||
```bash
|
||||
just api-test
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Changes to `api/` directory
|
||||
- Runs `pytest` with all configured tests
|
||||
- Includes unit and integration tests
|
||||
|
||||
**Use when:**
|
||||
- Before committing API changes
|
||||
- Validating API functionality
|
||||
- Continuous integration
|
||||
|
||||
## Frontend Development Commands
|
||||
|
||||
### `just fe-run`
|
||||
Run the dashboard development server.
|
||||
|
||||
```bash
|
||||
just fe-run
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Changes to `dashboard/` directory
|
||||
- Installs dependencies (`bun install`)
|
||||
- Starts development server (`bun run dev`)
|
||||
- Available at `http://localhost:3000`
|
||||
|
||||
**Use when:**
|
||||
- Active dashboard development
|
||||
- Testing frontend changes
|
||||
- Full-stack development
|
||||
|
||||
### `just fe-build`
|
||||
Build the dashboard for production.
|
||||
|
||||
```bash
|
||||
just fe-build
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Changes to `dashboard/` directory
|
||||
- Builds optimized production bundle (`bun run build`)
|
||||
- Generates static assets
|
||||
|
||||
**Use when:**
|
||||
- Preparing for production deployment
|
||||
- Testing production build locally
|
||||
- Performance optimization
|
||||
|
||||
### `just fe-test`
|
||||
Run frontend tests.
|
||||
|
||||
```bash
|
||||
just fe-test
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Changes to `dashboard/` directory
|
||||
- Runs test suite (`bun test`)
|
||||
- Includes unit and component tests
|
||||
|
||||
**Use when:**
|
||||
- Before committing frontend changes
|
||||
- Validating UI functionality
|
||||
- Continuous integration
|
||||
|
||||
## Code Quality Commands
|
||||
|
||||
### `just lint`
|
||||
Run all linting checks across the project.
|
||||
|
||||
```bash
|
||||
just lint
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Runs `bun run lint` from project root
|
||||
- Checks JavaScript/TypeScript files with ESLint
|
||||
- Checks Python files with Ruff
|
||||
- Validates code style and quality
|
||||
|
||||
**Use when:**
|
||||
- Before committing changes
|
||||
- Code review preparation
|
||||
- Maintaining code quality
|
||||
|
||||
### `just format`
|
||||
Format all code using project standards.
|
||||
|
||||
```bash
|
||||
just format
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Runs `ruff format` for Python files
|
||||
- Applies consistent code formatting
|
||||
- Fixes automatically correctable issues
|
||||
|
||||
**Use when:**
|
||||
- Before committing changes
|
||||
- Standardizing code style
|
||||
- Preparing for code review
|
||||
|
||||
### `just test`
|
||||
Run all tests across the project.
|
||||
|
||||
```bash
|
||||
just test
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Runs `just api-test` (API tests)
|
||||
- Runs `just fe-test` (frontend tests)
|
||||
- Comprehensive test suite execution
|
||||
|
||||
**Use when:**
|
||||
- Before major releases
|
||||
- Validating entire system
|
||||
- Continuous integration
|
||||
|
||||
## Docker Management Commands
|
||||
|
||||
### `just up`
|
||||
Start all services with Docker Compose.
|
||||
|
||||
```bash
|
||||
just up
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Runs `docker-compose up -d`
|
||||
- Starts all defined services in detached mode
|
||||
- Creates networks and volumes as needed
|
||||
|
||||
**Use when:**
|
||||
- Starting development environment
|
||||
- Testing full system integration
|
||||
- Docker-based development
|
||||
|
||||
### `just down`
|
||||
Stop all Docker services.
|
||||
|
||||
```bash
|
||||
just down
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Runs `docker-compose down`
|
||||
- Stops and removes containers
|
||||
- Preserves volumes and networks
|
||||
|
||||
**Use when:**
|
||||
- Stopping development environment
|
||||
- Switching between development modes
|
||||
- Cleaning up running services
|
||||
|
||||
### `just logs`
|
||||
View Docker logs for all services.
|
||||
|
||||
```bash
|
||||
just logs
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Runs `docker-compose logs -f`
|
||||
- Shows real-time logs from all services
|
||||
- Useful for debugging and monitoring
|
||||
|
||||
**Use when:**
|
||||
- Debugging service issues
|
||||
- Monitoring application behavior
|
||||
- Troubleshooting problems
|
||||
|
||||
### `just clean`
|
||||
Clean up Docker resources.
|
||||
|
||||
```bash
|
||||
just clean
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Runs `docker-compose down -v` (stops services and removes volumes)
|
||||
- Runs `docker system prune -f` (removes unused Docker resources)
|
||||
- Frees up disk space
|
||||
|
||||
**Use when:**
|
||||
- Cleaning up development environment
|
||||
- Freeing disk space
|
||||
- Resolving Docker issues
|
||||
- Fresh start needed
|
||||
|
||||
## Command Combinations and Workflows
|
||||
|
||||
### Full Development Setup
|
||||
```bash
|
||||
# First time setup
|
||||
just setup
|
||||
|
||||
# Start development
|
||||
just api-native # Terminal 1
|
||||
just fe-run # Terminal 2
|
||||
```
|
||||
|
||||
### Docker Development
|
||||
```bash
|
||||
# Setup and start with Docker
|
||||
just setup
|
||||
just up
|
||||
just logs
|
||||
```
|
||||
|
||||
### Testing Workflow
|
||||
```bash
|
||||
# Run quality checks before committing
|
||||
just lint
|
||||
just format
|
||||
just test
|
||||
```
|
||||
|
||||
### Production Preparation
|
||||
```bash
|
||||
# Build and test production assets
|
||||
just api-build
|
||||
just fe-build
|
||||
just test
|
||||
```
|
||||
|
||||
## Environment-Specific Usage
|
||||
|
||||
### Development Environment
|
||||
```bash
|
||||
# Fast iteration cycle
|
||||
just api-native # Native API for speed
|
||||
just fe-run # Frontend dev server
|
||||
```
|
||||
|
||||
### Integration Testing
|
||||
```bash
|
||||
# Full Docker environment
|
||||
just up # All services in Docker
|
||||
just test # Run full test suite
|
||||
```
|
||||
|
||||
### Production Testing
|
||||
```bash
|
||||
# Production-like environment
|
||||
just api-build # Build production API image
|
||||
just fe-build # Build production frontend
|
||||
just up # Run with Docker
|
||||
```
|
||||
|
||||
## Troubleshooting Just Commands
|
||||
|
||||
### Command Not Found
|
||||
```bash
|
||||
# Verify Just is installed
|
||||
just --version
|
||||
|
||||
# Install if missing
|
||||
brew install just # macOS
|
||||
```
|
||||
|
||||
### Permission Issues
|
||||
```bash
|
||||
# Make sure justfile is executable
|
||||
chmod +x justfile
|
||||
|
||||
# Check file permissions
|
||||
ls -la justfile
|
||||
```
|
||||
|
||||
### Environment Issues
|
||||
```bash
|
||||
# Verify environment files exist
|
||||
ls -la .env api/.env dashboard/.env.local
|
||||
|
||||
# Run setup to create missing files
|
||||
just setup
|
||||
```
|
||||
|
||||
### Docker Issues
|
||||
```bash
|
||||
# Clean Docker environment
|
||||
just clean
|
||||
|
||||
# Restart Docker service
|
||||
sudo systemctl restart docker # Linux
|
||||
```
|
||||
|
||||
## Custom Commands and Extensions
|
||||
|
||||
You can extend the `justfile` with your own commands. Add to the bottom of the file:
|
||||
|
||||
```bash
|
||||
# Custom command example
|
||||
my-command:
|
||||
@echo "Running my custom command"
|
||||
# Your commands here
|
||||
|
||||
# Command with parameters
|
||||
deploy env:
|
||||
@echo "Deploying to {{env}}"
|
||||
# Deployment commands
|
||||
```
|
||||
|
||||
Use custom commands:
|
||||
```bash
|
||||
just my-command
|
||||
just deploy staging
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### Daily Development
|
||||
1. `just api-native` for API development (fastest)
|
||||
2. `just fe-run` for frontend development
|
||||
3. `just lint && just test` before commits
|
||||
|
||||
### Integration Testing
|
||||
1. `just up` for full environment
|
||||
2. `just logs` for monitoring
|
||||
3. `just clean` when issues arise
|
||||
|
||||
### Production Preparation
|
||||
1. `just api-build && just fe-build` for production builds
|
||||
2. `just test` for validation
|
||||
3. `just up` for final testing
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [Backend Setup Guide](/v2/self-hosting/backend-setup) - Complete setup instructions
|
||||
- [Docker Guide](/v2/self-hosting/docker-guide) - Docker-specific commands
|
||||
- [Development Workflow](/v2/self-hosting/development) - Development best practices
|
||||
@@ -0,0 +1,532 @@
|
||||
---
|
||||
title: "Native Development Guide"
|
||||
description: "Complete guide for running AgentOps backend services natively without Docker"
|
||||
---
|
||||
|
||||
# Native Development Guide
|
||||
|
||||
This guide covers how to run AgentOps backend services natively on your local machine without Docker. Native development provides the fastest iteration cycles and is ideal for active development work.
|
||||
|
||||
## Overview
|
||||
|
||||
Running natively means:
|
||||
- **Faster startup times** - No container overhead
|
||||
- **Direct file system access** - Immediate code changes
|
||||
- **Native debugging** - Use your preferred IDE debugger
|
||||
- **Resource efficiency** - Lower memory and CPU usage
|
||||
|
||||
## Prerequisites
|
||||
|
||||
### System Requirements
|
||||
- **Python 3.12+** with pip or uv
|
||||
- **Node.js 18+** with npm, yarn, or bun
|
||||
- **Git** for version control
|
||||
- **Just** (optional) for convenience commands
|
||||
|
||||
### External Services
|
||||
You'll need these external services configured:
|
||||
- **Supabase** - Database and authentication
|
||||
- **ClickHouse** - Analytics database
|
||||
- **Stripe** (optional) - Payment processing
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 1. Clone and Setup
|
||||
```bash
|
||||
git clone https://github.com/AgentOps-AI/AgentOps.Next.git
|
||||
cd AgentOps.Next/app
|
||||
|
||||
# Copy environment files
|
||||
cp .env.example .env
|
||||
cp api/.env.example api/.env
|
||||
cp dashboard/.env.example dashboard/.env.local
|
||||
```
|
||||
|
||||
### 2. Install Dependencies
|
||||
|
||||
#### Root Dependencies
|
||||
```bash
|
||||
# Install shared tools (linting, formatting)
|
||||
bun install
|
||||
|
||||
# Install Python development tools
|
||||
uv pip install -r requirements-dev.txt
|
||||
```
|
||||
|
||||
#### API Dependencies
|
||||
```bash
|
||||
cd api
|
||||
|
||||
# Using uv (recommended)
|
||||
uv pip install -e .
|
||||
|
||||
# Or using pip
|
||||
pip install -e .
|
||||
|
||||
cd ..
|
||||
```
|
||||
|
||||
#### Dashboard Dependencies
|
||||
```bash
|
||||
cd dashboard
|
||||
|
||||
# Using bun (recommended)
|
||||
bun install
|
||||
|
||||
# Or using npm
|
||||
npm install
|
||||
|
||||
cd ..
|
||||
```
|
||||
|
||||
### 3. Configure Environment Variables
|
||||
Update your environment files with your service credentials. See [External Services Setup](#external-services-setup) below.
|
||||
|
||||
### 4. Start Services
|
||||
```bash
|
||||
# Terminal 1: API Server
|
||||
cd api && uv run python run.py
|
||||
|
||||
# Terminal 2: Dashboard (in a new terminal)
|
||||
cd dashboard && bun dev
|
||||
|
||||
# Terminal 3: Landing Page (optional, in a new terminal)
|
||||
cd landing && bun dev
|
||||
```
|
||||
|
||||
### 5. Verify Setup
|
||||
- **API Health**: http://localhost:8000/health
|
||||
- **API Documentation**: http://localhost:8000/redoc
|
||||
- **Dashboard**: http://localhost:3000
|
||||
- **Landing Page**: http://localhost:3001
|
||||
|
||||
## External Services Setup
|
||||
|
||||
### Supabase Configuration
|
||||
1. Create a new project at [supabase.com](https://supabase.com)
|
||||
2. Get your project credentials from Settings → API
|
||||
3. Set up the database schema:
|
||||
```bash
|
||||
cd supabase
|
||||
npx supabase db push
|
||||
```
|
||||
4. Update `api/.env` and `dashboard/.env.local`:
|
||||
```env
|
||||
# API environment
|
||||
SUPABASE_URL=https://your-project-id.supabase.co
|
||||
SUPABASE_KEY=your-service-role-key
|
||||
|
||||
# Dashboard environment
|
||||
NEXT_PUBLIC_SUPABASE_URL=https://your-project-id.supabase.co
|
||||
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
|
||||
```
|
||||
|
||||
### ClickHouse Configuration
|
||||
1. Sign up for [ClickHouse Cloud](https://clickhouse.com/cloud) or self-host
|
||||
2. Create a database and get connection details
|
||||
3. Apply the schema:
|
||||
```bash
|
||||
# Use the schema from clickhouse/schema_dump.sql
|
||||
clickhouse-client --host your-host --query "$(cat clickhouse/schema_dump.sql)"
|
||||
```
|
||||
4. Update `api/.env`:
|
||||
```env
|
||||
CLICKHOUSE_HOST=your-host.clickhouse.cloud
|
||||
CLICKHOUSE_PORT=8123
|
||||
CLICKHOUSE_USER=default
|
||||
CLICKHOUSE_PASSWORD=your-password
|
||||
CLICKHOUSE_DATABASE=your-database
|
||||
CLICKHOUSE_SECURE=true
|
||||
```
|
||||
|
||||
## API Server Setup
|
||||
|
||||
### Environment Configuration
|
||||
Key variables in `api/.env`:
|
||||
|
||||
```env
|
||||
# Database Connections
|
||||
SUPABASE_URL=https://your-project.supabase.co
|
||||
SUPABASE_KEY=your-service-role-key
|
||||
CLICKHOUSE_HOST=your-clickhouse-host
|
||||
CLICKHOUSE_PASSWORD=your-password
|
||||
|
||||
# Application Settings
|
||||
APP_URL=http://localhost:3000
|
||||
LOGGING_LEVEL=INFO
|
||||
JWT_SECRET_KEY=your-jwt-secret-key
|
||||
|
||||
# Optional Integrations
|
||||
SENTRY_DSN=your-sentry-dsn
|
||||
SENTRY_ENVIRONMENT=development
|
||||
```
|
||||
|
||||
### Running the API Server
|
||||
|
||||
#### Using Just (Recommended)
|
||||
```bash
|
||||
just api-native
|
||||
```
|
||||
|
||||
#### Manual Command
|
||||
```bash
|
||||
cd api
|
||||
uv run python run.py
|
||||
```
|
||||
|
||||
#### Alternative Methods
|
||||
```bash
|
||||
# Using pip and python directly
|
||||
cd api
|
||||
pip install -e .
|
||||
python run.py
|
||||
|
||||
# Using uvicorn directly
|
||||
cd api
|
||||
uvicorn agentops.main:app --host 0.0.0.0 --port 8000 --reload
|
||||
```
|
||||
|
||||
### API Development Features
|
||||
- **Auto-reload** on file changes
|
||||
- **Interactive API docs** at http://localhost:8000/docs
|
||||
- **ReDoc documentation** at http://localhost:8000/redoc
|
||||
- **Health check** at http://localhost:8000/health
|
||||
|
||||
## Dashboard Setup
|
||||
|
||||
### Environment Configuration
|
||||
Key variables in `dashboard/.env.local`:
|
||||
|
||||
```env
|
||||
# Supabase Configuration
|
||||
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
|
||||
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
|
||||
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key
|
||||
|
||||
# Application URLs
|
||||
NEXT_PUBLIC_APP_URL=http://localhost:8000
|
||||
NEXT_PUBLIC_SITE_URL=http://localhost:3000
|
||||
|
||||
# Feature Flags
|
||||
NEXT_PUBLIC_ENVIRONMENT_TYPE=development
|
||||
NEXT_PUBLIC_PLAYGROUND=true
|
||||
|
||||
# Optional Services
|
||||
NEXT_PUBLIC_POSTHOG_KEY=your-posthog-key
|
||||
NEXT_PUBLIC_SENTRY_DSN=your-sentry-dsn
|
||||
```
|
||||
|
||||
### Running the Dashboard
|
||||
|
||||
#### Using Just (Recommended)
|
||||
```bash
|
||||
just fe-run
|
||||
```
|
||||
|
||||
#### Manual Commands
|
||||
```bash
|
||||
cd dashboard
|
||||
|
||||
# Using bun
|
||||
bun install
|
||||
bun dev
|
||||
|
||||
# Using npm
|
||||
npm install
|
||||
npm run dev
|
||||
|
||||
# Using yarn
|
||||
yarn install
|
||||
yarn dev
|
||||
```
|
||||
|
||||
### Dashboard Development Features
|
||||
- **Hot reload** on file changes
|
||||
- **Fast Refresh** for React components
|
||||
- **Development tools** integration
|
||||
- **Source maps** for debugging
|
||||
|
||||
## Development Workflow
|
||||
|
||||
### Daily Development Routine
|
||||
1. **Start services**:
|
||||
```bash
|
||||
# Terminal 1
|
||||
just api-native
|
||||
|
||||
# Terminal 2
|
||||
just fe-run
|
||||
```
|
||||
|
||||
2. **Make changes** to your code
|
||||
3. **Test changes** - services auto-reload
|
||||
4. **Run tests** before committing:
|
||||
```bash
|
||||
just test
|
||||
```
|
||||
|
||||
### Code Quality Workflow
|
||||
```bash
|
||||
# Format code
|
||||
just format
|
||||
|
||||
# Run linting
|
||||
just lint
|
||||
|
||||
# Run tests
|
||||
just test
|
||||
|
||||
# All-in-one quality check
|
||||
just format && just lint && just test
|
||||
```
|
||||
|
||||
### Database Development
|
||||
```bash
|
||||
# Apply Supabase migrations
|
||||
cd supabase
|
||||
npx supabase db push
|
||||
|
||||
# Reset database (development only)
|
||||
npx supabase db reset
|
||||
|
||||
# Generate TypeScript types
|
||||
npx supabase gen types typescript --local > types/database.types.ts
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
### API Testing
|
||||
```bash
|
||||
cd api
|
||||
|
||||
# Run all tests
|
||||
pytest
|
||||
|
||||
# Run with coverage
|
||||
pytest --cov=agentops
|
||||
|
||||
# Run specific test file
|
||||
pytest tests/test_auth.py
|
||||
|
||||
# Run with verbose output
|
||||
pytest -v
|
||||
```
|
||||
|
||||
### Dashboard Testing
|
||||
```bash
|
||||
cd dashboard
|
||||
|
||||
# Run all tests
|
||||
bun test
|
||||
|
||||
# Run tests in watch mode
|
||||
bun test --watch
|
||||
|
||||
# Run tests with coverage
|
||||
bun test --coverage
|
||||
```
|
||||
|
||||
### Integration Testing
|
||||
```bash
|
||||
# Run full test suite
|
||||
just test
|
||||
|
||||
# Test API and dashboard separately
|
||||
just api-test
|
||||
just fe-test
|
||||
```
|
||||
|
||||
## Debugging
|
||||
|
||||
### API Debugging
|
||||
1. **Set breakpoints** in your IDE
|
||||
2. **Run with debugger**:
|
||||
```bash
|
||||
cd api
|
||||
python -m debugpy --listen 5678 --wait-for-client run.py
|
||||
```
|
||||
3. **Attach your IDE debugger** to port 5678
|
||||
|
||||
### Dashboard Debugging
|
||||
1. **Use browser dev tools** (F12)
|
||||
2. **Next.js debugging**:
|
||||
```bash
|
||||
cd dashboard
|
||||
NODE_OPTIONS='--inspect' bun dev
|
||||
```
|
||||
3. **Attach debugger** at chrome://inspect
|
||||
|
||||
### Log Debugging
|
||||
```bash
|
||||
# API logs with debug level
|
||||
cd api
|
||||
LOGGING_LEVEL=DEBUG uv run python run.py
|
||||
|
||||
# Dashboard logs
|
||||
cd dashboard
|
||||
DEBUG=* bun dev
|
||||
```
|
||||
|
||||
## Performance Optimization
|
||||
|
||||
### API Performance
|
||||
- **Use native Python** for fastest development
|
||||
- **Enable hot reload** with uvicorn
|
||||
- **Profile with py-spy**:
|
||||
```bash
|
||||
pip install py-spy
|
||||
py-spy top --pid $(pgrep -f "python run.py")
|
||||
```
|
||||
|
||||
### Dashboard Performance
|
||||
- **Use bun** for faster package management
|
||||
- **Enable Fast Refresh** (enabled by default)
|
||||
- **Analyze bundle size**:
|
||||
```bash
|
||||
cd dashboard
|
||||
ANALYZE=true bun run build
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
**Python import errors:**
|
||||
```bash
|
||||
# Reinstall in editable mode
|
||||
cd api
|
||||
uv pip install -e .
|
||||
```
|
||||
|
||||
**Node.js module not found:**
|
||||
```bash
|
||||
# Clear and reinstall
|
||||
cd dashboard
|
||||
rm -rf node_modules package-lock.json
|
||||
bun install
|
||||
```
|
||||
|
||||
**Port already in use:**
|
||||
```bash
|
||||
# Find and kill process
|
||||
lsof -i :8000 # API port
|
||||
lsof -i :3000 # Dashboard port
|
||||
kill -9 <PID>
|
||||
```
|
||||
|
||||
**Database connection issues:**
|
||||
- Verify credentials in `.env` files
|
||||
- Check network connectivity
|
||||
- Ensure external services are running
|
||||
|
||||
### Performance Issues
|
||||
|
||||
**Slow API startup:**
|
||||
```bash
|
||||
# Use uv for faster Python package management
|
||||
uv pip install -e .
|
||||
```
|
||||
|
||||
**Slow dashboard reload:**
|
||||
```bash
|
||||
# Use bun instead of npm
|
||||
cd dashboard
|
||||
rm -rf node_modules
|
||||
bun install
|
||||
```
|
||||
|
||||
### Development Environment Reset
|
||||
```bash
|
||||
# Clean everything and start fresh
|
||||
rm -rf api/.venv dashboard/node_modules node_modules
|
||||
just setup
|
||||
```
|
||||
|
||||
## IDE Configuration
|
||||
|
||||
### VS Code
|
||||
Recommended extensions:
|
||||
- Python
|
||||
- Pylance
|
||||
- ES7+ React/Redux/React-Native snippets
|
||||
- Tailwind CSS IntelliSense
|
||||
- Prettier - Code formatter
|
||||
|
||||
Settings (`.vscode/settings.json`):
|
||||
```json
|
||||
{
|
||||
"python.defaultInterpreterPath": "./api/.venv/bin/python",
|
||||
"python.linting.enabled": true,
|
||||
"python.linting.ruffEnabled": true,
|
||||
"editor.formatOnSave": true,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### PyCharm
|
||||
1. **Set Python interpreter** to `./api/.venv/bin/python`
|
||||
2. **Enable Ruff** for Python linting
|
||||
3. **Configure Node.js** interpreter for dashboard
|
||||
4. **Set up run configurations** for API and dashboard
|
||||
|
||||
## Advanced Configuration
|
||||
|
||||
### Custom Environment Variables
|
||||
Add custom variables to your `.env` files:
|
||||
|
||||
```env
|
||||
# Custom API settings
|
||||
CUSTOM_FEATURE_FLAG=true
|
||||
DEBUG_SQL_QUERIES=false
|
||||
|
||||
# Custom dashboard settings
|
||||
NEXT_PUBLIC_CUSTOM_FEATURE=enabled
|
||||
```
|
||||
|
||||
### Development Proxy
|
||||
Set up a proxy for API calls in development:
|
||||
|
||||
```javascript
|
||||
// dashboard/next.config.js
|
||||
module.exports = {
|
||||
async rewrites() {
|
||||
return [
|
||||
{
|
||||
source: '/api/:path*',
|
||||
destination: 'http://localhost:8000/:path*',
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
### Hot Reload Configuration
|
||||
Fine-tune hot reload behavior:
|
||||
|
||||
```python
|
||||
# api/run.py
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(
|
||||
"agentops.main:app",
|
||||
host="0.0.0.0",
|
||||
port=8000,
|
||||
reload=True,
|
||||
reload_dirs=["agentops"], # Only watch specific directories
|
||||
reload_excludes=["*.pyc", "*.log"], # Exclude certain files
|
||||
)
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
Once your native development environment is running:
|
||||
|
||||
1. **Explore the codebase** - Start with `api/agentops/main.py` and `dashboard/pages/index.tsx`
|
||||
2. **Make your first changes** - Try modifying a simple component or API endpoint
|
||||
3. **Set up testing** - Write tests for your changes
|
||||
4. **Configure your IDE** - Set up debugging and linting
|
||||
5. **Join the community** - Connect with other developers
|
||||
|
||||
For production deployment, see our [Deployment Guide](/v2/self-hosting/deployment).
|
||||
@@ -0,0 +1,302 @@
|
||||
---
|
||||
title: "Self-Hosting Overview"
|
||||
description: "Complete guide to self-hosting AgentOps backend services and infrastructure"
|
||||
---
|
||||
|
||||
# Self-Hosting AgentOps
|
||||
|
||||
Welcome to the AgentOps self-hosting documentation. This section provides comprehensive guides for running AgentOps backend services and infrastructure on your own infrastructure.
|
||||
|
||||
## What is Self-Hosting?
|
||||
|
||||
Self-hosting AgentOps means running the entire AgentOps platform on your own servers or cloud infrastructure, giving you complete control over:
|
||||
|
||||
- **Data sovereignty** - Your data stays on your infrastructure
|
||||
- **Customization** - Modify the platform to fit your needs
|
||||
- **Security** - Implement your own security policies
|
||||
- **Compliance** - Meet specific regulatory requirements
|
||||
- **Cost control** - Manage infrastructure costs directly
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
The AgentOps platform consists of several key components:
|
||||
|
||||
### Core Services
|
||||
- **API Server** - FastAPI backend handling authentication, data processing, and business logic
|
||||
- **Dashboard** - Next.js frontend providing the user interface and visualization
|
||||
- **Database Layer** - Supabase (PostgreSQL) for primary data and ClickHouse for analytics
|
||||
|
||||
### Supporting Infrastructure
|
||||
- **OpenTelemetry Collector** - Trace and metrics collection
|
||||
- **Authentication** - Supabase Auth for user management
|
||||
- **File Storage** - Supabase Storage for file handling
|
||||
- **Monitoring** - Optional Sentry, PostHog integration
|
||||
|
||||
### External Dependencies
|
||||
- **Supabase** - Database and authentication services
|
||||
- **ClickHouse** - Analytics database for traces and metrics
|
||||
- **Stripe** (optional) - Payment processing for billing features
|
||||
|
||||
## Deployment Options
|
||||
|
||||
### 1. Development Setup
|
||||
Perfect for local development and testing:
|
||||
- **Native Development** - Run services directly on your machine
|
||||
- **Docker Development** - Use Docker Compose for isolated environment
|
||||
- **Hybrid Approach** - Mix native and containerized services
|
||||
|
||||
### 2. Production Deployment
|
||||
For production workloads:
|
||||
- **Docker Compose** - Simple single-server deployment
|
||||
- **Kubernetes** - Scalable container orchestration
|
||||
- **Cloud Platforms** - Deploy to AWS, GCP, Azure
|
||||
- **Serverless** - Use cloud functions and managed services
|
||||
|
||||
## Quick Start Guide
|
||||
|
||||
### Prerequisites
|
||||
Before you begin, ensure you have:
|
||||
- Modern operating system (Linux, macOS, or Windows with WSL)
|
||||
- Docker and Docker Compose
|
||||
- Node.js 18+ and Python 3.12+
|
||||
- Access to external services (Supabase, ClickHouse)
|
||||
|
||||
### 5-Minute Setup
|
||||
```bash
|
||||
# 1. Clone the repository
|
||||
git clone https://github.com/AgentOps-AI/AgentOps.Next.git
|
||||
cd AgentOps.Next/app
|
||||
|
||||
# 2. Copy environment files
|
||||
cp .env.example .env
|
||||
cp api/.env.example api/.env
|
||||
cp dashboard/.env.example dashboard/.env.local
|
||||
|
||||
# 3. Configure external services (see guides below)
|
||||
# Edit .env files with your service credentials
|
||||
|
||||
# 4. Start with Docker
|
||||
docker-compose up -d
|
||||
|
||||
# Or start natively for development
|
||||
just api-native # Terminal 1
|
||||
just fe-run # Terminal 2
|
||||
```
|
||||
|
||||
Visit http://localhost:3000 to access the dashboard!
|
||||
|
||||
## Documentation Sections
|
||||
|
||||
### [Backend Setup Guide](/v2/self-hosting/backend-setup)
|
||||
Complete guide for setting up the AgentOps backend services, including:
|
||||
- Prerequisites and system requirements
|
||||
- Environment configuration
|
||||
- External service setup
|
||||
- Service verification and troubleshooting
|
||||
|
||||
### [Docker Guide](/v2/self-hosting/docker-guide)
|
||||
Comprehensive Docker and Docker Compose documentation:
|
||||
- Docker configuration and setup
|
||||
- Container management commands
|
||||
- Production Docker configurations
|
||||
- Performance optimization and monitoring
|
||||
|
||||
### [Native Development Guide](/v2/self-hosting/native-development)
|
||||
Guide for running services natively without Docker:
|
||||
- Fastest development setup
|
||||
- IDE configuration and debugging
|
||||
- Performance optimization
|
||||
- Advanced development workflows
|
||||
|
||||
### [Just Commands Reference](/v2/self-hosting/just-commands)
|
||||
Complete reference for all available Just commands:
|
||||
- Setup and installation commands
|
||||
- Development workflow commands
|
||||
- Testing and quality assurance
|
||||
- Docker management utilities
|
||||
|
||||
## Choosing Your Setup
|
||||
|
||||
### For Active Development
|
||||
**Recommended: Native Development**
|
||||
- Fastest iteration cycles
|
||||
- Direct debugging capabilities
|
||||
- Lower resource usage
|
||||
- Immediate file system access
|
||||
|
||||
```bash
|
||||
just api-native # Start API natively
|
||||
just fe-run # Start dashboard
|
||||
```
|
||||
|
||||
### For Team Development
|
||||
**Recommended: Docker Development**
|
||||
- Consistent environment across team
|
||||
- Isolated service dependencies
|
||||
- Easy environment reset
|
||||
- Production-like testing
|
||||
|
||||
```bash
|
||||
just up # Start all services
|
||||
just logs # Monitor services
|
||||
```
|
||||
|
||||
### For Production
|
||||
**Recommended: Docker with External Services**
|
||||
- Scalable and maintainable
|
||||
- Health checks and restart policies
|
||||
- Resource limits and monitoring
|
||||
- Easy backup and recovery
|
||||
|
||||
## External Services Setup
|
||||
|
||||
### Required Services
|
||||
|
||||
#### Supabase (Database & Auth)
|
||||
- **Purpose**: Primary PostgreSQL database and user authentication
|
||||
- **Setup**: Create project at [supabase.com](https://supabase.com)
|
||||
- **Cost**: Free tier available, pay-as-you-scale
|
||||
- **Alternatives**: Self-hosted PostgreSQL + custom auth
|
||||
|
||||
#### ClickHouse (Analytics)
|
||||
- **Purpose**: High-performance analytics database for traces and metrics
|
||||
- **Setup**: ClickHouse Cloud or self-hosted
|
||||
- **Cost**: Pay-per-usage or fixed instances
|
||||
- **Alternatives**: PostgreSQL (with performance trade-offs)
|
||||
|
||||
### Optional Services
|
||||
|
||||
#### Stripe (Billing)
|
||||
- **Purpose**: Payment processing and subscription management
|
||||
- **Setup**: Create account at [stripe.com](https://stripe.com)
|
||||
- **Required for**: Billing features
|
||||
- **Alternatives**: Remove billing features or custom payment solution
|
||||
|
||||
#### Monitoring & Analytics
|
||||
- **Sentry**: Error tracking and performance monitoring
|
||||
- **PostHog**: Product analytics and feature flags
|
||||
- **Setup**: Optional but recommended for production
|
||||
|
||||
## Security Considerations
|
||||
|
||||
### Authentication & Authorization
|
||||
- JWT tokens for API authentication
|
||||
- Supabase Auth for user management
|
||||
- Row-level security in PostgreSQL
|
||||
- API key management for SDK access
|
||||
|
||||
### Data Protection
|
||||
- HTTPS/TLS encryption in transit
|
||||
- Database encryption at rest
|
||||
- Environment variable security
|
||||
- Secret management best practices
|
||||
|
||||
### Network Security
|
||||
- Firewall configuration
|
||||
- VPN or private networks
|
||||
- API rate limiting
|
||||
- CORS configuration
|
||||
|
||||
## Monitoring & Observability
|
||||
|
||||
### Application Monitoring
|
||||
- Health check endpoints
|
||||
- Application metrics and logs
|
||||
- Error tracking with Sentry
|
||||
- Performance monitoring
|
||||
|
||||
### Infrastructure Monitoring
|
||||
- Container resource usage
|
||||
- Database performance
|
||||
- Network connectivity
|
||||
- Storage utilization
|
||||
|
||||
### Alerting
|
||||
- Service availability alerts
|
||||
- Error rate monitoring
|
||||
- Resource usage thresholds
|
||||
- Custom business metrics
|
||||
|
||||
## Scaling Considerations
|
||||
|
||||
### Horizontal Scaling
|
||||
- Multiple API server instances
|
||||
- Load balancer configuration
|
||||
- Database connection pooling
|
||||
- Shared session storage
|
||||
|
||||
### Vertical Scaling
|
||||
- Resource allocation optimization
|
||||
- Database performance tuning
|
||||
- Caching strategies
|
||||
- CDN for static assets
|
||||
|
||||
### Database Scaling
|
||||
- Read replicas for PostgreSQL
|
||||
- ClickHouse cluster setup
|
||||
- Connection pool management
|
||||
- Query optimization
|
||||
|
||||
## Backup & Recovery
|
||||
|
||||
### Database Backups
|
||||
- Automated Supabase backups
|
||||
- ClickHouse data exports
|
||||
- Point-in-time recovery
|
||||
- Cross-region replication
|
||||
|
||||
### Application Backups
|
||||
- Configuration files
|
||||
- Environment variables
|
||||
- Custom modifications
|
||||
- SSL certificates
|
||||
|
||||
### Disaster Recovery
|
||||
- Recovery time objectives (RTO)
|
||||
- Recovery point objectives (RPO)
|
||||
- Failover procedures
|
||||
- Data integrity verification
|
||||
|
||||
## Cost Optimization
|
||||
|
||||
### Infrastructure Costs
|
||||
- Right-size compute resources
|
||||
- Use spot instances where appropriate
|
||||
- Implement auto-scaling
|
||||
- Monitor and optimize usage
|
||||
|
||||
### Service Costs
|
||||
- Optimize database queries
|
||||
- Implement caching strategies
|
||||
- Use appropriate service tiers
|
||||
- Monitor third-party service usage
|
||||
|
||||
## Getting Help
|
||||
|
||||
### Documentation
|
||||
- Follow the step-by-step guides
|
||||
- Check troubleshooting sections
|
||||
- Review configuration examples
|
||||
- Understand architecture decisions
|
||||
|
||||
### Community Support
|
||||
- GitHub Issues for bug reports
|
||||
- GitHub Discussions for questions
|
||||
- Discord community for real-time help
|
||||
- Stack Overflow for technical questions
|
||||
|
||||
### Professional Support
|
||||
- Enterprise support options
|
||||
- Consulting services available
|
||||
- Custom deployment assistance
|
||||
- Training and onboarding
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Start with the [Backend Setup Guide](/v2/self-hosting/backend-setup)** - Get your environment running
|
||||
2. **Choose your deployment method** - Docker or native development
|
||||
3. **Configure external services** - Set up Supabase and ClickHouse
|
||||
4. **Customize for your needs** - Modify configuration and features
|
||||
5. **Plan for production** - Implement monitoring, backups, and scaling
|
||||
|
||||
Ready to begin? Start with our [Backend Setup Guide](/v2/self-hosting/backend-setup)!
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: "Advanced Configuration"
|
||||
---
|
||||
|
||||
In AgentOps fashion, you only need to add one line of "code" to your `.env` file 😊
|
||||
|
||||
<CodeGroup>
|
||||
```python .env
|
||||
AGENTOPS_API_KEY=<YOUR API KEY>
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
<Tip>
|
||||
Find your AgentOps API Key in your Settings > [Projects & API Keys](https://app.agentops.ai/settings/projects) page.
|
||||
</Tip>
|
||||
|
||||
#### Optional settings:
|
||||
|
||||
```python .env
|
||||
# The AgentOps API endpoint. Defaults to https://api.agentops.ai
|
||||
AGENTOPS_API_ENDPOINT=https://api.agentops.ai
|
||||
# Logging level. <DEBUG, INFO, CRITICAL>. Defaults to INFO
|
||||
AGENTOPS_LOG_LEVEL=INFO
|
||||
# Write logs to file <TRUE, FALSE>. Defaults to TRUE
|
||||
AGENTOPS_LOGGING_TO_FILE=TRUE
|
||||
# Whether to opt out of recording environment data. <FALSE, TRUE>. Defaults to FALSE
|
||||
AGENTOPS_ENV_DATA_OPT_OUT=FALSE
|
||||
```
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,328 @@
|
||||
---
|
||||
title: "Context Managers"
|
||||
description: "Use AgentOps traces as Python context managers for automatic lifecycle management"
|
||||
---
|
||||
|
||||
# Context Managers
|
||||
|
||||
AgentOps provides native context manager support for traces, allowing you to use Python's `with` statement for automatic trace lifecycle management. This approach ensures traces are properly started and ended, even when exceptions occur.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
The simplest way to use context managers is with the `start_trace()` function:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# Use context manager for automatic trace management
|
||||
with agentops.start_trace("my_workflow") as trace:
|
||||
# Your code here
|
||||
print("Processing data...")
|
||||
# Trace automatically ends when exiting the with block
|
||||
```
|
||||
|
||||
The trace will automatically:
|
||||
- Start when entering the `with` block
|
||||
- End with "Success" status when exiting normally
|
||||
- End with "Error" status if an exception occurs
|
||||
- Clean up resources properly in all cases
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### Traces with Tags
|
||||
|
||||
You can add tags to traces for better organization and filtering:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# Using list tags
|
||||
with agentops.start_trace("data_processing", tags=["batch", "production"]):
|
||||
process_batch_data()
|
||||
|
||||
# Using dictionary tags for more structured metadata
|
||||
with agentops.start_trace("user_request", tags={
|
||||
"user_id": "12345",
|
||||
"request_type": "query",
|
||||
"priority": "high"
|
||||
}):
|
||||
handle_user_request()
|
||||
```
|
||||
|
||||
### Parallel Traces
|
||||
|
||||
Context managers create independent parallel traces, not parent-child relationships:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# Sequential parallel traces
|
||||
with agentops.start_trace("task_1"):
|
||||
print("Task 1 executing")
|
||||
|
||||
with agentops.start_trace("task_2"):
|
||||
print("Task 2 executing")
|
||||
|
||||
# Nested context managers create parallel traces
|
||||
with agentops.start_trace("outer_workflow"):
|
||||
print("Outer workflow started")
|
||||
|
||||
with agentops.start_trace("inner_task"):
|
||||
print("Inner task executing (parallel to outer)")
|
||||
|
||||
print("Outer workflow continuing")
|
||||
```
|
||||
|
||||
### Exception Handling
|
||||
|
||||
Context managers automatically handle exceptions and set appropriate trace states:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# Automatic error handling
|
||||
try:
|
||||
with agentops.start_trace("risky_operation"):
|
||||
# This will automatically set trace status to "Error"
|
||||
raise ValueError("Something went wrong")
|
||||
except ValueError as e:
|
||||
print(f"Caught error: {e}")
|
||||
# Trace has already been ended with Error status
|
||||
|
||||
# Graceful degradation pattern
|
||||
try:
|
||||
with agentops.start_trace("primary_service"):
|
||||
result = call_primary_service()
|
||||
except ServiceUnavailableError:
|
||||
with agentops.start_trace("fallback_service"):
|
||||
result = call_fallback_service()
|
||||
```
|
||||
|
||||
### Concurrent Execution
|
||||
|
||||
Context managers work seamlessly with threading and asyncio:
|
||||
|
||||
<CodeGroup>
|
||||
|
||||
```python Threading
|
||||
import agentops
|
||||
import threading
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# With threading
|
||||
def worker_function(worker_id):
|
||||
with agentops.start_trace(f"worker_{worker_id}"):
|
||||
# Each thread gets its own independent trace
|
||||
process_work(worker_id)
|
||||
|
||||
threads = []
|
||||
for i in range(3):
|
||||
thread = threading.Thread(target=worker_function, args=(i,))
|
||||
threads.append(thread)
|
||||
thread.start()
|
||||
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
```
|
||||
|
||||
```python Asyncio
|
||||
import agentops
|
||||
import asyncio
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# With asyncio
|
||||
async def async_task(task_id):
|
||||
with agentops.start_trace(f"async_task_{task_id}"):
|
||||
await asyncio.sleep(0.1) # Simulate async work
|
||||
return f"result_{task_id}"
|
||||
|
||||
async def main():
|
||||
tasks = [async_task(i) for i in range(3)]
|
||||
results = await asyncio.gather(*tasks)
|
||||
return results
|
||||
|
||||
# Run async tasks
|
||||
results = asyncio.run(main())
|
||||
```
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
## Production Patterns
|
||||
|
||||
### API Endpoint Monitoring
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from flask import Flask, request
|
||||
|
||||
app = Flask(__name__)
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
@app.route('/api/process', methods=['POST'])
|
||||
def process_request():
|
||||
# Create trace for each API request
|
||||
with agentops.start_trace("api_request", tags={
|
||||
"endpoint": "/api/process",
|
||||
"method": "POST",
|
||||
"user_id": request.headers.get("user-id")
|
||||
}):
|
||||
try:
|
||||
data = request.get_json()
|
||||
result = process_data(data)
|
||||
return {"status": "success", "result": result}
|
||||
except Exception as e:
|
||||
# Exception automatically sets trace to Error status
|
||||
return {"status": "error", "message": str(e)}, 500
|
||||
```
|
||||
|
||||
### Batch Processing
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
def process_batch(items):
|
||||
with agentops.start_trace("batch_processing", tags={
|
||||
"batch_size": len(items),
|
||||
"batch_type": "data_processing"
|
||||
}):
|
||||
successful = 0
|
||||
failed = 0
|
||||
|
||||
for item in items:
|
||||
try:
|
||||
with agentops.start_trace("item_processing", tags={
|
||||
"item_id": item.get("id"),
|
||||
"item_type": item.get("type")
|
||||
}):
|
||||
process_item(item)
|
||||
successful += 1
|
||||
except Exception as e:
|
||||
failed += 1
|
||||
print(f"Failed to process item {item.get('id')}: {e}")
|
||||
|
||||
print(f"Batch completed: {successful} successful, {failed} failed")
|
||||
```
|
||||
|
||||
### Retry Logic
|
||||
|
||||
```python
|
||||
import agentops
|
||||
import time
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
def retry_operation(operation_name, max_retries=3):
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
with agentops.start_trace(f"{operation_name}_attempt_{attempt + 1}", tags={
|
||||
"operation": operation_name,
|
||||
"attempt": attempt + 1,
|
||||
"max_retries": max_retries
|
||||
}):
|
||||
# Your operation here
|
||||
result = perform_operation()
|
||||
return result # Success - exit retry loop
|
||||
|
||||
except Exception as e:
|
||||
if attempt < max_retries - 1:
|
||||
wait_time = 2 ** attempt # Exponential backoff
|
||||
print(f"Attempt {attempt + 1} failed: {e}. Retrying in {wait_time}s...")
|
||||
time.sleep(wait_time)
|
||||
else:
|
||||
print(f"All {max_retries} attempts failed")
|
||||
raise
|
||||
```
|
||||
|
||||
## Backward Compatibility
|
||||
|
||||
Context managers are fully backward compatible with existing AgentOps code patterns:
|
||||
|
||||
<CodeGroup>
|
||||
|
||||
```python Manual Management
|
||||
import agentops
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# Manual trace management (legacy)
|
||||
trace = agentops.start_trace("manual_trace")
|
||||
# ... your code ...
|
||||
agentops.end_trace(trace, "Success")
|
||||
```
|
||||
|
||||
```python Context Manager
|
||||
import agentops
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# Context manager (new, recommended)
|
||||
with agentops.start_trace("context_managed_trace") as trace:
|
||||
# ... your code ...
|
||||
pass # Automatically ended
|
||||
```
|
||||
|
||||
```python Property Access
|
||||
import agentops
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# Accessing trace properties
|
||||
with agentops.start_trace("property_access") as trace:
|
||||
span = trace.span # Access underlying span
|
||||
trace_id = trace.span.get_span_context().trace_id
|
||||
```
|
||||
|
||||
```python Mixed Usage
|
||||
import agentops
|
||||
|
||||
agentops.init(api_key="your-api-key")
|
||||
|
||||
# Mixed usage
|
||||
trace = agentops.start_trace("mixed_usage")
|
||||
try:
|
||||
with trace: # Use existing trace as context manager
|
||||
# ... your code ...
|
||||
pass
|
||||
except Exception:
|
||||
agentops.end_trace(trace, "Error")
|
||||
```
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
## Examples
|
||||
|
||||
For complete working examples, see the following files in the AgentOps repository:
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Basic Usage" icon="play" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/context_manager/basic_usage.py">
|
||||
Simple context manager patterns and error handling
|
||||
</Card>
|
||||
<Card title="Parallel Traces" icon="arrows-split-up-and-left" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/context_manager/parallel_traces.py">
|
||||
Sequential, nested, and concurrent trace patterns
|
||||
</Card>
|
||||
<Card title="Error Handling" icon="shield-exclamation" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/context_manager/error_handling.py">
|
||||
Exception handling, retry patterns, and graceful degradation
|
||||
</Card>
|
||||
<Card title="Production Patterns" icon="server" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/context_manager/production_patterns.py">
|
||||
API endpoints, batch processing, microservices, and monitoring
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
These examples demonstrate real-world usage patterns and best practices for using AgentOps context managers in production applications.
|
||||
|
||||
## API Reference
|
||||
|
||||
For detailed API information, see the [SDK Reference](/v2/usage/sdk-reference#trace-management) documentation.
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
title: "Dashboard"
|
||||
description: "Visualize your AgentOps analysis."
|
||||
---
|
||||
|
||||
## Insights Dashboard
|
||||
|
||||
You need better insights to turn error-prone AI into stable workflows. Here's your new best friend.
|
||||
|
||||
### Session Drilldown
|
||||
Here you will find a list of all of your previously recorded sessions and useful data about each such as total execution time.
|
||||
You also get helpful debugging info such as any SDK versions you were on if you're building on a supported agent framework like Crew or AutoGen.
|
||||
LLM calls are presented as a familiar chat history view, and charts give you a breakdown of the types of events that were called and how long they took.
|
||||
|
||||
<Frame type="glass" caption="Session Summary">
|
||||
<img height="200" src="/images/session-summary.gif" />
|
||||
</Frame>
|
||||
|
||||
Find any past sessions from your Session Drawer.
|
||||
<Frame type="glass" caption="Session Drawer">
|
||||
<img height="250" src="/images/session-drawer.gif" />
|
||||
</Frame>
|
||||
|
||||
Most powerful of all is the Session Waterfall. On the left, a time visualization of all your LLM calls, Action events, Tool calls, and Errors.
|
||||
On the right, specific details about the event you've selected on the waterfall. For instance the exact prompt and completion for a given LLM call.
|
||||
Most of which has been automatically recorded for you.
|
||||
|
||||
<Frame type="glass" caption="Session Waterfall">
|
||||
<img height="200" src="/images/session-waterfall.gif" />
|
||||
</Frame>
|
||||
|
||||
|
||||
### Session Overview
|
||||
View a meta-analysis of all of your sessions in a single view.
|
||||
<Frame type="glass" caption="Session Overview">
|
||||
<img height="200" src="/images/overview.png" />
|
||||
</Frame>
|
||||
|
||||
<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/intro.css"></script>
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
title: 'LangChain Callback Handler'
|
||||
description: 'How to use AgentOps with LangChain'
|
||||
---
|
||||
|
||||
By default, AgentOps is compatible with agents using LangChain with our LLM Instrumentor as long as they're using
|
||||
supported models.
|
||||
|
||||
As an alternative to instrumenting, the LangChain Callback Handler is available.
|
||||
|
||||
## Constructor
|
||||
|
||||
- `api_key` (Optional, string): API Key for AgentOps services. If not provided, the key will be read from the `AGENTOPS_API_KEY` environment variable.
|
||||
- `endpoint` (Optional, string): The endpoint for the AgentOps service. Defaults to 'https://api.agentops.ai'.
|
||||
- `max_wait_time` (Optional, int): The maximum time to wait in milliseconds before flushing the queue. Defaults to 30,000 (30 seconds).
|
||||
- `max_queue_size` (Optional, int): The maximum size of the event queue. Defaults to 100.
|
||||
- `tags` (Optional, List[string]): Tags for the sessions for grouping or sorting (e.g., ["GPT-4"]).
|
||||
|
||||
## Usage
|
||||
|
||||
### Install Dependencies
|
||||
<CodeGroup>
|
||||
```bash pip
|
||||
pip install agentops
|
||||
```
|
||||
```bash poetry
|
||||
poetry add agentops
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Implement Callback Handler
|
||||
|
||||
Initialize the handler with its constructor and pass it into the callbacks array from LangChain.
|
||||
```python
|
||||
from agentops.integration.callbacks.langchain import LangchainCallbackHandler
|
||||
ChatOpenAI(callbacks=[LangchainCallbackHandler()])
|
||||
```
|
||||
|
||||
Example:
|
||||
```python
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
from langchain_openai import ChatOpenAI
|
||||
from agentops import LangchainCallbackHandler
|
||||
|
||||
prompt = ChatPromptTemplate.from_messages(["Tell me a joke about {animal}"])
|
||||
|
||||
model = ChatOpenAI(callbacks=[LangchainCallbackHandler()])
|
||||
|
||||
chain = prompt | model
|
||||
response = chain.invoke({"animal": "bears"})
|
||||
```
|
||||
|
||||
## Why use the handler?
|
||||
|
||||
If your project uses LangChain for Agents, Events and Tools, it may be easier to use the callback Handler for observability.
|
||||
|
||||
If your project uses models with LangChain that are not yet supported by AgentOps, they can be supported by the Handler.
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,354 @@
|
||||
---
|
||||
title: "Manual Trace Control"
|
||||
description: "Advanced trace management with start_trace and end_trace methods"
|
||||
---
|
||||
|
||||
## Basic Manual Trace Control
|
||||
|
||||
### Starting and Ending Traces
|
||||
|
||||
The most basic form of manual trace control involves starting a trace, executing your code, and then ending the trace with a specific state:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize without automatic session creation
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
# Start a trace manually
|
||||
trace = agentops.start_trace("my-workflow")
|
||||
|
||||
try:
|
||||
# Your application logic here
|
||||
result = perform_some_operation()
|
||||
|
||||
# End the trace successfully
|
||||
agentops.end_trace(trace, "Success")
|
||||
except Exception as e:
|
||||
# End the trace with failure state
|
||||
agentops.end_trace(trace, "Indeterminate")
|
||||
```
|
||||
|
||||
### Trace Names and Tags
|
||||
|
||||
You can provide meaningful names and tags when starting traces:
|
||||
|
||||
```python
|
||||
# Start a trace with custom name and tags
|
||||
trace = agentops.start_trace(
|
||||
trace_name="customer-service-workflow",
|
||||
tags=["customer-123", "priority-high", "support"]
|
||||
)
|
||||
```
|
||||
|
||||
### Batch Processing with Selective Trace Ending
|
||||
|
||||
For batch processing scenarios, you can selectively end traces based on processing results:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
# Sample batch items to process
|
||||
batch_items = [
|
||||
{"id": 1, "data": "item_1_data", "valid": True},
|
||||
{"id": 2, "data": "item_2_data", "valid": False},
|
||||
{"id": 3, "data": "item_3_data", "valid": True},
|
||||
]
|
||||
@agentops.operation(name="process_item")
|
||||
def process_item(item):
|
||||
"""Simulate processing an item"""
|
||||
if not item.get("valid", False):
|
||||
raise ValueError(f"Invalid item: {item['id']}")
|
||||
return {"processed": True, "result": f"Processed {item['data']}"}
|
||||
|
||||
# Start traces for batch items
|
||||
for i, item in enumerate(batch_items):
|
||||
trace = agentops.start_trace(f"batch_item_{i+1}")
|
||||
try:
|
||||
result = process_item(item)
|
||||
if result.get("processed"):
|
||||
agentops.end_trace(trace, "Success")
|
||||
else:
|
||||
agentops.end_trace(trace, "Indeterminate")
|
||||
except Exception as e:
|
||||
agentops.end_trace(trace, "Error")
|
||||
```
|
||||
|
||||
## Updating Trace Metadata During Execution
|
||||
|
||||
You can update metadata on running traces at any point during execution using the `update_trace_metadata` function. This is useful for adding context, tracking progress, or storing intermediate results.
|
||||
|
||||
### Basic Metadata Updates
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
# Start a trace with initial tags
|
||||
trace = agentops.start_trace("ai-agent-workflow", tags=["startup", "initialization"])
|
||||
|
||||
# Your AI agent code runs here...
|
||||
process_user_request()
|
||||
|
||||
# Update metadata with results
|
||||
agentops.update_trace_metadata({
|
||||
"operation_name": "AI Agent Processing Complete",
|
||||
"stage": "completed",
|
||||
"response_quality": "high",
|
||||
"tags": ["ai-agent", "completed", "success"] # Tags show current status
|
||||
})
|
||||
|
||||
# End the trace
|
||||
agentops.end_trace(trace, "Success")
|
||||
```
|
||||
|
||||
### Semantic Convention Support
|
||||
|
||||
The function automatically maps user-friendly keys to semantic conventions when possible:
|
||||
|
||||
```python
|
||||
# These keys will be mapped to semantic conventions
|
||||
agentops.update_trace_metadata({
|
||||
"operation_name": "AI Agent Data Processing",
|
||||
"tags": ["production", "batch-job", "gpt-4"], # Maps to core.tags
|
||||
"agent_name": "DataProcessorAgent", # Maps to agent.name
|
||||
"workflow_name": "Intelligent ETL Pipeline", # Maps to workflow.name
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
### Advanced Metadata with Custom Prefix
|
||||
|
||||
You can specify a custom prefix for your metadata attributes:
|
||||
|
||||
```python
|
||||
# Use a custom prefix for business-specific metadata
|
||||
agentops.update_trace_metadata({
|
||||
"customer_id": "CUST_456",
|
||||
"order_value": 99.99,
|
||||
"payment_method": "credit_card",
|
||||
"agent_interaction": "customer_support"
|
||||
}, prefix="business")
|
||||
|
||||
# Results in:
|
||||
# business.customer_id = "CUST_456"
|
||||
# business.order_value = 99.99
|
||||
# business.payment_method = "credit_card"
|
||||
```
|
||||
|
||||
### Real-World Example: Progress Tracking
|
||||
|
||||
Here's how to use metadata updates to track progress through a complex workflow:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import operation
|
||||
|
||||
agentops.init(auto_start_session=False)
|
||||
|
||||
@operation
|
||||
def process_batch(batch_data):
|
||||
# Simulate batch processing
|
||||
return f"Processed {len(batch_data)} items"
|
||||
|
||||
def run_etl_pipeline(data_batches):
|
||||
"""ETL pipeline with progress tracking via metadata"""
|
||||
|
||||
trace = agentops.start_trace("etl-pipeline", tags=["data-processing"])
|
||||
|
||||
total_batches = len(data_batches)
|
||||
processed_records = 0
|
||||
|
||||
# Initial metadata
|
||||
agentops.update_trace_metadata({
|
||||
"operation_name": "ETL Pipeline Execution",
|
||||
"pipeline_stage": "starting",
|
||||
"total_batches": total_batches,
|
||||
"processed_batches": 0,
|
||||
"processed_records": 0,
|
||||
"estimated_completion": "calculating...",
|
||||
"tags": ["etl", "data-processing", "async-operation"]
|
||||
})
|
||||
|
||||
try:
|
||||
for i, batch in enumerate(data_batches):
|
||||
# Update progress
|
||||
agentops.update_trace_metadata({
|
||||
"pipeline_stage": "processing",
|
||||
"current_batch": i + 1,
|
||||
"processed_batches": i,
|
||||
"progress_percentage": round((i / total_batches) * 100, 2)
|
||||
})
|
||||
|
||||
# Process the batch
|
||||
result = process_batch(batch)
|
||||
processed_records += len(batch)
|
||||
|
||||
# Update running totals
|
||||
agentops.update_trace_metadata({
|
||||
"processed_records": processed_records,
|
||||
"last_batch_result": result
|
||||
})
|
||||
|
||||
# Final metadata update
|
||||
agentops.update_trace_metadata({
|
||||
"operation_name": "ETL Pipeline Completed",
|
||||
"pipeline_stage": "completed",
|
||||
"processed_batches": total_batches,
|
||||
"progress_percentage": 100.0,
|
||||
"completion_status": "success",
|
||||
"total_execution_time": "calculated_automatically",
|
||||
"tags": ["etl", "completed", "success"]
|
||||
})
|
||||
|
||||
agentops.end_trace(trace, "Success")
|
||||
|
||||
except Exception as e:
|
||||
# Error metadata
|
||||
agentops.update_trace_metadata({
|
||||
"operation_name": "ETL Pipeline Failed",
|
||||
"pipeline_stage": "failed",
|
||||
"error_message": str(e),
|
||||
"completion_status": "error",
|
||||
"failed_at_batch": i + 1 if 'i' in locals() else 0,
|
||||
"tags": ["etl", "failed", "error"]
|
||||
})
|
||||
|
||||
agentops.end_trace(trace, "Error")
|
||||
raise
|
||||
|
||||
# Example usage
|
||||
data_batches = [
|
||||
["record1", "record2", "record3"],
|
||||
["record4", "record5"],
|
||||
["record6", "record7", "record8", "record9"]
|
||||
]
|
||||
|
||||
run_etl_pipeline(data_batches)
|
||||
```
|
||||
|
||||
### Supported Data Types
|
||||
|
||||
The `update_trace_metadata` function supports various data types:
|
||||
|
||||
```python
|
||||
agentops.update_trace_metadata({
|
||||
"operation_name": "Multi-type Data Example",
|
||||
"successful_operation": True,
|
||||
"tags": ["example", "demo", "multi-agent"],
|
||||
"processing_steps": ["validation", "transformation", "output"]
|
||||
})
|
||||
|
||||
# Note: Lists are automatically converted to JSON strings for OpenTelemetry compatibility
|
||||
```
|
||||
|
||||
## Integration with Decorators
|
||||
|
||||
Manual trace control works seamlessly with AgentOps decorators:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import agent, operation, tool
|
||||
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@agent
|
||||
class CustomerServiceAgent:
|
||||
@operation
|
||||
def analyze_request(self, request):
|
||||
return f"Analyzed: {request}"
|
||||
|
||||
@tool(cost=0.02)
|
||||
def lookup_customer(self, customer_id):
|
||||
return f"Customer data for {customer_id}"
|
||||
|
||||
# Manual trace with decorated components
|
||||
trace = agentops.start_trace("customer-service")
|
||||
|
||||
try:
|
||||
agent = CustomerServiceAgent()
|
||||
customer_data = agent.lookup_customer("CUST_123")
|
||||
analysis = agent.analyze_request("billing issue")
|
||||
|
||||
agentops.end_trace(trace, "Success")
|
||||
except Exception as e:
|
||||
agentops.end_trace(trace, "Error")
|
||||
```
|
||||
|
||||
## Real-World Example
|
||||
|
||||
Here's a comprehensive example showing manual trace control in a customer service application:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import agent, operation, tool
|
||||
from openai import OpenAI
|
||||
|
||||
agentops.init(auto_start_session=False)
|
||||
client = OpenAI()
|
||||
|
||||
@operation
|
||||
def analyze_sentiment(text):
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=[{"role": "user", "content": f"Analyze sentiment: {text}"}]
|
||||
)
|
||||
return response.choices[0].message.content.strip()
|
||||
|
||||
@tool(cost=0.01)
|
||||
def lookup_order(order_id):
|
||||
return f"Order {order_id} details"
|
||||
|
||||
def process_customer_requests(requests):
|
||||
"""Process multiple customer requests with individual trace tracking"""
|
||||
results = []
|
||||
for i, request in enumerate(requests):
|
||||
trace = agentops.start_trace(
|
||||
f"customer_request_{i+1}",
|
||||
tags=["customer-service", request.get("priority", "normal")]
|
||||
)
|
||||
try:
|
||||
sentiment = analyze_sentiment(request["message"])
|
||||
|
||||
if "order" in request:
|
||||
order_info = lookup_order(request["order"])
|
||||
|
||||
if "positive" in sentiment.lower() or "neutral" in sentiment.lower():
|
||||
agentops.end_trace(trace, "Success")
|
||||
results.append({"status": "resolved", "sentiment": sentiment})
|
||||
else:
|
||||
agentops.end_trace(trace, "Escalation_Required")
|
||||
results.append({"status": "escalated", "sentiment": sentiment})
|
||||
|
||||
except Exception as e:
|
||||
agentops.end_trace(trace, "Error")
|
||||
results.append({"status": "error", "error": str(e)})
|
||||
|
||||
return results
|
||||
|
||||
customer_requests = [
|
||||
{"message": "I love this product!", "priority": "low"},
|
||||
{"message": "My order is completely wrong!", "order": "12345", "priority": "high"},
|
||||
{"message": "When will my package arrive?", "order": "67890", "priority": "normal"}
|
||||
]
|
||||
|
||||
results = process_customer_requests(customer_requests)
|
||||
print(f"Processed {len(results)} customer requests")
|
||||
```
|
||||
|
||||
This example demonstrates:
|
||||
- Individual trace management for each customer request
|
||||
- Integration with decorated agents and tools
|
||||
- Different end states based on business logic
|
||||
- Proper error handling with appropriate trace states
|
||||
- Use of tags for categorization
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
title: "MCP Docs"
|
||||
description: "Chat with the AgentOps documentation directly from your IDE using the Mintlify MCP Docs Server."
|
||||
---
|
||||
|
||||
|
||||
<Note>
|
||||
Looking for the AgentOps API tools instead? See the <a href="/v2/usage/mcp-server">MCP Server</a> guide.
|
||||
</Note>
|
||||
|
||||
# MCP Docs Server
|
||||
|
||||
The **Mintlify MCP Docs Server** gives you and your coding agents instant, programmatic access to every page in the AgentOps docs. It works in **Cursor**, **Windsurf**, **VS Code**, **Zed**, **Claude Code**, or any other tool that speaks the [Model Context Protocol](https://modelcontextprotocol.io/).
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npx mint-mcp add agentops
|
||||
```
|
||||
|
||||
Run the command above in any folder and your IDE will automatically register the docs server. No extra configuration required.
|
||||
|
||||
## How it works
|
||||
|
||||
When you (or your in-editor AI assistant) ask a question, the IDE sends a request to the MCP Docs Server. The server searches the AgentOps docs and returns the most relevant sections so the assistant can craft a precise answer.
|
||||
|
||||
> **Example prompt**
|
||||
> "How do I record custom spans with the `@operation` decorator?"
|
||||
|
||||
## What you can ask
|
||||
|
||||
Here are a few ideas to get you started:
|
||||
|
||||
### Add features
|
||||
- "Add a chat interface with streaming support to my app"
|
||||
- "Instrument my agent with the `@trace` decorator"
|
||||
|
||||
### Ask about integrations
|
||||
- "How do I integrate AgentOps with the Vercel AI SDK?"
|
||||
- "Show me a working example for CrewAI"
|
||||
|
||||
### Debug or update existing code
|
||||
- "My trace isn't showing spans—what could be wrong?"
|
||||
- "How do I customize the styling of the Session Waterfall?"
|
||||
|
||||
If the answer lives in the docs, the server will find it.
|
||||
|
||||
## Common issues
|
||||
|
||||
### Server not starting
|
||||
1. Ensure `npx` is installed and working.
|
||||
2. Check for other MCP servers running on the same port.
|
||||
3. Verify your configuration file syntax.
|
||||
4. On Windows, confirm Node.js and npm are installed.
|
||||
|
||||
### Tool calls failing
|
||||
1. Restart the MCP Docs Server and/or your IDE.
|
||||
2. Update to the latest version of your IDE.
|
||||
3. Confirm that the AgentOps docs server appears in your IDE’s list of MCP servers.
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
title: "MCP Server"
|
||||
description: "MCP server for accessing AgentOps trace and span data"
|
||||
---
|
||||
|
||||
<iframe
|
||||
width="100%"
|
||||
height="300"
|
||||
src="https://www.youtube.com/embed/lTa3Sk8C4f0?si=3r7GO8N1Csh0P9C5RR"
|
||||
title="AgentOps MCP Server"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
||||
allowFullScreen
|
||||
></iframe>
|
||||
|
||||
# MCP Server
|
||||
|
||||
AgentOps provides a [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server that exposes the Public API as a set of tools for AI assistants. This allows AI models to directly query your AgentOps data during conversations and debug AI agents with greater context.
|
||||
|
||||
### Configuration & Installation
|
||||
|
||||
Add the AgentOps MCP to your MCP client's configuration file.
|
||||
|
||||
**npx configuration:**
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"agentops": {
|
||||
"command": "npx",
|
||||
"args": [
|
||||
"agentops-mcp"
|
||||
],
|
||||
"env": {
|
||||
"AGENTOPS_API_KEY": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Cursor Deeplink:**
|
||||
|
||||
Add the AgentOps MCP to Cursor with Deeplink.
|
||||
|
||||
[](https://cursor.com/install-mcp?name=agentops&config=eyJjb21tYW5kIjoibnB4IGFnZW50b3BzLW1jcCIsImVudiI6eyJBR0VOVE9QU19BUElfS0VZIjoiIn19)
|
||||
|
||||
**Smithery:**
|
||||
|
||||
To install agentops-mcp for Claude Desktop automatically via [Smithery](https://smithery.ai/server/@AgentOps-AI/agentops-mcp):
|
||||
|
||||
```bash
|
||||
npx -y @smithery/cli install @AgentOps-AI/agentops-mcp --client claude
|
||||
```
|
||||
|
||||
### Available Tools
|
||||
|
||||
The MCP server exposes the following tools that mirror the Public API endpoints:
|
||||
|
||||
#### `auth`
|
||||
Authorize using an AgentOps project API key.
|
||||
- **Parameters**: `api_key` (string) - Your AgentOps project API key
|
||||
- **Usage**: The server will automatically prompt for authentication when needed
|
||||
|
||||
#### `get_trace`
|
||||
Get trace information by ID.
|
||||
- **Parameters**: `trace_id` (string) - The trace identifier
|
||||
- **Returns**: Trace details and metrics
|
||||
|
||||
#### `get_span`
|
||||
Get span information by ID.
|
||||
- **Parameters**: `span_id` (string) - The span identifier
|
||||
- **Returns**: Span attributes and metrics
|
||||
|
||||
#### `get_complete_trace`
|
||||
Get complete trace information by ID.
|
||||
- **Parameters**: `span_id` (string) - The trace identifier
|
||||
- **Returns**: Complete trace and associated span details.
|
||||
|
||||
### Environment Variables
|
||||
|
||||
The MCP server supports the following environment variables:
|
||||
|
||||
- `AGENTOPS_API_KEY`: Your AgentOps project API key
|
||||
- `HOST`: API endpoint (defaults to `https://api.agentops.ai`)
|
||||
@@ -0,0 +1,342 @@
|
||||
---
|
||||
title: "Public API"
|
||||
description: "Read-only HTTP API for accessing AgentOps trace and span data"
|
||||
---
|
||||
|
||||
# Public API
|
||||
|
||||
The AgentOps Public API provides read-only HTTP access to your monitoring data. This RESTful API allows you to retrieve trace information, span details, and metrics from any application or framework, regardless of programming language.
|
||||
|
||||
<Note>
|
||||
This is a **read-only API** for accessing existing data. To create traces and spans, use the [AgentOps SDK](/v2/quickstart) or our instrumentation libraries.
|
||||
</Note>
|
||||
|
||||
## Base URL
|
||||
|
||||
All API requests should be made to:
|
||||
```
|
||||
https://api.agentops.ai
|
||||
```
|
||||
|
||||
## Authentication
|
||||
|
||||
The API uses JWT token authentication. You'll need to exchange your API key for a JWT token first.
|
||||
|
||||
### Get Access Token
|
||||
|
||||
Convert your API key to a bearer token for API access.
|
||||
|
||||
<CodeGroup>
|
||||
```bash curl
|
||||
curl -X POST https://api.agentops.ai/public/v1/auth/access_token \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"api_key": "YOUR_API_KEY"
|
||||
}'
|
||||
```
|
||||
|
||||
```json Response
|
||||
{
|
||||
"bearer": "eyJhbGciOiJIUzI1NiIs..."
|
||||
}
|
||||
```
|
||||
|
||||
```json Error Response
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body", "api_key"],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
**Important**: Bearer tokens are valid for **30 days**. Store them securely and refresh before expiration.
|
||||
|
||||
## Core Endpoints
|
||||
|
||||
### Get Project Information
|
||||
|
||||
Retrieve details about your current project.
|
||||
|
||||
<CodeGroup>
|
||||
```bash curl
|
||||
curl -X GET https://api.agentops.ai/public/v1/project \
|
||||
-H "Authorization: Bearer YOUR_BEARER_TOKEN"
|
||||
```
|
||||
|
||||
```json Response
|
||||
{
|
||||
"id": "proj_abc123",
|
||||
"name": "My AI Project",
|
||||
"environment": "production"
|
||||
}
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
This endpoint returns information about the project associated with your API key.
|
||||
|
||||
### Get Trace Details
|
||||
|
||||
Retrieve comprehensive information about a specific trace, including all its spans.
|
||||
|
||||
<CodeGroup>
|
||||
```bash curl
|
||||
curl -X GET https://api.agentops.ai/public/v1/traces/trace_123 \
|
||||
-H "Authorization: Bearer YOUR_BEARER_TOKEN"
|
||||
```
|
||||
|
||||
```json Response
|
||||
{
|
||||
"trace_id": "trace_123",
|
||||
"project_id": "proj_abc123",
|
||||
"tags": ["production", "chatbot", "gpt-4"],
|
||||
"spans": [
|
||||
{
|
||||
"span_id": "span_456",
|
||||
"parent_span_id": null,
|
||||
"span_name": "User Query Processing",
|
||||
"span_kind": "SPAN_KIND_INTERNAL",
|
||||
"start_time": "2024-03-14T12:00:00.000Z",
|
||||
"end_time": "2024-03-14T12:00:05.000Z",
|
||||
"duration": 5000,
|
||||
"status_code": "STATUS_CODE_OK",
|
||||
"status_message": "Success"
|
||||
},
|
||||
{
|
||||
"span_id": "span_789",
|
||||
"parent_span_id": "span_456",
|
||||
"span_name": "OpenAI GPT-4 Call",
|
||||
"span_kind": "SPAN_KIND_CLIENT",
|
||||
"start_time": "2024-03-14T12:00:01.000Z",
|
||||
"end_time": "2024-03-14T12:00:03.000Z",
|
||||
"duration": 2000,
|
||||
"status_code": "STATUS_CODE_OK",
|
||||
"status_message": "Success"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
```json Error Response
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["path", "trace_id"],
|
||||
"msg": "trace not found",
|
||||
"type": "value_error.not_found"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
**Parameters:**
|
||||
- `trace_id` (path, required): The unique identifier of the trace
|
||||
|
||||
**Response Fields:**
|
||||
- `trace_id`: Unique trace identifier
|
||||
- `project_id`: Associated project ID
|
||||
- `tags`: Array of tags associated with the trace
|
||||
- `spans`: Array of span summaries within the trace
|
||||
|
||||
### Get Trace Metrics
|
||||
|
||||
Retrieve aggregated metrics and statistics for a trace.
|
||||
|
||||
<CodeGroup>
|
||||
```bash curl
|
||||
curl -X GET https://api.agentops.ai/public/v1/traces/trace_123/metrics \
|
||||
-H "Authorization: Bearer YOUR_BEARER_TOKEN"
|
||||
```
|
||||
|
||||
```json Response
|
||||
{
|
||||
"span_count": 5,
|
||||
"trace_count": 1,
|
||||
"success_count": 4,
|
||||
"fail_count": 1,
|
||||
"indeterminate_count": 0,
|
||||
"prompt_tokens": 150,
|
||||
"completion_tokens": 75,
|
||||
"cache_read_input_tokens": 0,
|
||||
"reasoning_tokens": 25,
|
||||
"total_tokens": 250,
|
||||
"prompt_cost": "0.0030",
|
||||
"completion_cost": "0.0015",
|
||||
"average_cost_per_trace": "0.0045",
|
||||
"total_cost": "0.0045"
|
||||
}
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
**Metrics Explained:**
|
||||
- `span_count`: Total number of spans in the trace
|
||||
- `success_count`/`fail_count`/`indeterminate_count`: Status breakdown
|
||||
- `*_tokens`: Token usage breakdown by type
|
||||
- `*_cost`: Cost calculations in USD
|
||||
|
||||
### Get Span Details
|
||||
|
||||
Retrieve comprehensive information about a specific span, including full attribute payloads.
|
||||
|
||||
<CodeGroup>
|
||||
```bash curl
|
||||
curl -X GET https://api.agentops.ai/public/v1/spans/span_456 \
|
||||
-H "Authorization: Bearer YOUR_BEARER_TOKEN"
|
||||
```
|
||||
|
||||
```json Response
|
||||
{
|
||||
"span_id": "span_456",
|
||||
"parent_span_id": null,
|
||||
"span_name": "User Query Processing",
|
||||
"span_kind": "SPAN_KIND_INTERNAL",
|
||||
"service_name": "chatbot-service",
|
||||
"start_time": "2024-03-14T12:00:00.000Z",
|
||||
"end_time": "2024-03-14T12:00:05.000Z",
|
||||
"duration": 5000,
|
||||
"status_code": "STATUS_CODE_OK",
|
||||
"status_message": "Success",
|
||||
"attributes": {
|
||||
"llm.model": "gpt-4-turbo",
|
||||
"llm.prompt": "What is the weather like today?",
|
||||
"llm.completion": "I need your location to provide weather information.",
|
||||
"llm.usage.prompt_tokens": 50,
|
||||
"llm.usage.completion_tokens": 25
|
||||
},
|
||||
"resource_attributes": {
|
||||
"service.name": "chatbot-service",
|
||||
"service.version": "1.2.3"
|
||||
},
|
||||
"span_attributes": {
|
||||
"user_id": "user_123",
|
||||
"session_id": "session_456"
|
||||
}
|
||||
}
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
**Parameters:**
|
||||
- `span_id` (path, required): The unique identifier of the span
|
||||
|
||||
**Response Fields:**
|
||||
- `attributes`: Core span data (LLM calls, tool usage, etc.)
|
||||
- `resource_attributes`: Service and infrastructure metadata
|
||||
- `span_attributes`: Custom attributes set by your application
|
||||
|
||||
### Get Span Metrics
|
||||
|
||||
Retrieve detailed metrics for a specific span.
|
||||
|
||||
<CodeGroup>
|
||||
```bash curl
|
||||
curl -X GET https://api.agentops.ai/public/v1/spans/span_456/metrics \
|
||||
-H "Authorization: Bearer YOUR_BEARER_TOKEN"
|
||||
```
|
||||
|
||||
```json Response
|
||||
{
|
||||
"total_tokens": 75,
|
||||
"prompt_tokens": 50,
|
||||
"completion_tokens": 25,
|
||||
"cache_read_input_tokens": 0,
|
||||
"reasoning_tokens": 0,
|
||||
"success_tokens": 75,
|
||||
"fail_tokens": 0,
|
||||
"indeterminate_tokens": 0,
|
||||
"prompt_cost": "0.0015",
|
||||
"completion_cost": "0.0005",
|
||||
"total_cost": "0.0020"
|
||||
}
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## MCP Server
|
||||
|
||||
AgentOps provides a [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server that exposes the Public API as tools for AI assistants. This allows AI models to directly query your AgentOps data during conversations.
|
||||
|
||||
### Configuration
|
||||
|
||||
Create an MCP server configuration file (typically `mcp_config.json`):
|
||||
|
||||
**Python-based configuration:**
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"agentops": {
|
||||
"command": "python",
|
||||
"args": ["-m", "agentops.mcp.server"],
|
||||
"env": {
|
||||
"AGENTOPS_API_KEY": "your-api-key-here"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Docker-based configuration:**
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"agentops": {
|
||||
"command": "docker",
|
||||
"args": [
|
||||
"run",
|
||||
"-i",
|
||||
"--rm",
|
||||
"-e",
|
||||
"AGENTOPS_API_KEY",
|
||||
"agentops/agentops-mcp:latest"
|
||||
],
|
||||
"env": {
|
||||
"AGENTOPS_API_KEY": "your-agentops-api-key-here"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Available Tools
|
||||
|
||||
The MCP server exposes the following tools that mirror the Public API endpoints:
|
||||
|
||||
#### `auth`
|
||||
Authorize using an AgentOps project API key.
|
||||
- **Parameters**: `api_key` (string) - Your AgentOps project API key
|
||||
- **Usage**: The server will automatically prompt for authentication when needed
|
||||
|
||||
#### `get_project`
|
||||
Get details about the current project.
|
||||
- **Parameters**: None
|
||||
- **Returns**: Project information including ID, name, and environment
|
||||
|
||||
#### `get_trace`
|
||||
Get comprehensive trace information by ID.
|
||||
- **Parameters**: `trace_id` (string) - The trace identifier
|
||||
- **Returns**: Trace details with associated spans
|
||||
|
||||
#### `get_trace_metrics`
|
||||
Get aggregated metrics for a specific trace.
|
||||
- **Parameters**: `trace_id` (string) - The trace identifier
|
||||
- **Returns**: Cost, token usage, and performance metrics
|
||||
|
||||
#### `get_span`
|
||||
Get detailed span information by ID.
|
||||
- **Parameters**: `span_id` (string) - The span identifier
|
||||
- **Returns**: Complete span data including attributes
|
||||
|
||||
#### `get_span_metrics`
|
||||
Get metrics for a specific span.
|
||||
- **Parameters**: `span_id` (string) - The span identifier
|
||||
- **Returns**: Span-specific cost and token metrics
|
||||
|
||||
### Environment Variables
|
||||
|
||||
The MCP server supports the following environment variables:
|
||||
|
||||
- `AGENTOPS_API_KEY`: Your AgentOps project API key
|
||||
- `HOST`: API endpoint (defaults to `https://api.agentops.ai`)
|
||||
@@ -0,0 +1,149 @@
|
||||
---
|
||||
title: "Recording Operations"
|
||||
description: "Track operations and LLM calls in your agent applications."
|
||||
---
|
||||
|
||||
AgentOps makes it easy to track operations and interactions in your AI applications with minimal setup.
|
||||
|
||||
## Basic Setup
|
||||
|
||||
The simplest way to get started with AgentOps is to initialize it at the beginning of your application:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps with your API key
|
||||
agentops.init("your-api-key")
|
||||
```
|
||||
|
||||
That's it! This single line of code will:
|
||||
- Automatically create a session for tracking your application run
|
||||
- Intercept and track all LLM calls to supported providers (OpenAI, Anthropic, etc.)
|
||||
- Record relevant metrics such as token counts, costs, and response times
|
||||
|
||||
You can also set a custom trace name during initialization:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize with custom trace name
|
||||
agentops.init("your-api-key", trace_name="my-custom-workflow")
|
||||
```
|
||||
|
||||
## Automatic Instrumentation
|
||||
|
||||
AgentOps automatically instruments calls to popular LLM providers without requiring any additional code:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init("your-api-key")
|
||||
|
||||
# Make LLM calls as usual - AgentOps will track them automatically
|
||||
client = OpenAI()
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=[{"role": "user", "content": "Hello, world!"}]
|
||||
)
|
||||
```
|
||||
|
||||
This works with many popular LLM providers including:
|
||||
- OpenAI
|
||||
- Anthropic
|
||||
- Google (Gemini)
|
||||
- Cohere
|
||||
- And more
|
||||
|
||||
## Advanced: Using Decorators for Detailed Instrumentation
|
||||
|
||||
For more detailed tracking, AgentOps provides decorators that allow you to explicitly instrument your code. This is optional but can provide more context in the dashboard.
|
||||
|
||||
### `@operation` Decorator
|
||||
|
||||
The `@operation` decorator helps track specific operations in your application:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import operation
|
||||
|
||||
@operation
|
||||
def process_data(data):
|
||||
# Process the data
|
||||
return result
|
||||
```
|
||||
|
||||
### `@agent` Decorator
|
||||
|
||||
If you use agent classes, you can track them with the `@agent` decorator:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import agent, operation
|
||||
|
||||
@agent
|
||||
class ResearchAgent:
|
||||
@operation
|
||||
def search(self, query):
|
||||
# Implementation of search
|
||||
return f"Results for: {query}"
|
||||
|
||||
def research_workflow(topic):
|
||||
agent = ResearchAgent()
|
||||
results = agent.search(topic)
|
||||
return results
|
||||
|
||||
results = research_workflow("quantum computing")
|
||||
```
|
||||
|
||||
### `@tool` Decorator
|
||||
|
||||
Track tool usage and costs with the `@tool` decorator. You can specify costs to get total cost tracking directly in your dashboard summary:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import tool
|
||||
|
||||
@tool(cost=0.05)
|
||||
def web_search(query):
|
||||
# Tool implementation
|
||||
return f"Search results for: {query}"
|
||||
|
||||
@tool
|
||||
def calculator(expression):
|
||||
# Tool without cost tracking
|
||||
return eval(expression)
|
||||
```
|
||||
|
||||
### `@trace` Decorator
|
||||
|
||||
Create custom traces to group related operations using the `@trace` decorator. This is the recommended approach for most applications:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import trace, agent, operation
|
||||
|
||||
# Initialize AgentOps without auto-starting session since we use @trace
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@trace(name="customer-service-workflow", tags=["customer-support"])
|
||||
def customer_service_workflow(customer_id):
|
||||
agent = ResearchAgent()
|
||||
results = agent.search(f"customer {customer_id}")
|
||||
return results
|
||||
```
|
||||
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Keep it Simple**: For most applications, just initializing AgentOps with `agentops.init()` is sufficient.
|
||||
|
||||
2. **Use @trace for Custom Workflows**: When you need to group operations, use the `@trace` decorator instead of manual trace management.
|
||||
|
||||
3. **Meaningful Names and Tags**: When using decorators, choose descriptive names and relevant tags to make them easier to identify in the dashboard.
|
||||
|
||||
4. **Cost Tracking**: Use the `@tool` decorator with cost parameters to track tool usage costs in your dashboard.
|
||||
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,268 @@
|
||||
---
|
||||
title: "SDK Reference"
|
||||
description: "All functions and classes exposed in the top layer of the SDK"
|
||||
---
|
||||
|
||||
# SDK Reference
|
||||
|
||||
This reference documents the functions and classes available with `import agentops` for the Python SDK. The AgentOps SDK is designed for easy integration with your agent applications, offering both simple auto-instrumentation and more detailed manual tracing capabilities.
|
||||
|
||||
<Note>
|
||||
This documentation covers the Python SDK. A TypeScript/JavaScript SDK is also available - see our [TypeScript SDK guide](/v2/usage/typescript-sdk) for details.
|
||||
</Note>
|
||||
|
||||
## Core Functions
|
||||
|
||||
These are the primary functions you'll use to initialize and configure AgentOps in your application.
|
||||
|
||||
### `init()`
|
||||
|
||||
Initializes the AgentOps SDK and automatically starts tracking your application.
|
||||
|
||||
**Parameters**:
|
||||
|
||||
- `api_key` (str, optional): API Key for AgentOps services. If not provided, the key will be read from the `AGENTOPS_API_KEY` environment variable.
|
||||
- `endpoint` (str, optional): The endpoint for the AgentOps service. If not provided, will be read from the `AGENTOPS_API_ENDPOINT` environment variable. Defaults to 'https://api.agentops.ai'.
|
||||
- `app_url` (str, optional): The dashboard URL for the AgentOps app. If not provided, will be read from the `AGENTOPS_APP_URL` environment variable. Defaults to 'https://app.agentops.ai'.
|
||||
- `max_wait_time` (int, optional): The maximum time to wait in milliseconds before flushing the queue. Defaults to 5,000 (5 seconds).
|
||||
- `max_queue_size` (int, optional): The maximum size of the event queue. Defaults to 512.
|
||||
- `default_tags` (List[str], optional): Default tags for the sessions that can be used for grouping or sorting later (e.g. ["GPT-4"]).
|
||||
- `tags` (List[str], optional): **[Deprecated]** Use `default_tags` instead. Will be removed in v4.0.
|
||||
- `instrument_llm_calls` (bool, optional): Whether to instrument LLM calls automatically. Defaults to True.
|
||||
- `auto_start_session` (bool, optional): Whether to start a session automatically when the client is created. Set to False if running in a Jupyter Notebook. Defaults to True.
|
||||
- `auto_init` (bool, optional): Whether to automatically initialize the client on import. Defaults to True.
|
||||
- `skip_auto_end_session` (bool, optional): Don't automatically end session based on your framework's decision-making. Defaults to False.
|
||||
- `env_data_opt_out` (bool, optional): Whether to opt out of collecting environment data. Defaults to False.
|
||||
- `log_level` (str, int, optional): The log level to use for the client. Defaults to 'INFO'.
|
||||
- `fail_safe` (bool, optional): Whether to suppress errors and continue execution when possible. Defaults to False.
|
||||
- `exporter_endpoint` (str, optional): Endpoint for the exporter. If not provided, will be read from the `AGENTOPS_EXPORTER_ENDPOINT` environment variable. Defaults to 'https://otlp.agentops.ai/v1/traces'.
|
||||
- `export_flush_interval` (int, optional): Time interval in milliseconds between automatic exports of telemetry data. Defaults to 1000.
|
||||
- `trace_name` (str, optional): Custom name for the automatically created trace. If not provided, a default name will be used.
|
||||
|
||||
**Returns**:
|
||||
|
||||
- If `auto_start_session=True`, returns the created Session object. Otherwise, returns None.
|
||||
|
||||
**Example**:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Basic initialization with automatic session creation
|
||||
agentops.init("your-api-key")
|
||||
|
||||
# Initialize with custom trace name
|
||||
agentops.init("your-api-key", trace_name="my-workflow")
|
||||
```
|
||||
|
||||
### `configure()`
|
||||
|
||||
Updates client configuration after initialization. Supports the same parameters as `init()`.
|
||||
|
||||
**Parameters**:
|
||||
|
||||
- `api_key` (str, optional): API Key for AgentOps services.
|
||||
- `endpoint` (str, optional): The endpoint for the AgentOps service.
|
||||
- `app_url` (str, optional): The dashboard URL for the AgentOps app.
|
||||
- `max_wait_time` (int, optional): Maximum time to wait in milliseconds before flushing the queue.
|
||||
- `max_queue_size` (int, optional): Maximum size of the event queue.
|
||||
- `default_tags` (List[str], optional): Default tags for the sessions.
|
||||
- `instrument_llm_calls` (bool, optional): Whether to instrument LLM calls.
|
||||
- `auto_start_session` (bool, optional): Whether to start a session automatically.
|
||||
- `auto_init` (bool, optional): Whether to automatically initialize the client on import.
|
||||
- `skip_auto_end_session` (bool, optional): Don't automatically end session.
|
||||
- `env_data_opt_out` (bool, optional): Whether to opt out of collecting environment data.
|
||||
- `log_level` (str, int, optional): The log level to use for the client.
|
||||
- `fail_safe` (bool, optional): Whether to suppress errors and continue execution.
|
||||
- `exporter` (object, optional): Custom span exporter for OpenTelemetry trace data.
|
||||
- `processor` (object, optional): Custom span processor for OpenTelemetry trace data.
|
||||
- `exporter_endpoint` (str, optional): Endpoint for the exporter.
|
||||
- `export_flush_interval` (int, optional): Time interval in milliseconds between automatic exports of telemetry data.
|
||||
- `trace_name` (str, optional): Custom name for traces.
|
||||
|
||||
**Example**:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize first
|
||||
agentops.init()
|
||||
|
||||
# Later, update configuration
|
||||
agentops.configure(
|
||||
max_wait_time=10000,
|
||||
max_queue_size=200,
|
||||
default_tags=["production", "gpt-4"],
|
||||
trace_name="production-workflow"
|
||||
)
|
||||
```
|
||||
|
||||
### `get_client()`
|
||||
|
||||
Gets the singleton client instance. Most users won't need to use this function directly.
|
||||
|
||||
**Returns**:
|
||||
|
||||
- The AgentOps client instance.
|
||||
|
||||
## Trace Management
|
||||
|
||||
These functions help you manage the lifecycle of tracking traces.
|
||||
|
||||
### `start_trace()`
|
||||
|
||||
Starts a new AgentOps trace manually. This is useful when you've disabled automatic session creation or need multiple separate traces.
|
||||
|
||||
**Parameters**:
|
||||
|
||||
- `trace_name` (str, optional): Name for the trace. If not provided, a default name will be used.
|
||||
- `tags` (Union[Dict[str, Any], List[str]], optional): Optional tags to attach to the trace, useful for filtering in the dashboard. Can be a list of strings or a dict of key-value pairs.
|
||||
|
||||
**Returns**:
|
||||
|
||||
- TraceContext object representing the started trace.
|
||||
|
||||
**Example**:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# Initialize without auto-starting a session
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
# Start a trace manually
|
||||
trace = agentops.start_trace("customer-service-workflow", tags=["customer-query"])
|
||||
```
|
||||
|
||||
### `end_trace()`
|
||||
|
||||
Ends a specific trace or all active traces.
|
||||
|
||||
**Parameters**:
|
||||
|
||||
- `trace` (TraceContext, optional): The specific trace to end. If not provided, all active traces will be ended.
|
||||
- `end_state` (str, optional): The end state for the trace(s). You can use any descriptive string that makes sense for your application (e.g., "Success", "Indeterminate", "Error", "Timeout", etc.).
|
||||
|
||||
**Example**:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
|
||||
# End a specific trace
|
||||
trace = agentops.start_trace("my-workflow")
|
||||
# ... your code ...
|
||||
agentops.end_trace(trace, "Success")
|
||||
|
||||
# End all active traces
|
||||
agentops.end_trace(end_state="Emergency_Shutdown")
|
||||
```
|
||||
|
||||
### `update_trace_metadata()`
|
||||
|
||||
Updates metadata on the currently running trace. This is useful for adding context, tracking progress, or storing intermediate results during trace execution.
|
||||
|
||||
**Parameters**:
|
||||
|
||||
- `metadata` (Dict[str, Any]): Dictionary of key-value pairs to set as trace metadata. Values must be strings, numbers, booleans, or lists of these types. Lists are automatically converted to JSON string representation.
|
||||
- `prefix` (str, optional): Prefix for metadata attributes. Defaults to "trace.metadata". Ignored for semantic convention attributes.
|
||||
|
||||
**Returns**:
|
||||
|
||||
- `bool`: True if metadata was successfully updated, False otherwise.
|
||||
|
||||
**Features**:
|
||||
|
||||
- **Semantic Convention Support**: User-friendly keys like "tags", "agent_name", "workflow_name" are automatically mapped to OpenTelemetry semantic conventions.
|
||||
- **Custom Attributes**: Non-semantic keys are prefixed with the specified prefix (default: "trace.metadata").
|
||||
- **Type Safety**: Validates input types and converts lists to JSON strings for OpenTelemetry compatibility.
|
||||
- **Error Handling**: Returns boolean success indicator and logs warnings for invalid data.
|
||||
|
||||
**Example**:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops import update_trace_metadata
|
||||
|
||||
# Initialize and start trace with initial tags
|
||||
agentops.init(auto_start_session=False)
|
||||
trace = agentops.start_trace("ai-workflow", tags=["startup", "initialization"])
|
||||
|
||||
# Your code here...
|
||||
|
||||
# Update metadata mid-run with new tags and operation info
|
||||
update_trace_metadata({
|
||||
"operation_name": "OpenAI GPT-4o-mini",
|
||||
"tags": ["ai-agent", "processing", "gpt-4"], # Updates tags
|
||||
"status": "processing"
|
||||
})
|
||||
|
||||
# End the trace
|
||||
agentops.end_trace(trace, "Success")
|
||||
```
|
||||
|
||||
For detailed examples and use cases, see [Manual Trace Control](/v2/usage/manual-trace-control#updating-trace-metadata-during-execution).
|
||||
|
||||
## Decorators for Detailed Instrumentation
|
||||
|
||||
For more granular control, AgentOps provides decorators that explicitly track different components of your application. **The `@trace` decorator is the recommended approach for creating custom traces**, especially in multi-threaded environments. These decorators are imported from `agentops.sdk.decorators`.
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import trace, agent, operation, tool
|
||||
|
||||
# Initialize without automatic session creation
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
# Create and run a trace using the decorator
|
||||
@trace
|
||||
def my_workflow():
|
||||
# Your workflow code here
|
||||
pass
|
||||
|
||||
# Run the workflow, which creates and manages the trace
|
||||
my_workflow()
|
||||
```
|
||||
|
||||
### Available Decorators
|
||||
|
||||
- `@trace`: Creates a trace span for grouping related operations
|
||||
- `@agent`: Creates an agent span for tracking agent operations
|
||||
- `@operation` / `@task`: Creates operation/task spans for tracking specific operations (these are aliases)
|
||||
- `@workflow`: Creates workflow spans for organizing related operations
|
||||
- `@tool`: Creates tool spans for tracking tool usage and cost in agent operations. Supports cost parameter for tracking tool usage costs.
|
||||
|
||||
**Tool Decorator Example**:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import tool
|
||||
|
||||
@tool(cost=0.05)
|
||||
def web_search(query):
|
||||
# Tool implementation with cost tracking
|
||||
return f"Search results for: {query}"
|
||||
|
||||
@tool
|
||||
def calculator(expression):
|
||||
# Tool without cost tracking
|
||||
return eval(expression)
|
||||
```
|
||||
|
||||
See [Decorators](/v2/concepts/decorators) for more detailed documentation on using these decorators.
|
||||
|
||||
## Legacy Functions
|
||||
|
||||
<Warning>
|
||||
The following functions are **deprecated** and will be removed in v4.0. They are maintained for backward compatibility with older versions of the SDK and integrations. New code should use the functions and decorators described above instead. When used, these functions will log deprecation warnings.
|
||||
</Warning>
|
||||
|
||||
- `start_session()`: **Deprecated.** Legacy function for starting sessions. Use `@trace` decorator or `start_trace()` instead.
|
||||
- `end_session()`: **Deprecated.** Legacy function for ending sessions. Use `end_trace()` instead.
|
||||
- `record(event)`: **Deprecated.** Legacy function to record an event. Replaced by decorator-based tracing.
|
||||
- `track_agent()`: **Deprecated.** Legacy decorator for marking agents. Replaced by the `@agent` decorator.
|
||||
- `track_tool()`: **Deprecated.** Legacy decorator for marking tools. Replaced by the `@tool` decorator.
|
||||
- `ToolEvent()`, `ErrorEvent()`, `ActionEvent()`, `LLMEvent()`: **Deprecated.** Legacy event types. Replaced by automatic instrumentation and decorators.
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,644 @@
|
||||
---
|
||||
title: "Trace Decorator"
|
||||
description: "Create custom traces with the @trace decorator"
|
||||
---
|
||||
|
||||
## Basic Usage
|
||||
|
||||
### Simple Trace Creation
|
||||
|
||||
The `@trace` decorator automatically creates a trace span that encompasses the entire function execution. You can optionally specify custom names and tags to better organize and categorize your traces:
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import trace
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@trace(name="customer-workflow", tags=["production", "customer-service"])
|
||||
def my_workflow():
|
||||
"""A simple workflow wrapped in a trace"""
|
||||
print("🚀 Starting customer workflow...")
|
||||
print("📋 Processing customer request...")
|
||||
# Your application logic here
|
||||
print("✅ Customer workflow completed successfully!")
|
||||
return "Workflow completed"
|
||||
|
||||
# Run the function - this creates and manages the trace automatically
|
||||
print("🎬 Running traced workflow...")
|
||||
result = my_workflow()
|
||||
print(f"📊 Result: {result}")
|
||||
```
|
||||
|
||||
Both `name` and `tags` parameters are optional. If no name is provided, the function name will be used as the trace name.
|
||||
|
||||
### Custom Trace Names
|
||||
|
||||
You can specify custom names for your traces:
|
||||
|
||||
```python
|
||||
@trace(name="customer-onboarding-flow")
|
||||
def onboard_customer(customer_data):
|
||||
"""Customer onboarding process"""
|
||||
print(f"👋 Onboarding customer: {customer_data['name']}")
|
||||
print("📝 Creating customer profile...")
|
||||
print("📧 Sending welcome email...")
|
||||
print("✅ Customer onboarding complete!")
|
||||
return f"Onboarded customer: {customer_data['name']}"
|
||||
|
||||
@trace(name="data-processing-pipeline")
|
||||
def process_data(input_data):
|
||||
"""Data processing workflow"""
|
||||
print(f"📊 Processing {len(input_data)} data items...")
|
||||
print("🔄 Applying transformations...")
|
||||
print("✅ Data processing complete!")
|
||||
return f"Processed {len(input_data)} items"
|
||||
|
||||
# Usage examples
|
||||
customer = {"name": "Alice Johnson", "email": "alice@example.com"}
|
||||
result1 = onboard_customer(customer)
|
||||
print(f"📋 Onboarding result: {result1}")
|
||||
|
||||
data_items = ["item1", "item2", "item3", "item4", "item5"]
|
||||
result2 = process_data(data_items)
|
||||
print(f"📋 Processing result: {result2}")
|
||||
```
|
||||
|
||||
### Adding Tags to Traces
|
||||
|
||||
Tags help categorize and filter traces in your dashboard:
|
||||
|
||||
```python
|
||||
@trace(tags=["production", "high-priority"])
|
||||
def critical_workflow():
|
||||
"""Critical production workflow"""
|
||||
print("🚨 Executing critical production workflow...")
|
||||
print("⚡ High priority processing...")
|
||||
print("✅ Critical task completed successfully!")
|
||||
return "Critical task completed"
|
||||
|
||||
@trace(name="user-analysis", tags=["analytics", "user-behavior"])
|
||||
def analyze_user_behavior(user_id):
|
||||
"""Analyze user behavior patterns"""
|
||||
print(f"🔍 Analyzing behavior for user: {user_id}")
|
||||
print("📈 Gathering user interaction data...")
|
||||
print("🧠 Running behavior analysis algorithms...")
|
||||
print("✅ User behavior analysis complete!")
|
||||
return f"Analysis complete for user {user_id}"
|
||||
|
||||
# Usage examples
|
||||
print("🎬 Running critical workflow...")
|
||||
result1 = critical_workflow()
|
||||
print(f"📊 Critical workflow result: {result1}")
|
||||
|
||||
print("\n🎬 Running user analysis...")
|
||||
result2 = analyze_user_behavior("user_12345")
|
||||
print(f"📊 Analysis result: {result2}")
|
||||
```
|
||||
|
||||
## Integration with Other Decorators
|
||||
|
||||
### Combining with Agent and Operation Decorators
|
||||
|
||||
The `@trace` decorator works seamlessly with other AgentOps decorators:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import trace, agent, operation, tool
|
||||
|
||||
# Initialize AgentOps without auto-starting session since we use @trace
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@agent
|
||||
class DataAnalysisAgent:
|
||||
def __init__(self):
|
||||
print("🤖 DataAnalysisAgent initialized")
|
||||
|
||||
@operation
|
||||
def collect_data(self, source):
|
||||
print(f"📊 Collecting data from {source}...")
|
||||
data = f"Data collected from {source}"
|
||||
print(f"✅ Data collection complete: {data}")
|
||||
return data
|
||||
|
||||
@tool(cost=0.05)
|
||||
def analyze_data(self, data):
|
||||
print(f"🧠 Analyzing data: {data}")
|
||||
analysis = f"Analysis of {data}"
|
||||
print(f"✅ Analysis complete: {analysis}")
|
||||
return analysis
|
||||
|
||||
@operation
|
||||
def generate_report(self, analysis):
|
||||
print(f"📝 Generating report from: {analysis}")
|
||||
report = f"Report: {analysis}"
|
||||
print(f"✅ Report generated: {report}")
|
||||
return report
|
||||
|
||||
@trace(name="complete-analysis-workflow")
|
||||
def run_analysis_workflow(data_source):
|
||||
"""Complete data analysis workflow"""
|
||||
print(f"🚀 Starting analysis workflow for: {data_source}")
|
||||
print("=" * 50)
|
||||
|
||||
agent = DataAnalysisAgent()
|
||||
|
||||
# Collect data
|
||||
print("\n📋 Step 1: Data Collection")
|
||||
data = agent.collect_data(data_source)
|
||||
|
||||
# Analyze data
|
||||
print("\n📋 Step 2: Data Analysis")
|
||||
analysis = agent.analyze_data(data)
|
||||
|
||||
# Generate report
|
||||
print("\n📋 Step 3: Report Generation")
|
||||
report = agent.generate_report(analysis)
|
||||
|
||||
print("\n🎉 Workflow completed successfully!")
|
||||
print("=" * 50)
|
||||
|
||||
return {
|
||||
"source": data_source,
|
||||
"report": report
|
||||
}
|
||||
|
||||
# Usage
|
||||
print("🎬 Running complete analysis workflow...")
|
||||
result = run_analysis_workflow("customer_database")
|
||||
print(f"\n📊 Final Result:")
|
||||
print(f" Source: {result['source']}")
|
||||
print(f" Report: {result['report']}")
|
||||
```
|
||||
|
||||
## Async Function Support
|
||||
|
||||
The `@trace` decorator fully supports async functions:
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
import agentops
|
||||
from agentops.sdk.decorators import trace, operation
|
||||
|
||||
# Initialize AgentOps without auto-starting session since we use @trace
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@operation
|
||||
async def fetch_user_data(user_id):
|
||||
"""Simulate async data fetching"""
|
||||
print(f"🌐 Fetching data for user: {user_id}")
|
||||
await asyncio.sleep(1) # Simulate API call
|
||||
data = f"User data for {user_id}"
|
||||
print(f"✅ Data fetched: {data}")
|
||||
return data
|
||||
|
||||
@operation
|
||||
async def process_user_data(user_data):
|
||||
"""Simulate async data processing"""
|
||||
print(f"⚙️ Processing user data: {user_data}")
|
||||
await asyncio.sleep(0.5) # Simulate processing
|
||||
processed = f"Processed: {user_data}"
|
||||
print(f"✅ Processing complete: {processed}")
|
||||
return processed
|
||||
|
||||
@trace(name="async-user-workflow")
|
||||
async def async_user_workflow(user_id):
|
||||
"""Async workflow for user processing"""
|
||||
print(f"🚀 Starting async workflow for user: {user_id}")
|
||||
print("=" * 45)
|
||||
|
||||
print("\n📋 Step 1: Fetching user data")
|
||||
user_data = await fetch_user_data(user_id)
|
||||
|
||||
print("\n📋 Step 2: Processing user data")
|
||||
processed_data = await process_user_data(user_data)
|
||||
|
||||
print("\n🎉 Async workflow completed!")
|
||||
print("=" * 45)
|
||||
|
||||
return processed_data
|
||||
|
||||
# Usage
|
||||
async def main():
|
||||
print("🎬 Running async user workflow...")
|
||||
result = await async_user_workflow("user_123")
|
||||
print(f"\n📊 Final Result: {result}")
|
||||
print("✨ Check your AgentOps dashboard to see the traced async workflow!")
|
||||
|
||||
# Run the async workflow
|
||||
print("🔄 Starting async demo...")
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
## Error Handling and Trace States
|
||||
|
||||
### Automatic Error Handling
|
||||
|
||||
The `@trace` decorator automatically handles exceptions and sets appropriate trace states:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import trace
|
||||
|
||||
# Initialize AgentOps without auto-starting session since we use @trace
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@trace(name="error-prone-workflow")
|
||||
def risky_operation():
|
||||
"""Operation that might fail"""
|
||||
import random
|
||||
|
||||
print("🎲 Running risky operation...")
|
||||
print("⚠️ This operation has a 50% chance of failure")
|
||||
|
||||
if random.random() < 0.5:
|
||||
print("❌ Operation failed!")
|
||||
raise ValueError("Random failure occurred")
|
||||
|
||||
print("✅ Operation succeeded!")
|
||||
return "Operation succeeded"
|
||||
|
||||
# The trace will automatically be marked with failure state if an exception occurs
|
||||
print("🎬 Testing automatic error handling...")
|
||||
for i in range(3):
|
||||
print(f"\n🔄 Attempt {i+1}:")
|
||||
try:
|
||||
result = risky_operation()
|
||||
print(f"📊 Success: {result}")
|
||||
break
|
||||
except ValueError as e:
|
||||
print(f"📊 Operation failed: {e}")
|
||||
print("🔍 Trace automatically ended with error state")
|
||||
```
|
||||
|
||||
### Custom Error Handling
|
||||
|
||||
You can implement custom error handling within traced functions:
|
||||
|
||||
```python
|
||||
@trace(name="robust-workflow")
|
||||
def robust_operation(data):
|
||||
"""Operation with custom error handling"""
|
||||
print(f"🚀 Starting robust operation with data: {data}")
|
||||
|
||||
try:
|
||||
# Risky operation
|
||||
if not data:
|
||||
print("⚠️ No data provided!")
|
||||
raise ValueError("No data provided")
|
||||
|
||||
# Process data
|
||||
print("⚙️ Processing data...")
|
||||
result = f"Processed: {data}"
|
||||
print(f"✅ Processing successful: {result}")
|
||||
return {"success": True, "result": result}
|
||||
|
||||
except ValueError as e:
|
||||
# Handle specific errors
|
||||
print(f"❌ Validation error: {e}")
|
||||
return {"success": False, "error": str(e)}
|
||||
except Exception as e:
|
||||
# Handle unexpected errors
|
||||
print(f"💥 Unexpected error: {e}")
|
||||
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
||||
|
||||
# Usage examples
|
||||
print("\n🎬 Testing custom error handling...")
|
||||
|
||||
print("\n📋 Test 1: Valid data")
|
||||
result1 = robust_operation("valid_data")
|
||||
print(f"📊 Result: {result1}")
|
||||
|
||||
print("\n📋 Test 2: Empty data")
|
||||
result2 = robust_operation("")
|
||||
print(f"📊 Result: {result2}")
|
||||
|
||||
print("\n📋 Test 3: None data")
|
||||
result3 = robust_operation(None)
|
||||
print(f"📊 Result: {result3}")
|
||||
```
|
||||
|
||||
## Real-World Examples
|
||||
|
||||
### E-commerce Order Processing
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import trace, agent, operation, tool
|
||||
from openai import OpenAI
|
||||
import agentops
|
||||
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@agent
|
||||
class OrderProcessor:
|
||||
def __init__(self):
|
||||
print("🛒 OrderProcessor initialized")
|
||||
|
||||
@tool(cost=0.01)
|
||||
def validate_payment(self, payment_info):
|
||||
"""Payment validation service"""
|
||||
print(f"💳 Validating payment: {payment_info['card']}")
|
||||
result = {"valid": True, "transaction_id": "txn_123"}
|
||||
print(f"✅ Payment validation successful: {result['transaction_id']}")
|
||||
return result
|
||||
|
||||
@tool(cost=0.02)
|
||||
def check_inventory(self, product_id, quantity):
|
||||
"""Inventory check service"""
|
||||
print(f"📦 Checking inventory for {product_id} (qty: {quantity})")
|
||||
result = {"available": True, "reserved": quantity}
|
||||
print(f"✅ Inventory check complete: {quantity} units available")
|
||||
return result
|
||||
|
||||
@operation
|
||||
def calculate_shipping(self, address, items):
|
||||
"""Calculate shipping costs"""
|
||||
print(f"🚚 Calculating shipping to {address['city']}, {address['state']}")
|
||||
result = {"cost": 9.99, "method": "standard"}
|
||||
print(f"✅ Shipping calculated: ${result['cost']} ({result['method']})")
|
||||
return result
|
||||
|
||||
@tool(cost=0.005)
|
||||
def send_confirmation_email(self, email, order_details):
|
||||
"""Email service"""
|
||||
print(f"📧 Sending confirmation email to {email}")
|
||||
result = f"Confirmation sent to {email}"
|
||||
print(f"✅ Email sent successfully")
|
||||
return result
|
||||
|
||||
@trace(name="order-processing", tags=["ecommerce", "orders"])
|
||||
def process_order(order_data):
|
||||
"""Complete order processing workflow"""
|
||||
print(f"🚀 Starting order processing for {order_data['customer_email']}")
|
||||
print("=" * 60)
|
||||
|
||||
processor = OrderProcessor()
|
||||
|
||||
try:
|
||||
# Validate payment
|
||||
print("\n📋 Step 1: Payment Validation")
|
||||
payment_result = processor.validate_payment(order_data["payment"])
|
||||
if not payment_result["valid"]:
|
||||
print("❌ Payment validation failed!")
|
||||
return {"success": False, "error": "Payment validation failed"}
|
||||
|
||||
# Check inventory for all items
|
||||
print("\n📋 Step 2: Inventory Check")
|
||||
for item in order_data["items"]:
|
||||
inventory_result = processor.check_inventory(
|
||||
item["product_id"],
|
||||
item["quantity"]
|
||||
)
|
||||
if not inventory_result["available"]:
|
||||
print(f"❌ Item {item['product_id']} not available!")
|
||||
return {"success": False, "error": f"Item {item['product_id']} not available"}
|
||||
|
||||
# Calculate shipping
|
||||
print("\n📋 Step 3: Shipping Calculation")
|
||||
shipping = processor.calculate_shipping(
|
||||
order_data["shipping_address"],
|
||||
order_data["items"]
|
||||
)
|
||||
|
||||
# Send confirmation
|
||||
print("\n📋 Step 4: Confirmation Email")
|
||||
confirmation = processor.send_confirmation_email(
|
||||
order_data["customer_email"],
|
||||
{
|
||||
"items": order_data["items"],
|
||||
"shipping": shipping,
|
||||
"payment": payment_result
|
||||
}
|
||||
)
|
||||
|
||||
print("\n🎉 Order processing completed successfully!")
|
||||
print("=" * 60)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"order_id": "ORD_12345",
|
||||
"payment": payment_result,
|
||||
"shipping": shipping,
|
||||
"confirmation": confirmation
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"💥 Order processing failed: {e}")
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
# Usage
|
||||
print("🎬 Running e-commerce order processing demo...")
|
||||
|
||||
order = {
|
||||
"customer_email": "customer@example.com",
|
||||
"payment": {"card": "****1234", "amount": 99.99},
|
||||
"items": [{"product_id": "PROD_001", "quantity": 2}],
|
||||
"shipping_address": {"city": "New York", "state": "NY"}
|
||||
}
|
||||
|
||||
result = process_order(order)
|
||||
|
||||
print(f"\n📊 ORDER PROCESSING RESULT:")
|
||||
print(f" Success: {result['success']}")
|
||||
if result['success']:
|
||||
print(f" Order ID: {result['order_id']}")
|
||||
print(f" Transaction: {result['payment']['transaction_id']}")
|
||||
print(f" Shipping: ${result['shipping']['cost']}")
|
||||
else:
|
||||
print(f" Error: {result['error']}")
|
||||
```
|
||||
|
||||
### Data Analysis Workflow
|
||||
|
||||
```python
|
||||
from agentops.sdk.decorators import trace, agent, operation, tool
|
||||
from openai import OpenAI
|
||||
import agentops
|
||||
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@agent
|
||||
class DataAnalysisAgent:
|
||||
def __init__(self):
|
||||
self.client = OpenAI()
|
||||
print("🤖 DataAnalysisAgent initialized")
|
||||
|
||||
@operation
|
||||
def collect_data(self, source):
|
||||
"""Simulate data collection"""
|
||||
print(f"📊 Collecting data from {source}...")
|
||||
data = f"Raw data collected from {source}: [sample_data_1, sample_data_2, sample_data_3]"
|
||||
print(f"✅ Data collection complete: {len(data)} characters collected")
|
||||
return data
|
||||
|
||||
@operation
|
||||
def analyze_data_with_llm(self, data):
|
||||
"""Use LLM to analyze the collected data"""
|
||||
print("🧠 Analyzing data with LLM...")
|
||||
response = self.client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a data analyst. Analyze the provided data and give insights."},
|
||||
{"role": "user", "content": f"Please analyze this data: {data}"}
|
||||
]
|
||||
)
|
||||
analysis = response.choices[0].message.content
|
||||
print(f"✅ LLM analysis complete: {len(analysis)} characters generated")
|
||||
return analysis
|
||||
|
||||
@tool(cost=0.05)
|
||||
def generate_visualization(self, analysis):
|
||||
"""Generate data visualization"""
|
||||
print("📈 Generating visualization...")
|
||||
visualization = f"Chart generated for: {analysis[:50]}..."
|
||||
print(f"✅ Visualization generated: {visualization}")
|
||||
return visualization
|
||||
|
||||
@operation
|
||||
def generate_report(self, analysis, visualization):
|
||||
"""Generate final report using LLM"""
|
||||
print("📝 Generating final report with LLM...")
|
||||
response = self.client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a report writer. Create a professional data analysis report."},
|
||||
{"role": "user", "content": f"Create a report based on this analysis: {analysis} and visualization: {visualization}"}
|
||||
]
|
||||
)
|
||||
report = response.choices[0].message.content
|
||||
print(f"✅ Final report generated: {len(report)} characters")
|
||||
return report
|
||||
|
||||
@trace(name="data-analysis-workflow", tags=["analytics", "reporting"])
|
||||
def run_data_analysis(data_source):
|
||||
"""Complete data analysis workflow with LLM integration"""
|
||||
print(f"🚀 Starting data analysis workflow for: {data_source}")
|
||||
print("=" * 60)
|
||||
|
||||
agent = DataAnalysisAgent()
|
||||
|
||||
# Collect data
|
||||
print("\n📋 Step 1: Data Collection")
|
||||
raw_data = agent.collect_data(data_source)
|
||||
|
||||
# Analyze data using LLM
|
||||
print("\n📋 Step 2: LLM Analysis")
|
||||
analysis = agent.analyze_data_with_llm(raw_data)
|
||||
|
||||
# Generate visualization
|
||||
print("\n📋 Step 3: Visualization Generation")
|
||||
visualization = agent.generate_visualization(analysis)
|
||||
|
||||
# Generate final report using LLM
|
||||
print("\n📋 Step 4: Report Generation")
|
||||
report = agent.generate_report(analysis, visualization)
|
||||
|
||||
print("\n🎉 Workflow completed successfully!")
|
||||
print("=" * 60)
|
||||
|
||||
return {
|
||||
"source": data_source,
|
||||
"raw_data": raw_data,
|
||||
"analysis": analysis,
|
||||
"visualization": visualization,
|
||||
"final_report": report
|
||||
}
|
||||
|
||||
# Usage
|
||||
print("🎬 Running data analysis workflow demo...")
|
||||
|
||||
result = run_data_analysis("customer_database")
|
||||
|
||||
print(f"\n📊 ANALYSIS RESULTS:")
|
||||
print(f" Data Source: {result['source']}")
|
||||
print(f" Raw Data: {result['raw_data'][:80]}...")
|
||||
print(f" Analysis Preview: {result['analysis'][:100]}...")
|
||||
print(f" Visualization: {result['visualization']}")
|
||||
print(f" Final Report Preview: {result['final_report'][:150]}...")
|
||||
|
||||
print(f"\n✨ Analysis complete! Check your AgentOps dashboard to see the traced workflow.")
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Use Meaningful Names
|
||||
|
||||
Choose descriptive names that clearly indicate what the trace represents:
|
||||
|
||||
```python
|
||||
# Good
|
||||
@trace(name="user-authentication-flow")
|
||||
def authenticate_user(credentials):
|
||||
pass
|
||||
|
||||
@trace(name="payment-processing-pipeline")
|
||||
def process_payment(payment_data):
|
||||
pass
|
||||
|
||||
# Less descriptive
|
||||
@trace(name="trace1")
|
||||
def some_function():
|
||||
pass
|
||||
```
|
||||
|
||||
### 2. Add Relevant Tags
|
||||
|
||||
Use tags to categorize traces for easier filtering and analysis:
|
||||
|
||||
```python
|
||||
@trace(name="order-fulfillment", tags=["ecommerce", "fulfillment", "high-priority"])
|
||||
def fulfill_order(order_id):
|
||||
pass
|
||||
|
||||
@trace(name="data-sync", tags=["background-job", "data-processing"])
|
||||
def sync_data():
|
||||
pass
|
||||
```
|
||||
|
||||
### 3. Keep Traces Focused
|
||||
|
||||
Each trace should represent a logical unit of work:
|
||||
|
||||
```python
|
||||
# Good - focused on a single workflow
|
||||
@trace(name="customer-onboarding")
|
||||
def onboard_customer(customer_data):
|
||||
validate_customer(customer_data)
|
||||
create_account(customer_data)
|
||||
send_welcome_email(customer_data)
|
||||
|
||||
# Less focused - mixing different concerns
|
||||
@trace(name="mixed-operations")
|
||||
def do_everything():
|
||||
onboard_customer(data1)
|
||||
process_orders(data2)
|
||||
generate_reports(data3)
|
||||
```
|
||||
|
||||
### 4. Handle Errors Appropriately
|
||||
|
||||
Implement proper error handling within traced functions:
|
||||
|
||||
```python
|
||||
@trace(name="data-processing")
|
||||
def process_data(data):
|
||||
try:
|
||||
# Main processing logic
|
||||
result = complex_processing(data)
|
||||
return {"success": True, "result": result}
|
||||
except ValidationError as e:
|
||||
# Expected errors
|
||||
return {"success": False, "error": "validation_failed", "details": str(e)}
|
||||
except Exception as e:
|
||||
# Unexpected errors
|
||||
logger.error(f"Unexpected error in data processing: {e}")
|
||||
return {"success": False, "error": "processing_failed"}
|
||||
```
|
||||
|
||||
The `@trace` decorator provides a powerful and flexible way to organize your application's telemetry data. By creating logical groupings of operations, you can better understand your application's behavior and performance characteristics in the AgentOps dashboard.
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
title: "Track Endpoint Decorator"
|
||||
description: "HTTP endpoint tracing for Flask applications using the @track_endpoint decorator"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The `@track_endpoint` decorator provides HTTP endpoint tracing for Flask applications with automatic request/response monitoring. It's designed to work seamlessly with Flask and extends the functionality of the basic `@trace` decorator.
|
||||
|
||||
## Quick Example with OpenAI
|
||||
|
||||
Here's a simple Flask endpoint that generates text using OpenAI:
|
||||
|
||||
```python
|
||||
from flask import Flask, request
|
||||
from openai import OpenAI
|
||||
import agentops
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init(
|
||||
api_key="your-api-key",
|
||||
auto_start_session=False, # Required for endpoint tracing
|
||||
)
|
||||
|
||||
app = Flask(__name__)
|
||||
client = OpenAI()
|
||||
|
||||
@app.route("/api/generate", methods=["POST"])
|
||||
@agentops.track_endpoint(
|
||||
name="generate_text",
|
||||
tags=["ai", "openai"]
|
||||
)
|
||||
def generate_text():
|
||||
"""Generate text using OpenAI"""
|
||||
data = request.get_json()
|
||||
prompt = data.get("prompt", "Hello!")
|
||||
|
||||
# OpenAI call is automatically traced
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
max_tokens=150
|
||||
)
|
||||
|
||||
return {
|
||||
"text": response.choices[0].message.content,
|
||||
"usage": {
|
||||
"total_tokens": response.usage.total_tokens
|
||||
}
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
```
|
||||
|
||||
The decorator automatically captures:
|
||||
- HTTP request data (method, URL, headers, body)
|
||||
- HTTP response data (status code, headers, body)
|
||||
- OpenAI API calls and their results
|
||||
- Any errors that occur during request processing
|
||||
|
||||
You can customize tracing with parameters like:
|
||||
- `name`: Custom name for the trace
|
||||
- `tags`: List or dict of tags for categorizing traces
|
||||
- `capture_request`: Whether to capture request data (default: True)
|
||||
- `capture_response`: Whether to capture response data (default: True)
|
||||
@@ -0,0 +1,245 @@
|
||||
---
|
||||
title: "Tracking Agents"
|
||||
description: "Associate operations with specific named agents"
|
||||
---
|
||||
|
||||
AgentOps automatically tracks LLM interactions in your application. For more detailed tracking, especially in multi-agent systems, you can use the `@agent` decorator to associate operations with specific agents.
|
||||
|
||||
## Using the Agent Decorator
|
||||
|
||||
For structured tracking in complex applications, you can use the `@agent` decorator to explicitly identify different agents in your system:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import agent, operation, trace
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps without auto-starting session since we use @trace
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
# Create a decorated agent class
|
||||
@agent(name='ResearchAgent')
|
||||
class MyAgent:
|
||||
def __init__(self):
|
||||
self.client = OpenAI()
|
||||
|
||||
@operation
|
||||
def search(self, query):
|
||||
response = self.client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=[{"role": "user", "content": f"Research about: {query}"}]
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
|
||||
# Create a trace to group the agent operations
|
||||
@trace(name="research-workflow")
|
||||
def research_workflow(topic):
|
||||
agent = MyAgent()
|
||||
result = agent.search(topic)
|
||||
return result
|
||||
|
||||
# Execute the function to properly register the agent span
|
||||
result = research_workflow("quantum computing")
|
||||
```
|
||||
|
||||
If you don't specify a name, the agent will use the class name by default:
|
||||
|
||||
```python
|
||||
@agent
|
||||
class ResearchAgent:
|
||||
# This agent will have the name "ResearchAgent"
|
||||
pass
|
||||
```
|
||||
|
||||
## Basic Agent Tracking (Simple Applications)
|
||||
|
||||
For simple applications, AgentOps will automatically track your LLM calls without additional configuration:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init("your-api-key")
|
||||
|
||||
# Create a simple agent function
|
||||
def research_agent(query):
|
||||
client = OpenAI()
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=[{"role": "user", "content": f"Research about: {query}"}]
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
|
||||
# Use your agent - all LLM calls will be tracked automatically
|
||||
result = research_agent("quantum computing")
|
||||
```
|
||||
|
||||
## Multi-Agent Systems
|
||||
|
||||
For complex multi-agent systems, you can organize multiple agents within a single trace:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import agent, operation, tool, trace
|
||||
|
||||
# Initialize AgentOps without auto-starting session since we use @trace
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@agent
|
||||
class DataCollectionAgent:
|
||||
@tool(cost=0.02)
|
||||
def fetch_data(self, source):
|
||||
return f"Data from {source}"
|
||||
|
||||
@agent
|
||||
class AnalysisAgent:
|
||||
@operation
|
||||
def analyze_data(self, data):
|
||||
return f"Analysis of {data}"
|
||||
|
||||
@agent
|
||||
class ReportingAgent:
|
||||
@tool(cost=0.01)
|
||||
def generate_report(self, analysis):
|
||||
return f"Report: {analysis}"
|
||||
|
||||
@trace(name="multi-agent-workflow")
|
||||
def collaborative_workflow(data_source):
|
||||
"""Workflow using multiple specialized agents"""
|
||||
|
||||
# Data collection
|
||||
collector = DataCollectionAgent()
|
||||
raw_data = collector.fetch_data(data_source)
|
||||
|
||||
# Analysis
|
||||
analyzer = AnalysisAgent()
|
||||
analysis = analyzer.analyze_data(raw_data)
|
||||
|
||||
# Reporting
|
||||
reporter = ReportingAgent()
|
||||
report = reporter.generate_report(analysis)
|
||||
|
||||
return {
|
||||
"source": data_source,
|
||||
"analysis": analysis,
|
||||
"report": report
|
||||
}
|
||||
|
||||
# Run the collaborative workflow
|
||||
result = collaborative_workflow("customer_database")
|
||||
```
|
||||
|
||||
## Agent Communication and Coordination
|
||||
|
||||
You can track complex agent interactions and communication patterns:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from agentops.sdk.decorators import agent, operation, tool, trace
|
||||
|
||||
# Initialize AgentOps without auto-starting session since we use @trace
|
||||
agentops.init("your-api-key", auto_start_session=False)
|
||||
|
||||
@agent
|
||||
class CoordinatorAgent:
|
||||
def __init__(self):
|
||||
self.task_queue = []
|
||||
|
||||
@operation
|
||||
def assign_task(self, task, agent_type):
|
||||
self.task_queue.append({"task": task, "agent": agent_type})
|
||||
return f"Task assigned to {agent_type}: {task}"
|
||||
|
||||
@operation
|
||||
def collect_results(self, results):
|
||||
return f"Collected {len(results)} results"
|
||||
|
||||
@agent
|
||||
class WorkerAgent:
|
||||
def __init__(self, agent_id):
|
||||
self.agent_id = agent_id
|
||||
|
||||
@tool(cost=0.05)
|
||||
def process_task(self, task):
|
||||
return f"Agent {self.agent_id} processed: {task}"
|
||||
|
||||
@trace(name="coordinated-processing")
|
||||
def coordinated_processing_workflow(tasks):
|
||||
"""Workflow with agent coordination"""
|
||||
coordinator = CoordinatorAgent()
|
||||
workers = [WorkerAgent(f"worker_{i}") for i in range(3)]
|
||||
|
||||
# Assign tasks
|
||||
assignments = []
|
||||
for i, task in enumerate(tasks):
|
||||
worker_type = f"worker_{i % len(workers)}"
|
||||
assignment = coordinator.assign_task(task, worker_type)
|
||||
assignments.append(assignment)
|
||||
|
||||
# Process tasks
|
||||
results = []
|
||||
for i, task in enumerate(tasks):
|
||||
worker = workers[i % len(workers)]
|
||||
result = worker.process_task(task)
|
||||
results.append(result)
|
||||
|
||||
# Collect results
|
||||
summary = coordinator.collect_results(results)
|
||||
|
||||
return {
|
||||
"assignments": assignments,
|
||||
"results": results,
|
||||
"summary": summary
|
||||
}
|
||||
|
||||
# Run coordinated workflow
|
||||
tasks = ["analyze_data", "generate_report", "send_notification"]
|
||||
result = coordinated_processing_workflow(tasks)
|
||||
```
|
||||
|
||||
## Dashboard Visualization
|
||||
|
||||
All operations are automatically associated with the agent that originated them. Agents are given a name which is what you will see in the dashboard.
|
||||
|
||||
<Frame type="glass" caption="Operations are labeled with the name of the Agent that originated them">
|
||||
<img height="200" src="/images/agent-name.png" />
|
||||
</Frame>
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Start Simple**: For most applications, just using `agentops.init()` is sufficient.
|
||||
|
||||
2. **Use Decorators When Needed**: Add the `@agent` decorator when you need to clearly distinguish between multiple agents in your system.
|
||||
|
||||
3. **Meaningful Names**: Choose descriptive names for your agents to make them easier to identify in the dashboard.
|
||||
|
||||
4. **Organize with Traces**: Use the `@trace` decorator to group related agent operations into logical workflows.
|
||||
|
||||
5. **Track Costs**: Use the `@tool` decorator with cost parameters to track the expenses associated with agent operations.
|
||||
|
||||
6. **Agent Specialization**: Create specialized agents for different types of tasks to improve observability and maintainability.
|
||||
|
||||
## Migration from Session Decorator
|
||||
|
||||
If you're migrating from the legacy `@session` decorator, replace it with the `@trace` decorator:
|
||||
|
||||
```python
|
||||
# New approach (recommended)
|
||||
from agentops.sdk.decorators import trace, agent
|
||||
|
||||
@trace(name="my-workflow")
|
||||
def my_workflow():
|
||||
# workflow code
|
||||
pass
|
||||
|
||||
# Old approach (deprecated)
|
||||
from agentops.sdk.decorators import session, agent
|
||||
|
||||
@session
|
||||
def my_workflow():
|
||||
# workflow code
|
||||
pass
|
||||
```
|
||||
|
||||
The `@trace` decorator provides the same functionality as the legacy `@session` decorator but with more flexibility and better integration with the new trace management features.
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
title: "Tracking LLM Calls"
|
||||
description: "Tracking LLM Calls using the AgentOps SDK"
|
||||
---
|
||||
|
||||
## Automatic LLM Call Tracking
|
||||
|
||||
AgentOps makes tracking LLM calls incredibly simple. Just initialize the SDK with your API key, and AgentOps will automatically track all your LLM calls:
|
||||
|
||||
```python
|
||||
import agentops
|
||||
from openai import OpenAI
|
||||
|
||||
# Initialize AgentOps
|
||||
agentops.init("your-api-key")
|
||||
|
||||
# Make LLM calls as usual - they'll be tracked automatically
|
||||
client = OpenAI()
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=[{"role": "user", "content": "Hello, world!"}]
|
||||
)
|
||||
```
|
||||
|
||||
### How it works
|
||||
|
||||
When the AgentOps SDK detects a supported LLM provider module installed, it will automatically
|
||||
start tracking its usage. No further work is required from you! 😊
|
||||
|
||||
### Supported LLM Providers
|
||||
|
||||
AgentOps supports automatic tracking for many popular LLM providers, including:
|
||||
|
||||
- OpenAI
|
||||
- Anthropic
|
||||
- Google (Gemini)
|
||||
- LiteLLM
|
||||
- And more
|
||||
|
||||
### Not working?
|
||||
|
||||
Try these steps:
|
||||
1. Make sure you have the latest version of the AgentOps SDK installed. We are constantly updating it to support new LLM libraries and releases.
|
||||
2. Make sure you are calling `agentops.init()` *after* importing the LLM module but *before* you are calling the LLM method.
|
||||
3. Make sure the `instrument_llm_calls` parameter of `agentops.init()` is set to `True` (default).
|
||||
|
||||
Still not working? Please let us know! You can find us on [Discord](https://discord.gg/DR2abmETjZ),
|
||||
[GitHub](https://github.com/AgentOps-AI/agentops),
|
||||
or email us at [engineering@agentops.ai](mailto:engineering@agentops.ai).
|
||||
|
||||
To get started, just follow the quick start guide.
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Quick Start" icon="code" href="/v2/quickstart">
|
||||
Get started with AgentOps in under 5 minutes
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<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="module" src="/scripts/adjust_api_dynamically.js"></script>
|
||||
@@ -0,0 +1,139 @@
|
||||
---
|
||||
title: "TypeScript SDK"
|
||||
description: "Get started with the AgentOps TypeScript SDK for Node.js applications"
|
||||
---
|
||||
|
||||
# TypeScript SDK
|
||||
|
||||
AgentOps provides TypeScript/JavaScript support through two SDK options:
|
||||
|
||||
## Modern TypeScript SDK (Recommended)
|
||||
|
||||
The modern TypeScript SDK is built on OpenTelemetry standards and provides comprehensive instrumentation for AI agents.
|
||||
|
||||
### Installation
|
||||
|
||||
```bash
|
||||
npm install agentops
|
||||
```
|
||||
|
||||
### Quick Start
|
||||
|
||||
```typescript
|
||||
import { agentops } from 'agentops';
|
||||
|
||||
// Initialize with environment variable AGENTOPS_API_KEY
|
||||
await agentops.init();
|
||||
|
||||
// Or pass API key explicitly
|
||||
await agentops.init({
|
||||
apiKey: 'your-api-key'
|
||||
});
|
||||
|
||||
// Your AI agent code here - instrumentation happens automatically!
|
||||
```
|
||||
|
||||
### Features
|
||||
|
||||
- 🔌 **Plugin Architecture**: Dynamic loading and configuration of instrumentors
|
||||
- 🤖 **GenAI Support**: Built-in support for OpenTelemetry GenAI semantic conventions
|
||||
- 📊 **Standards Compliant**: Exports to any OpenTelemetry-compatible collector
|
||||
- 🛠️ **Framework Agnostic**: Instrument multiple agent frameworks simultaneously
|
||||
- 🔧 **TypeScript First**: Full TypeScript support with comprehensive type definitions
|
||||
|
||||
### OpenAI Agents Integration
|
||||
|
||||
The SDK provides first-class support for the [OpenAI Agents SDK](https://github.com/openai/openai-agents-js/):
|
||||
|
||||
```typescript
|
||||
import { agentops } from 'agentops';
|
||||
import { Agent, run } from '@openai/agents';
|
||||
|
||||
// Initialize AgentOps first
|
||||
await agentops.init();
|
||||
|
||||
// Create your agent with tools and instructions
|
||||
const agent = new Agent({
|
||||
name: 'My Assistant',
|
||||
instructions: 'You are a helpful assistant.',
|
||||
tools: [/* your tools */],
|
||||
});
|
||||
|
||||
// Run the agent - instrumentation happens automatically
|
||||
const result = await run(agent, "Hello, how can you help me?");
|
||||
console.log(result.finalOutput);
|
||||
```
|
||||
|
||||
Automatically captures:
|
||||
- **Agent Lifecycle**: Track agent creation, execution, and completion
|
||||
- **LLM Generation**: Capture model requests, responses, and token usage
|
||||
- **Function Calls**: Monitor tool usage and function execution
|
||||
- **Audio Processing**: Observe speech-to-text and text-to-speech operations
|
||||
- **Handoffs**: Track agent-to-agent communication and workflow transitions
|
||||
|
||||
### Debug Logging
|
||||
|
||||
Enable detailed instrumentation logs:
|
||||
|
||||
```bash
|
||||
DEBUG=agentops:* node your-app.js
|
||||
```
|
||||
|
||||
## Legacy TypeScript SDK (Alpha)
|
||||
|
||||
<Warning>
|
||||
The legacy TypeScript SDK has limited functionality compared to the Python SDK. The modern TypeScript SDK above is recommended for new projects.
|
||||
</Warning>
|
||||
|
||||
### Installation
|
||||
|
||||
```bash
|
||||
npm install agentops
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
```typescript
|
||||
import OpenAI from "openai";
|
||||
import { Client } from 'agentops';
|
||||
|
||||
const openai = new OpenAI();
|
||||
|
||||
const agentops = new Client({
|
||||
apiKey: "your-agentops-api-key",
|
||||
tags: ["typescript", "example"],
|
||||
patchApi: [openai] // Automatically record OpenAI calls
|
||||
});
|
||||
|
||||
// Sample OpenAI call (automatically recorded)
|
||||
async function chat() {
|
||||
const completion = await openai.chat.completions.create({
|
||||
messages: [
|
||||
{ "role": "system", "content": "You are a helpful assistant." },
|
||||
{ "role": "user", "content": "Hello!" }
|
||||
],
|
||||
model: "gpt-3.5-turbo",
|
||||
});
|
||||
return completion;
|
||||
}
|
||||
|
||||
// Track custom functions
|
||||
function customFunction(x: string) {
|
||||
console.log(x);
|
||||
return 5;
|
||||
}
|
||||
|
||||
const wrappedFunction = agentops.wrap(customFunction);
|
||||
wrappedFunction("hello");
|
||||
|
||||
// Run your agent
|
||||
chat().then(() => {
|
||||
agentops.endSession("Success");
|
||||
});
|
||||
```
|
||||
|
||||
## Getting Help
|
||||
|
||||
- [Discord Community](https://discord.gg/FagdcwwXRR)
|
||||
- [GitHub Issues](https://github.com/AgentOps-AI/agentops/issues)
|
||||
- [Documentation](https://docs.agentops.ai)
|
||||
Reference in New Issue
Block a user