using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using OpenAI.Chat; using SqlSugar; using Volo.Abp.Application.Services; using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAiDto; using Yi.Framework.AiHub.Domain.Entities.Model; using Yi.Framework.AiHub.Domain.Managers; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.AiHub.Application.Services; public class OpenApiService : ApplicationService { private readonly IHttpContextAccessor _httpContextAccessor; private readonly ILogger _logger; private readonly TokenManager _tokenManager; private readonly AiGateWayManager _aiGateWayManager; private readonly ISqlSugarRepository _aiModelRepository; public OpenApiService(IHttpContextAccessor httpContextAccessor, ILogger logger, TokenManager tokenManager, AiGateWayManager aiGateWayManager, ISqlSugarRepository aiModelRepository) { _httpContextAccessor = httpContextAccessor; _logger = logger; _tokenManager = tokenManager; _aiGateWayManager = aiGateWayManager; _aiModelRepository = aiModelRepository; } /// /// 对话 /// /// /// [HttpPost("openApi/v1/chat/completions")] public async Task ChatCompletionsAsync(ChatCompletionsInput input, CancellationToken cancellationToken) { //前面都是校验,后面才是真正的调用 var httpContext = this._httpContextAccessor.HttpContext; var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext)); var history = new List(); 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)); } } //ai网关代理httpcontext await _aiGateWayManager.CompleteChatForHttpContextAsync(_httpContextAccessor.HttpContext, input.Model, history, userId, null, cancellationToken); } /// /// 获取模型列表 /// /// [HttpGet("openApi/v1/models")] public async Task ModelsAsync() { var data = await _aiModelRepository._DbQueryable .OrderByDescending(x => x.OrderNum) .Select(x => new ModelDataOutput { ModelId = x.ModelId, Object = "model", Owned_by = "organization-owner", Permission = new List() }).ToListAsync(); return new ModelGetOutput() { Data = data }; } 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; } }