feat: 完成ai-hub第一期功能

This commit is contained in:
chenchun
2025-06-25 17:12:09 +08:00
parent 4f71d874bd
commit 695aaedfba
18 changed files with 360 additions and 103 deletions

View File

@@ -1,28 +1,24 @@
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenAI.Chat;
using Yi.Framework.AiHub.Application.Contracts.Options;
using Yi.Framework.AiHub.Domain.Extensions;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
namespace Yi.Framework.AiHub.Domain.AiChat.Impl;
public class AzureRestChatService : IChatService
{
private readonly AiChatModelOptions _options;
public AzureRestChatService(IOptions<AiGateWayOptions> options)
public AzureRestChatService()
{
this._options = options.Value.Chats[nameof(AzureRestChatService)];
}
public async IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages,
public async IAsyncEnumerable<string> CompleteChatAsync(AiModelDescribe aiModelDescribe, List<ChatMessage> messages,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
// 设置API URL
var apiUrl = $"{_options.Endpoint}models/chat/completions";
var apiUrl = $"{aiModelDescribe.Endpoint}models/chat/completions";
var ss = messages.Select(x => new
@@ -45,7 +41,7 @@ public class AzureRestChatService : IChatService
top_p = 0.1,
presence_penalty = 0,
frequency_penalty = 0,
model = modelId
model = aiModelDescribe.ModelId
};
// 序列化请求内容为JSON
@@ -53,24 +49,25 @@ public class AzureRestChatService : IChatService
using var httpClient = new HttpClient();
// 设置请求头
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_options.ApiKey}");
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {aiModelDescribe.ApiKey}");
// 其他头信息如Content-Type在StringContent中设置
// 构造 POST 请求
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
// 设置请求内容(示例)
request.Content =new StringContent(jsonBody, Encoding.UTF8, "application/json");
request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
// 发送POST请求
HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
HttpResponseMessage response =
await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
// 确认响应成功
response.EnsureSuccessStatusCode();
// 读取响应内容
var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
// 从流中读取数据并输出到控制台
using var streamReader = new System.IO.StreamReader(responseStream);
using var streamReader = new StreamReader(responseStream);
string line;
while ((line = await streamReader.ReadLineAsync(cancellationToken)) != null)
{
@@ -117,7 +114,5 @@ public class AzureRestChatService : IChatService
// 解析失败
return null;
}
return null;
}
}