diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/ModelGetListOutput.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/ModelGetListOutput.cs new file mode 100644 index 00000000..556599c0 --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/ModelGetListOutput.cs @@ -0,0 +1,59 @@ +namespace Yi.Framework.AiHub.Application.Contracts.Dtos; + +public class ModelGetListOutput +{ + /// + /// 模型ID + /// + public long Id { get; set; } + + /// + /// 模型分类 + /// + public string Category { get; set; } + + /// + /// 模型名称 + /// + public string ModelName { get; set; } + + /// + /// 模型描述 + /// + public string ModelDescribe { get; set; } + + /// + /// 模型价格 + /// + public double ModelPrice { get; set; } + + /// + /// 模型类型 + /// + public string ModelType { get; set; } + + /// + /// 模型展示状态 + /// + public string ModelShow { get; set; } + + /// + /// 系统提示 + /// + public string SystemPrompt { get; set; } + + /// + /// API 主机地址 + /// + public string ApiHost { get; set; } + + /// + /// API 密钥 + /// + public string ApiKey { get; set; } + + /// + /// 备注信息 + /// + public string Remark { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/SendMessageInput.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/SendMessageInput.cs new file mode 100644 index 00000000..91486e94 --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/SendMessageInput.cs @@ -0,0 +1,13 @@ +namespace Yi.Framework.AiHub.Application.Contracts.Dtos; + +public class SendMessageInput +{ + public List Messages { get; set; } + public string Model { get; set; } +} + +public class Message +{ + public string Role { get; set; } + public string Content { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/SendMessageOutputDto.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/SendMessageOutputDto.cs new file mode 100644 index 00000000..d3a30bca --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/SendMessageOutputDto.cs @@ -0,0 +1,164 @@ +namespace Yi.Framework.AiHub.Application.Contracts.Dtos; + +public class SendMessageOutputDto +{ + /// + /// 唯一标识符 + /// + public int Id { get; set; } + + /// + /// 对象类型 + /// + public string Object { get; set; } + + /// + /// 创建时间,Unix时间戳格式 + /// + public long Created { get; set; } + + /// + /// 模型名称 + /// + public string Model { get; set; } + + /// + /// 选择项列表 + /// + public List Choices { get; set; } + + /// + /// 系统指纹(可能为空) + /// + public string SystemFingerprint { get; set; } + + /// + /// 使用情况信息 + /// + public Usage Usage { get; set; } +} + +/// +/// 选择项类,表示模型返回的一个选择 +/// +public class Choice +{ + /// + /// 选择索引 + /// + public int Index { get; set; } + + /// + /// 变化内容,包括内容字符串和角色 + /// + public Delta Delta { get; set; } + + /// + /// 结束原因,可能为空 + /// + public string FinishReason { get; set; } + + /// + /// 内容过滤结果 + /// + public ContentFilterResults ContentFilterResults { get; set; } +} + +/// +/// 变化内容 +/// +public class Delta +{ + /// + /// 内容文本 + /// + public string Content { get; set; } + + /// + /// 角色,例如"assistant" + /// + public string Role { get; set; } +} + +/// +/// 内容过滤结果 +/// +public class ContentFilterResults +{ + public FilterStatus Hate { get; set; } + public FilterStatus SelfHarm { get; set; } + public FilterStatus Sexual { get; set; } + public FilterStatus Violence { get; set; } + public FilterStatus Jailbreak { get; set; } + public FilterStatus Profanity { get; set; } +} + +/// +/// 过滤状态,表示是否经过过滤以及检测是否命中 +/// +public class FilterStatus +{ + /// + /// 是否被过滤 + /// + public bool Filtered { get; set; } + + /// + /// 是否检测到该类型(例如 Jailbreak 中存在此字段) + /// + public bool? Detected { get; set; } +} + +/// +/// 使用情况,记录 token 数量等信息 +/// +public class Usage +{ + /// + /// 提示词数量 + /// + public int PromptTokens { get; set; } + + /// + /// 补全词数量 + /// + public int CompletionTokens { get; set; } + + /// + /// 总的 Token 数量 + /// + public int TotalTokens { get; set; } + + /// + /// 提示词详细信息 + /// + public PromptTokensDetails PromptTokensDetails { get; set; } + + /// + /// 补全文字详细信息 + /// + public CompletionTokensDetails CompletionTokensDetails { get; set; } +} + +/// +/// 提示词相关 token 详细信息 +/// +public class PromptTokensDetails +{ + public int AudioTokens { get; set; } + public int CachedTokens { get; set; } +} + +/// +/// 补全相关 token 详细信息 +/// +public class CompletionTokensDetails +{ + public int AudioTokens { get; set; } + + public int ReasoningTokens { get; set; } + + public int AcceptedPredictionTokens { get; set; } + + public int RejectedPredictionTokens { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiService.cs new file mode 100644 index 00000000..eaf205ac --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiService.cs @@ -0,0 +1,195 @@ +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 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 IHttpContextAccessor httpContextAccessor; + + public AiService(SemanticKernelClient skClient, IHttpContextAccessor httpContextAccessor) + { + _skClient = skClient; + this.httpContextAccessor = httpContextAccessor; + } + + /// + /// 获取模型列表 + /// + /// + public async Task> GetModelAsync() + { + return new List() + { + new ModelGetListOutput + { + Id = 001, + Category = "chat", + ModelName = "gpt-4.1-mini", + ModelDescribe = "gpt下的ai", + ModelPrice = 4, + ModelType = "1", + ModelShow = "0", + SystemPrompt = "", + ApiHost = "", + ApiKey = "", + Remark = "牛逼" + }, + new ModelGetListOutput + { + Id = 002, + Category = "chat", + ModelName = "grok-3-mini", + ModelDescribe = "马斯克的ai", + ModelPrice = 5, + ModelType = "1", + ModelShow = "0", + SystemPrompt = "", + ApiHost = "", + ApiKey = "", + Remark = "牛逼啊" + } + }; + } + + + /// + /// 发送消息 + /// + /// + public async Task PostSendAsync(SendMessageInput input) + { + var httpContext = this.httpContextAccessor.HttpContext; + var response = httpContext.Response; + // 设置响应头,声明是 SSE 流 + response.ContentType = "text/event-stream"; + response.Headers.Add("Cache-Control", "no-cache"); + response.Headers.Add("Connection", "keep-alive"); + + + var chatCompletionService = this._skClient.Kernel.GetRequiredService(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); + + + 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(); // 确保立即推送数据 + } + } + + + private SendMessageOutputDto GetMessage(string modelId, string content) + { + var output = new SendMessageOutputDto + { + Id = 001, + Object = "chat.completion.chunk", + Created = 1750336171, + Model = modelId, + Choices = new() + { + new Choice + { + Index = 0, + Delta = new Delta + { + Content = content, + Role = "assistant" + }, + FinishReason = null, + ContentFilterResults = new() + { + Hate = new() + { + Filtered = false, + Detected = null + }, + SelfHarm = new() + { + Filtered = false, + Detected = null + }, + Sexual = new() + { + Filtered = false, + Detected = null + }, + Violence = new() + { + Filtered = false, + Detected = null + }, + Jailbreak = new() + { + Filtered = false, + Detected = false + }, + Profanity = new() + { + Filtered = false, + Detected = false + }, + } + } + }, + SystemFingerprint = "", + Usage = new Usage + { + PromptTokens = 75, + CompletionTokens = 25, + TotalTokens = 100, + PromptTokensDetails = new() + { + AudioTokens = 0, + CachedTokens = 0 + }, + CompletionTokensDetails = new() + { + AudioTokens = 0, + ReasoningTokens = 0, + AcceptedPredictionTokens = 0, + RejectedPredictionTokens = 0 + } + } + }; + + return output; + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/chat-hub/Yi.Framework.ChatHub.Domain/Managers/AiManager.cs b/Yi.Abp.Net8/module/chat-hub/Yi.Framework.ChatHub.Domain/Managers/AiManager.cs index 73f1c05e..60371679 100644 --- a/Yi.Abp.Net8/module/chat-hub/Yi.Framework.ChatHub.Domain/Managers/AiManager.cs +++ b/Yi.Abp.Net8/module/chat-hub/Yi.Framework.ChatHub.Domain/Managers/AiManager.cs @@ -27,8 +27,7 @@ namespace Yi.Framework.ChatHub.Domain.Managers var openSettings = new AzureOpenAIPromptExecutionSettings() { - MaxTokens = 3000, - //MaxTokens = 1000 + MaxTokens = 3000 }; var chatCompletionService = this._client.Kernel.GetRequiredService(model); diff --git a/Yi.Abp.Net8/src/Yi.Abp.Application/Yi.Abp.Application.csproj b/Yi.Abp.Net8/src/Yi.Abp.Application/Yi.Abp.Application.csproj index 97b45b8c..e5866da9 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Application/Yi.Abp.Application.csproj +++ b/Yi.Abp.Net8/src/Yi.Abp.Application/Yi.Abp.Application.csproj @@ -4,6 +4,7 @@ + diff --git a/Yi.Abp.Net8/src/Yi.Abp.Application/YiAbpApplicationModule.cs b/Yi.Abp.Net8/src/Yi.Abp.Application/YiAbpApplicationModule.cs index f3a523b6..cd971d65 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Application/YiAbpApplicationModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Application/YiAbpApplicationModule.cs @@ -1,6 +1,7 @@ using Volo.Abp.SettingManagement; using Yi.Abp.Application.Contracts; using Yi.Abp.Domain; +using Yi.Framework.AiHub.Application; using Yi.Framework.Bbs.Application; using Yi.Framework.ChatHub.Application; using Yi.Framework.CodeGen.Application; @@ -23,6 +24,7 @@ namespace Yi.Abp.Application typeof(YiFrameworkDigitalCollectiblesApplicationModule), typeof(YiFrameworkChatHubApplicationModule), typeof(YiFrameworkStockApplicationModule), + typeof(YiFrameworkAiHubApplicationModule), typeof(YiFrameworkTenantManagementApplicationModule), typeof(YiFrameworkCodeGenApplicationModule), diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs index f0db25c3..2ac6b509 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs @@ -33,6 +33,7 @@ using Volo.Abp.MultiTenancy; using Volo.Abp.Swashbuckle; using Yi.Abp.Application; using Yi.Abp.SqlsugarCore; +using Yi.Framework.AiHub.Application; using Yi.Framework.AspNetCore; using Yi.Framework.AspNetCore.Authentication.OAuth; using Yi.Framework.AspNetCore.Authentication.OAuth.Gitee; @@ -96,6 +97,8 @@ namespace Yi.Abp.Web options => options.RemoteServiceName = "digital-collectibles"); options.ConventionalControllers.Create(typeof(YiFrameworkStockApplicationModule).Assembly, options => options.RemoteServiceName = "ai-stock"); + options.ConventionalControllers.Create(typeof(YiFrameworkAiHubApplicationModule).Assembly, + options => options.RemoteServiceName = "ai-hub"); //统一前缀 options.ConventionalControllers.ConventionalControllerSettings.ForEach(x => x.RootPath = "api/app"); });