Skip to content

Commit 8ca8d5e

Browse files
authored
Merge pull request #21 from RobiladK/patch-4
Create CodeInterpreterToolWithStreaming.md
2 parents b308829 + 540aadf commit 8ca8d5e

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Sample using agents with streaming in Azure.AI.Agents
2+
3+
In this example we will demonstrate the agent streaming support.
4+
5+
1. First we need to create agent client and read the environment variables that will be used in the next steps.
6+
```C# Snippet:AgentsStreamingAsync_CreateClient
7+
var projectEndpoint = configuration["ProjectEndpoint"];
8+
var modelDeploymentName = configuration["ModelDeploymentName"];
9+
PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential());
10+
```
11+
2. We will create agent with the Interpreter tool support. It is needed to allow fow writing mathematical formulas in [LaTeX](https://en.wikipedia.org/wiki/LaTeX) format.
12+
13+
Synchronous sample:
14+
```C# Snippet:AgentsStreaming_CreateAgent
15+
PersistentAgent agent = client.CreateAgent(
16+
model: modelDeploymentName,
17+
name: "My Friendly Test Agent",
18+
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
19+
tools: [new CodeInterpreterToolDefinition()]
20+
);
21+
```
22+
23+
Asynchronous sample:
24+
```C# Snippet:AgentsStreamingAsync_CreateAgent
25+
PersistentAgent agent = await client.CreateAgentAsync(
26+
model: modelDeploymentName,
27+
name: "My Friendly Test Agent",
28+
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
29+
tools: [new CodeInterpreterToolDefinition()]
30+
);
31+
```
32+
33+
3. Create `Thread` with the message.
34+
35+
Synchronous sample:
36+
```C# Snippet:AgentsStreaming_CreateThread
37+
PersistentAgentThread thread = client.CreateThread();
38+
39+
client.CreateMessage(
40+
thread.Id,
41+
MessageRole.User,
42+
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
43+
```
44+
45+
Asynchronous sample:
46+
```C# Snippet:AgentsStreamingAsync_CreateThread
47+
PersistentAgentThread thread = await client.CreateThreadAsync();
48+
49+
await client.CreateMessageAsync(
50+
thread.Id,
51+
MessageRole.User,
52+
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
53+
```
54+
55+
4. Read the output from the stream.
56+
57+
Synchronous sample:
58+
```C# Snippet:AgentsStreaming_StreamLoop
59+
foreach (StreamingUpdate streamingUpdate in client.CreateRunStreaming(thread.Id, agent.Id))
60+
{
61+
if (streamingUpdate is MessageContentUpdate contentUpdate)
62+
{
63+
Console.Write(contentUpdate?.Text);
64+
if (contentUpdate?.ImageFileId is not null)
65+
{
66+
Console.WriteLine($"[Image content file ID: {contentUpdate.ImageFileId}");
67+
Console.WriteLine();
68+
}
69+
}
70+
}
71+
```
72+
73+
Asynchronous sample:
74+
```C# Snippet:AgentsStreamingAsync_StreamLoop
75+
await foreach (StreamingUpdate streamingUpdate in client.CreateRunStreamingAsync(thread.Id, agent.Id))
76+
{
77+
if (streamingUpdate is MessageContentUpdate contentUpdate)
78+
{
79+
Console.Write(contentUpdate?.Text);
80+
if (contentUpdate?.ImageFileId is not null)
81+
{
82+
Console.WriteLine($"[Image content file ID: {contentUpdate.ImageFileId}");
83+
Console.WriteLine();
84+
}
85+
}
86+
}
87+
```
88+
89+
5. Finally, we delete all the resources, we have created in this sample.
90+
91+
Synchronous sample:
92+
```C# Snippet:AgentsStreaming_Cleanup
93+
client.DeleteThread(thread.Id);
94+
client.DeleteAgent(agent.Id);
95+
```
96+
97+
Asynchronous sample:
98+
```C# Snippet:AgentsStreamingAsync_Cleanup
99+
await client.DeleteThreadAsync(thread.Id);
100+
await client.DeleteAgentAsync(agent.Id);
101+
```

0 commit comments

Comments
 (0)