참고소스 수정본
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
from copy import deepcopy
|
||||
from typing import Any, Dict, Optional, Union, cast
|
||||
import json
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_core.runnables import Runnable, RunnableConfig
|
||||
from guardrails.classes.input_type import InputType
|
||||
from guardrails.classes.output_type import OT
|
||||
|
||||
|
||||
class BaseRunnable(Runnable):
|
||||
name: Union[str, None]
|
||||
|
||||
def invoke(
|
||||
self,
|
||||
input: InputType,
|
||||
config: Optional[RunnableConfig] = None,
|
||||
**kwargs: Any,
|
||||
) -> InputType:
|
||||
return self._call_with_config(
|
||||
self._process_input, input, config, run_type="parser", **kwargs
|
||||
)
|
||||
|
||||
def _process_input(self, input: InputType) -> InputType:
|
||||
str_input = str(input.content) if isinstance(input, BaseMessage) else str(input)
|
||||
|
||||
validated_output = self._validate(str_input)
|
||||
|
||||
if isinstance(validated_output, Dict):
|
||||
validated_output = json.dumps(validated_output)
|
||||
|
||||
if isinstance(input, BaseMessage):
|
||||
output = deepcopy(input)
|
||||
output.content = validated_output
|
||||
return cast(InputType, output)
|
||||
|
||||
return cast(InputType, validated_output)
|
||||
|
||||
def _validate(self, input: str) -> OT:
|
||||
raise NotImplementedError
|
||||
@@ -0,0 +1,25 @@
|
||||
from guardrails.integrations.langchain.base_runnable import BaseRunnable
|
||||
from guardrails.guard import Guard
|
||||
from guardrails.errors import ValidationError
|
||||
from guardrails.classes.output_type import OT
|
||||
from guardrails.classes.validation_outcome import ValidationOutcome
|
||||
|
||||
|
||||
class GuardRunnable(BaseRunnable):
|
||||
guard: Guard
|
||||
|
||||
def __init__(self, guard: Guard):
|
||||
self.name = guard.name
|
||||
self.guard = guard
|
||||
|
||||
def _validate(self, input: str) -> OT:
|
||||
response: ValidationOutcome[OT] = self.guard.validate(input)
|
||||
validated_output = response.validated_output
|
||||
if validated_output is None or response.validation_passed is False:
|
||||
raise ValidationError(
|
||||
(
|
||||
"The response from the LLM failed validation!"
|
||||
"See `guard.history` for more details."
|
||||
)
|
||||
)
|
||||
return validated_output
|
||||
@@ -0,0 +1,22 @@
|
||||
from guardrails.integrations.langchain.base_runnable import BaseRunnable
|
||||
from guardrails.validator_base import FailResult, Validator
|
||||
from guardrails.errors import ValidationError
|
||||
|
||||
|
||||
class ValidatorRunnable(BaseRunnable):
|
||||
validator: Validator
|
||||
|
||||
def __init__(self, validator: Validator):
|
||||
self.name = validator.rail_alias
|
||||
self.validator = validator
|
||||
|
||||
def _validate(self, input: str) -> str:
|
||||
response = self.validator.validate(input, self.validator._metadata)
|
||||
if isinstance(response, FailResult):
|
||||
raise ValidationError(
|
||||
(
|
||||
"The response from the LLM failed validation!"
|
||||
f" {response.error_message}"
|
||||
)
|
||||
)
|
||||
return input
|
||||
Reference in New Issue
Block a user