mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-07 12:16:04 +00:00
Update AzureAi.cs
This commit is contained in:
parent
2a9ebaff51
commit
1742ca385b
|
@ -10,9 +10,8 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
public class AzureAi : IResourceTool
|
public class AzureAi : IResourceTool
|
||||||
{
|
{
|
||||||
private ResourceServer Server;
|
private ResourceServer Server;
|
||||||
private static readonly HttpClient HttpClient = new HttpClient();
|
|
||||||
private const string Prefix = "azure-ai/";
|
private const string Prefix = "azure-ai/";
|
||||||
private const int StreamingBufferSize = 4096;
|
private const int ChunkSize = 4096;
|
||||||
private readonly string AzureAiServiceUrl;
|
private readonly string AzureAiServiceUrl;
|
||||||
private readonly string AzureAiServiceApiKey;
|
private readonly string AzureAiServiceApiKey;
|
||||||
|
|
||||||
|
@ -34,7 +33,7 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
string apiKey = AzureAiServiceApiKey;
|
string apiKey = AzureAiServiceApiKey;
|
||||||
if (string.IsNullOrEmpty(apiKey))
|
if (string.IsNullOrEmpty(apiKey))
|
||||||
{
|
{
|
||||||
WriteError(context, "Missing 'api-key' header.", HttpStatusCode.BadRequest);
|
WriteError(context, "Missing the API key.", HttpStatusCode.BadRequest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,15 +63,18 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
|
|
||||||
private async Task<HttpResponseMessage> SendAzureRequestAsync(string apiKey, string requestBody, bool isStreaming)
|
private async Task<HttpResponseMessage> SendAzureRequestAsync(string apiKey, string requestBody, bool isStreaming)
|
||||||
{
|
{
|
||||||
var requestMessage = new HttpRequestMessage(HttpMethod.Post, AzureAiServiceUrl);
|
using (var client = new HttpClient())
|
||||||
requestMessage.Headers.Add("api-key", apiKey);
|
{
|
||||||
requestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
|
var requestMessage = new HttpRequestMessage(HttpMethod.Post, AzureAiServiceUrl);
|
||||||
|
requestMessage.Headers.Add("api-key", apiKey);
|
||||||
|
requestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
var completionOption = isStreaming
|
var completionOption = isStreaming
|
||||||
? HttpCompletionOption.ResponseHeadersRead
|
? HttpCompletionOption.ResponseHeadersRead
|
||||||
: HttpCompletionOption.ResponseContentRead;
|
: HttpCompletionOption.ResponseContentRead;
|
||||||
|
|
||||||
return await HttpClient.SendAsync(requestMessage, completionOption);
|
return await client.SendAsync(requestMessage, completionOption);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ForwardResponseAsync(HttpListenerContext context, HttpResponseMessage response, bool isStreaming)
|
private async Task ForwardResponseAsync(HttpListenerContext context, HttpResponseMessage response, bool isStreaming)
|
||||||
|
@ -88,7 +90,7 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
context.Response.Headers.Add("Transfer-Encoding", "chunked");
|
context.Response.Headers.Add("Transfer-Encoding", "chunked");
|
||||||
context.Response.Headers.Add("Cache-Control", "no-cache");
|
context.Response.Headers.Add("Cache-Control", "no-cache");
|
||||||
|
|
||||||
byte[] buffer = new byte[StreamingBufferSize];
|
byte[] buffer = new byte[ChunkSize];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
|
while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user