feat: 完成AzureOpenAI改造
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.Connectors.OpenAI;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace Yi.Framework.SemanticKernel;
|
||||
|
||||
public class SemanticKernelClient : ITransientDependency
|
||||
{
|
||||
public Kernel Kernel { get; }
|
||||
|
||||
public SemanticKernelClient(Kernel kernel)
|
||||
{
|
||||
this.Kernel = kernel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行插件
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="pluginName"></param>
|
||||
/// <param name="functionName"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string?> InvokerFunctionAsync(string input, string pluginName, string functionName)
|
||||
{
|
||||
KernelFunction jsonFun = this.Kernel.Plugins.GetFunction(pluginName, functionName);
|
||||
var result = await this.Kernel.InvokeAsync(function: jsonFun, new KernelArguments() { ["input"] = input });
|
||||
return result.GetValue<string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 聊天完成,FunctionCall
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<IReadOnlyList<ChatMessageContent>> ChatCompletionAsync(string question,
|
||||
params (string, string)[] functions)
|
||||
{
|
||||
if (functions is null)
|
||||
{
|
||||
throw new Exception("请选择插件");
|
||||
}
|
||||
|
||||
var openSettings = new OpenAIPromptExecutionSettings()
|
||||
{
|
||||
FunctionChoiceBehavior =
|
||||
FunctionChoiceBehavior.Auto(
|
||||
functions.Select(x => this.Kernel.Plugins.GetFunction(x.Item1, x.Item2)).ToList(), true),
|
||||
// ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
|
||||
// MaxTokens =1000
|
||||
};
|
||||
|
||||
var chatCompletionService = this.Kernel.GetRequiredService<IChatCompletionService>();
|
||||
|
||||
var results = await chatCompletionService.GetChatMessageContentsAsync(
|
||||
question,
|
||||
executionSettings: openSettings,
|
||||
kernel: Kernel);
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user