iangithub / devllmapp

極速ChatGPT開發者兵器指南一書範例源始碼
MIT License
24 stars 10 forks source link

關於CH5-6 Demo1 自訂不同的 KernelClient 進行 DI 注入的範例,在第二次呼叫API之後會拋出例外的問題 #5

Closed JFMio07 closed 3 months ago

JFMio07 commented 4 months ago

老師您好, 在CH5-6 Demo1 自訂不同的 KernelClient 進行 DI 注入 的範例中, api/Chat3/datetime 及 api/Chat3/summarize 只有第一次呼叫時能夠正常執行, 第二次(含)之後,都會拋出例外, 例外訊息如下:

api/Chat3/datetime
System.ArgumentException: An item with the same key has already been added. Key: DateTimeInformation
----
api/Chat3/summarize
System.ArgumentException: An item with the same key has already been added. Key: SummarizePlugin

拋出例外的地方如下, DateTimeKernelClient image

SummarizeKernelClient image

推測是第二次執行時出現重覆註冊導致產生例外 再麻煩老師解惑,感謝您。

iangithub commented 3 months ago

請將Kernel及KernelClient註冊為Scoped層級

builder.Services.AddScoped(sp => { var builder = Kernel.CreateBuilder(); builder.Services.AddAzureOpenAIChatCompletion(deploymentName: deploy_Name, endpoint: aoai_Endpoint, apiKey: api_Key); return builder.Build(); }); builder.Services.AddScoped(); builder.Services.AddScoped();