Skip to content

Commit a5b70b3

Browse files
committed
added fact checking
1 parent a728773 commit a5b70b3

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,14 @@ consisTent.LabelsValidator(openai_key=OPENAI_KEY).validate(
7575
model_output="What do you call a rabbit that tells jokes? A funny bunny!",
7676
)
7777
```
78+
79+
### facts validation
80+
81+
```python
82+
OPENAI_KEY = "XXXXXXXXXXXXXXX"
83+
84+
consisTent.FactsValidator(openai_key=OPENAI_KEY).validate(
85+
facts=["this car weighs 1000KG"],
86+
model_output="I can lift this car",
87+
)
88+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import List
2+
from langchain import LLMChain, PromptTemplate
3+
from langchain.llms import OpenAI
4+
5+
from ..base_validator import Validator
6+
7+
8+
class FactsValidator(Validator):
9+
def __init__(
10+
self,
11+
openai_key: str,
12+
):
13+
self._model = OpenAI(
14+
temperature=0,
15+
openai_api_key=openai_key,
16+
model_name="text-davinci-003",
17+
)
18+
19+
self._template = """
20+
In the next answer only address the data that was given to answer yes/no.
21+
Given the following facts:
22+
{facts}
23+
assert if the following is factually true:
24+
{response}
25+
respond with yes/no
26+
27+
YOUR RESPONSE:
28+
"""
29+
30+
self._prompt = PromptTemplate(
31+
template=self._template, input_variables=["facts", "response"]
32+
)
33+
34+
def validate(
35+
self,
36+
facts: List[str],
37+
model_output: str,
38+
):
39+
parsed_facts = ", ".join(facts)
40+
41+
fact_check_chain = LLMChain(
42+
prompt=self._prompt,
43+
llm=self._model,
44+
)
45+
entails = fact_check_chain.predict(
46+
facts=parsed_facts,
47+
response=model_output,
48+
)
49+
50+
entails = entails.lower().strip()
51+
52+
assert (
53+
"yes" in entails
54+
), "llm validation check validation failed on fact check" # noqa: E501

examples/consistency_check.py

Whitespace-only changes.

examples/labels_check.py

Whitespace-only changes.

examples/syntactic_validations.py

Whitespace-only changes.

0 commit comments

Comments
 (0)