下面的代码中使用 ConfigureHttpClientDefaults 遇到问题
var builder = new KernelMemoryBuilder();
// builder.Services.ConfigureHttpClientDefaults(b =>
// b.ConfigurePrimaryHttpMessageHandler(() => new LoggingHandler()));
var memory = builder
.WithOpenAIDefaults(OPENAI_API_KEY)
.Build<MemoryServerless>();
await memory.ImportDocumentAsync("sample-KM-Readme.pdf", documentId: "doc001");
如果注释 ConfigureHttpClientDefaults 部分的代码,运行时控制台可以正常输出
info: Microsoft.KernelMemory.Handlers.TextExtractionHandler[0]
Handler 'extract' ready
info: Microsoft.KernelMemory.Handlers.TextPartitioningHandler[0]
Handler 'partition' ready
info: Microsoft.KernelMemory.Handlers.SummarizationHandler[0]
Handler 'summarize' ready
info: Microsoft.KernelMemory.Handlers.GenerateEmbeddingsHandler[0]
Handler 'gen_embeddings' ready, 1 embedding generators
info: Microsoft.KernelMemory.Handlers.SaveRecordsHandler[0]
Handler save_records ready, 1 vector storages
info: Microsoft.KernelMemory.Handlers.DeleteDocumentHandler[0]
Handler 'private_delete_document' ready
info: Microsoft.KernelMemory.Handlers.DeleteIndexHandler[0]
Handler 'private_delete_index' ready
info: Microsoft.KernelMemory.Handlers.DeleteGeneratedFilesHandler[0]
Handler 'delete_generated_files' ready
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
Queueing upload of 1 files for further processing [request doc001]
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
File uploaded: sample-KM-Readme.pdf, 174013 bytes
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
Handler 'extract' processed pipeline 'default/doc001' successfully
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
Handler 'partition' processed pipeline 'default/doc001' successfully
如果取消注释,运行时控制台无任何输出
LoggingHandler 的实现代码
public class LoggingHandler() : DelegatingHandler(new HttpClientHandler())
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine(await request.Content!.ReadAsStringAsync());
var response = await base.SendAsync(request, cancellationToken);
Console.WriteLine(await response.Content!.ReadAsStringAsync());
return response;
}
}
找到了一个变通的解决方法
IServiceCollection services = new ServiceCollection();
services.ConfigureHttpClientDefaults(b =>
b.ConfigurePrimaryHttpMessageHandler(() => new LoggingHandler()));
IServiceProvider sp = services.BuildServiceProvider();
var httpClient = sp.GetRequiredService<IHttpClientFactory>().CreateClient();
var builder = new KernelMemoryBuilder();
var memory = builder
.WithOpenAIDefaults(OPENAI_API_KEY, httpClient: httpClient)
.Build<MemoryServerless>();