@@ -46,17 +46,17 @@ def __exit__(self, exc_type, exc_value, traceback):
46
46
class GuardrailIO (BaseModel ):
47
47
"""The data structure for the guardrail inputs and outputs.
48
48
49
- This is designed to be the standard data object passing through the guardrails .
49
+ This is designed to be the standard data object passing through the GuardrailSequence .
50
50
51
51
The data property is designed to hold user facing data, like prompt and completion/response.
52
- When a guardrail is chained (|) with a LangChain (non-guardrail) ``Runnable`` component,
53
- the data property is used as the ``Input`` of the LangChain component.
52
+ In a GuardrailSequence, when a guardrail is chained (|) with a LangChain (non-guardrail)
53
+ ``Runnable`` component, the data property is used as the ``Input`` of the LangChain component.
54
54
The ``Output`` of the LangChain component will be saved into the data property once it is invoked.
55
55
When processed by a guardrail, the data property is passed to the ``compute()`` method of the guardrail.
56
56
See the ``Guardrail`` base class for more details.
57
57
58
- As the ``GuardrailIO`` object is processed by a guardrail, a new ``RunInfo`` object will be attached ,
59
- so that the metric and parameters can be tracked.
58
+ As the ``GuardrailIO`` object is processed by a guardrail in a GuardrailSequence ,
59
+ a new ``RunInfo`` object will be attached, so that the metric and parameters can be tracked.
60
60
If the ``GuardrailIO`` object is processed by a LangChain (non-guardrail) component,
61
61
the ``RunInfo`` attached could be empty or contains only parameters,
62
62
as LangChain components do not return structured metrics.
@@ -122,10 +122,7 @@ def wrapper(self: "Guardrail", metrics: dict, data: list, *args, **kwargs):
122
122
class Guardrail (BaseTool ):
123
123
"""Base class for guardrails.
124
124
125
- Each Guardrail should be compatible with the LangChain Serializable and Runnable interface.
126
- A new ``RunnableSerializable`` class was added in LangChain v0.0.307.
127
- https://github.com/langchain-ai/langchain/blob/v0.0.307/libs/langchain/langchain/schema/runnable/base.py#L863
128
- The Guardrail class may inherit from ``RunnableSerializable`` in the future.
125
+ Each Guardrail is designed to be a LangChain "tool".
129
126
130
127
To implement a new guardrail:
131
128
1. Add the guardrail config/spec as class properties (similar to the ``name`` property).
@@ -135,19 +132,13 @@ class Guardrail(BaseTool):
135
132
and save into the class attributes, this is handled by ``pydantic``.
136
133
137
134
The ``Input`` to the guardrail could be any data types you would like to support,
138
- the data may be wrapped by a ``GuardrailIO`` object when processed by another guardrail upstream .
139
- The ``GuardrailIO`` object is used by the guardrail internally and track the metrics.
135
+ the data may be wrapped by a ``GuardrailIO`` object when processed by GuardrailSequence .
136
+ The ``GuardrailIO`` object is used by the GuardrailSequence internally and track the metrics.
140
137
Normally your guardrail implementation do not need to handle the ``GuardrailIO`` object.
141
- If the ``Input`` is not a ``GuardrailIO`` object,
142
- the ``preprocess()`` method will do the following to wrap it as a ``GuardrailIO`` object:
143
- 1. For a single string, it will be converted to a list with a single string.
144
- 2. For a LangChain `PromptValue`, it will be converted to a string (to_string) then put it into a list.
145
- 3. Otherwise, the ``Input`` will be saved into the ``data`` property of the ``GuardrailIO`` object.
146
- You may want to override the ``preprocess()`` method if you need additional handling.
147
-
148
- After preprocessing, the data will be passed into the ``compute()`` and ``moderate()`` methods
138
+
139
+ After preprocessing, the input will be passed into the ``compute()`` and ``moderate()`` methods
149
140
in the following ways:
150
- 1. If the data is a dict, it will be passed as ``**kwargs``. You may have ``data`` as keys in the dict.
141
+ 1. If the input is a dict, it will be passed as ``**kwargs``. You may have ``data`` as keys in the dict.
151
142
2. Otherwise, the data will be passed as the ``data`` argument.
152
143
153
144
The ``compute()`` method should compute the metrics and return them as a dictionary.
@@ -160,12 +151,6 @@ class Guardrail(BaseTool):
160
151
you may do all the work in ``compute()`` and save the moderated data in the metrics,
161
152
then in ``moderate()`` simply return (or pop) the moderated data from the metrics.
162
153
163
- In LangChain, the ``Input`` and ``Output`` types of a ``Runnable`` are usually well defined.
164
- In Guardrails, to enable additional data going through the guardrails,
165
- the Input and Output are wrapped as ``GuardrailIO`` objects.
166
- Although not required, you may restrict them by adding the types like
167
- ``class YourGuardrail(Guardrail[Union[GuardrailIO, YourInputType], GuardrailIO):``
168
-
169
154
"""
170
155
171
156
class Config :
0 commit comments