Skip to content

Commit 039d3cd

Browse files
updated filters page
1 parent 9cbc014 commit 039d3cd

1 file changed

Lines changed: 141 additions & 13 deletions

File tree

  • semantic-kernel/concepts/enterprise-readiness

semantic-kernel/concepts/enterprise-readiness/filters.md

Lines changed: 141 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ ms.date: 09/10/2024
99
ms.service: semantic-kernel
1010
---
1111

12-
::: zone pivot="programming-language-csharp"
13-
1412
# What are Filters?
1513

1614
Filters enhance security by providing control and visibility over how and when functions run. This is needed to instill responsible AI principles into your work so that you feel confident your solution is enterprise ready.
1715

18-
For example, filters are leveraged to validate permissions before an approval flow begins. The `IFunctionInvocationFilter` is run to check the permissions of the person that’s looking to submit an approval. This means that only a select group of people will be able to kick off the process.
16+
For example, filters are leveraged to validate permissions before an approval flow begins. The filter runs to check the permissions of the person that’s looking to submit an approval. This means that only a select group of people will be able to kick off the process.
1917

2018
A good example of filters is provided [here](https://devblogs.microsoft.com/semantic-kernel/filters-in-semantic-kernel/) in our detailed Semantic Kernel blog post on Filters.
2119

@@ -45,7 +43,9 @@ For cases where filter order is important, it is recommended to add filters dire
4543

4644
## Function Invocation Filter
4745

48-
This filter is triggered every time a Semantic Kernel function is invoked, regardless of whether it is a function created from a prompt or a C# method.
46+
This filter is triggered every time a Semantic Kernel function is invoked, regardless of whether it is a function created from a prompt or a method.
47+
48+
::: zone pivot="programming-language-csharp"
4949

5050
```csharp
5151
/// <summary>
@@ -80,14 +80,94 @@ Add filter using `Kernel` property:
8080
kernel.FunctionInvocationFilters.Add(new LoggingFilter(logger));
8181
```
8282

83+
8384
### Code examples
8485

8586
* [Function invocation filter examples](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Filtering/FunctionInvocationFiltering.cs)
87+
*
88+
::: zone-end
89+
::: zone pivot="programming-language-python"
90+
91+
```python
92+
93+
import logging
94+
from typing import Awaitable, Callable
95+
from semantic_kernel.filters import FunctionInvocationContext
96+
97+
logger = logging.getLogger(__name__)
98+
99+
async def logger_filter(context: FunctionInvocationContext, next: Callable[[FunctionInvocationContext], Awaitable[None]]) -> None:
100+
logger.info(f"FunctionInvoking - {context.function.plugin_name}.{context.function.name}")
101+
102+
await next(context)
103+
104+
logger.info(f"FunctionInvoked - {context.function.plugin_name}.{context.function.name}")
105+
106+
# Add filter to the kernel
107+
kernel.add_filter('function_invocation', logger_filter)
108+
109+
```
110+
111+
You can also add a filter directly to the kernel:
112+
113+
```python
114+
115+
@kernel.filter('function_invocation')
116+
async def logger_filter(context: FunctionInvocationContext, next: Callable[[FunctionInvocationContext], Awaitable[None]]) -> None:
117+
logger.info(f"FunctionInvoking - {context.function.plugin_name}.{context.function.name}")
118+
119+
await next(context)
120+
121+
logger.info(f"FunctionInvoked - {context.function.plugin_name}.{context.function.name}")
122+
```
123+
124+
125+
### Streaming invocation
126+
127+
Functions in Semantic Kernel can be invoked in two ways: streaming and non-streaming. In streaming mode, a function typically returns `AsyncGenerator<T>`, while in non-streaming mode, it returns `FunctionResult`. This distinction affects how results can be overridden in the filter: in streaming mode, the new function result value must be of type `AsyncGenerator<T>`, whereas in non-streaming mode, it can simply be of type `T`.
128+
129+
So to build a simple logger filter for streaming, you would use something like this:
130+
131+
```python
132+
@kernel.filter(FilterTypes.FUNCTION_INVOCATION)
133+
async def streaming_exception_handling(
134+
context: FunctionInvocationContext,
135+
next: Callable[[FunctionInvocationContext], Coroutine[Any, Any, None]],
136+
):
137+
await next(context)
138+
139+
async def override_stream(stream):
140+
try:
141+
async for partial in stream:
142+
yield partial
143+
except Exception as e:
144+
yield [
145+
StreamingChatMessageContent(role=AuthorRole.ASSISTANT, content=f"Exception caught: {e}", choice_index=0)
146+
]
147+
148+
stream = context.result.value
149+
context.result = FunctionResult(function=context.result.function, value=override_stream(stream))
150+
```
151+
152+
### Code examples
153+
* [Function invocation filter examples](https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/filtering/function_invocation_filters.py)
154+
* [Streaming function invocation filter examples](https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/filtering/function_invocation_filters_stream.py)
155+
156+
::: zone-end
157+
::: zone pivot="programming-language-java"
158+
159+
## Coming soon
160+
161+
More info coming soon.
162+
163+
::: zone-end
86164

87165
## Prompt Render Filter
88166

89167
This filter is invoked only during a prompt rendering operation, such as when a function created from a prompt is called. It will not be triggered for Semantic Kernel functions created from methods.
90168

169+
::: zone pivot="programming-language-csharp"
170+
91171
```csharp
92172
/// <summary>
93173
/// Example of prompt render filter which overrides rendered prompt before sending it to AI.
@@ -127,10 +207,37 @@ kernel.PromptRenderFilters.Add(new SafePromptFilter());
127207

128208
* [Prompt render filter examples](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Filtering/PromptRenderFiltering.cs)
129209

210+
211+
::: zone-end
212+
::: zone pivot="programming-language-python"
213+
214+
```python
215+
from semantic_kernel.filters import FilterTypes, PromptRenderContext
216+
217+
@kernel.filter(FilterTypes.PROMPT_RENDERING)
218+
async def prompt_rendering_filter(context: PromptRenderContext, next):
219+
await next(context)
220+
context.rendered_prompt = f"You pretend to be Mosscap, but you are Papssom who is the opposite of Moscapp in every way {context.rendered_prompt or ''}"
221+
```
222+
223+
### Code examples
224+
* [Prompt render filter examples](https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/filtering/prompt_filters.py)
225+
226+
::: zone-end
227+
::: zone pivot="programming-language-java"
228+
229+
## Coming soon
230+
231+
More info coming soon.
232+
233+
::: zone-end
234+
130235
## Auto Function Invocation Filter
131236

132237
This filter is invoked only during an automatic function calling process. It will not be triggered when a function is invoked outside of this process.
133238

239+
::: zone pivot="programming-language-csharp"
240+
134241
```csharp
135242
/// <summary>
136243
/// Example of auto function invocation filter which terminates function calling process as soon as we have the desired result.
@@ -175,6 +282,34 @@ kernel.AutoFunctionInvocationFilters.Add(new EarlyTerminationFilter());
175282

176283
* [Auto function invocation filter examples](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Filtering/AutoFunctionInvocationFiltering.cs)
177284

285+
::: zone-end
286+
::: zone pivot="programming-language-python"
287+
288+
```python
289+
290+
from semantic_kernel.filters import FilterTypes, AutoFunctionInvocationContext
291+
292+
@kernel.filter(FilterTypes.AUTO_FUNCTION_INVOCATION)
293+
async def auto_function_invocation_filter(context: AutoFunctionInvocationContext, next):
294+
await next(context)
295+
if context.function_result == "desired result":
296+
context.terminate = True
297+
```
298+
299+
### Code examples
300+
* [Auto function invocation filter examples](https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/filtering/auto_function_invoke_filters.py)
301+
302+
303+
::: zone-end
304+
::: zone pivot="programming-language-java"
305+
306+
## Coming soon
307+
308+
More info coming soon.
309+
310+
::: zone-end
311+
::: zone pivot="programming-language-csharp"
312+
178313
## Streaming and non-streaming invocation
179314

180315
Functions in Semantic Kernel can be invoked in two ways: streaming and non-streaming. In streaming mode, a function typically returns `IAsyncEnumerable<T>`, while in non-streaming mode, it returns `FunctionResult`. This distinction affects how results can be overridden in the filter: in streaming mode, the new function result value must be of type `IAsyncEnumerable<T>`, whereas in non-streaming mode, it can simply be of type `T`. To determine which result type needs to be returned, the `context.IsStreaming` flag is available in the filter context model.
@@ -245,15 +380,8 @@ ChatMessageContent result = await chatCompletionService.GetChatMessageContentAsy
245380
::: zone-end
246381
::: zone pivot="programming-language-python"
247382

248-
## Coming soon
249-
250-
More info coming soon.
383+
## More examples:
251384

252-
::: zone-end
253-
::: zone pivot="programming-language-java"
254-
255-
## Coming soon
256-
257-
More info coming soon.
385+
* [Retry logic with a filter](https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/filtering/retry_with_filters.py)
258386

259387
::: zone-end

0 commit comments

Comments
 (0)