Skip to content

Commit 8467517

Browse files
authored
[AI] [Projects] add ConnectedAgentToolDefinition sample (Azure#49530)
* [AI] [Projects] add ConnectedAgentToolDefinition sample * add markdown sample for connected agent * fix typo * update changelog
1 parent ece37b1 commit 8467517

File tree

3 files changed

+411
-1
lines changed

3 files changed

+411
-1
lines changed

sdk/ai/Azure.AI.Projects/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 1.0.0-beta.8 (Unreleased)
44

5-
### Features Added
5+
### Sample Updates
6+
* New sample added for connected agent tool.
67

78
### Breaking Changes
89

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# Sample for use of an agent with a connected agent in Azure.AI.Projects.
2+
3+
To enable your Agent to use a connected agent, you use `ConnectedAgentToolDefinition` along with the agent id, name, and a description.
4+
1. First we need to create agent client and read the environment variables, which will be used in the next steps.
5+
6+
```C# Snippet:ConnectedAgent_CreateProject
7+
var connectionString = System.Environment.GetEnvironmentVariable("PROJECT_CONNECTION_STRING");
8+
var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME");
9+
10+
var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential());
11+
12+
AgentsClient agentClient = projectClient.GetAgentsClient();
13+
```
14+
15+
2. Next we will create the connected agent using the agent client. This agent will be used to initialize the `ConnectedAgentToolDefinition`.
16+
17+
Synchronous sample:
18+
```C# Snippet:ConnectedAgent_CreateConnectedAgent
19+
Agent connectedAgent = agentClient.CreateAgent(
20+
model: modelDeploymentName,
21+
name: "stock_price_bot",
22+
instructions: "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.");
23+
24+
ConnectedAgentToolDefinition connectedAgentDefinition = new(new ConnectedAgentDetails(connectedAgent.Id, connectedAgent.Name, "Gets the stock price of a company"));
25+
```
26+
27+
Asynchronous sample:
28+
```C# Snippet:ConnectedAgentAsync_CreateConnectedAgent
29+
Agent connectedAgent = await agentClient.CreateAgentAsync(
30+
model: modelDeploymentName,
31+
name: "stock_price_bot",
32+
instructions: "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.");
33+
34+
ConnectedAgentToolDefinition connectedAgentDefinition = new(new ConnectedAgentDetails(connectedAgent.Id, connectedAgent.Name, "Gets the stock price of a company"));
35+
```
36+
37+
3. We will use the `ConnectedAgentToolDefinition` during the agent initialization.
38+
39+
Synchronous sample:
40+
```C# Snippet:ConnectedAgent_CreateAgent
41+
Agent agent = agentClient.CreateAgent(
42+
model: modelDeploymentName,
43+
name: "my-assistant",
44+
instructions: "You are a helpful assistant, and use the connected agent to get stock prices.",
45+
tools: [ connectedAgentDefinition ]);
46+
```
47+
48+
Asynchronous sample:
49+
```C# Snippet:ConnectedAgentAsync_CreateAgent
50+
Agent agent = await agentClient.CreateAgentAsync(
51+
model: modelDeploymentName,
52+
name: "my-assistant",
53+
instructions: "You are a helpful assistant, and use the connected agent to get stock prices.",
54+
tools: [ connectedAgentDefinition ]);
55+
```
56+
57+
4. Now we will create the thread, add the message, containing a question for agent and start the run.
58+
59+
Synchronous sample:
60+
```C# Snippet:ConnectedAgent_CreateThreadMessage
61+
AgentThread thread = agentClient.CreateThread();
62+
63+
// Create message to thread
64+
ThreadMessage message = agentClient.CreateMessage(
65+
thread.Id,
66+
MessageRole.User,
67+
"What is the stock price of Microsoft?");
68+
69+
// Run the agent
70+
ThreadRun run = agentClient.CreateRun(thread, agent);
71+
do
72+
{
73+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
74+
run = agentClient.GetRun(thread.Id, run.Id);
75+
}
76+
while (run.Status == RunStatus.Queued
77+
|| run.Status == RunStatus.InProgress);
78+
79+
Assert.AreEqual(
80+
RunStatus.Completed,
81+
run.Status,
82+
run.LastError?.Message);
83+
```
84+
85+
Asynchronous sample:
86+
```C# Snippet:ConnectedAgentAsync_CreateThreadMessage
87+
AgentThread thread = await agentClient.CreateThreadAsync();
88+
89+
// Create message to thread
90+
ThreadMessage message = await agentClient.CreateMessageAsync(
91+
thread.Id,
92+
MessageRole.User,
93+
"What is the stock price of Microsoft?");
94+
95+
// Run the agent
96+
ThreadRun run = await agentClient.CreateRunAsync(thread, agent);
97+
do
98+
{
99+
await Task.Delay(TimeSpan.FromMilliseconds(500));
100+
run = await agentClient.GetRunAsync(thread.Id, run.Id);
101+
}
102+
while (run.Status == RunStatus.Queued
103+
|| run.Status == RunStatus.InProgress);
104+
105+
Assert.AreEqual(
106+
RunStatus.Completed,
107+
run.Status,
108+
run.LastError?.Message);
109+
```
110+
111+
5. Print the agent messages to console in chronological order.
112+
113+
Synchronous sample:
114+
```C# Snippet:ConnectedAgent_Print
115+
PageableList<ThreadMessage> messages = agentClient.GetMessages(
116+
threadId: thread.Id,
117+
order: ListSortOrder.Ascending
118+
);
119+
120+
foreach (ThreadMessage threadMessage in messages)
121+
{
122+
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
123+
foreach (MessageContent contentItem in threadMessage.ContentItems)
124+
{
125+
if (contentItem is MessageTextContent textItem)
126+
{
127+
string response = textItem.Text;
128+
if (textItem.Annotations != null)
129+
{
130+
foreach (MessageTextAnnotation annotation in textItem.Annotations)
131+
{
132+
if (annotation is MessageTextUrlCitationAnnotation urlAnnotation)
133+
{
134+
response = response.Replace(urlAnnotation.Text, $" [{urlAnnotation.UrlCitation.Title}]({urlAnnotation.UrlCitation.Url})");
135+
}
136+
}
137+
}
138+
Console.Write($"Agent response: {response}");
139+
}
140+
else if (contentItem is MessageImageFileContent imageFileItem)
141+
{
142+
Console.Write($"<image from ID: {imageFileItem.FileId}");
143+
}
144+
Console.WriteLine();
145+
}
146+
}
147+
```
148+
149+
Asynchronous sample:
150+
```C# Snippet:ConnectedAgentAsync_Print
151+
PageableList<ThreadMessage> messages = await agentClient.GetMessagesAsync(
152+
threadId: thread.Id,
153+
order: ListSortOrder.Ascending
154+
);
155+
156+
foreach (ThreadMessage threadMessage in messages)
157+
{
158+
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
159+
foreach (MessageContent contentItem in threadMessage.ContentItems)
160+
{
161+
if (contentItem is MessageTextContent textItem)
162+
{
163+
string response = textItem.Text;
164+
if (textItem.Annotations != null)
165+
{
166+
foreach (MessageTextAnnotation annotation in textItem.Annotations)
167+
{
168+
if (annotation is MessageTextUrlCitationAnnotation urlAnnotation)
169+
{
170+
response = response.Replace(urlAnnotation.Text, $" [{urlAnnotation.UrlCitation.Title}]({urlAnnotation.UrlCitation.Url})");
171+
}
172+
}
173+
}
174+
Console.Write($"Agent response: {response}");
175+
}
176+
else if (contentItem is MessageImageFileContent imageFileItem)
177+
{
178+
Console.Write($"<image from ID: {imageFileItem.FileId}");
179+
}
180+
Console.WriteLine();
181+
}
182+
}
183+
```
184+
185+
6. Clean up resources by deleting thread and agent.
186+
187+
Synchronous sample:
188+
```C# Snippet:ConnectedAgentCleanup
189+
agentClient.DeleteThread(threadId: thread.Id);
190+
agentClient.DeleteAgent(agentId: agent.Id);
191+
agentClient.DeleteAgent(agentId: connectedAgent.Id);
192+
```
193+
194+
Asynchronous sample:
195+
```C# Snippet:ConnectedAgentCleanupAsync
196+
await agentClient.DeleteThreadAsync(threadId: thread.Id);
197+
await agentClient.DeleteAgentAsync(agentId: agent.Id);
198+
await agentClient.DeleteAgentAsync(agentId: connectedAgent.Id);
199+
```

0 commit comments

Comments
 (0)