Skip to content

Commit d41cfe9

Browse files
committed
Add option to log guardrail sequence run info.
1 parent 2503a55 commit d41cfe9

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

ads/llm/chain.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
logger = logging.getLogger(__name__)
2525
SPEC_CHAIN_TYPE = "_type"
2626
SPEC_CHAIN = "chain"
27+
LOG_ADS_GUARDRAIL_INFO = "LOG_ADS_GUARDRAIL_INFO"
2728

2829

2930
class GuardrailSequence(RunnableSequence):
@@ -46,6 +47,11 @@ class GuardrailSequence(RunnableSequence):
4647
When this is ``True``, the ``BlockedByGuardrail`` exception from the guardrail will be raised.
4748
"""
4849

50+
log_info: bool = False
51+
"""Indicate whether to print the run info at the end of each invocation.
52+
This option can also be turned on if the environment variable LOG_ADS_GUARDRAIL_INFO is set to "1".
53+
"""
54+
4955
@property
5056
def steps(self) -> List[Runnable[Any, Any]]:
5157
"""Steps in the sequence."""
@@ -158,6 +164,9 @@ def run(self, input: Any, num_generations: int = 1, **kwargs) -> GuardrailIO:
158164
raise ex
159165
obj.data = [ex.message]
160166
obj.info.append(ex.info)
167+
if self.log_info or os.environ.get(LOG_ADS_GUARDRAIL_INFO) == "1":
168+
# LOG_ADS_GUARDRAIL_INFO is set to "1" in score.py by default.
169+
print(obj.dict())
161170
return obj
162171

163172
def _save_to_file(self, chain_dict, filename, overwrite=False):

ads/llm/guardrails/huggingface.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class HuggingFaceEvaluation(Guardrail):
1717

1818
@root_validator(skip_on_failure=True)
1919
def load_model(cls, values):
20+
"""Loads the model from Huggingface."""
2021
if values.get("path"):
2122
path = values["path"]
2223
else:

0 commit comments

Comments
 (0)