Availability
| Edition | Deployment Type |
|---|---|
| Community & Enterprise | Self-Managed, Hybrid |
Use cases
- Prompt Validation with Filters: Ensures that only compliant and secure prompts are sent to LLMs. For example, blocking a prompt with sensitive data that should not be processed by an unapproved vendor.
- Data Preprocessing with Middleware: Prepares data from tools or external sources for safe interaction with LLMs by modifying or anonymizing content. For example, removing sensitive ticket details from a JIRA query before sending to an LLM.
- Organizational Security: Both filters and middleware ensure sensitive information is protected and handled in line with organizational governance policies.
- Enhanced Tool Interactions: Middleware supports tools by transforming their outputs, enabling richer and safer LLM interactions.
Details About Filters
The Filters List View allows administrators to manage filters and middleware applied to prompts or data sent to Large Language Models (LLMs) via the AI Gateway or Chat Rooms. Filters and middleware ensure data governance, compliance, and security by processing or controlling the flow of information.Filters: Unified Blocking and Modification
Filters in Midsommar provide comprehensive request/response processing with both blocking and modification capabilities:-
Blocking Filters:
- Purpose: Governance controls that deny requests based on content analysis.
- Behavior: Analyze message content, metadata, and context. Block requests that violate policies or contain restricted content.
- Example: Block prompts containing PII, sensitive keywords, or unauthorized patterns.
-
Modification Filters:
- Purpose: Transform message content before it reaches the LLM or after tool responses.
- Behavior: Redact sensitive information (emails, phone numbers, SSNs). Enhance system prompts with safety instructions. Normalize or transform content across vendors.
- Example: Automatically redact PII while allowing the request to proceed.
-
Combined Approach:
- Filters can both inspect AND modify in a single script.
- Example: Redact emails from user messages, but block if SSN is detected.
- ✅ Request Filters: Modify/block requests before reaching LLM
- LLM Proxy Requests (before reaching LLM)
- Chat Session Messages (before RAG search and LLM)
- File Content (before RAG indexing)
- Tool Responses (after tool execution)
- ✅ Response Filters: Block LLM responses based on content
- LLM Proxy Responses (REST and streaming)
- Chat Session Responses (regular and streaming)
Response Filters
Response Filters enable administrators to block LLM responses based on content analysis, providing governance controls on what LLMs can say to end users. Key Characteristics:- Block-Only: Response filters can only block responses, not modify them
- Works on LLM Responses: Applied to LLM responses only (not tool responses)
- Streaming Support: Execute per-chunk during streaming with access to accumulated buffer
- Script-Controlled: Filter scripts decide when to evaluate based on buffer length
- Proxy (REST): Executes after response hooks (if any). Full response available in
input.raw_input. If blocked: Error returned to client instead of response. - Proxy (Streaming): Executes on every chunk. Access to both
raw_input(current chunk) andcurrent_buffer(accumulated text). If blocked: Streaming stops, error sent to client. - Chat (Non-Streaming): Executes before adding to chat history. If blocked: Error published to queue instead of response.
- Chat (Streaming): Executes on every chunk before publishing to queue. If blocked: Error published, streaming callback returns error to stop further chunks.
Tool Response Filters
Filters can also be applied to tool responses (e.g., API calls, database queries). Tool responses are plain strings, not JSON-structured messages, so they require simpler handling.Filter Execution Order in Chat Sessions
When a user sends a message in a chat session, filters are executed at multiple points in the pipeline:-
User Message Filters (Before RAG)
- When: After preprocessing, before RAG vector search
- Purpose: Redact PII from user messages before they’re used for vector similarity search
- Context:
input.messages[0].role == "user" - Impact: The filtered/modified message is used for RAG vector similarity search, subsequent LLM processing, and chat history storage.
-
File Content Filters (Before RAG)
- When: When files are attached to messages
- Purpose: Filter sensitive content from uploaded files before indexing
- Context:
input.context.file_refcontains the file reference
-
Tool Response Filters (After Tool Execution)
- When: After a tool returns data, before sending to LLM
- Purpose: Filter sensitive data from external API responses
- Context:
input.messages[0].role == "tool",input.context.tool_nameavailable
Examples of Filters and Middleware
-
Filters:
- PII Detector: A regex-based filter that blocks prompts containing sensitive PII.
- JIRA Field Analysis: Ensures no PII is included in data retrieved from JIRA fields before passing to the LLM.
-
Middleware:
- Anonymize PII (LLM): Uses an LLM to anonymize sensitive data before sending it downstream.
- NER Service Filter: A Named Entity Recognition (NER) microservice that modifies outputs to remove identified entities.
Key Benefits
- Improved Data Governance: Filters and middleware work together to enforce strict controls over data flow, protecting sensitive information.
- Flexibility: Middleware allows for data transformation, enhancing interoperability between tools and LLMs. Filters ensure compliance without altering user-provided prompts.
- Compliance and Security: Prevent unauthorized or sensitive data from reaching unapproved vendors, ensuring regulatory compliance.
Configuration
Modern filters use a unified API that provides rich context and supports both blocking and modification. Filters are written using the Tengo scripting language.New Unified Script API
Input Object:Available Tengo Modules
Filters have access to powerful standard library modules:text - String Operations
json - JSON Operations
fmt - Formatting and Printing
tyk - Extended Capabilities (Enterprise)
Available Helper Functions
Themidsommar module provides helper functions for common message modification tasks:
redact_pattern(input, pattern, replacement):- Redacts a regex pattern from all messages (system, user, assistant)
- Parameters:
input- The input object provided to your scriptpattern- Regular expression pattern (string)replacement- Replacement string
- Returns: Modified payload as string
- Example:
tyk.redact_pattern(input, "\d{3}-\d{2}-\d{4}", "[SSN]")
Message Modification Approaches
Approach 1: Helper Functions (Simple, recommended for pattern-based redaction)Accessing Message Context
Scripts can access rich contextual information:Example Scripts
Example 1: Blocking Filter (PII Detection)
This script blocks requests containing PII patterns:Example 2: Modification Filter (Email Redaction)
This script redacts emails while allowing the request to proceed:Example 3: Advanced Modification (Messages Array)
This script shows complex message modification using the messages array approach:Example 4: Combined Blocking + Modification
This script redacts emails but blocks if SSN is detected:Example 5: Block Harmful Content (Response Filter)
Best Practices
- Always define
output- Scripts must set the output variable - Use
tyk.redact_patternfor LLM messages - Handles vendor differences automatically - Use
textmodule for tool responses - Direct string manipulation - Use messages array for complex LLM modifications - Gives you full control
- Provide clear block messages - Help users understand policy violations
- Test across vendors - OpenAI, Anthropic, and Google AI have different formats
- Check message roles - Different logic for system, user, assistant, and tool messages
- Handle edge cases - Empty arrays, missing fields, etc.
- Consider RAG impact - Filters run before RAG, so redactions affect vector search
Migration from Legacy API
Old API (still supported for backward compatibility):How to Create a Filter
Administrators can create and manage filters from the Filters List View in the AI Studio dashboard.Prerequisites
- Admin access to Tyk AI Studio.
- Understanding of Tengo scripting for custom logic.
Steps to Create a Filter
- Navigate to the Filters section in the AI Studio dashboard.
- Click the + ADD FILTER button in the top-right corner.
- Fill in the filter configuration form:
- Name (Required): Enter a descriptive name for the filter (e.g.,
PII Detector). - Description: Provide a brief summary of the filter’s functionality.
- Is this a Response Filter?: Check this box if the filter should run on LLM responses only. Remember that response filters can only block, not modify, and will interrupt streaming responses if blocked.
- Script (Required): Write or paste your Tengo script. You can use the Load Template option to start with a predefined script.
- Available Namespaces: Select which edge namespaces this configuration should be available to. Leave empty for global availability.
- Name (Required): Enter a descriptive name for the filter (e.g.,
- Test your filter script: Before saving, use the testing panel to verify your script with sample input:
- Enter a Raw Input (e.g.,
Tell me how to bypass security systems). - Specify the LLM provider (e.g.,
openai). - Specify the Model Name (e.g.,
gpt-4). - Toggle Is Response, Is Chat, and Is Chunk (Streaming) as needed.
- Provide any Context (JSON) (e.g.,
{"app_id": 1, "user_id": 5}). - Click Test Script to see the output.
- Enter a Raw Input (e.g.,
- Click Create Filter (or Update Filter) to save the configuration.
TODO: Add product screenshots of the Filter List View and Filter Edit View.