Skip to content

Commit

Permalink
Merge pull request #851 from Portkey-AI/chore/shorthand-guardrails-up…
Browse files Browse the repository at this point in the history
…dates

Chore: shorthand guardrails updates
  • Loading branch information
VisargD authored Dec 27, 2024
2 parents 66e9213 + 45d0f42 commit 24ead1b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
26 changes: 18 additions & 8 deletions src/handlers/handlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,26 @@ export function convertGuardrailsShorthand(guardrailsArr: any, type: string) {
};

// 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];
}
[
'deny',
'on_fail',
'on_success',
'async',
'id',
'type',
'guardrail_version_id',
].forEach((key) => {
if (guardrails.hasOwnProperty(key)) {
hooksObject[key] = guardrails[key];
delete guardrails[key];
}
);
});

hooksObject = convertKeysToCamelCase(hooksObject);

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

Expand Down Expand Up @@ -983,6 +991,8 @@ export function constructConfigFromRequestHeaders(
'checks',
'vertex_service_account_json',
'conditions',
'input_guardrails',
'output_guardrails',
]) as any;
}

Expand Down
28 changes: 25 additions & 3 deletions src/middlewares/requestValidator/schema/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,28 @@ export const configSchema: any = z
// Google Vertex AI specific
vertex_project_id: z.string().optional(),
vertex_region: z.string().optional(),
after_request_hooks: z.any().optional(),
before_request_hooks: z.any().optional(),
after_request_hooks: z
.array(z.object({}).catchall(z.any())) // Allows any object structure
.optional(),
before_request_hooks: z
.array(z.object({}).catchall(z.any())) // Allows any object structure
.optional(),
input_guardrails: z
.union([
z.array(z.string()),
z.array(
z.object({}).catchall(z.any()) // Allows any object structure
),
])
.optional(),
output_guardrails: z
.union([
z.array(z.string()),
z.array(
z.object({}).catchall(z.any()) // Allows any object structure
),
])
.optional(),
vertex_service_account_json: z.object({}).catchall(z.string()).optional(),
// OpenAI specific
openai_project: z.string().optional(),
Expand Down Expand Up @@ -115,7 +135,9 @@ export const configSchema: any = z
hasAWSDetails ||
isVertexAIProvider ||
value.after_request_hooks ||
value.before_request_hooks
value.before_request_hooks ||
value.input_guardrails ||
value.output_guardrails
);
},
{
Expand Down

0 comments on commit 24ead1b

Please sign in to comment.