Skip to content

Commit e275bf0

Browse files
authored
Create Basics.md
Original file name: Sample3_PersistentAgents_Basics.md, cleaned up the sample code, using uri. first pass.
1 parent f7281a3 commit e275bf0

File tree

1 file changed

+156
-0
lines changed
  • doc-samples/agents/c#/azure-ai-agent-sdk

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Sample for basic use of an agent in Azure.AI.Agents.
2+
3+
In this example we will demonstrate creation and basic use of an agent step by step.
4+
5+
1. First get `ProjectEndpoint` and `ModelDeploymentName` from config and create a `PersistentAgentsClient`.
6+
```C# Snippet:AgentsOverviewCreateAgentClient
7+
var projectEndpoint = configuration["ProjectEndpoint"];
8+
var modelDeploymentName = configuration["ModelDeploymentName"];
9+
PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential());
10+
```
11+
12+
2. Next we will need to create an agent.
13+
14+
Synchronous sample:
15+
```C# Snippet:AgentsOverviewCreateAgentSync
16+
PersistentAgent agent = client.CreateAgent(
17+
model: modelDeploymentName,
18+
name: "Math Tutor",
19+
instructions: "You are a personal math tutor. Write and run code to answer math questions."
20+
);
21+
```
22+
23+
Asynchronous sample:
24+
```C# Snippet:AgentsOverviewCreateAgent
25+
PersistentAgent agent = await client.CreateAgentAsync(
26+
model: modelDeploymentName,
27+
name: "Math Tutor",
28+
instructions: "You are a personal math tutor. Write and run code to answer math questions."
29+
);
30+
```
31+
32+
3. We will create thread as a separate resource.
33+
34+
Synchronous sample:
35+
```C# Snippet:AgentsOverviewCreateThreadSync
36+
PersistentAgentThread thread = client.CreateThread();
37+
```
38+
39+
Asynchronous sample:
40+
```C# Snippet:AgentsOverviewCreateThread
41+
PersistentAgentThread thread = await client.CreateThreadAsync();
42+
```
43+
44+
4. We will add the message to the thread, containing a question for agent. This message must have `User` role.
45+
46+
Synchronous sample:
47+
```C# Snippet:AgentsOverviewCreateMessageSync
48+
client.CreateMessage(
49+
thread.Id,
50+
MessageRole.User,
51+
"I need to solve the equation `3x + 11 = 14`. Can you help me?");
52+
```
53+
54+
Asynchronous sample:
55+
```C# Snippet:AgentsOverviewCreateMessage
56+
await client.CreateMessageAsync(
57+
thread.Id,
58+
MessageRole.User,
59+
"I need to solve the equation `3x + 11 = 14`. Can you help me?");
60+
```
61+
62+
5. Now we will need to create the run, which will assign agent to the thread.
63+
64+
Synchronous sample:
65+
```C# Snippet:AgentsOverviewCreateRunSync
66+
ThreadRun run = client.CreateRun(
67+
thread.Id,
68+
agent.Id,
69+
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
70+
```
71+
72+
Asynchronous sample:
73+
```C# Snippet:AgentsOverviewCreateRun
74+
ThreadRun run = await client.CreateRunAsync(
75+
thread.Id,
76+
agent.Id,
77+
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
78+
```
79+
80+
6. It may take some time to get the response from agent, so we will wait, when it will get to the terminal state. If the run is not successful, we will raise the assertion error with the last error message.
81+
82+
Synchronous sample:
83+
```C# Snippet:AgentsOverviewWaitForRunSync
84+
do
85+
{
86+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
87+
run = client.GetRun(thread.Id, run.Id);
88+
}
89+
while (run.Status == RunStatus.Queued
90+
|| run.Status == RunStatus.InProgress);
91+
```
92+
93+
Asynchronous sample:
94+
```C# Snippet:AgentsOverviewWaitForRun
95+
do
96+
{
97+
await Task.Delay(TimeSpan.FromMilliseconds(500));
98+
run = await client.GetRunAsync(thread.Id, run.Id);
99+
}
100+
while (run.Status == RunStatus.Queued
101+
|| run.Status == RunStatus.InProgress);
102+
```
103+
104+
7. Print the agent's messages to console in chronological order.
105+
106+
Synchronous sample:
107+
```C# Snippet:AgentsOverviewListUpdatedMessagesSync
108+
PageableList<ThreadMessage> messages = client.GetMessages(
109+
threadId: thread.Id,
110+
order: ListSortOrder.Ascending);
111+
112+
foreach (ThreadMessage threadMessage in messages)
113+
{
114+
foreach (MessageContent contentItem in threadMessage.ContentItems)
115+
{
116+
if (contentItem is MessageTextContent textItem)
117+
{
118+
Console.Write($"{threadMessage.Role}: {textItem.Text}");
119+
}
120+
Console.WriteLine();
121+
}
122+
}
123+
```
124+
125+
Asynchronous sample:
126+
```C# Snippet:AgentsOverviewListUpdatedMessages
127+
PageableList<ThreadMessage> messages = await client.GetMessagesAsync(
128+
threadId: thread.Id,
129+
order: ListSortOrder.Ascending);
130+
131+
foreach (ThreadMessage threadMessage in messages)
132+
{
133+
foreach (MessageContent contentItem in threadMessage.ContentItems)
134+
{
135+
if (contentItem is MessageTextContent textItem)
136+
{
137+
Console.Write($"{threadMessage.Role}: {textItem.Text}");
138+
}
139+
Console.WriteLine();
140+
}
141+
}
142+
```
143+
144+
8. Clean up resources by deleting thread and agent.
145+
146+
Synchronous sample:
147+
```C# Snippet:AgentsOverviewCleanupSync
148+
client.DeleteThread(threadId: thread.Id);
149+
client.DeleteAgent(agentId: agent.Id);
150+
```
151+
152+
Asynchronous sample:
153+
```C# Snippet:AgentsOverviewCleanup
154+
await client.DeleteThreadAsync(threadId: thread.Id);
155+
await client.DeleteAgentAsync(agentId: agent.Id);
156+
```

0 commit comments

Comments
 (0)