-
Notifications
You must be signed in to change notification settings - Fork 25
Add semantic tool filtering Policy to WSO2 API Platform #104
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
Merged
RakhithaRR
merged 12 commits into
wso2:main
from
NaveenSandaruwan:addSemanticToolFiltering
Mar 27, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0c2cec4
Add semantic tool filtering policy, add docs
NaveenSandaruwan 6dff7c4
change parameter names Limit -> limit and Threshold -> threshold
NaveenSandaruwan f12c109
changed go module repository name
NaveenSandaruwan 9025ca9
Update embeddingModel parameter description for conditional requireme…
NaveenSandaruwan a2719ff
Add validation patterns and minimum length for JSONPath and embedding…
NaveenSandaruwan d7fb496
Refactor error messages in semantic tool filtering to remove wrapping
NaveenSandaruwan 4555824
Normalize parameter names in error messages for consistency
NaveenSandaruwan 7cd8f58
Refactor error handling in buildErrorResponse to log warnings without…
NaveenSandaruwan 319f788
Add advanced parameter flag to semantic tool filtering properties for…
NaveenSandaruwan 1159583
change semantic tool filtering policy adhere to new interface.
NaveenSandaruwan 95e7a21
add methods as v1 alpha compatible
NaveenSandaruwan 6e721a7
add v1 alpha 2 compatible methods.
NaveenSandaruwan 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
178 changes: 178 additions & 0 deletions
178
docs/semantic-tool-filtering/v0.1/docs/semantic-tool-filtering.md
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,178 @@ | ||
| --- | ||
| title: "Overview" | ||
| --- | ||
|
|
||
| # Semantic Tool Filtering policy | ||
|
|
||
| ## Overview | ||
| The **Semantic Tool Filtering** policy dynamically filters the tools provided within an API request based on their semantic relevance to the user query. This policy extracts both the query and the tool definitions from the incoming payload, generates embeddings for the query, and performs a similarity search against the provided tools. It then replaces the original tools array with a filtered subset, optimizing the request before it reaches the LLM. | ||
|
|
||
| This policy helps reduce token consumption and improve LLM response quality by sending only the most relevant tools for each request. | ||
|
|
||
| ## Features | ||
| - **Semantic similarity-based filtering** of tools using embedding vectors. | ||
| - **Two selection modes**: "By Rank" (top-K) and "By Threshold". | ||
| - **Flexible Format Support**: Supports both JSON and text-based tool/query extraction. | ||
| - **Embedding cache** with LRU eviction to minimize redundant API calls. | ||
| - **Configurable JSONPath expressions** for payload extraction. | ||
| - **Mixed mode support** (extract query from JSON and tools from text, or vice versa). | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### User Parameters (API Definition) | ||
|
|
||
| | Parameter | Type | Required | Default | Description | | ||
| |-----------|------|----------|---------|-------------| | ||
| | selectionMode | string | Yes | `By Rank` | Method used to filter tools: `By Rank` (selects top-K) or `By Threshold` (selects based on score). | | ||
| | limit | integer | No | `5` | The number of most relevant tools to include (used if selectionMode is `By Rank`). | | ||
| | threshold | number | No | `0.7` | Similarity threshold for filtering (0.0 to 1.0). Only tools with a score above this value are included (used if selectionMode is `By Threshold`). | | ||
| | queryJSONPath | string | No | `$.messages[-1].content` | JSONPath expression to extract the user's query from the request body. | | ||
| | toolsJSONPath | string | No | `$.tools` | JSONPath expression to extract the tools array from the request body (used when `toolsIsJson` is true). | | ||
| | userQueryIsJson | boolean | No | `true` | Specifies format of user query. `true`: use `queryJSONPath`. `false`: extract from text using `<userq>` tags. | | ||
| | toolsIsJson | boolean | No | `true` | Specifies format of tools definition. `true`: use `toolsJSONPath`. `false`: extract from text using `<toolname>`/`<tooldescription>` tags. | | ||
|
|
||
| ### System Parameters (From config.toml) | ||
|
|
||
| These parameters are configured in the gateway's `config.toml` to set up the embedding provider. | ||
|
|
||
| | Parameter | Type | Required | Default | Description | | ||
| |-----------|------|----------|---------|-------------| | ||
| | embeddingProvider | string | Yes | - | Embedding provider: `OPENAI`, `MISTRAL`, or `AZURE_OPENAI`. | | ||
| | embeddingEndpoint | string | Yes | - | Endpoint URL for the embedding service. | | ||
| | embeddingModel | string | Conditional | - | Model name (e.g., `text-embedding-3-small` or `mistral-embed`). Required for `OPENAI` and `MISTRAL`; optional for `AZURE_OPENAI` (deployment name is derived from the endpoint). | | ||
| | apiKey | string | Yes | - | API key for the embedding service. | | ||
|
|
||
| #### Sample System Configuration | ||
|
|
||
| Add the following configuration section under the root level in your `config.toml` file: | ||
|
|
||
| ```toml | ||
| embedding_provider = "MISTRAL" # Supported: MISTRAL, OPENAI, AZURE_OPENAI | ||
| embedding_provider_endpoint = "https://api.mistral.ai/v1/embeddings" | ||
| embedding_provider_model = "mistral-embed" | ||
| embedding_provider_dimension = 1024 | ||
NaveenSandaruwan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| embedding_provider_api_key = "" | ||
| ``` | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### build.yaml | ||
|
|
||
| Add the following entry to the `policies` section in `/gateway/build.yaml`: | ||
|
|
||
| ```yaml | ||
| - name: semantic-tool-filtering | ||
| gomodule: github.com/wso2/gateway-controllers/policies/semantic-tool-filtering@v0 | ||
| ``` | ||
|
|
||
| ## Reference Scenarios | ||
|
|
||
| ### Scenario 1: Filtering Tools by Rank (JSON Format) | ||
|
|
||
| This scenario demonstrates filtering tools to select the top 3 most relevant ones based on a user query in a JSON payload. | ||
|
|
||
| **Configuration:** | ||
|
|
||
| ```yaml | ||
| type: http | ||
| policies: | ||
| - policy: | ||
| name: semantic-tool-filtering | ||
| parameters: | ||
| selectionMode: "By Rank" | ||
| limit: 3 | ||
| queryJSONPath: "$.contents[0].parts[0].text" | ||
| toolsJSONPath: "$.tools[0].function_declarations" | ||
| userQueryIsJson: true | ||
| toolsIsJson: true | ||
| ``` | ||
|
|
||
| **Request:** | ||
|
|
||
| ```json | ||
| { | ||
| "contents": [ | ||
| { | ||
| "role": "user", | ||
| "parts": [ | ||
| { | ||
| "text": "Get weather forecast. what are the tools you have?" | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "tools": [ | ||
| { | ||
| "function_declarations": [ | ||
| { | ||
| "name": "get_weather", | ||
| "description": "Get current weather and 7-day forecast for a location.", | ||
| "parameters": { "type": "OBJECT", "properties": { "location": { "type": "string" } }, "required": ["location"] } | ||
| }, | ||
| { | ||
| "name": "book_venue", | ||
| "description": "Reserve a conference room or meeting space.", | ||
| "parameters": { "type": "OBJECT", "properties": { "location": { "type": "string" } }, "required": ["location"] } | ||
| }, | ||
| { | ||
| "name": "send_email", | ||
| "description": "Send an email to a specific recipient.", | ||
| "parameters": { "type": "OBJECT", "properties": { "recipient": { "type": "string" } }, "required": ["recipient"] } | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| The policy will interpret the request, calculate embeddings, and filter the `tools` array to include only the top 3 matches (e.g., `get_weather`, `book_venue`, `send_email`). | ||
|
|
||
| ### Scenario 2: Filtering Tools by Threshold | ||
|
|
||
| In this scenario, only tools with a semantic similarity score of 0.7 or higher are included. | ||
|
|
||
| **Configuration:** | ||
|
|
||
| ```yaml | ||
| type: http | ||
| policies: | ||
| - policy: | ||
| name: semantic-tool-filtering | ||
| parameters: | ||
| selectionMode: "By Threshold" | ||
| threshold: 0.7 | ||
| ``` | ||
|
|
||
| ### Scenario 3: Text Format (XML-like Tags) | ||
|
|
||
| This scenario handles cases where the user query and tool definitions are embedded in a text payload using custom tags. | ||
|
|
||
| **Configuration:** | ||
|
|
||
| ```yaml | ||
| type: http | ||
| policies: | ||
| - policy: | ||
| name: semantic-tool-filtering | ||
| parameters: | ||
| selectionMode: "By Rank" | ||
| limit: 3 | ||
| userQueryIsJson: false | ||
| toolsIsJson: false | ||
| ``` | ||
|
|
||
| **Request Body:** | ||
|
|
||
| ```json | ||
| { | ||
| "contents": [ | ||
| { | ||
| "parts": [ | ||
| { | ||
| "text": "You are a logistics assistant with access to the following tools:\n\n<toolname>get_weather</toolname><tooldescription>Get current weather and 7-day forecast for a location</tooldescription>\n<toolname>book_venue</toolname><tooldescription>Reserve meeting spaces</tooldescription>\n\n<userq>I'm planning a corporate retreat in Denver next weekend. Check the weather.</userq>" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| The policy extracts `<userq>` as the query and `<toolname>`/`<tooldescription>` as tools, then performs filtering. After the filtering process, the tags are removed. | ||
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,11 @@ | ||
| { | ||
| "name": "semantic-tool-filtering", | ||
| "displayName": "Semantic Tool Filtering", | ||
| "version": "0.1", | ||
| "provider": "WSO2", | ||
| "categories": [ | ||
| "Guardrails", | ||
| "AI" | ||
| ], | ||
| "description": "Dynamically filters the tools provided within an API request based on their semantic relevance to the user query. This policy extracts both the query and the tool definitions from the incoming payload, generates embeddings for the query, and performs a similarity search against the provided tools. It then replaces the original tools array with a filtered subset, optimizing the request before it reaches the LLM." | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.