feat: 完成ai接口

This commit is contained in:
ccnetcore
2025-06-19 21:24:13 +08:00
parent b39f15c798
commit 8040010b98
8 changed files with 438 additions and 2 deletions

View File

@@ -0,0 +1,59 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos;
public class ModelGetListOutput
{
/// <summary>
/// 模型ID
/// </summary>
public long Id { get; set; }
/// <summary>
/// 模型分类
/// </summary>
public string Category { get; set; }
/// <summary>
/// 模型名称
/// </summary>
public string ModelName { get; set; }
/// <summary>
/// 模型描述
/// </summary>
public string ModelDescribe { get; set; }
/// <summary>
/// 模型价格
/// </summary>
public double ModelPrice { get; set; }
/// <summary>
/// 模型类型
/// </summary>
public string ModelType { get; set; }
/// <summary>
/// 模型展示状态
/// </summary>
public string ModelShow { get; set; }
/// <summary>
/// 系统提示
/// </summary>
public string SystemPrompt { get; set; }
/// <summary>
/// API 主机地址
/// </summary>
public string ApiHost { get; set; }
/// <summary>
/// API 密钥
/// </summary>
public string ApiKey { get; set; }
/// <summary>
/// 备注信息
/// </summary>
public string Remark { get; set; }
}

View File

@@ -0,0 +1,13 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos;
public class SendMessageInput
{
public List<Message> Messages { get; set; }
public string Model { get; set; }
}
public class Message
{
public string Role { get; set; }
public string Content { get; set; }
}

View File

@@ -0,0 +1,164 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos;
public class SendMessageOutputDto
{
/// <summary>
/// 唯一标识符
/// </summary>
public int Id { get; set; }
/// <summary>
/// 对象类型
/// </summary>
public string Object { get; set; }
/// <summary>
/// 创建时间Unix时间戳格式
/// </summary>
public long Created { get; set; }
/// <summary>
/// 模型名称
/// </summary>
public string Model { get; set; }
/// <summary>
/// 选择项列表
/// </summary>
public List<Choice> Choices { get; set; }
/// <summary>
/// 系统指纹(可能为空)
/// </summary>
public string SystemFingerprint { get; set; }
/// <summary>
/// 使用情况信息
/// </summary>
public Usage Usage { get; set; }
}
/// <summary>
/// 选择项类,表示模型返回的一个选择
/// </summary>
public class Choice
{
/// <summary>
/// 选择索引
/// </summary>
public int Index { get; set; }
/// <summary>
/// 变化内容,包括内容字符串和角色
/// </summary>
public Delta Delta { get; set; }
/// <summary>
/// 结束原因,可能为空
/// </summary>
public string FinishReason { get; set; }
/// <summary>
/// 内容过滤结果
/// </summary>
public ContentFilterResults ContentFilterResults { get; set; }
}
/// <summary>
/// 变化内容
/// </summary>
public class Delta
{
/// <summary>
/// 内容文本
/// </summary>
public string Content { get; set; }
/// <summary>
/// 角色,例如"assistant"
/// </summary>
public string Role { get; set; }
}
/// <summary>
/// 内容过滤结果
/// </summary>
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; }
}
/// <summary>
/// 过滤状态,表示是否经过过滤以及检测是否命中
/// </summary>
public class FilterStatus
{
/// <summary>
/// 是否被过滤
/// </summary>
public bool Filtered { get; set; }
/// <summary>
/// 是否检测到该类型(例如 Jailbreak 中存在此字段)
/// </summary>
public bool? Detected { get; set; }
}
/// <summary>
/// 使用情况,记录 token 数量等信息
/// </summary>
public class Usage
{
/// <summary>
/// 提示词数量
/// </summary>
public int PromptTokens { get; set; }
/// <summary>
/// 补全词数量
/// </summary>
public int CompletionTokens { get; set; }
/// <summary>
/// 总的 Token 数量
/// </summary>
public int TotalTokens { get; set; }
/// <summary>
/// 提示词详细信息
/// </summary>
public PromptTokensDetails PromptTokensDetails { get; set; }
/// <summary>
/// 补全文字详细信息
/// </summary>
public CompletionTokensDetails CompletionTokensDetails { get; set; }
}
/// <summary>
/// 提示词相关 token 详细信息
/// </summary>
public class PromptTokensDetails
{
public int AudioTokens { get; set; }
public int CachedTokens { get; set; }
}
/// <summary>
/// 补全相关 token 详细信息
/// </summary>
public class CompletionTokensDetails
{
public int AudioTokens { get; set; }
public int ReasoningTokens { get; set; }
public int AcceptedPredictionTokens { get; set; }
public int RejectedPredictionTokens { get; set; }
}

View File

@@ -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;
}
/// <summary>
/// 获取模型列表
/// </summary>
/// <returns></returns>
public async Task<List<ModelGetListOutput>> GetModelAsync()
{
return new List<ModelGetListOutput>()
{
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 = "牛逼啊"
}
};
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="input"></param>
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<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);
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;
}
}

View File

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

View File

@@ -4,6 +4,7 @@
<ItemGroup>
<ProjectReference Include="..\..\framework\Yi.Framework.Ddd.Application\Yi.Framework.Ddd.Application.csproj" />
<ProjectReference Include="..\..\module\ai-hub\Yi.Framework.AiHub.Application\Yi.Framework.AiHub.Application.csproj" />
<ProjectReference Include="..\..\module\bbs\Yi.Framework.Bbs.Application\Yi.Framework.Bbs.Application.csproj" />
<ProjectReference Include="..\..\module\chat-hub\Yi.Framework.ChatHub.Application\Yi.Framework.ChatHub.Application.csproj" />
<ProjectReference Include="..\..\module\code-gen\Yi.Framework.CodeGen.Application\Yi.Framework.CodeGen.Application.csproj" />

View File

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

View File

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