Skip to content

Latest commit

 

History

History
101 lines (86 loc) · 3.27 KB

File metadata and controls

101 lines (86 loc) · 3.27 KB

Sample using agents with streaming in Azure.AI.Agents

In this example we will demonstrate the agent streaming support.

  1. 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());
  1. 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()]
);
  1. Create Thread with 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.");
  1. 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();
        }
    }
}
  1. 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);