Skip to content
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

Chore: shorthand guardrails updates #851

Merged
merged 4 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading