-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
fact_checking.py
78 lines (51 loc) · 3.01 KB
/
fact_checking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
""" This example demonstrates evidence and fact checking capabilities in the Prompt class. """
import os
from llmware.prompts import Prompt
from llmware.setup import Setup
from llmware.configs import LLMWareConfig
from llmware.dataset_tools import Datasets
def contract_analysis_with_fact_checking (model_name):
# Load the llmware sample files
print (f"\n > Loading the llmware sample files...")
sample_files_path = Setup().load_sample_files()
contracts_path = os.path.join(sample_files_path,"Agreements")
print (f"\n > Loading model {model_name}...")
prompter = Prompt().load_model(model_name, temperature=0.0, sample=False)
research = {"topic": "base salary", "prompt": "What is the executive's base salary?"}
for i, contract in enumerate(os.listdir(contracts_path)):
print("\nAnalyzing Contract - ", str(i+1), contract)
print("Question: ", research["prompt"])
# contract is parsed, text-chunked, and then filtered by "base salary'
source = prompter.add_source_document(contracts_path, contract, query=research["topic"])
# calling the LLM with 'source' information from the contract automatically packaged into the prompt
responses = prompter.prompt_with_source(research["prompt"], prompt_name="default_with_context")
# run several fact checks
ev_numbers = prompter.evidence_check_numbers(responses)
ev_sources = prompter.evidence_check_sources(responses)
ev_stats = prompter.evidence_comparison_stats(responses)
z = prompter.classify_not_found_response(responses, parse_response=True, evidence_match=True,ask_the_model=False)
for r, response in enumerate(responses):
print("LLM Response: ", response["llm_response"])
print("Numbers: ", ev_numbers[r]["fact_check"])
print("Sources: ", ev_sources[r]["source_review"])
print("Stats: ", ev_stats[r]["comparison_stats"])
print("Not Found Check: ", z[r])
# We're done with this contract, clear the source from the prompt
prompter.clear_source_materials()
# Save jsonl report to jsonl to /prompt_history folder
print("\nPrompt state saved at: ", os.path.join(LLMWareConfig.get_prompt_path(),prompter.prompt_id))
prompter.save_state()
# Optional - builds a dataset from prompt history that is 'model-training-ready'
ds = Datasets().build_gen_ds_from_prompt_history(prompt_wrapper="human_bot")
return 0
if __name__ == "__main__":
hf_model_list = ["llmware/bling-1b-0.1",
"llmware/bling-1.4b-0.1",
"llmware/bling-falcon-1b-0.1",
"llmware/bling-sheared-llama-1.3b-0.1",
"bling-phi-3-gguf"]
model_name = hf_model_list[0]
# to use a 3rd party model, select model_name, e.g., "gpt-4"
# --if model requires an api_key, then it must be set as an os.environ variable
# --see example on 'set_model_api_keys.py'
contract_analysis_with_fact_checking(model_name)