|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | + |
| 6 | +""" |
| 7 | +DESCRIPTION: |
| 8 | + This sample demonstrates how to use basic agent operations from |
| 9 | + the Azure Agents service using an asynchronous client with tracing to console. |
| 10 | +
|
| 11 | +USAGE: |
| 12 | + python sample_agents_basics_with_console_tracing_async.py |
| 13 | +
|
| 14 | + Before running the sample: |
| 15 | +
|
| 16 | + pip install azure-ai-projects azure-ai-agents azure-identity opentelemetry-sdk azure-core-tracing-opentelemetry aiohttp |
| 17 | +
|
| 18 | + If you want to export telemetry to OTLP endpoint (such as Aspire dashboard |
| 19 | + https://learn.microsoft.com/dotnet/aspire/fundamentals/dashboard/standalone?tabs=bash) |
| 20 | + install: |
| 21 | +
|
| 22 | + pip install azure-ai-projects opentelemetry-exporter-otlp-proto-grpc |
| 23 | +
|
| 24 | + Set these environment variables with your own values: |
| 25 | + 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview |
| 26 | + page of your Azure AI Foundry portal. |
| 27 | + 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in |
| 28 | + the "Models + endpoints" tab in your Azure AI Foundry project. |
| 29 | + 3) OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat |
| 30 | + messages, which may contain personal data. False by default. |
| 31 | +""" |
| 32 | + |
| 33 | +import asyncio |
| 34 | +import os |
| 35 | +from azure.ai.projects.aio import AIProjectClient |
| 36 | +from azure.ai.agents.models import ListSortOrder |
| 37 | +from azure.identity.aio import DefaultAzureCredential |
| 38 | + |
| 39 | +from azure.core.settings import settings |
| 40 | +settings.tracing_implementation = "opentelemetry" |
| 41 | + |
| 42 | +# Install opentelemetry with command "pip install azure-ai-projects opentelemetry-sdk". |
| 43 | +from opentelemetry import trace |
| 44 | +from opentelemetry.sdk.trace import TracerProvider |
| 45 | +from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter |
| 46 | + |
| 47 | +# Setup tracing to console |
| 48 | +# Requires opentelemetry-sdk |
| 49 | +span_exporter = ConsoleSpanExporter() |
| 50 | +tracer_provider = TracerProvider() |
| 51 | +tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter)) |
| 52 | +trace.set_tracer_provider(tracer_provider) |
| 53 | +tracer = trace.get_tracer(__name__) |
| 54 | + |
| 55 | +from azure.ai.agents.telemetry import AIAgentsInstrumentor |
| 56 | +AIAgentsInstrumentor().instrument() |
| 57 | + |
| 58 | + |
| 59 | +async def main() -> None: |
| 60 | + |
| 61 | + credential = DefaultAzureCredential() |
| 62 | + |
| 63 | + async with credential: |
| 64 | + |
| 65 | + project_client = AIProjectClient( |
| 66 | + endpoint=os.environ["PROJECT_ENDPOINT"], |
| 67 | + credential=credential, |
| 68 | + ) |
| 69 | + |
| 70 | + scenario = os.path.basename(__file__) |
| 71 | + with tracer.start_as_current_span(scenario): |
| 72 | + |
| 73 | + async with project_client: |
| 74 | + |
| 75 | + agent = await project_client.agents.create_agent( |
| 76 | + model=os.environ["MODEL_DEPLOYMENT_NAME"], name="my-agent", instructions="You are helpful agent" |
| 77 | + ) |
| 78 | + print(f"Created agent, agent ID: {agent.id}") |
| 79 | + |
| 80 | + thread = await project_client.agents.threads.create() |
| 81 | + print(f"Created thread, thread ID: {thread.id}") |
| 82 | + |
| 83 | + message = await project_client.agents.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke") |
| 84 | + print(f"Created message, message ID: {message.id}") |
| 85 | + |
| 86 | + run = await project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id) |
| 87 | + print(f"Run completed with status: {run.status}") |
| 88 | + |
| 89 | + await project_client.agents.delete_agent(agent.id) |
| 90 | + print("Deleted agent") |
| 91 | + |
| 92 | + messages = project_client.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING) |
| 93 | + async for msg in messages: |
| 94 | + if msg.text_messages: |
| 95 | + last_text = msg.text_messages[-1] |
| 96 | + print(f"{msg.role}: {last_text.text.value}") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + asyncio.run(main()) |
0 commit comments