mirror of
https://github.com/invariantlabs-ai/invariant-gateway.git
synced 2026-07-07 03:17:51 +02:00
update guardrail metadata
This commit is contained in:
@@ -95,16 +95,22 @@ class GatewayConfigManager:
|
||||
|
||||
|
||||
async def GuardrailsInHeader(request: fastapi.Request) -> Optional[GuardrailRuleSet]:
|
||||
"""
|
||||
Extracts Invariant-Guardrails from the request header if provided, and returns a corresponding
|
||||
GuardrailRuleSet. If no guardrails are provided, returns None.
|
||||
"""
|
||||
# if provided in header, use custom guardrailing policy
|
||||
if guardrails := extract_policy_from_headers(request):
|
||||
guardrails = [
|
||||
Guardrail(
|
||||
id="guardrails-from-header",
|
||||
name="guardrails from request header",
|
||||
content=guardrails,
|
||||
action=GuardrailAction.BLOCK,
|
||||
)
|
||||
]
|
||||
|
||||
return GuardrailRuleSet(
|
||||
blocking_guardrails=[
|
||||
Guardrail(
|
||||
id="guardrail-from-header",
|
||||
name="guardrails from request header",
|
||||
content=guardrails,
|
||||
action=GuardrailAction.BLOCK,
|
||||
)
|
||||
],
|
||||
blocking_guardrails=guardrails,
|
||||
logging_guardrails=[],
|
||||
)
|
||||
|
||||
@@ -46,9 +46,8 @@ class RequestContext:
|
||||
if key != "guardrails_from_file"
|
||||
}
|
||||
|
||||
# If no guardrails are configured for the dataset on Explorer,
|
||||
# and the config specifies guardrails_from_file, use that.
|
||||
guardrails = guardrails
|
||||
# If no guardrails are configured and the config specifies
|
||||
# guardrails_from_file, use those instead.
|
||||
if (
|
||||
(
|
||||
not guardrails
|
||||
@@ -60,12 +59,11 @@ class RequestContext:
|
||||
and config
|
||||
and config.guardrails_from_file
|
||||
):
|
||||
# TODO: Support logging guardrails via file.
|
||||
guardrails = GuardrailRuleSet(
|
||||
blocking_guardrails=[
|
||||
Guardrail(
|
||||
id="default",
|
||||
name="default",
|
||||
id="guardrails-from-gateway-config-file",
|
||||
name="guardrails from gateway configuration file",
|
||||
content=config.guardrails_from_file,
|
||||
action=GuardrailAction.BLOCK,
|
||||
)
|
||||
@@ -87,7 +85,7 @@ class RequestContext:
|
||||
f"RequestContext("
|
||||
f"request_json={self.request_json}, "
|
||||
f"dataset_name={self.dataset_name}, "
|
||||
f"invariant_authorization={self.invariant_authorization}, "
|
||||
f"invariant_authorization=inv-*****{self.invariant_authorization[-4:]}, "
|
||||
f"guardrails={self.guardrails}, "
|
||||
f"config={self.config})"
|
||||
)
|
||||
|
||||
@@ -50,16 +50,10 @@ def create_annotations_from_guardrails_errors(
|
||||
address=r,
|
||||
extra_metadata={
|
||||
"source": "guardrails-error",
|
||||
# if included in error, also include the source of the guardrail
|
||||
# if included in error, also include information about guardrail source
|
||||
**(
|
||||
{"guardrail_content": error.get("guardrail_content")}
|
||||
if error.get("guardrail_content")
|
||||
else {}
|
||||
),
|
||||
# same for guardrail-action
|
||||
**(
|
||||
{"guardrail_action": error.get("guardrail_action")}
|
||||
if error.get("guardrail_action")
|
||||
{"guardrail": error.get("guardrail")}
|
||||
if error.get("guardrail")
|
||||
else {}
|
||||
),
|
||||
},
|
||||
|
||||
@@ -370,12 +370,17 @@ async def check_guardrails(
|
||||
aggregated_errors = {"errors": []}
|
||||
for res, guardrail in zip(guardrails_result.get("result", []), guardrails):
|
||||
for error in res.get("errors", []):
|
||||
# aggregated_errors["errors"].extend(res.get("errors", []))
|
||||
# add each error to the aggregated errors but keep track
|
||||
# of which guardrail it belongs to
|
||||
aggregated_errors["errors"].append(
|
||||
{
|
||||
**error,
|
||||
"guardrail_content": guardrail.content,
|
||||
"guardrail_action": guardrail.action,
|
||||
"guardrail": {
|
||||
"id": guardrail.id,
|
||||
"name": guardrail.name,
|
||||
"content": guardrail.content,
|
||||
"action": guardrail.action,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user