首页 新闻 会员 周边 捐助

Kernel Memory 的 ServiceCollectionPool 配置 HttpClient 问题

0
悬赏园豆:30 [待解决问题]

下面的代码中使用 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;
    }
}
dudu的主页 dudu | 高人七级 | 园豆:29642
提问于:2024-03-03 06:58
< >
分享
所有回答(1)
0

找到了一个变通的解决方法

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>();
dudu | 园豆:29642 (高人七级) | 2024-03-03 08:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册