feat: 完成ai网关搭建
This commit is contained in:
@@ -50,8 +50,8 @@ public class AiService : ApplicationService
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="cancelToken"></param>
|
||||
public async Task PostSendAsync(SendMessageInput input, CancellationToken cancelToken)
|
||||
/// <param name="cancellationToken"></param>
|
||||
public async Task PostSendAsync(SendMessageInput input, CancellationToken cancellationToken)
|
||||
{
|
||||
var httpContext = this._httpContextAccessor.HttpContext;
|
||||
var response = httpContext.Response;
|
||||
@@ -75,7 +75,7 @@ public class AiService : ApplicationService
|
||||
}
|
||||
|
||||
var gateWay = LazyServiceProvider.GetRequiredService<AiGateWayManager>();
|
||||
var completeChatResponse = gateWay.CompleteChatAsync(input.Model, history);
|
||||
var completeChatResponse = gateWay.CompleteChatAsync(input.Model, history,cancellationToken);
|
||||
await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true);
|
||||
await foreach (var data in completeChatResponse)
|
||||
{
|
||||
@@ -86,8 +86,12 @@ public class AiService : ApplicationService
|
||||
});
|
||||
|
||||
await writer.WriteLineAsync($"data: {message}\n");
|
||||
await writer.FlushAsync(cancelToken); // 确保立即推送数据
|
||||
await writer.FlushAsync(cancellationToken); // 确保立即推送数据
|
||||
}
|
||||
|
||||
//断开连接
|
||||
await writer.WriteLineAsync($"data: done\n");
|
||||
await writer.FlushAsync(cancellationToken); // 确保立即推送数据
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user