include checked guardrails in annotation extra metadata

This commit is contained in:
Luca Beurer-Kellner
2025-04-02 18:01:44 +02:00
parent 74ac597e9b
commit 5afb142ab2
4 changed files with 24 additions and 4 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ openai_client = OpenAI(
"Invariant-Authorization": "Bearer " + os.getenv("INVARIANT_API_KEY"),
"Invariant-Guardrails": guardrails,
},
base_url="http://localhost:8000/api/v1/gateway/non-streaming/openai",
base_url="http://localhost:9999/api/v1/gateway/non-streaming/openai",
)
response = openai_client.chat.completions.create(
+1 -1
View File
@@ -33,7 +33,7 @@ class GatewayConfig:
"""Common configurations for the Gateway Server."""
def __init__(self):
self.guardrails = self._load_guardrails_from_file()
self.guardrails_from_file = self._load_guardrails_from_file()
def _load_guardrails_from_file(self) -> str:
"""
+12
View File
@@ -51,6 +51,18 @@ def create_annotations_from_guardrails_errors(
extra_metadata={
"source": "guardrails-error",
"guardrail-action": action,
# if included in error, also include the source of the guardrail
**(
{"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")
else {}
),
},
)
)
+10 -2
View File
@@ -368,8 +368,16 @@ async def check_guardrails(
guardrails_result = result.json()
aggregated_errors = {"errors": []}
for res in guardrails_result.get("result", []):
aggregated_errors["errors"].extend(res.get("errors", []))
for res, guardrail in zip(guardrails_result.get("result", []), guardrails):
for error in res.get("errors", []):
# aggregated_errors["errors"].extend(res.get("errors", []))
aggregated_errors["errors"].append(
{
**error,
"guardrail_content": guardrail.content,
"guardrail_action": guardrail.action,
}
)
# check for any error_message
if error_message := res.get("error_message"):