Skip to content

Commit

Permalink
added fact checking
Browse files Browse the repository at this point in the history
  • Loading branch information
drorIvry committed Apr 28, 2023
1 parent a728773 commit a5b70b3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,14 @@ consisTent.LabelsValidator(openai_key=OPENAI_KEY).validate(
model_output="What do you call a rabbit that tells jokes? A funny bunny!",
)
```

### facts validation

```python
OPENAI_KEY = "XXXXXXXXXXXXXXX"

consisTent.FactsValidator(openai_key=OPENAI_KEY).validate(
facts=["this car weighs 1000KG"],
model_output="I can lift this car",
)
```
54 changes: 54 additions & 0 deletions consisTent/validators/semantic_validators/facts_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing import List
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

from ..base_validator import Validator


class FactsValidator(Validator):
def __init__(
self,
openai_key: str,
):
self._model = OpenAI(
temperature=0,
openai_api_key=openai_key,
model_name="text-davinci-003",
)

self._template = """
In the next answer only address the data that was given to answer yes/no.
Given the following facts:
{facts}
assert if the following is factually true:
{response}
respond with yes/no
YOUR RESPONSE:
"""

self._prompt = PromptTemplate(
template=self._template, input_variables=["facts", "response"]
)

def validate(
self,
facts: List[str],
model_output: str,
):
parsed_facts = ", ".join(facts)

fact_check_chain = LLMChain(
prompt=self._prompt,
llm=self._model,
)
entails = fact_check_chain.predict(
facts=parsed_facts,
response=model_output,
)

entails = entails.lower().strip()

assert (
"yes" in entails
), "llm validation check validation failed on fact check" # noqa: E501
Empty file added examples/consistency_check.py
Empty file.
Empty file added examples/labels_check.py
Empty file.
Empty file.

0 comments on commit a5b70b3

Please sign in to comment.