-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #568 from guardrails-ai/lcel-support
Initial LCEL Support
- Loading branch information
Showing
13 changed files
with
216 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
- "ValidationResult" | ||
- "PassResult" | ||
- "FailResult" | ||
- "ValidatorError" | ||
- "ValidationError" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
from guardrails.validator_base import ValidatorError | ||
# Never actually used in any validators so the description is misleading. | ||
# The naming is confusing so we're updating it. | ||
class ValidatorError(Exception): | ||
""" | ||
deprecated: 0.3.3 | ||
Use :class:`ValidationError` instead. | ||
__all__ = ["ValidatorError"] | ||
Base class for all validator errors. | ||
""" | ||
|
||
|
||
# Open to naming this something more generic like GuardrailsError or something, | ||
# let's just decide in this PR | ||
class ValidationError(Exception): | ||
"""Top level validation error.""" | ||
|
||
|
||
__all__ = ["ValidatorError", "ValidationError"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from guardrails.functional.chain.guard import Guard | ||
|
||
__all__ = ["Guard"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
from copy import deepcopy | ||
from typing import Dict, Optional, TypeVar, cast | ||
|
||
from langchain_core.messages import BaseMessage | ||
from langchain_core.runnables import Runnable, RunnableConfig | ||
|
||
from guardrails.errors import ValidationError | ||
from guardrails.functional.guard import Guard as FGuard | ||
|
||
T = TypeVar("T", str, BaseMessage) | ||
|
||
|
||
class Guard(FGuard, Runnable): | ||
def invoke(self, input: T, config: Optional[RunnableConfig] = None) -> T: | ||
output = BaseMessage(content="", type="") | ||
str_input = None | ||
input_is_chat_message = False | ||
if isinstance(input, BaseMessage): | ||
input_is_chat_message = True | ||
str_input = str(input.content) | ||
output = deepcopy(input) | ||
else: | ||
str_input = str(input) | ||
|
||
response = self.validate(str_input) | ||
|
||
validated_output = response.validated_output | ||
if not validated_output: | ||
raise ValidationError( | ||
( | ||
"The response from the LLM failed validation!" | ||
"See `guard.history` for more details." | ||
) | ||
) | ||
|
||
if isinstance(validated_output, Dict): | ||
validated_output = json.dumps(validated_output) | ||
|
||
if input_is_chat_message: | ||
output.content = validated_output | ||
return cast(T, output) | ||
return cast(T, validated_output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.