|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | + |
| 6 | +""" |
| 7 | +DESCRIPTION: |
| 8 | + This sample demonstrates how to use Agent operations with the Connected Agent tool from |
| 9 | + the Azure Agents service using a synchronous client. |
| 10 | +
|
| 11 | +USAGE: |
| 12 | + python sample_agents_connected_agent.py |
| 13 | +
|
| 14 | + Before running the sample: |
| 15 | +
|
| 16 | + pip install azure-ai-projects azure-identity |
| 17 | +
|
| 18 | + Set these environment variables with your own values: |
| 19 | + 1) PROJECT_CONNECTION_STRING - The project connection string, as found in the overview page of your |
| 20 | + Azure AI Foundry project. |
| 21 | + 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in |
| 22 | + the "Models + endpoints" tab in your Azure AI Foundry project. |
| 23 | +""" |
| 24 | + |
| 25 | +import os |
| 26 | +from azure.ai.projects import AIProjectClient |
| 27 | +from azure.ai.projects.models import ConnectedAgentTool, MessageRole |
| 28 | +from azure.identity import DefaultAzureCredential |
| 29 | + |
| 30 | + |
| 31 | +project_client = AIProjectClient.from_connection_string( |
| 32 | + credential=DefaultAzureCredential(), |
| 33 | + conn_str=os.environ["PROJECT_CONNECTION_STRING"], |
| 34 | +) |
| 35 | + |
| 36 | +connected_agent_name = "stock_price_bot" |
| 37 | + |
| 38 | +stock_price_agent = project_client.agents.create_agent( |
| 39 | + model=os.environ["MODEL_DEPLOYMENT_NAME"], |
| 40 | + name=connected_agent_name, |
| 41 | + instructions=( |
| 42 | + "Your job is to get the stock price of a company. If you don't know the realtime stock price, return the last known stock price." |
| 43 | + ), |
| 44 | +) |
| 45 | + |
| 46 | +# [START create_agent_with_connected_agent_tool] |
| 47 | +# Initialize Connected Agent tool with the agent id, name, and description |
| 48 | +connected_agent = ConnectedAgentTool(id=stock_price_agent.id, name=connected_agent_name, description="Gets the stock price of a company") |
| 49 | + |
| 50 | +# Create agent with the Connected Agent tool and process assistant run |
| 51 | +with project_client: |
| 52 | + agent = project_client.agents.create_agent( |
| 53 | + model=os.environ["MODEL_DEPLOYMENT_NAME"], |
| 54 | + name="my-assistant", |
| 55 | + instructions="You are a helpful assistant, and use the connected agent to get stock prices.", |
| 56 | + tools=connected_agent.definitions, |
| 57 | + ) |
| 58 | + # [END create_agent_with_connected_agent_tool] |
| 59 | + |
| 60 | + print(f"Created agent, ID: {agent.id}") |
| 61 | + |
| 62 | + # Create thread for communication |
| 63 | + thread = project_client.agents.create_thread() |
| 64 | + print(f"Created thread, ID: {thread.id}") |
| 65 | + |
| 66 | + # Create message to thread |
| 67 | + message = project_client.agents.create_message( |
| 68 | + thread_id=thread.id, |
| 69 | + role=MessageRole.USER, |
| 70 | + content="What is the stock price of Microsoft?", |
| 71 | + ) |
| 72 | + print(f"Created message, ID: {message.id}") |
| 73 | + |
| 74 | + # Create and process Agent run in thread with tools |
| 75 | + run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id) |
| 76 | + print(f"Run finished with status: {run.status}") |
| 77 | + |
| 78 | + if run.status == "failed": |
| 79 | + print(f"Run failed: {run.last_error}") |
| 80 | + |
| 81 | + # Delete the Agent when done |
| 82 | + project_client.agents.delete_agent(agent.id) |
| 83 | + print("Deleted agent") |
| 84 | + |
| 85 | + # Delete the connected Agent when done |
| 86 | + project_client.agents.delete_agent(stock_price_agent.id) |
| 87 | + print("Deleted connected agent") |
| 88 | + |
| 89 | + # Print the Agent's response message with optional citation |
| 90 | + response_message = project_client.agents.list_messages(thread_id=thread.id).get_last_message_by_role( |
| 91 | + MessageRole.AGENT |
| 92 | + ) |
| 93 | + if response_message: |
| 94 | + for text_message in response_message.text_messages: |
| 95 | + print(f"Agent response: {text_message.text.value}") |
| 96 | + for annotation in response_message.url_citation_annotations: |
| 97 | + print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})") |
0 commit comments