Files
Yi.Admin/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/OpenApiService.cs

99 lines
3.4 KiB
C#
Raw Normal View History

2025-07-05 15:11:56 +08:00
using Microsoft.AspNetCore.Http;
2025-07-02 23:30:29 +08:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OpenAI.Chat;
2025-07-05 15:11:56 +08:00
using SqlSugar;
2025-07-02 23:30:29 +08:00
using Volo.Abp.Application.Services;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAiDto;
2025-07-05 15:11:56 +08:00
using Yi.Framework.AiHub.Domain.Entities.Model;
2025-07-02 23:30:29 +08:00
using Yi.Framework.AiHub.Domain.Managers;
2025-07-03 22:31:39 +08:00
using Yi.Framework.SqlSugarCore.Abstractions;
2025-07-02 23:30:29 +08:00
namespace Yi.Framework.AiHub.Application.Services;
public class OpenApiService : ApplicationService
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<OpenApiService> _logger;
2025-07-03 22:31:39 +08:00
private readonly TokenManager _tokenManager;
2025-07-05 15:11:56 +08:00
private readonly AiGateWayManager _aiGateWayManager;
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
2025-07-02 23:30:29 +08:00
2025-07-03 22:31:39 +08:00
public OpenApiService(IHttpContextAccessor httpContextAccessor, ILogger<OpenApiService> logger,
2025-07-05 15:11:56 +08:00
TokenManager tokenManager, AiGateWayManager aiGateWayManager,
ISqlSugarRepository<AiModelEntity> aiModelRepository)
2025-07-02 23:30:29 +08:00
{
_httpContextAccessor = httpContextAccessor;
_logger = logger;
2025-07-03 22:31:39 +08:00
_tokenManager = tokenManager;
2025-07-05 15:11:56 +08:00
_aiGateWayManager = aiGateWayManager;
_aiModelRepository = aiModelRepository;
2025-07-02 23:30:29 +08:00
}
2025-07-03 22:31:39 +08:00
/// <summary>
/// 对话
/// </summary>
/// <param name="input"></param>
/// <param name="cancellationToken"></param>
2025-07-03 22:44:52 +08:00
[HttpPost("openApi/v1/chat/completions")]
2025-07-02 23:30:29 +08:00
public async Task ChatCompletionsAsync(ChatCompletionsInput input, CancellationToken cancellationToken)
{
//前面都是校验,后面才是真正的调用
var httpContext = this._httpContextAccessor.HttpContext;
2025-07-03 22:31:39 +08:00
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
2025-07-02 23:30:29 +08:00
var history = new List<ChatMessage>();
foreach (var aiChatContextDto in input.Messages)
{
if (aiChatContextDto.Role == "assistant")
{
history.Add(ChatMessage.CreateAssistantMessage(aiChatContextDto.Content));
}
else if (aiChatContextDto.Role == "user")
{
history.Add(ChatMessage.CreateUserMessage(aiChatContextDto.Content));
}
}
2025-07-05 15:11:56 +08:00
//ai网关代理httpcontext
await _aiGateWayManager.CompleteChatForHttpContextAsync(_httpContextAccessor.HttpContext, input.Model, history,
userId, null, cancellationToken);
2025-07-02 23:30:29 +08:00
}
2025-07-04 00:16:58 +08:00
/// <summary>
/// 获取模型列表
/// </summary>
/// <returns></returns>
[HttpGet("openApi/v1/models")]
public async Task<ModelGetOutput> ModelsAsync()
{
2025-07-05 15:11:56 +08:00
var data = await _aiModelRepository._DbQueryable
.OrderByDescending(x => x.OrderNum)
.Select(x => new ModelDataOutput
2025-07-04 00:16:58 +08:00
{
2025-07-05 15:11:56 +08:00
ModelId = x.ModelId,
Object = "model",
Owned_by = "organization-owner",
Permission = new List<string>()
}).ToListAsync();
return new ModelGetOutput()
2025-07-02 23:30:29 +08:00
{
2025-07-05 15:11:56 +08:00
Data = data
2025-07-02 23:30:29 +08:00
};
}
2025-07-03 22:31:39 +08:00
private string? GetTokenByHttpContext(HttpContext httpContext)
{
// 获取Authorization头
string authHeader = httpContext.Request.Headers["Authorization"];
// 检查是否有Bearer token
if (authHeader != null && authHeader.StartsWith("Bearer "))
{
return authHeader.Substring("Bearer ".Length).Trim();
}
return null;
}
2025-07-02 23:30:29 +08:00
}