Draft
Conversation
Owner
|
That is super interesting! Can I ask for an example and a new docs page, too? Thank you for this! |
1 similar comment
Owner
|
That is super interesting! Can I ask for an example and a new docs page, too? Thank you for this! |
Contributor
Author
|
I'm going to play with this in my app for a bit and see if the API I defined works out when using it. The most simple middlware example: final agent = ai.Agent(
"provider:model",
tools: [...],
middleware: [
FunctionToolMiddleware((toolPart, tool, next) async {
print('Logging Middleware - toolPart: ${toolPart.toJson()}');
print('Logging Middleware - Tool: ${tool?.name}');
final result = await next();
print('Logging Middleware - Result: ${result}');
return result;
}),
],
);FunctionToolMiddleware is just a helper class implementation of the abstract middlware class, to inline a middlware in a functional style. This might not always be the cleanest approach in case you have some dependency injection or complicated logic :) therefore the pattern also allows extending the middleware class. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I wanted to be able to have a way to inject some logic into the tool calling at runtime, (my use case was confirming the use of certain tools).
This PR adds the ability to inject Middleware into the agent loop to control the usage of tools.
Middleware can intercept the tool call, pre and post process it.
The implementation allows for chaining multiple middlware(s? :D).