import os
from openai import OpenAI
from openinference.instrumentation.openai_agents import OpenAIAgentsInstrumentor
from gentrace import init, interaction
# Initialize Gentrace with OpenAI Agents instrumentation
init(
api_key=os.getenv("GENTRACE_API_KEY"),
base_url=os.getenv("GENTRACE_BASE_URL", "https://gentrace.ai/api"),
otel_setup={
"service_name": "openai-agents-demo",
"instrumentations": [OpenAIAgentsInstrumentor()],
},
)
# Define tools for your agent
def check_weather(city: str) -> str:
"""Get current weather for a city (simulated)."""
weather_data = {
"san francisco": "☁️ 62°F, cloudy",
"new york": "☀️ 75°F, sunny",
"london": "🌧️ 55°F, rainy",
}
return weather_data.get(city.lower(), f"No weather data for {city}")
# Define agent configuration
travel_agent = {
"name": "Travel Assistant",
"instructions": "You are a helpful travel agent. You can check weather and book flights for customers.",
"tools": [{"type": "function", "function": {"name": "check_weather", "description": "Get current weather for a city"}}],
}
# Initialize OpenAI client
from openai import OpenAI
client = OpenAI()
@interaction(name="travel_planning", pipeline_id=os.getenv("GENTRACE_PIPELINE_ID", ""))
def plan_trip(request: str) -> str:
"""Handle a travel planning request - automatically traced."""
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": travel_agent["instructions"]},
{"role": "user", "content": request}
],
tools=travel_agent["tools"],
)
return response.choices[0].message.content
# Usage
response = plan_trip("What's the weather like in San Francisco?")
print(f"Response: {response}")