Skip to content

Commit

Permalink
Merge pull request #743 from Portkey-AI/feat/gr-config-shorthand
Browse files Browse the repository at this point in the history
Support shorthand format for guardrails in config
  • Loading branch information
VisargD authored Dec 16, 2024
2 parents 036a1c2 + 8c5b5cf commit dea8b87
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/handlers/handlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ export function selectProviderByWeight(providers: Options[]): Options {
throw new Error('No provider selected, please check the weights');
}

export function convertGuardrailsShorthand(guardrailsArr: any, type: string) {
return guardrailsArr.map((guardrails: any) => {
let hooksObject: any = {
type: 'guardrail',
id: `${type}_guardrail_${Math.random().toString(36).substring(2, 5)}`,
};

// if the deny key is present (true or false), add it to hooksObject and remove it from guardrails
['deny', 'on_fail', 'on_success', 'async', 'onFail', 'onSuccess'].forEach(
(key) => {
if (guardrails.hasOwnProperty(key)) {
hooksObject[key] = guardrails[key];
delete guardrails[key];
}
}
);

// Now, add all the checks to the checks array
hooksObject.checks = Object.keys(guardrails).map((key) => ({
id: key,
parameters: guardrails[key],
}));

return hooksObject;
});
}

/**
* Makes a POST request to a provider and returns the response.
* The POST request is constructed using the provider, apiKey, and requestBody parameters.
Expand Down Expand Up @@ -495,6 +522,20 @@ export async function tryTargetsRecursively(
currentTarget.requestTimeout = inheritedConfig.requestTimeout;
}

if (currentTarget.inputGuardrails) {
currentTarget.beforeRequestHooks = [
...(currentTarget.beforeRequestHooks || []),
...convertGuardrailsShorthand(currentTarget.inputGuardrails, 'input'),
];
}

if (currentTarget.outputGuardrails) {
currentTarget.afterRequestHooks = [
...(currentTarget.afterRequestHooks || []),
...convertGuardrailsShorthand(currentTarget.outputGuardrails, 'output'),
];
}

if (currentTarget.afterRequestHooks) {
currentInheritedConfig.afterRequestHooks = [
...currentTarget.afterRequestHooks,
Expand Down

0 comments on commit dea8b87

Please sign in to comment.