mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-08-04 09:48:57 +00:00
Remove TraceHttpClientHandler and update DI setup
Removed the custom TraceHttpClientHandler and switched OpenAIClient initialization to use the default HTTP transport. Cleaned up unused using directives. Updated SqlDatabaseVectorSearch_WebApp.png with a new version.
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
using System.ClientModel;
|
using System.ClientModel;
|
||||||
using System.ClientModel.Primitives;
|
|
||||||
using System.Net.Mime;
|
using System.Net.Mime;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Encodings.Web;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using Microsoft.Agents.AI;
|
using Microsoft.Agents.AI;
|
||||||
@@ -85,8 +82,7 @@ builder.Services.AddChatClient(_ =>
|
|||||||
{
|
{
|
||||||
var chatClient = new OpenAIClient(new ApiKeyCredential(aiSettings.ChatCompletion.ApiKey), new()
|
var chatClient = new OpenAIClient(new ApiKeyCredential(aiSettings.ChatCompletion.ApiKey), new()
|
||||||
{
|
{
|
||||||
Endpoint = new(aiSettings.ChatCompletion.Endpoint),
|
Endpoint = new(aiSettings.ChatCompletion.Endpoint)
|
||||||
Transport = new HttpClientPipelineTransport(new HttpClient(new TraceHttpClientHandler()))
|
|
||||||
}).GetResponsesClient().AsIChatClientWithStoredOutputDisabled(aiSettings.ChatCompletion.Deployment);
|
}).GetResponsesClient().AsIChatClientWithStoredOutputDisabled(aiSettings.ChatCompletion.Deployment);
|
||||||
|
|
||||||
return chatClient;
|
return chatClient;
|
||||||
@@ -332,48 +328,4 @@ static async Task ConfigureDatabaseAsync(IServiceProvider serviceProvider)
|
|||||||
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||||
|
|
||||||
await dbContext.Database.MigrateAsync();
|
await dbContext.Database.MigrateAsync();
|
||||||
}
|
|
||||||
|
|
||||||
public class TraceHttpClientHandler : HttpClientHandler
|
|
||||||
{
|
|
||||||
private static readonly JsonSerializerOptions jsonSerializerOptions = new()
|
|
||||||
{
|
|
||||||
WriteIndented = true,
|
|
||||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
|
||||||
};
|
|
||||||
|
|
||||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var requestString = request.Content is null ? "(no request body)" : await request.Content.ReadAsStringAsync(cancellationToken);
|
|
||||||
|
|
||||||
PrintText($"Raw Request ({request.RequestUri})", ConsoleColor.Green);
|
|
||||||
PrintText(FormatJson(requestString), ConsoleColor.DarkGray);
|
|
||||||
PrintSeparator();
|
|
||||||
|
|
||||||
var response = await base.SendAsync(request, cancellationToken);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
|
|
||||||
static void PrintText(string message, ConsoleColor color)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = color;
|
|
||||||
Console.WriteLine(message);
|
|
||||||
Console.ResetColor();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PrintSeparator() => Console.WriteLine(new string('-', 50));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string FormatJson(string input)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var jsonElement = JsonSerializer.Deserialize<JsonElement>(input);
|
|
||||||
return JsonSerializer.Serialize(jsonElement, jsonSerializerOptions);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 111 KiB |
Reference in New Issue
Block a user