-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Metadata Guardrail #1149
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
vrushankportkey
wants to merge
8
commits into
Portkey-AI:main
from
vrushankportkey:codex/create-default-guardrail-for-request-metadata
Closed
Metadata Guardrail #1149
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2f23382
chore: update model whitelist copy
vrushankportkey 1c474aa
Merge pull request #4 from vrushankportkey/codex/update-model-whiteli…
vrushankportkey 7cd3a85
Add metadata guardrail
vrushankportkey 0fd7342
docs: clarify how metadata header is used
vrushankportkey 498b412
Update plugins/default/metadata.ts
vrushankportkey e5bd432
Update plugins/default/metadata.ts
vrushankportkey 5910ab8
Update plugins/default/metadata.ts
vrushankportkey 2abcfd4
Update plugins/default/default.test.ts
vrushankportkey 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
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
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 |
|---|---|---|
|
|
@@ -595,14 +595,14 @@ | |
| } | ||
| }, | ||
| { | ||
| "name": "Model whitelisting", | ||
| "name": "Allowed Models", | ||
| "id": "modelwhitelist", | ||
| "type": "guardrail", | ||
| "supportedHooks": ["beforeRequestHook"], | ||
| "description": [ | ||
| { | ||
| "type": "subHeading", | ||
| "text": "Check if the model in the request is part of the allowed model list." | ||
| "text": "Blocks any request whose model isn’t on this list." | ||
| } | ||
| ], | ||
| "parameters": { | ||
|
|
@@ -614,7 +614,7 @@ | |
| "description": [ | ||
| { | ||
| "type": "subHeading", | ||
| "text": "Enter the allowed models." | ||
| "text": "gpt-4o, llama-3-70b, mixtral-8x7b" | ||
| } | ||
| ], | ||
| "items": { | ||
|
|
@@ -627,7 +627,7 @@ | |
| "description": [ | ||
| { | ||
| "type": "subHeading", | ||
| "text": "If true, the verdict will be true when model is not in the list" | ||
| "text": "When on, any model in the list is blocked instead of allowed." | ||
| } | ||
| ], | ||
| "default": false | ||
|
|
@@ -706,6 +706,43 @@ | |
| }, | ||
| "required": ["jwksUri", "headerKey"] | ||
| } | ||
| }, | ||
| { | ||
| "name": "Metadata Check", | ||
| "id": "metadata", | ||
| "type": "guardrail", | ||
| "supportedHooks": ["beforeRequestHook"], | ||
| "description": [ | ||
| { | ||
| "type": "subHeading", | ||
| "text": "Verify metadata key/value pairs" | ||
| } | ||
| ], | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "pairs": { | ||
| "type": "json", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey there are other "type":"json" in the code right? |
||
| "label": "Key/Value pairs", | ||
| "description": [ | ||
| { | ||
| "type": "subHeading", | ||
| "text": "e.g. {\"foo\":\"bar\"}" | ||
| } | ||
| ] | ||
| }, | ||
| "operator": { | ||
| "type": "string", | ||
| "enum": ["any", "all", "none"], | ||
| "default": "all" | ||
| }, | ||
| "not": { | ||
| "type": "boolean", | ||
| "default": false | ||
| } | ||
| }, | ||
| "required": ["pairs"] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
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,90 @@ | ||
| import { | ||
| HookEventType, | ||
| PluginContext, | ||
| PluginHandler, | ||
| PluginParameters, | ||
| } from '../types'; | ||
|
|
||
| export const handler: PluginHandler = async ( | ||
| context: PluginContext, | ||
| parameters: PluginParameters, | ||
| _eventType: HookEventType | ||
| ) => { | ||
| let error = null; | ||
| let verdict = false; | ||
| let data: any = null; | ||
|
|
||
| try { | ||
| const pairs = parameters.pairs; | ||
| const operator = parameters.operator || 'all'; | ||
| const not = parameters.not || false; | ||
|
|
||
| if (!pairs) { | ||
| throw new Error('Missing metadata pairs parameter'); | ||
| } | ||
|
|
||
| if (typeof pairs !== 'object' || Array.isArray(pairs)) { | ||
| throw new Error('Metadata pairs must be an object with key-value pairs'); | ||
| } | ||
|
|
||
| if (!context.metadata) { | ||
| data = { | ||
| verdict: false, | ||
| explanation: 'No metadata provided in the request context', | ||
| operator, | ||
| not, | ||
| foundKeys: [], | ||
| missingKeys: Object.keys(pairs) | ||
| }; | ||
| return { error: null, verdict: not, data }; | ||
| } | ||
|
|
||
| const metadata = context.metadata; | ||
| const foundKeys: string[] = []; | ||
| const missingKeys: string[] = []; | ||
|
|
||
| for (const [key, value] of Object.entries(pairs)) { | ||
| if (metadata[key] === value) { | ||
| foundKeys.push(key); | ||
| } else { | ||
| missingKeys.push(key); | ||
| } | ||
| } | ||
|
|
||
| let result = false; | ||
| if (operator === 'any') { | ||
| result = foundKeys.length > 0; | ||
| } else if (operator === 'all') { | ||
| result = missingKeys.length === 0; | ||
| } else if (operator === 'none') { | ||
| result = foundKeys.length === 0; | ||
| } else { | ||
| throw new Error(`Invalid operator: ${operator}. Must be one of: any, all, none`); | ||
| } | ||
|
|
||
| verdict = not ? !result : result; | ||
| data = { | ||
| verdict, | ||
| not, | ||
| operator, | ||
| foundKeys, | ||
| missingKeys, | ||
| explanation: verdict | ||
| ? not | ||
| ? 'Metadata does not match the specified pairs as expected.' | ||
| : 'Metadata matches the specified pairs.' | ||
| : not | ||
| ? 'Metadata matches the specified pairs when it should not.' | ||
| : 'Metadata does not match the specified pairs.', | ||
| }; | ||
| } catch (e: any) { | ||
| error = e; | ||
| data = { | ||
| explanation: `An error occurred while verifying metadata: ${e.message}`, | ||
| operator: parameters.operator || 'all', | ||
| not: parameters.not || false, | ||
| }; | ||
| } | ||
|
|
||
| return { error, verdict, data }; | ||
| }; |
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
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.