Repository navigation
Replies: 3 comments 2 replies
|
Based on the current implementation of AddBedrockChatClient, it seems there is no direct way to inject custom middleware at this point. builder.Services.AddKeyedTransient<Kernel>("Kernel", (sp, _) =>
{
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddChatClient(sp =>
{
var bedrockRuntime = new AmazonBedrockRuntimeClient(credentials, clientConfig);
return bedrockRuntime.AsIChatClient();
})
.UseLogging()
.UseOpenTelemetry()
.UseKernelFunctionInvocation()
.UseYourMiddleware();
return kernelBuilder.Build();
});The key point here is to use UseKernelFunctionInvocation to ensure that the middleware responsible for handling automatic plugin invocation in Semantic Kernel is properly configured. |
|
Thanks jooooel for laying out the full setup, and Kazuki Ota (@runceel) for the Both approaches work, and the under-the-hood list is short: Measured on
So if What AddBedrockChatClient registers (source, identical on main and at dotnet-1.61.0)
services.AddKeyedSingleton<IChatClient>(serviceId, (serviceProvider, _) =>
{
...
var builder = runtime
.AsIChatClient(modelId)
.AsBuilder();
if (loggerFactory is not null)
{
builder.UseLogging(loggerFactory);
}
builder.UseOpenTelemetry(loggerFactory, openTelemetrySourceName, openTelemetryConfig);
return builder
.UseKernelFunctionInvocation(loggerFactory)
.Build(serviceProvider);
});There is a single overload, and the only customization points are |
|
Middleware doesn't go on the Kernel, it goes on the IChatClient when you build it. services.AddChatClient(new OpenAIChatClient(apiKey, model))
.Use(client => new MyDelegatingChatClient(client));
services.AddKeyedSingleton<Kernel>("Ker", (sp, key) =>
new Kernel(sp, sp.GetRequiredService<IChatClient>().AsChatCompletionService(sp)));
— |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to figure out how to add middleware to a chat client added to the Kernel. It seems like I need the actual instance of the chat client in order to add the middleware?
This is the documentation for the chat client middleware https://learn.microsoft.com/en-us/dotnet/ai/microsoft-extensions-ai#custom-ichatclient-middleware
This is my setup
This is the middleware:
And this is how I finally use the kernel
At what point should I/can I extend the Bedrock chat client?
All reactions