-
Notifications
You must be signed in to change notification settings - Fork 138
Open
Description
Since streaming APIs add a grade of complexity, It would be nice to include a sample in how users can handle approvals with IAsyncEnumerable-based APIs.
using System;
using System.ComponentModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
#pragma warning disable MEAI001
AIFunction weatherFunction = AIFunctionFactory.Create(GetWeather);
AIFunction approvalRequiredWeatherFunction = new ApprovalRequiredAIFunction(weatherFunction);
AIAgent agent = new AzureOpenAIClient(
new Uri("https://<myresource>.openai.azure.com"),
new AzureCliCredential())
.GetChatClient("gpt-4o-mini")
.CreateAIAgent(instructions: "You are a helpful assistant", tools: [approvalRequiredWeatherFunction]);
AgentThread thread = agent.GetNewThread();
var response = agent.RunStreamingAsync("What is the weather like in Amsterdam?", thread);
while (true)
{
List<FunctionApprovalRequestContent> functionApprovalRequests = [];
await foreach (AgentRunResponseUpdate update in response)
{
if (update.Contents.OfType<FunctionApprovalRequestContent>().ToList() is { Count: > 0 } approvals)
{
functionApprovalRequests.AddRange(approvals);
}
else if (update.Text is { Length: > 0 } text)
{
Console.WriteLine(text);
}
}
if (functionApprovalRequests.FirstOrDefault() is { } requestContent)
{
Console.WriteLine($"We require approval to execute '{requestContent.FunctionCall.Name}'");
var approvalMessage = new ChatMessage(ChatRole.User, [requestContent.CreateResponse(true)]);
response = agent.RunStreamingAsync(approvalMessage, thread);
}
else
{
break;
}
}
[Description("Get the weather for a given location.")]
static string GetWeather([Description("The location to get the weather for.")] string location)
=> $"The weather in {location} is cloudy with a high of 15°C.";Metadata
Metadata
Assignees
Labels
No labels