| 1 | # First we will configure the OpenTelemetry |
| 2 | from opentelemetry import trace |
| 3 | from opentelemetry.exporter.otlp.proto.http.trace_exporter import ( |
| 4 | OTLPSpanExporter |
| 5 | ) |
| 6 | from opentelemetry.sdk.resources import Resource |
| 7 | from opentelemetry.sdk.trace import TracerProvider |
| 8 | from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 9 | from opentelemetry.instrumentation.openai import OpenAIInstrumentor |
| 10 | |
| 11 | def setup_telemetry(): |
| 12 | """Configure OpenTelemetry with HTTP exporter""" |
| 13 | # Create a resource with service name and other metadata |
| 14 | resource = Resource.create({ |
| 15 | "service.name": "autogen-demo", |
| 16 | "service.version": "1.0.0", |
| 17 | "deployment.environment": "development" |
| 18 | }) |
| 19 | |
| 20 | # Create TracerProvider with the resource |
| 21 | provider = TracerProvider(resource=resource) |
| 22 | |
| 23 | # Create BatchSpanProcessor with OTLPSpanExporter |
| 24 | processor = BatchSpanProcessor( |
| 25 | OTLPSpanExporter() |
| 26 | ) |
| 27 | provider.add_span_processor(processor) |
| 28 | |
| 29 | # Set the TracerProvider |
| 30 | trace.set_tracer_provider(provider) |
| 31 | |
| 32 | # Instrument OpenAI calls |
| 33 | OpenAIInstrumentor().instrument() |
| 34 | |
| 35 | # Now we can define and call the Agent |
| 36 | import asyncio |
| 37 | from autogen_agentchat.agents import AssistantAgent |
| 38 | from autogen_agentchat.ui import Console |
| 39 | from autogen_ext.models.openai import OpenAIChatCompletionClient |
| 40 | |
| 41 | |
| 42 | # Define a model client. You can use other model client that implements |
| 43 | # the `ChatCompletionClient` interface. |
| 44 | model_client = OpenAIChatCompletionClient( |
| 45 | model="gpt-4o", |
| 46 | # api_key="YOUR_API_KEY", |
| 47 | ) |
| 48 | |
| 49 | # Define a simple function tool that the agent can use. |
| 50 | # For this example, we use a fake weather tool for demonstration purposes. |
| 51 | async def get_weather(city: str) -> str: |
| 52 | """Get the weather for a given city.""" |
| 53 | return f"The weather in {city} is 73 degrees and Sunny." |
| 54 | |
| 55 | |
| 56 | # Define an AssistantAgent with the model, tool, system message, and reflection |
| 57 | # enabled. The system message instructs the agent via natural language. |
| 58 | agent = AssistantAgent( |
| 59 | name="weather_agent", |
| 60 | model_client=model_client, |
| 61 | tools=[get_weather], |
| 62 | system_message="You are a helpful assistant.", |
| 63 | reflect_on_tool_use=True, |
| 64 | model_client_stream=True, # Enable streaming tokens from the model client. |
| 65 | ) |
| 66 | |
| 67 | |
| 68 | # Run the agent and stream the messages to the console. |
| 69 | async def main() -> None: |
| 70 | tracer = trace.get_tracer(__name__) |
| 71 | with tracer.start_as_current_span("agent_conversation") as span: |
| 72 | task = "What is the weather in New York?" |
| 73 | |
| 74 | span.set_attribute("input", task) # Manually log the question |
| 75 | res = await Console(agent.run_stream(task=task)) |
| 76 | |
| 77 | # Manually log the response |
| 78 | span.set_attribute("output", res.messages[-1].content) |
| 79 | |
| 80 | # Close the connection to the model client. |
| 81 | await model_client.close() |
| 82 | |
| 83 | |
| 84 | if __name__ == "__main__": |
| 85 | setup_telemetry() |
| 86 | asyncio.run(main()) |