In this example we will demonstrate the agent streaming support.
- First we need to create agent client and read the environment variables that will be used in the next steps.
var projectEndpoint = configuration["ProjectEndpoint"];
var modelDeploymentName = configuration["ModelDeploymentName"];
PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential());- We will create agent with the Interpreter tool support. It is needed to allow fow writing mathematical formulas in LaTeX format.
Synchronous sample:
PersistentAgent agent = client.CreateAgent(
model: modelDeploymentName,
name: "My Friendly Test Agent",
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
tools: [new CodeInterpreterToolDefinition()]
);Asynchronous sample:
PersistentAgent agent = await client.CreateAgentAsync(
model: modelDeploymentName,
name: "My Friendly Test Agent",
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
tools: [new CodeInterpreterToolDefinition()]
);- Create
Threadwith the message.
Synchronous sample:
PersistentAgentThread thread = client.CreateThread();
client.CreateMessage(
thread.Id,
MessageRole.User,
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");Asynchronous sample:
PersistentAgentThread thread = await client.CreateThreadAsync();
await client.CreateMessageAsync(
thread.Id,
MessageRole.User,
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");- Read the output from the stream.
Synchronous sample:
foreach (StreamingUpdate streamingUpdate in client.CreateRunStreaming(thread.Id, agent.Id))
{
if (streamingUpdate is MessageContentUpdate contentUpdate)
{
Console.Write(contentUpdate?.Text);
if (contentUpdate?.ImageFileId is not null)
{
Console.WriteLine($"[Image content file ID: {contentUpdate.ImageFileId}");
Console.WriteLine();
}
}
}Asynchronous sample:
await foreach (StreamingUpdate streamingUpdate in client.CreateRunStreamingAsync(thread.Id, agent.Id))
{
if (streamingUpdate is MessageContentUpdate contentUpdate)
{
Console.Write(contentUpdate?.Text);
if (contentUpdate?.ImageFileId is not null)
{
Console.WriteLine($"[Image content file ID: {contentUpdate.ImageFileId}");
Console.WriteLine();
}
}
}- Finally, we delete all the resources, we have created in this sample.
Synchronous sample:
client.DeleteThread(thread.Id);
client.DeleteAgent(agent.Id);Asynchronous sample:
await client.DeleteThreadAsync(thread.Id);
await client.DeleteAgentAsync(agent.Id);