Skip to content

Commit 9e82210

Browse files
committed
Update dosctrings in base.py
1 parent fa29529 commit 9e82210

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

ads/llm/guardrails/base.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ def __exit__(self, exc_type, exc_value, traceback):
4646
class GuardrailIO(BaseModel):
4747
"""The data structure for the guardrail inputs and outputs.
4848
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.
5050
5151
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.
5454
The ``Output`` of the LangChain component will be saved into the data property once it is invoked.
5555
When processed by a guardrail, the data property is passed to the ``compute()`` method of the guardrail.
5656
See the ``Guardrail`` base class for more details.
5757
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.
6060
If the ``GuardrailIO`` object is processed by a LangChain (non-guardrail) component,
6161
the ``RunInfo`` attached could be empty or contains only parameters,
6262
as LangChain components do not return structured metrics.
@@ -122,10 +122,7 @@ def wrapper(self: "Guardrail", metrics: dict, data: list, *args, **kwargs):
122122
class Guardrail(BaseTool):
123123
"""Base class for guardrails.
124124
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".
129126
130127
To implement a new guardrail:
131128
1. Add the guardrail config/spec as class properties (similar to the ``name`` property).
@@ -135,19 +132,13 @@ class Guardrail(BaseTool):
135132
and save into the class attributes, this is handled by ``pydantic``.
136133
137134
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.
140137
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
149140
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.
151142
2. Otherwise, the data will be passed as the ``data`` argument.
152143
153144
The ``compute()`` method should compute the metrics and return them as a dictionary.
@@ -160,12 +151,6 @@ class Guardrail(BaseTool):
160151
you may do all the work in ``compute()`` and save the moderated data in the metrics,
161152
then in ``moderate()`` simply return (or pop) the moderated data from the metrics.
162153
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-
169154
"""
170155

171156
class Config:

0 commit comments

Comments
 (0)