feat: 提交

This commit is contained in:
cc
2025-06-20 18:06:33 +08:00
parent f16e1cd7a6
commit 6abcc49ed4
15 changed files with 138 additions and 118 deletions

View File

@@ -5,7 +5,6 @@ using Yi.Framework.Stock.Domain.Managers.SemanticKernel;
using Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
using System.Text;
using System.IO;
using Yi.Framework.SemanticKernel;
namespace Yi.Framework.Stock.Domain.Managers;

View File

@@ -0,0 +1,60 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Volo.Abp.DependencyInjection;
namespace Yi.Framework.Stock.Domain.Managers.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;
}
}

View File

@@ -0,0 +1,9 @@
namespace Yi.Framework.Stock.Domain.Managers.SemanticKernel
{
public class SemanticKernelOptions
{
public List<string> ModelIds { get; set; }
public string Endpoint { get; set; }
public string ApiKey { get; set; }
}
}

View File

@@ -13,7 +13,6 @@ using Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
using Microsoft.Extensions.Hosting;
using System.Text;
using System.IO;
using Yi.Framework.SemanticKernel;
namespace Yi.Framework.Stock.Domain.Managers
{

View File

@@ -3,12 +3,11 @@
<ItemGroup>
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.57.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Mapster\Yi.Framework.Mapster.csproj" />
<ProjectReference Include="..\..\..\framework\Yi.Framework.SemanticKernel\Yi.Framework.SemanticKernel.csproj" />
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
<ProjectReference Include="..\Yi.Framework.Stock.Domain.Shared\Yi.Framework.Stock.Domain.Shared.csproj" />
</ItemGroup>
@@ -17,5 +16,5 @@
<Folder Include="EventHandlers\" />
<Folder Include="Repositories\" />
</ItemGroup>
</Project>

View File

@@ -1,10 +1,11 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Volo.Abp.Caching;
using Volo.Abp.Domain;
using Yi.Framework.Mapster;
using Yi.Framework.SemanticKernel;
using Yi.Framework.Stock.Domain.Managers;
using Yi.Framework.Stock.Domain.Managers.SemanticKernel;
using Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
using Yi.Framework.Stock.Domain.Shared;
@@ -14,7 +15,6 @@ namespace Yi.Framework.Stock.Domain
typeof(YiFrameworkStockDomainSharedModule),
typeof(YiFrameworkMapsterModule),
typeof(AbpDddDomainModule),
typeof(YiFrameworkSemanticKernelModule),
typeof(AbpCachingModule)
)]
public class YiFrameworkStockDomainModule : AbpModule
@@ -24,6 +24,27 @@ namespace Yi.Framework.Stock.Domain
var configuration = context.Services.GetConfiguration();
var services = context.Services;
// 配置绑定
var semanticKernelSection = configuration.GetSection("SemanticKernel");
services.Configure<SemanticKernelOptions>(configuration.GetSection("SemanticKernel"));
// 从配置中获取值
var options = semanticKernelSection.Get<SemanticKernelOptions>();
foreach (var optionsModelId in options.ModelIds)
{
services.AddKernel()
.AddAzureOpenAIChatCompletion(
deploymentName: optionsModelId,
endpoint: options.Endpoint,
apiKey: options.ApiKey,
serviceId: optionsModelId,
modelId: optionsModelId);
// .AddOpenAIChatCompletion(
// serviceId: optionsModelId,
// modelId: optionsModelId,
// endpoint: new Uri(options.Endpoint),
// apiKey: options.ApiKey);
}
// 添加插件
services.AddSingleton<KernelPlugin>(sp =>
KernelPluginFactory.CreateFromType<NewsPlugins>(serviceProvider: sp));