-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
consisTent/validators/semantic_validators/facts_validator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
Empty file.