using System.Runtime.CompilerServices; using Microsoft.Extensions.DependencyInjection; using OpenAI.Chat; using Volo.Abp.Domain.Services; using Yi.Framework.AiHub.Domain.AiChat; using Yi.Framework.AiHub.Domain.Entities; using Yi.Framework.AiHub.Domain.Shared.Dtos; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.AiHub.Domain.Managers; public class AiGateWayManager : DomainService { private readonly ISqlSugarRepository _aiAppRepository; public AiGateWayManager(ISqlSugarRepository aiAppRepository) { _aiAppRepository = aiAppRepository; } /// /// 获取模型 /// /// /// private async Task GetModelAsync(string modelId) { var allApp = await _aiAppRepository._DbQueryable.Includes(x => x.AiModels).ToListAsync(); foreach (var app in allApp) { var model = app.AiModels.FirstOrDefault(x => x.ModelId == modelId); if (model is not null) { return new AiModelDescribe { AppId = app.Id, AppName = app.Name, Endpoint = app.Endpoint, ApiKey = app.ApiKey, OrderNum = model.OrderNum, HandlerName = model.HandlerName, ModelId = model.ModelId, ModelName = model.Name, Description = model.Description }; } } throw new UserFriendlyException($"{modelId}模型当前版本不支持"); } /// /// 聊天完成 /// /// /// /// /// public async IAsyncEnumerable CompleteChatAsync(string modelId, List messages, [EnumeratorCancellation] CancellationToken cancellationToken) { var modelDescribe = await GetModelAsync(modelId); var chatService = LazyServiceProvider.GetRequiredKeyedService(modelDescribe.HandlerName); await foreach (var result in chatService.CompleteChatAsync(modelDescribe, messages, cancellationToken)) { yield return result; } } }