How to Implement Streaming Chat Using AzureAiAgent? .Net #11087
-
I have used the below code: using System.Text.Json;
using System.Threading;
using Azure;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Extensions.AI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.AzureAI;
using Microsoft.SemanticKernel.ChatCompletion;
class Program
{
static async Task Main()
{
var connectionString = "ABC";
AIProjectClient client = AzureAIAgent.CreateAzureAIClient(connectionString, new AzureCliCredential());
AgentsClient agentsClient = client.GetAgentsClient();
// List of multiple files to upload
List<string> filePaths = new List<string>
{
"employees.pdf",
"Grimms-The-King-of-the-Golden-Mountain.txt",
"Grimms-The-Water-of-Life.txt",
"Grimms-The-White-Snake.txt",
};
List<string> uploadedFileIds = new List<string>();
// Upload multiple files
foreach (string filePath in filePaths)
{
Response<AgentFile> uploadAgentFileResponse = await agentsClient.UploadFileAsync(
filePath: filePath,
purpose: AgentFilePurpose.Agents);
uploadedFileIds.Add(uploadAgentFileResponse.Value.Id);
}
// Create a vector store with the file and wait for it to be processed.
// If you do not specify a vector store, create_message will create a vector store with a default expiration policy of seven days after they were last active
VectorStore vectorStore = await agentsClient.CreateVectorStoreAsync(
fileIds: uploadedFileIds,
name: "my_vector_store",
expiresAfter: new VectorStoreExpirationPolicy(VectorStoreExpirationPolicyAnchor.LastActiveAt, 1));
// Define an agent on the Azure AI agent service
Agent definition = await agentsClient.CreateAgentAsync(
model: "gpt-4o-2",
name: "FileExplorerAgent",
description: "Agent Used to Answer User Queries By Understanding the Provided Files",
instructions: "You are helpful AI Assistant Agent",
tools: [new FileSearchToolDefinition()],
toolResources:
new()
{
FileSearch = new()
{
VectorStoreIds = {vectorStore.Id},
}
});
AzureAIAgent agent = new(definition, agentsClient);
AgentThread thread = await agentsClient.CreateThreadAsync();
ChatMessageContent message = new(AuthorRole.User, "What is the paragraph count for each of the stories,Create a table that identifies the protagonist and antagonist for each story, and What is the moral in The White Snake?");
await agent.AddChatMessageAsync(thread.Id, message);
try
{
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(thread.Id))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{response.AuthorName}:");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{response.Content}");
Console.ResetColor();
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("An error occurred while processing the streaming messages:");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error: {ex.Message}");
Console.ResetColor();
}
}
} And I am getting But when I Use await foreach (ChatMessageContent response in agent.InvokeAsync(thread.Id)) Everything Works, Can someone please explain what is the reason behind this error and how I can resolve this? and why Non-Streaming does not counter this error. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
@sophialagerkranspandey Any Help regarding this? |
Beta Was this translation helpful? Give feedback.
-
@lovedeepatsgit thanks for creating the issue. We have created an issue for this here: #11313
|
Beta Was this translation helpful? Give feedback.
-
@lovedeepatsgit could you please let us know which version of Semantic Kernel you are using? |
Beta Was this translation helpful? Give feedback.
@lovedeepatsgit thanks for creating the issue. We have created an issue for this here: #11313