feat: 完成AzureOpenAI改造
This commit is contained in:
@@ -5,6 +5,7 @@ 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;
|
||||
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
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> InovkerFunctionAsync(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>
|
||||
/// 聊天对话,调用方法
|
||||
/// </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;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ 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
|
||||
{
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="..\..\..\common.props" />
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.40.0" />
|
||||
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
||||
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
|
||||
|
||||
@@ -9,6 +8,7 @@
|
||||
|
||||
<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>
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
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;
|
||||
|
||||
@@ -15,6 +14,7 @@ namespace Yi.Framework.Stock.Domain
|
||||
typeof(YiFrameworkStockDomainSharedModule),
|
||||
typeof(YiFrameworkMapsterModule),
|
||||
typeof(AbpDddDomainModule),
|
||||
typeof(YiFrameworkSemanticKernelModule),
|
||||
typeof(AbpCachingModule)
|
||||
)]
|
||||
public class YiFrameworkStockDomainModule : AbpModule
|
||||
@@ -23,27 +23,13 @@ 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"));
|
||||
|
||||
services.AddHttpClient();
|
||||
#pragma warning disable SKEXP0010
|
||||
// 从配置中获取值
|
||||
var options = semanticKernelSection.Get<SemanticKernelOptions>();
|
||||
//股市优先使用第一个ai模型
|
||||
services.AddKernel()
|
||||
.AddOpenAIChatCompletion(
|
||||
modelId: options.ModelIds.FirstOrDefault(),
|
||||
endpoint: new Uri(options.Endpoint),
|
||||
apiKey: options.ApiKey);
|
||||
#pragma warning restore SKEXP0010
|
||||
|
||||
|
||||
// 添加插件
|
||||
services.AddSingleton<KernelPlugin>(sp => KernelPluginFactory.CreateFromType<NewsPlugins>(serviceProvider: sp));
|
||||
services.AddSingleton<KernelPlugin>(sp => KernelPluginFactory.CreateFromType<StockPlugins>(serviceProvider: sp));
|
||||
|
||||
services.AddSingleton<KernelPlugin>(sp =>
|
||||
KernelPluginFactory.CreateFromType<NewsPlugins>(serviceProvider: sp));
|
||||
services.AddSingleton<KernelPlugin>(sp =>
|
||||
KernelPluginFactory.CreateFromType<StockPlugins>(serviceProvider: sp));
|
||||
|
||||
// 注册NewsManager
|
||||
services.AddTransient<NewsManager>();
|
||||
}
|
||||
|
||||
@@ -1,26 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.Connectors.OpenAI;
|
||||
// using OpenAI;
|
||||
// using OpenAI.Managers;
|
||||
// using OpenAI.ObjectModels;
|
||||
// using OpenAI.ObjectModels.RequestModels;
|
||||
// using OpenAI.ObjectModels.ResponseModels;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Options;
|
||||
using Yi.Framework.SemanticKernel;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
{
|
||||
public class AiManager : ISingletonDependency
|
||||
{
|
||||
private readonly Kernel _kernel;
|
||||
public AiManager(Kernel kernel)
|
||||
private readonly SemanticKernelClient _client;
|
||||
|
||||
public AiManager(SemanticKernelClient client)
|
||||
{
|
||||
_kernel = kernel;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<string?> ChatAsStreamAsync(string model, List<AiChatContextDto> aiChatContextDtos)
|
||||
@@ -29,38 +22,41 @@ namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
var openSettings = new OpenAIPromptExecutionSettings()
|
||||
{
|
||||
MaxTokens =1000
|
||||
//MaxTokens = 1000
|
||||
};
|
||||
|
||||
var chatCompletionService = this._kernel.GetRequiredService<IChatCompletionService>(model);
|
||||
var chatCompletionService = this._client.Kernel.GetRequiredService<IChatCompletionService>(model);
|
||||
|
||||
var history =new ChatHistory();
|
||||
var history = new ChatHistory();
|
||||
foreach (var aiChatContextDto in aiChatContextDtos)
|
||||
{
|
||||
if (aiChatContextDto.AnswererType==AnswererTypeEnum.Ai)
|
||||
if (aiChatContextDto.AnswererType == AnswererTypeEnum.Ai)
|
||||
{
|
||||
history.AddSystemMessage(aiChatContextDto.Message);
|
||||
history.AddAssistantMessage(aiChatContextDto.Message);
|
||||
}
|
||||
else if(aiChatContextDto.AnswererType==AnswererTypeEnum.User)
|
||||
else if (aiChatContextDto.AnswererType == AnswererTypeEnum.User)
|
||||
{
|
||||
history.AddUserMessage(aiChatContextDto.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var results = chatCompletionService.GetStreamingChatMessageContentsAsync(
|
||||
chatHistory: history,
|
||||
executionSettings: openSettings,
|
||||
kernel: _kernel);
|
||||
kernel: _client.Kernel);
|
||||
|
||||
if (results is null)
|
||||
{
|
||||
yield return null;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
await foreach (var result in results)
|
||||
{
|
||||
yield return result.Content;
|
||||
}
|
||||
await foreach (var result in results)
|
||||
{
|
||||
yield return result.Content;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,13 @@
|
||||
<Import Project="..\..\..\common.props" />
|
||||
|
||||
<ItemGroup>
|
||||
<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.ChatHub.Domain.Shared\Yi.Framework.ChatHub.Domain.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Volo.Abp.AspNetCore.SignalR" Version="$(AbpVersion)" />
|
||||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.40.0" />
|
||||
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
||||
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" />
|
||||
|
||||
@@ -1,43 +1,23 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Volo.Abp.Domain;
|
||||
using Yi.Framework.Caching.FreeRedis;
|
||||
using Yi.Framework.ChatHub.Domain.Shared;
|
||||
using Yi.Framework.Core.Options;
|
||||
|
||||
using Yi.Framework.SemanticKernel;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkChatHubDomainSharedModule),
|
||||
typeof(YiFrameworkCachingFreeRedisModule),
|
||||
|
||||
typeof(YiFrameworkSemanticKernelModule),
|
||||
typeof(AbpDddDomainModule)
|
||||
)]
|
||||
)]
|
||||
public class YiFrameworkChatHubDomainModule : AbpModule
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
var services = context.Services;
|
||||
|
||||
// 配置绑定
|
||||
var semanticKernelSection = configuration.GetSection("SemanticKernel");
|
||||
services.Configure<SemanticKernelOptions>(configuration.GetSection("SemanticKernel"));
|
||||
#pragma warning disable SKEXP0010
|
||||
// 从配置中获取值
|
||||
var options = semanticKernelSection.Get<SemanticKernelOptions>();
|
||||
foreach (var optionsModelId in options.ModelIds)
|
||||
{
|
||||
services.AddKernel()
|
||||
.AddOpenAIChatCompletion(
|
||||
serviceId: optionsModelId,
|
||||
modelId: optionsModelId,
|
||||
endpoint: new Uri(options.Endpoint),
|
||||
apiKey: options.ApiKey);
|
||||
}
|
||||
#pragma warning restore SKEXP0010
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user