feat: 完成AzureOpenAI改造

This commit is contained in:
ccnetcore
2025-06-17 23:25:55 +08:00
parent 0cd795f57a
commit 58fcc92e4d
13 changed files with 119 additions and 95 deletions

View File

@@ -186,6 +186,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.Stock.Domain.S
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.Stock.SqlSugarCore", "module\ai-stock\Yi.Framework.Stock.SqlSugarCore\Yi.Framework.Stock.SqlSugarCore.csproj", "{5F49318F-E6C7-4194-BAE0-83D4FB8D1983}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.SemanticKernel", "framework\Yi.Framework.SemanticKernel\Yi.Framework.SemanticKernel.csproj", "{2503116E-4D69-49E3-8ED7-A6CDD0C3B542}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -468,6 +470,10 @@ Global
{5F49318F-E6C7-4194-BAE0-83D4FB8D1983}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F49318F-E6C7-4194-BAE0-83D4FB8D1983}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F49318F-E6C7-4194-BAE0-83D4FB8D1983}.Release|Any CPU.Build.0 = Release|Any CPU
{2503116E-4D69-49E3-8ED7-A6CDD0C3B542}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2503116E-4D69-49E3-8ED7-A6CDD0C3B542}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2503116E-4D69-49E3-8ED7-A6CDD0C3B542}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2503116E-4D69-49E3-8ED7-A6CDD0C3B542}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -551,6 +557,7 @@ Global
{162821E4-8FE0-4A68-B3C0-49BD6596446F} = {DB46873F-981A-43D8-91B0-D464CCB65943}
{10273544-715D-4BB3-893C-6F010D947BDD} = {DB46873F-981A-43D8-91B0-D464CCB65943}
{5F49318F-E6C7-4194-BAE0-83D4FB8D1983} = {DB46873F-981A-43D8-91B0-D464CCB65943}
{2503116E-4D69-49E3-8ED7-A6CDD0C3B542} = {77B949E9-530E-45A5-9657-20F7D5C6875C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23D6FBC9-C970-4641-BC1E-2AEA59F51C18}

View File

@@ -3,16 +3,17 @@ using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Volo.Abp.DependencyInjection;
namespace Yi.Framework.Stock.Domain.Managers.SemanticKernel;
namespace Yi.Framework.SemanticKernel;
public class SemanticKernelClient:ITransientDependency
public class SemanticKernelClient : ITransientDependency
{
public Kernel Kernel { get;}
public Kernel Kernel { get; }
public SemanticKernelClient(Kernel kernel)
{
this.Kernel = kernel;
}
/// <summary>
/// 执行插件
/// </summary>
@@ -20,7 +21,7 @@ public class SemanticKernelClient:ITransientDependency
/// <param name="pluginName"></param>
/// <param name="functionName"></param>
/// <returns></returns>
public async Task<string> InovkerFunctionAsync(string input, string pluginName, string functionName)
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 });
@@ -28,25 +29,29 @@ public class SemanticKernelClient:ITransientDependency
}
/// <summary>
/// 聊天对话,调用方法
/// 聊天完成,FunctionCall
/// </summary>
/// <returns></returns>
public async Task<IReadOnlyList<ChatMessageContent>> ChatCompletionAsync(string question,params (string,string)[] functions)
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),
FunctionChoiceBehavior =
FunctionChoiceBehavior.Auto(
functions.Select(x => this.Kernel.Plugins.GetFunction(x.Item1, x.Item2)).ToList(), true),
// ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
MaxTokens =1000
// MaxTokens =1000
};
var chatCompletionService = this.Kernel.GetRequiredService<IChatCompletionService>();
var results =await chatCompletionService.GetChatMessageContentsAsync(
var results = await chatCompletionService.GetChatMessageContentsAsync(
question,
executionSettings: openSettings,
kernel: Kernel);

View File

@@ -1,4 +1,4 @@
namespace Yi.Framework.Stock.Domain.Managers.SemanticKernel
namespace Yi.Framework.SemanticKernel
{
public class SemanticKernelOptions
{

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel" Version="1.57.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,37 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Yi.Framework.Core.Options;
namespace Yi.Framework.SemanticKernel;
public class YiFrameworkSemanticKernelModule : 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"));
// 从配置中获取值
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);
}
}
}

View File

@@ -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;

View File

@@ -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
{

View File

@@ -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>

View File

@@ -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>();
}

View File

@@ -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;
}
}
}
}
}

View File

@@ -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" />

View File

@@ -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
}
}
}

View File

@@ -41,16 +41,16 @@ const currentInputValue = ref("");
const inputListDataStore = ref([
{key: "all", name: "官方学习交流群", titleName: "官方学习交流群", logo: "yilogo.png", value: ""},
{key: "ai@gpt-4o-mini", name: "ChatGpt聊天", titleName: "ChatGpt-全能神!综合能力最强!", logo: "openAi.png", value: ""},
{key: "ai@o4-mini", name: "ChatGpt聊天", titleName: "ChatGpt-全能神!综合能力最强!", logo: "openAi.png", value: ""},
//
// {key: "ai@claude-3-7-sonnet", name: "Claude聊天", titleName: "Claude3.7 代码逻辑地表最强!", logo: "claudeAi.png", value: ""},
// {key: "ai@claude-3.7-sonnet-thinking", name: "Claude思索", titleName: "Claude3.7 思索模式,强中强!", logo: "claudeAi.png", value: ""},
//
// {key: "ai@grok-3", name: "Grok聊天", titleName: "Grok3 为3.0王的诞生献上礼炮", logo: "grokAi.png", value: ""},
//
// {key: "ai@Qwen/QwQ-32B-Preview", name: "QWen聊天", titleName: "国产阿里千问通义", logo: "qwenAi.png", value: ""},
{key: "ai@claude-3-7-sonnet", name: "Claude聊天", titleName: "Claude3.7 代码逻辑地表最强!", logo: "claudeAi.png", value: ""},
{key: "ai@claude-3.7-sonnet-thinking", name: "Claude思索", titleName: "Claude3.7 思索模式,强中强!", logo: "claudeAi.png", value: ""},
{key: "ai@grok-3", name: "Grok聊天", titleName: "Grok3 为3.0王的诞生献上礼炮", logo: "grokAi.png", value: ""},
{key: "ai@Qwen/QwQ-32B-Preview", name: "QWen聊天", titleName: "国产阿里千问通义", logo: "qwenAi.png", value: ""},
{key: "ai@DeepSeek-V3", name: "DeepSeek聊天", titleName: "满血DeepSeek-聊天模式,开源模型第一", logo: "deepSeekAi.png", value: ""},
// {key: "ai@DeepSeek-V3", name: "DeepSeek聊天", titleName: "满血DeepSeek-聊天模式,开源模型第一", logo: "deepSeekAi.png", value: ""},
{key: "ai@DeepSeek-R1", name: "DeepSeek思索", titleName: "满血DeepSeek-思索模式", logo: "deepSeekAi.png", value: ""}
]);
//AI聊天临时存储