-
Notifications
You must be signed in to change notification settings - Fork 29
feat: guardrails middleware proof of concept #441
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
Conversation
ff0e42a to
5ea422d
Compare
| authors = [{ name = "Andrei Petraru", email = "andrei.petraru@uipath.com" }] | ||
| requires-python = ">=3.11" | ||
| dependencies = [ | ||
| "uipath-langchain>=0.5.1, <0.6.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After I merge this PR, I should modify to "uipath-langchain>=0.5.2"
| message will be generated. | ||
| """ | ||
|
|
||
| severity_level: AgentGuardrailSeverityLevel = AgentGuardrailSeverityLevel.WARNING |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think we can use a enum LogActionSeverity one of logging.ERROR, logging.WARNING or logging.INFO instead of using AgentGuardrailSeverityLevel which si coupled by agent.json ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduced a new enum that resides in langchain project
| GuardrailExecutionStage.PRE, | ||
| GuardrailExecutionStage.PRE_AND_POST, | ||
| ): | ||
| # At PRE stage, evaluate all rules with input_data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we extract this in a function? (same for the if block bellow)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| # 1-parameter rules receive output_data (tool output) | ||
| # 2-parameter rules receive both input_data and output_data | ||
| if middleware_instance.rules: | ||
| result = middleware_instance._evaluate_rules( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think here would be more readable if we pass PRE/POST + input_data && output_data instead of sending same param 2 times, it may be confusing with the current signature when you read for the first time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
91dbe8d to
6208ed2
Compare
samples/joke-agent/graph.py
Outdated
| Entity(PIIDetectionEntity.CREDIT_CARD_NUMBER, 0.5), | ||
| Entity(PIIDetectionEntity.PHONE_NUMBER, 0.5), | ||
| ], | ||
| tool_names=[analyze_joke_syntax], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call this "tools" instead of "tool_names"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good proposal, I had same question. Let's change to "tools", just forget about low-coded agent namings
6208ed2 to
64c6f04
Compare
18a6234 to
02db432
Compare
| UiPathPromptInjectionMiddleware, | ||
| ) | ||
|
|
||
| __all__ = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this file? why would we have backwards incompatibility when we haven't published this version at all until now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, it can be removed
9cd9ee1 to
bd3aa37
Compare
bd3aa37 to
c7a5061
Compare
Input Modification Support for Guardrail Actions
Summary
This PR implements the ability for guardrail actions to modify input/output data before execution continues, enabling sanitization and filtering capabilities beyond just logging or blocking.
Key Changes
1. Enhanced GuardrailAction Interface
handle_validation_result()to returnstr | dict[str, Any] | Noneinstead ofNoneLogAction,BlockAction) returnNone2. Helper Functions for Data Modification
_create_modified_tool_request(): Creates newToolCallRequestwith modified tool arguments_create_modified_tool_result(): Creates newToolMessage/Commandwith modified tool output3. Middleware Updates
All guardrail middleware classes now support input/output modification:
4. Custom FilterAction Example
CustomFilterActioninsamples/joke-agent/middleware.pydemonstrating how developers can implement custom actions that filter/modify data***Use Cases
Example
When the guardrail detects "donkey" in the tool input, it automatically filters it to "***" before the tool receives the input.
Testing
LogActionandBlockAction