using PolyglotKernel= Microsoft.DotNet.Interactive.Kernel;// 引入交互式的内核命名空间,以便用户输入 var aiProviderCode = await PolyglotKernel.GetInputAsync("请输入AI服务提供商编码:");
var kernel = GetKernel(aiProviderCode); var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
using Microsoft.SemanticKernel.Agents; using PolyglotKernel = Microsoft.DotNet.Interactive.Kernel;// 引入交互式的内核命名空间,以便用户输入
#pragmawarning disable SKEXP0110
// Create an empty chat AgentGroupChat chat = new ();
// Create a chat completion agent ChatCompletionAgent agent = new() { Name = "QA-Agent", Instructions = "Ask me anything!", Kernel = kernel };
// Add the agent to the chat chat.AddAgent(agent);
// Get a user request and then add it to the chat var userRequest = await PolyglotKernel.GetInputAsync("请输入您的问题:"); ChatMessageContent message = new(AuthorRole.User, userRequest); chat.AddChatMessage(message);
// Invoke the agent and display the response awaitforeach (ChatMessageContent message in chat.InvokeAsync(agent)) { message.Display(); }
// Get the chat history for the agent var agentHistory = await chat.GetChatMessagesAsync(agent).ToArrayAsync();
using Microsoft.SemanticKernel.Agents; using Microsoft.SemanticKernel.Agents.Chat;
AgentGroupChat chat = new(agentReviewer, agentWriter) { ExecutionSettings = new AgentGroupChatSettings { SelectionStrategy = new KernelFunctionSelectionStrategy(selectionFunction, kernel) { // Always start with the editor agent. InitialAgent = agentReviewer, // Save tokens by only including the final response HistoryReducer = historyReducer, // The prompt variable name for the history argument. HistoryVariableName = "lastmessage", // Returns the entire result value as a string. ResultParser = (result) => result.GetValue<string>() ?? agentReviewer.Name }, TerminationStrategy = new KernelFunctionTerminationStrategy(terminationFunction, kernel) { // Only evaluate for editor's response Agents = [agentReviewer], // Save tokens by only including the final response HistoryReducer = historyReducer, // The prompt variable name for the history argument. HistoryVariableName = "lastmessage", // Limit total number of turns MaximumIterations = 12, // Customer result parser to determine if the response is "yes" ResultParser = (result) => result.GetValue<string>()?.Contains(TerminationToken, StringComparison.OrdinalIgnoreCase) ?? false } } };