feat:完成ai网关搭建

This commit is contained in:
ccnetcore
2025-06-21 01:08:14 +08:00
parent 6abcc49ed4
commit 3b74dfd49a
10 changed files with 236 additions and 173 deletions

View File

@@ -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}】");
}
}

View File

@@ -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("结束");
}
}