1# pylint: disable=line-too-long,useless-suppression
2# ------------------------------------
3# Copyright (c) Microsoft Corporation.
4# Licensed under the MIT License.
5# ------------------------------------
6
7"""
8DESCRIPTION:
9 This sample demonstrates how to use Agent operations with the Connected Agent tool from
10 the Azure Agents service using a synchronous client.
11
12USAGE:
13 python sample_agents_connected_agent.py
14
15 Before running the sample:
16
17 pip install azure-ai-projects azure-ai-agents azure-identity
18
19 Set these environment variables with your own values:
20 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
21 page of your Azure AI Foundry portal.
22 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23 the "Models + endpoints" tab in your Azure AI Foundry project.
24"""
25
26import os
27from azure.ai.projects import AIProjectClient
28from azure.ai.agents.models import ConnectedAgentTool, MessageRole
29from azure.identity import DefaultAzureCredential
30
31project_client = AIProjectClient(
32 endpoint=os.environ["PROJECT_ENDPOINT"],
33 credential=DefaultAzureCredential(),
34)
35
36connected_agent_name = "stock_price_bot"
37
38with project_client:
39 agents_client = project_client.agents
40
41 stock_price_agent = agents_client.create_agent(
42 model=os.environ["MODEL_DEPLOYMENT_NAME"],
43 name=connected_agent_name,
44 instructions=(
45 "Your job is to get the stock price of a company. If asked for the Microsoft stock price, always return $350."
46 ),
47 )
48
49 # [START create_agent_with_connected_agent_tool]
50 # Initialize Connected Agent tool with the agent id, name, and description
51 connected_agent = ConnectedAgentTool(
52 id=stock_price_agent.id, name=connected_agent_name, description="Gets the stock price of a company"
53 )
54
55 # Create agent with the Connected Agent tool and process assistant run
56 agent = agents_client.create_agent(
57 model=os.environ["MODEL_DEPLOYMENT_NAME"],
58 name="my-assistant",
59 instructions="You are a helpful assistant, and use the connected agents to get stock prices.",
60 tools=connected_agent.definitions,
61 )
62 # [END create_agent_with_connected_agent_tool]
63
64 print(f"Created agent, ID: {agent.id}")
65
66 # Create thread for communication
67 thread = agents_client.threads.create()
68 print(f"Created thread, ID: {thread.id}")
69
70 # Create message to thread
71 message = agents_client.messages.create(
72 thread_id=thread.id,
73 role=MessageRole.USER,
74 content="What is the stock price of Microsoft?",
75 )
76 print(f"Created message, ID: {message.id}")
77
78 # Create and process Agent run in thread with tools
79 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
80 print(f"Run finished with status: {run.status}")
81
82 if run.status == "failed":
83 print(f"Run failed: {run.last_error}")
84
85 # Delete the Agent when done
86 agents_client.delete_agent(agent.id)
87 print("Deleted agent")
88
89 # Delete the connected Agent when done
90 agents_client.delete_agent(stock_price_agent.id)
91 print("Deleted stock price agent")
92
93 # Fetch and log all messages
94 messages = agents_client.messages.list(thread_id=thread.id)
95 for msg in messages:
96 if msg.text_messages:
97 last_text = msg.text_messages[-1]
98 print(f"{msg.role}: {last_text.text.value}")