-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Python: docs: add Python filters README with samples guide #13885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+81
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Filtering Samples | ||
|
|
||
| This directory contains samples demonstrating the Python filter system in Semantic Kernel. | ||
|
|
||
| Filters allow you to intercept and modify pipeline execution at specific points. The Python SDK supports three filter types. | ||
|
|
||
| ## Filter Types | ||
|
|
||
| | Filter | Decorator | Purpose | | ||
| |--------|-----------|---------| | ||
| | **Prompt Rendering** | `@kernel.filter(FilterTypes.PROMPT_RENDERING)` | Intercept before/after prompt is rendered | | ||
| | **Function Invocation** | `@kernel.filter(FilterTypes.FUNCTION_INVOCATION)` | Intercept before/after any function call | | ||
| | **Auto Function Invoke** | `@kernel.filter(FilterTypes.AUTO_FUNCTION_INVOCATION)` | Control automatic tool calls | | ||
|
|
||
| ## Samples | ||
|
|
||
| ### [prompt_filters.py](./prompt_filters.py) | ||
|
|
||
| Basic prompt rendering filter. Demonstrates how to inspect and modify prompts before they're sent to the model. | ||
|
|
||
| ### [function_invocation_filters.py](./function_invocation_filters.py) | ||
|
|
||
| Function invocation filter with logging and exception handling. Shows both `kernel.add_filter()` and `@kernel.filter()` decorator registration. | ||
|
|
||
| ### [function_invocation_filters_stream.py](./function_invocation_filters_stream.py) | ||
|
|
||
| Same as above but for **streaming** responses. Use this when working with streaming chat completions. | ||
|
|
||
| ### [auto_function_invoke_filters.py](./auto_function_invoke_filters.py) | ||
|
|
||
| Controls which automatic tool calls are allowed. Demonstrates `context.terminate = True` to skip specific function calls, and `FunctionResultContent` handling. | ||
|
|
||
| ### [retry_with_filters.py](./retry_with_filters.py) | ||
|
|
||
| Implements automatic retry logic using function invocation filters. Retries on failure with a different model. | ||
|
|
||
| ### [retry_with_different_model.py](./retry_with_different_model.py) | ||
|
|
||
| Similar retry pattern but specifically switches to a fallback model on failure. | ||
|
|
||
| ## Registration | ||
|
|
||
| There are two ways to register a filter: | ||
|
|
||
| ```python | ||
| # Method 1: Decorator | ||
| @kernel.filter(filter_type=FilterTypes.FUNCTION_INVOCATION) | ||
| async def my_filter(context, next): | ||
| await next(context) | ||
|
|
||
| # Method 2: Add function | ||
| kernel.add_filter("function_invocation", my_filter) | ||
| ``` | ||
|
|
||
| Both are equivalent. Use whichever fits your code style. | ||
|
|
||
| ## Filter Signature | ||
|
|
||
| All filters follow the same signature: | ||
|
|
||
| ```python | ||
| async def filter_name(context: <ContextType>, next: Callable) -> None: | ||
| # Code before next filter/function runs | ||
| await next(context) | ||
| # Code after next filter/function runs | ||
| ``` | ||
|
|
||
| The `next` callable passes control to the next filter in the chain, then to the actual function. You can skip execution by not calling `await next(context)`. | ||
|
|
||
| ## Terminating Auto Function Calls | ||
|
|
||
| To prevent an auto-invoked function from executing: | ||
|
|
||
| ```python | ||
| @kernel.filter(FilterTypes.AUTO_FUNCTION_INVOCATION) | ||
| async def selective_filter(context: AutoFunctionInvocationContext, next): | ||
| if context.function.name == "dangerous_function": | ||
| context.terminate = True # Skip this function call | ||
| return | ||
| await next(context) | ||
| ``` |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution. I'm a little hesitant to introduce another README when we have an overarching README for all samples. Additionally, this type of information should be live in our docs page, which I see exists here: https://learn.microsoft.com/en-us/semantic-kernel/concepts/enterprise-readiness/filters?pivots=programming-language-python. Are there gaps in our docs page?