feat: 重构聊天室语义内核
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Yi.Framework.Core.Options;
|
||||||
|
|
||||||
|
public class SemanticKernelOptions
|
||||||
|
{
|
||||||
|
public List<string> ModelIds { get; set; }
|
||||||
|
public string Endpoint { get; set; }
|
||||||
|
public string ApiKey { get; set; }
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ namespace Yi.Framework.Stock.Domain.Managers.SemanticKernel
|
|||||||
{
|
{
|
||||||
public class SemanticKernelOptions
|
public class SemanticKernelOptions
|
||||||
{
|
{
|
||||||
public string ModelId { get; set; }
|
public List<string> ModelIds { get; set; }
|
||||||
public string Endpoint { get; set; }
|
public string Endpoint { get; set; }
|
||||||
public string ApiKey { get; set; }
|
public string ApiKey { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,9 +32,10 @@ namespace Yi.Framework.Stock.Domain
|
|||||||
#pragma warning disable SKEXP0010
|
#pragma warning disable SKEXP0010
|
||||||
// 从配置中获取值
|
// 从配置中获取值
|
||||||
var options = semanticKernelSection.Get<SemanticKernelOptions>();
|
var options = semanticKernelSection.Get<SemanticKernelOptions>();
|
||||||
|
//股市优先使用第一个ai模型
|
||||||
services.AddKernel()
|
services.AddKernel()
|
||||||
.AddOpenAIChatCompletion(
|
.AddOpenAIChatCompletion(
|
||||||
modelId: options.ModelId,
|
modelId: options.ModelIds.FirstOrDefault(),
|
||||||
endpoint: new Uri(options.Endpoint),
|
endpoint: new Uri(options.Endpoint),
|
||||||
apiKey: options.ApiKey);
|
apiKey: options.ApiKey);
|
||||||
#pragma warning restore SKEXP0010
|
#pragma warning restore SKEXP0010
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using Microsoft.SemanticKernel;
|
||||||
|
using Microsoft.SemanticKernel.ChatCompletion;
|
||||||
|
using Microsoft.SemanticKernel.Connectors.OpenAI;
|
||||||
// using OpenAI;
|
// using OpenAI;
|
||||||
// using OpenAI.Managers;
|
// using OpenAI.Managers;
|
||||||
// using OpenAI.ObjectModels;
|
// using OpenAI.ObjectModels;
|
||||||
@@ -14,61 +17,46 @@ namespace Yi.Framework.ChatHub.Domain.Managers
|
|||||||
{
|
{
|
||||||
public class AiManager : ISingletonDependency
|
public class AiManager : ISingletonDependency
|
||||||
{
|
{
|
||||||
public AiManager(IOptions<AiOptions> options)
|
private readonly Kernel _kernel;
|
||||||
|
public AiManager(Kernel kernel)
|
||||||
{
|
{
|
||||||
// this.OpenAIService = new OpenAIService(new OpenAiOptions()
|
_kernel = kernel;
|
||||||
// {
|
|
||||||
// ApiKey = options.Value.ApiKey,
|
|
||||||
// BaseDomain = options.Value.BaseDomain
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
// private OpenAIService OpenAIService { get; }
|
|
||||||
|
|
||||||
public async IAsyncEnumerable<string> ChatAsStreamAsync(string model, List<AiChatContextDto> aiChatContextDtos)
|
public async IAsyncEnumerable<string?> ChatAsStreamAsync(string model, List<AiChatContextDto> aiChatContextDtos)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException("准备sk重构");
|
if (aiChatContextDtos.Count == 0)
|
||||||
yield break;
|
{
|
||||||
// if (aiChatContextDtos.Count == 0)
|
yield return null;
|
||||||
// {
|
}
|
||||||
// yield return null;
|
var openSettings = new OpenAIPromptExecutionSettings()
|
||||||
// }
|
{
|
||||||
//
|
MaxTokens =1000
|
||||||
// List<ChatMessage> messages = aiChatContextDtos.Select(x =>
|
};
|
||||||
// {
|
|
||||||
// if (x.AnswererType == AnswererTypeEnum.Ai)
|
var chatCompletionService = this._kernel.GetRequiredService<IChatCompletionService>(model);
|
||||||
// {
|
|
||||||
// return ChatMessage.FromSystem(x.Message);
|
var history =new ChatHistory();
|
||||||
// }
|
foreach (var aiChatContextDto in aiChatContextDtos)
|
||||||
// else
|
{
|
||||||
// {
|
if (aiChatContextDto.AnswererType==AnswererTypeEnum.Ai)
|
||||||
// return ChatMessage.FromUser(x.Message);
|
{
|
||||||
// }
|
history.AddSystemMessage(aiChatContextDto.Message);
|
||||||
// }).ToList();
|
}
|
||||||
// var completionResult = OpenAIService.ChatCompletion.CreateCompletionAsStream(new ChatCompletionCreateRequest
|
else if(aiChatContextDto.AnswererType==AnswererTypeEnum.User)
|
||||||
// {
|
{
|
||||||
// Messages = messages,
|
history.AddUserMessage(aiChatContextDto.Message);
|
||||||
// Model =model
|
}
|
||||||
// });
|
}
|
||||||
//
|
|
||||||
// HttpStatusCode? error = null;
|
var results = chatCompletionService.GetStreamingChatMessageContentsAsync(
|
||||||
// await foreach (var result in completionResult)
|
chatHistory: history,
|
||||||
// {
|
executionSettings: openSettings,
|
||||||
// if (result.Successful)
|
kernel: _kernel);
|
||||||
// {
|
await foreach (var result in results)
|
||||||
// yield return result.Choices.FirstOrDefault()?.Message.Content ?? null;
|
{
|
||||||
// }
|
yield return result.Content;
|
||||||
// else
|
}
|
||||||
// {
|
|
||||||
// error = result.HttpStatusCode;
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// if (error == HttpStatusCode.PaymentRequired)
|
|
||||||
// {
|
|
||||||
// yield return "余额不足,请联系站长充值";
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Volo.Abp.AspNetCore.SignalR" Version="$(AbpVersion)" />
|
<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.Ddd.Domain" Version="$(AbpVersion)" />
|
||||||
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
|
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
|
||||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" />
|
<ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" />
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
using Volo.Abp.Domain;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.SemanticKernel;
|
||||||
|
using Volo.Abp.Domain;
|
||||||
using Yi.Framework.Caching.FreeRedis;
|
using Yi.Framework.Caching.FreeRedis;
|
||||||
using Yi.Framework.ChatHub.Domain.Shared;
|
using Yi.Framework.ChatHub.Domain.Shared;
|
||||||
|
using Yi.Framework.Core.Options;
|
||||||
|
|
||||||
|
|
||||||
namespace Yi.Framework.ChatHub.Domain
|
namespace Yi.Framework.ChatHub.Domain
|
||||||
@@ -13,9 +17,27 @@ namespace Yi.Framework.ChatHub.Domain
|
|||||||
)]
|
)]
|
||||||
public class YiFrameworkChatHubDomainModule : AbpModule
|
public class YiFrameworkChatHubDomainModule : AbpModule
|
||||||
{
|
{
|
||||||
public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,18 +103,10 @@
|
|||||||
//开启定时数据库备份
|
//开启定时数据库备份
|
||||||
"EnableDataBaseBackup": false
|
"EnableDataBaseBackup": false
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
//OpenAi
|
|
||||||
"AiOptions": {
|
|
||||||
"ApiKey": "",
|
|
||||||
"BaseDomain": ""
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
//语义内核
|
//语义内核
|
||||||
"SemanticKernel": {
|
"SemanticKernel": {
|
||||||
"ModelId": "gpt-4o",
|
"ModelIds": ["gpt-4o"],
|
||||||
"Endpoint": "https://xxx.com/v1",
|
"Endpoint": "https://xxx.com/v1",
|
||||||
"ApiKey": "sk-xxxxxx"
|
"ApiKey": "sk-xxxxxx"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user