feat: 提交
This commit is contained in:
@@ -1,26 +1,19 @@
|
||||
using System.Text;
|
||||
using Azure;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.SemanticKernel.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Yi.Framework.AiHub.Application.Contracts.Dtos;
|
||||
using Yi.Framework.SemanticKernel;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Services;
|
||||
|
||||
public class AiService : ApplicationService
|
||||
{
|
||||
private readonly SemanticKernelClient _skClient;
|
||||
// private readonly SemanticKernelClient _skClient;
|
||||
private IHttpContextAccessor httpContextAccessor;
|
||||
|
||||
public AiService(SemanticKernelClient skClient, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_skClient = skClient;
|
||||
this.httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
// public AiService(SemanticKernelClient skClient, IHttpContextAccessor httpContextAccessor)
|
||||
// {
|
||||
// _skClient = skClient;
|
||||
// this.httpContextAccessor = httpContextAccessor;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型列表
|
||||
@@ -62,59 +55,59 @@ public class AiService : ApplicationService
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="cancelToken"></param>
|
||||
public async Task PostSendAsync(SendMessageInput input,CancellationToken cancelToken)
|
||||
{
|
||||
var httpContext = this.httpContextAccessor.HttpContext;
|
||||
var response = httpContext.Response;
|
||||
// 设置响应头,声明是 SSE 流
|
||||
response.ContentType = "text/event-stream";
|
||||
response.Headers.Append("Cache-Control", "no-cache");
|
||||
response.Headers.Append("Connection", "keep-alive");
|
||||
|
||||
|
||||
var chatCompletionService = this._skClient.Kernel.GetRequiredService<IChatCompletionService>(input.Model);
|
||||
var history = new ChatHistory();
|
||||
var openSettings = new AzureOpenAIPromptExecutionSettings()
|
||||
{
|
||||
MaxTokens = 3000
|
||||
};
|
||||
foreach (var aiChatContextDto in input.Messages)
|
||||
{
|
||||
if (aiChatContextDto.Role == "ai")
|
||||
{
|
||||
history.AddAssistantMessage(aiChatContextDto.Content);
|
||||
}
|
||||
else if (aiChatContextDto.Role == "user")
|
||||
{
|
||||
history.AddUserMessage(aiChatContextDto.Content);
|
||||
}
|
||||
}
|
||||
|
||||
var results = chatCompletionService.GetStreamingChatMessageContentsAsync(
|
||||
chatHistory: history,
|
||||
executionSettings: openSettings,
|
||||
kernel: _skClient.Kernel,
|
||||
cancelToken);
|
||||
|
||||
|
||||
await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true);
|
||||
await foreach (var result in results)
|
||||
{
|
||||
var modle = GetMessage(input.Model, result.Content);
|
||||
var message = JsonConvert.SerializeObject(modle, new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
});
|
||||
|
||||
await writer.WriteLineAsync($"data: {message}\n");
|
||||
await writer.FlushAsync(cancelToken); // 确保立即推送数据
|
||||
}
|
||||
}
|
||||
// /// <summary>
|
||||
// /// 发送消息
|
||||
// /// </summary>
|
||||
// /// <param name="input"></param>
|
||||
// /// <param name="cancelToken"></param>
|
||||
// public async Task PostSendAsync(SendMessageInput input,CancellationToken cancelToken)
|
||||
// {
|
||||
// var httpContext = this.httpContextAccessor.HttpContext;
|
||||
// var response = httpContext.Response;
|
||||
// // 设置响应头,声明是 SSE 流
|
||||
// response.ContentType = "text/event-stream";
|
||||
// response.Headers.Append("Cache-Control", "no-cache");
|
||||
// response.Headers.Append("Connection", "keep-alive");
|
||||
//
|
||||
//
|
||||
// var chatCompletionService = this._skClient.Kernel.GetRequiredService<IChatCompletionService>(input.Model);
|
||||
// var history = new ChatHistory();
|
||||
// var openSettings = new AzureOpenAIPromptExecutionSettings()
|
||||
// {
|
||||
// MaxTokens = 3000
|
||||
// };
|
||||
// foreach (var aiChatContextDto in input.Messages)
|
||||
// {
|
||||
// if (aiChatContextDto.Role == "ai")
|
||||
// {
|
||||
// history.AddAssistantMessage(aiChatContextDto.Content);
|
||||
// }
|
||||
// else if (aiChatContextDto.Role == "user")
|
||||
// {
|
||||
// history.AddUserMessage(aiChatContextDto.Content);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// var results = chatCompletionService.GetStreamingChatMessageContentsAsync(
|
||||
// chatHistory: history,
|
||||
// executionSettings: openSettings,
|
||||
// kernel: _skClient.Kernel,
|
||||
// cancelToken);
|
||||
//
|
||||
//
|
||||
// await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true);
|
||||
// await foreach (var result in results)
|
||||
// {
|
||||
// var modle = GetMessage(input.Model, result.Content);
|
||||
// var message = JsonConvert.SerializeObject(modle, new JsonSerializerSettings
|
||||
// {
|
||||
// ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
// });
|
||||
//
|
||||
// await writer.WriteLineAsync($"data: {message}\n");
|
||||
// await writer.FlushAsync(cancelToken); // 确保立即推送数据
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
private SendMessageOutputDto GetMessage(string modelId, string content)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
using Azure;
|
||||
using Azure.AI.OpenAI;
|
||||
using OpenAI.Chat;
|
||||
using Volo.Abp.Domain.Services;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Managers;
|
||||
|
||||
public class OpenAiManager : DomainService
|
||||
{
|
||||
public static async Task TestAsync()
|
||||
{
|
||||
var endpoint = new Uri("https://japan-ccnetcore-resource.cognitiveservices.azure.com/");
|
||||
// var deploymentName = "gpt-4.1-mini";
|
||||
var deploymentName = "o4-mini";
|
||||
var apiKey = "FaccnRh7Zvz25OCGH07kHPe2z1aCXMliLdr3esgWHgXQ2aivwFgDJQQJ99BFACi0881XJ3w3AAAAACOGAJ2G";
|
||||
|
||||
AzureOpenAIClient azureClient = new(
|
||||
endpoint,
|
||||
new AzureKeyCredential(apiKey));
|
||||
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
|
||||
|
||||
List<ChatMessage> messages = new List<ChatMessage>()
|
||||
{
|
||||
new UserChatMessage("使用c#写一个贪吃蛇代码"),
|
||||
};
|
||||
|
||||
var response = chatClient.CompleteChatStreamingAsync(messages);
|
||||
|
||||
await foreach (StreamingChatCompletionUpdate update in response)
|
||||
{
|
||||
foreach (ChatMessageContentPart updatePart in update.ContentUpdate)
|
||||
{
|
||||
System.Console.Write(updatePart.Text);
|
||||
}
|
||||
}
|
||||
|
||||
System.Console.WriteLine("结束");
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="..\..\..\common.props" />
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.AI.OpenAI" Version="2.2.0-beta.4" />
|
||||
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
||||
|
||||
</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.AiHub.Domain.Shared\Yi.Framework.AiHub.Domain.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Domain;
|
||||
using Yi.Framework.AiHub.Domain.Shared;
|
||||
using Yi.Framework.Mapster;
|
||||
using Yi.Framework.SemanticKernel;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkAiHubDomainSharedModule),
|
||||
typeof(YiFrameworkMapsterModule),
|
||||
typeof(AbpDddDomainModule),
|
||||
typeof(YiFrameworkSemanticKernelModule)
|
||||
typeof(AbpDddDomainModule)
|
||||
)]
|
||||
public class YiFrameworkAiHubDomainModule : AbpModule
|
||||
{
|
||||
@@ -21,5 +18,10 @@ namespace Yi.Framework.AiHub.Domain
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
var services = context.Services;
|
||||
}
|
||||
|
||||
public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
|
||||
{
|
||||
await Yi.Framework.AiHub.Domain.Managers.OpenAiManager.Test2Async();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -4,15 +4,14 @@ using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
|
||||
using Microsoft.SemanticKernel.Connectors.OpenAI;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.SemanticKernel;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
{
|
||||
public class AiManager : ISingletonDependency
|
||||
{
|
||||
private readonly SemanticKernelClient _client;
|
||||
private readonly Kernel _client;
|
||||
|
||||
public AiManager(SemanticKernelClient client)
|
||||
public AiManager(Kernel client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
@@ -30,7 +29,7 @@ namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
MaxTokens = 3000
|
||||
};
|
||||
|
||||
var chatCompletionService = this._client.Kernel.GetRequiredService<IChatCompletionService>(model);
|
||||
var chatCompletionService = this._client.GetRequiredService<IChatCompletionService>(model);
|
||||
|
||||
var history = new ChatHistory();
|
||||
foreach (var aiChatContextDto in aiChatContextDtos)
|
||||
@@ -48,7 +47,7 @@ namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
var results = chatCompletionService.GetStreamingChatMessageContentsAsync(
|
||||
chatHistory: history,
|
||||
executionSettings: openSettings,
|
||||
kernel: _client.Kernel);
|
||||
kernel: _client);
|
||||
|
||||
await foreach (var result in results)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
<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>
|
||||
@@ -13,6 +12,7 @@
|
||||
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" />
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.Mapster\Yi.Framework.Mapster.csproj" />
|
||||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.57.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -2,14 +2,12 @@
|
||||
using Volo.Abp.Domain;
|
||||
using Yi.Framework.Caching.FreeRedis;
|
||||
using Yi.Framework.ChatHub.Domain.Shared;
|
||||
using Yi.Framework.SemanticKernel;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkChatHubDomainSharedModule),
|
||||
typeof(YiFrameworkCachingFreeRedisModule),
|
||||
typeof(YiFrameworkSemanticKernelModule),
|
||||
typeof(AbpDddDomainModule)
|
||||
)]
|
||||
public class YiFrameworkChatHubDomainModule : AbpModule
|
||||
|
||||
Reference in New Issue
Block a user