feat:完成ai网关搭建
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
using OpenAI.Chat;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiChat;
|
||||
|
||||
public interface IChatService
|
||||
{
|
||||
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Azure;
|
||||
using Azure.AI.OpenAI;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenAI.Chat;
|
||||
using Yi.Framework.AiHub.Application.Contracts.Options;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiChat.Impl;
|
||||
|
||||
public class AzureChatService : IChatService
|
||||
{
|
||||
private readonly AiChatModelOptions _options;
|
||||
|
||||
public AzureChatService(IOptions<AiGateWayOptions> options)
|
||||
{
|
||||
this._options = options.Value.Chats[nameof(AzureChatService)];
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages)
|
||||
{
|
||||
var endpoint = new Uri(_options.Endpoint);
|
||||
|
||||
var deploymentName = modelId;
|
||||
var apiKey = _options.ApiKey;
|
||||
|
||||
AzureOpenAIClient azureClient = new(
|
||||
endpoint,
|
||||
new AzureKeyCredential(apiKey));
|
||||
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
|
||||
|
||||
var response = chatClient.CompleteChatStreamingAsync(messages);
|
||||
|
||||
await foreach (StreamingChatCompletionUpdate update in response)
|
||||
{
|
||||
foreach (ChatMessageContentPart updatePart in update.ContentUpdate)
|
||||
{
|
||||
yield return updatePart.Text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Azure;
|
||||
using Azure.AI.OpenAI;
|
||||
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;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Managers;
|
||||
|
||||
public class AiGateWayManager : DomainService
|
||||
{
|
||||
private readonly AiGateWayOptions _options;
|
||||
|
||||
public AiGateWayManager(IOptions<AiGateWayOptions> options)
|
||||
{
|
||||
this._options = options.Value;
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages)
|
||||
{
|
||||
foreach (var chat in _options.Chats)
|
||||
{
|
||||
if (chat.Value.ModelIds.Contains(modelId))
|
||||
{
|
||||
var chatService = LazyServiceProvider.GetRequiredKeyedService<IChatService>(chat.Key);
|
||||
return chatService.CompleteChatAsync(modelId, messages);
|
||||
}
|
||||
}
|
||||
throw new UserFriendlyException($"当前暂不支持该模型-【{modelId}】");
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
using Azure;
|
||||
using Azure.AI.OpenAI;
|
||||
using OpenAI.Chat;
|
||||
using Volo.Abp.Domain.Services;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Managers;
|
||||
|
||||
public class OpenAiManager : DomainService
|
||||
{
|
||||
public static async Task TestAsync()
|
||||
{
|
||||
var endpoint = new Uri("https://japan-ccnetcore-resource.cognitiveservices.azure.com/");
|
||||
// var deploymentName = "gpt-4.1-mini";
|
||||
var deploymentName = "o4-mini";
|
||||
var apiKey = "FaccnRh7Zvz25OCGH07kHPe2z1aCXMliLdr3esgWHgXQ2aivwFgDJQQJ99BFACi0881XJ3w3AAAAACOGAJ2G";
|
||||
|
||||
AzureOpenAIClient azureClient = new(
|
||||
endpoint,
|
||||
new AzureKeyCredential(apiKey));
|
||||
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
|
||||
|
||||
List<ChatMessage> messages = new List<ChatMessage>()
|
||||
{
|
||||
new UserChatMessage("使用c#写一个贪吃蛇代码"),
|
||||
};
|
||||
|
||||
var response = chatClient.CompleteChatStreamingAsync(messages);
|
||||
|
||||
await foreach (StreamingChatCompletionUpdate update in response)
|
||||
{
|
||||
foreach (ChatMessageContentPart updatePart in update.ContentUpdate)
|
||||
{
|
||||
System.Console.Write(updatePart.Text);
|
||||
}
|
||||
}
|
||||
|
||||
System.Console.WriteLine("结束");
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.Mapster\Yi.Framework.Mapster.csproj" />
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.AiHub.Application.Contracts\Yi.Framework.AiHub.Application.Contracts.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.AiHub.Domain.Shared\Yi.Framework.AiHub.Domain.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using OpenAI.Chat;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Domain;
|
||||
using Yi.Framework.AiHub.Application.Contracts.Options;
|
||||
using Yi.Framework.AiHub.Domain.AiChat;
|
||||
using Yi.Framework.AiHub.Domain.AiChat.Impl;
|
||||
using Yi.Framework.AiHub.Domain.Managers;
|
||||
using Yi.Framework.AiHub.Domain.Shared;
|
||||
using Yi.Framework.Mapster;
|
||||
|
||||
@@ -17,11 +22,17 @@ namespace Yi.Framework.AiHub.Domain
|
||||
{
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
var services = context.Services;
|
||||
|
||||
Configure<AiGateWayOptions>(configuration.GetSection("AiGateWay"));
|
||||
|
||||
|
||||
services.AddKeyedTransient<IChatService, AzureChatService>(nameof(AzureChatService));
|
||||
}
|
||||
|
||||
public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
|
||||
{
|
||||
await Yi.Framework.AiHub.Domain.Managers.OpenAiManager.Test2Async();
|
||||
var service = context.ServiceProvider;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user