Skip to content

Commit 5dce035

Browse files
authored
Merge pull request #506 from guardrails-ai/index-on-topic
index on topic nb in docs
2 parents 5fbf536 + 7d796e9 commit 5dce035

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

docs/examples/response_is_on_topic.ipynb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414
"This validator checks if a text is related with a topic. Using a list of valid topics (which can include one or many) and optionally a list of invalid topics, it validates that the text's main topic is one of the valid ones. If none of the valid topics are relevant, the topic 'Other' will be considered as the most relevant one and the validator will fail.\n",
1515
"\n",
1616
"The validator supports 3 different variants:\n",
17-
"- Using an ensemble of Zero-Shot classifier + LLM fallback: if the original classification score is less than 0.5, an LLM is used to classify the main topic. This is the default behavior, setting `disable_classifier = False` and `disable_llm = False`.\n",
18-
"- Using just a Zero-Shot classifier to get the main topic (`disable_classifier = False` and `disable_llm = True`).\n",
19-
"- Using just an LLM to classify the main topic (`disable_classifier = True` and `disable_llm = False`).\n",
17+
"\n",
18+
"1. Using an ensemble of Zero-Shot classifier + LLM fallback: if the original classification score is less than 0.5, an LLM is used to classify the main topic. This is the default behavior, setting `disable_classifier = False` and `disable_llm = False`.\n",
19+
"2. Using just a Zero-Shot classifier to get the main topic (`disable_classifier = False` and `disable_llm = True`).\n",
20+
"3. Using just an LLM to classify the main topic (`disable_classifier = True` and `disable_llm = False`).\n",
2021
"\n",
2122
"To use the LLM, you can pass in a name of any OpenAI ChatCompletion model like `gpt-3.5-turbo` or `gpt-4` as the `llm_callable`, or pass in a callable that handles LLM calls. This callable can use any LLM, that you define. For simplicity purposes, we show here a demo of using OpenAI's gpt-3.5-turbo model.\n",
2223
"\n",
2324
"To use the OpenAI API, you have 3 options:\n",
2425
"\n",
25-
"- Set the OPENAI_API_KEY environment variable: os.environ[\"OPENAI_API_KEY\"] = \"<OpenAI_API_KEY>\"\n",
26-
"- Set the OPENAI_API_KEY using openai.api_key=\"<OpenAI_API_KEY>\"\n",
27-
"- Pass the api_key as a parameter to the parse function as done below, in this example"
26+
"1. Set the OPENAI_API_KEY environment variable: os.environ[\"OPENAI_API_KEY\"] = \"<OpenAI_API_KEY>\"\n",
27+
"2. Set the OPENAI_API_KEY using openai.api_key=\"<OpenAI_API_KEY>\"\n",
28+
"3. Pass the api_key as a parameter to the parse function"
2829
]
2930
},
3031
{

guardrails/validators/on_topic.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ def get_topic_zero_shot(
220220
score = result["scores"][0] # type: ignore
221221
return topic, score # type: ignore
222222

223-
def validate(self, value: str, metadata: Dict[str, Any]) -> ValidationResult:
223+
def validate(
224+
self, value: str, metadata: Optional[Dict[str, Any]]
225+
) -> ValidationResult:
224226
valid_topics = set(self._valid_topics)
225227
invalid_topics = set(self._invalid_topics)
226228

@@ -236,7 +238,7 @@ def validate(self, value: str, metadata: Dict[str, Any]) -> ValidationResult:
236238

237239
# Add 'other' to the invalid topics list
238240
if "other" not in invalid_topics:
239-
self._invalid_topics.append("other")
241+
invalid_topics.add("other")
240242

241243
# Combine valid and invalid topics
242244
candidate_topics = valid_topics.union(invalid_topics)

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ nav:
6363
- 'Check whether a value is similar to a set of other values': examples/value_within_distribution.ipynb
6464
- 'Using GuardrailsOutputParser in LlamaIndex': examples/llamaindex-output-parsing.ipynb
6565
- 'Check if a competitor is named': examples/competitors_check.ipynb
66+
- 'Assure that responses are on topic': examples/response_is_on_topic.ipynb
6667
- 'Integrations':
6768
- 'Azure OpenAI': integrations/azure_openai.ipynb
6869
- 'OpenAI Functions': integrations/openai_functions.ipynb

tests/integration_tests/validators/test_on_topic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def test_validate_invalid_topic_cpu_disable_llm(self):
2424
valid_topics=["sports", "politics"],
2525
disable_classifier=False,
2626
disable_llm=True,
27-
model_threshold=0.6,
2827
)
2928
text = "This is an article about music."
3029
expected_result = FailResult(error_message="Most relevant topic is other.")

0 commit comments

Comments
 (0)