70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
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<AiAppAggregateRoot> _aiAppRepository;
|
|
|
|
public AiGateWayManager(ISqlSugarRepository<AiAppAggregateRoot> aiAppRepository)
|
|
{
|
|
_aiAppRepository = aiAppRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取模型
|
|
/// </summary>
|
|
/// <param name="modelId"></param>
|
|
/// <returns></returns>
|
|
private async Task<AiModelDescribe> 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}模型当前版本不支持");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 聊天完成
|
|
/// </summary>
|
|
/// <param name="modelId"></param>
|
|
/// <param name="messages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages,
|
|
[EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
var modelDescribe = await GetModelAsync(modelId);
|
|
var chatService = LazyServiceProvider.GetRequiredKeyedService<IChatService>(modelDescribe.HandlerName);
|
|
await foreach (var result in chatService.CompleteChatAsync(modelDescribe, messages, cancellationToken))
|
|
{
|
|
yield return result;
|
|
}
|
|
}
|
|
} |