feat: 完成ai-hub第一期功能
This commit is contained in:
@@ -1,35 +1,70 @@
|
||||
using Azure;
|
||||
using Azure.AI.OpenAI;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenAI.Chat;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.AiHub.Application.Contracts.Options;
|
||||
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 AiGateWayOptions _options;
|
||||
private readonly ISqlSugarRepository<AiAppAggregateRoot> _aiAppRepository;
|
||||
|
||||
public AiGateWayManager(IOptions<AiGateWayOptions> options)
|
||||
public AiGateWayManager(ISqlSugarRepository<AiAppAggregateRoot> aiAppRepository)
|
||||
{
|
||||
this._options = options.Value;
|
||||
_aiAppRepository = aiAppRepository;
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages,
|
||||
CancellationToken cancellationToken)
|
||||
/// <summary>
|
||||
/// 获取模型
|
||||
/// </summary>
|
||||
/// <param name="modelId"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<AiModelDescribe> GetModelAsync(string modelId)
|
||||
{
|
||||
foreach (var chat in _options.Chats)
|
||||
var allApp = await _aiAppRepository._DbQueryable.Includes(x => x.AiModels).ToListAsync();
|
||||
foreach (var app in allApp)
|
||||
{
|
||||
if (chat.Value.ModelIds.Contains(modelId))
|
||||
var model = app.AiModels.FirstOrDefault(x => x.ModelId == modelId);
|
||||
if (model is not null)
|
||||
{
|
||||
var chatService = LazyServiceProvider.GetRequiredKeyedService<IChatService>(chat.Key);
|
||||
return chatService.CompleteChatAsync(modelId, messages, cancellationToken);
|
||||
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}】");
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user