matthewfarant commited on
Commit
d565b10
1 Parent(s): f559c3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -95
app.py CHANGED
@@ -20,46 +20,9 @@ os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv('HF_KEY')
20
  os.environ["GOOGLE_CSE_ID"] = os.getenv('GOOGLE_CSE_ID')
21
  os.environ["GOOGLE_API_KEY"] = os.getenv('GOOGLE_API_KEY')
22
 
23
- llm = HuggingFaceEndpoint(
24
- repo_id="meta-llama/Meta-Llama-3.1-8B-Instruct",
25
- task="text-generation",
26
- max_new_tokens=4000,
27
- do_sample=False,
28
- repetition_penalty=1.03,
29
- )
30
- llama3 = ChatHuggingFace(llm=llm, temperature = 1)
31
- llama3_json = ChatHuggingFace(llm=llm, format = 'json', temperature = 0)
32
-
33
  google_search = GoogleSearchAPIWrapper()
34
  firecrawl_app = FirecrawlApp(api_key=os.getenv('FIRECRAWL_KEY'))
35
 
36
-
37
- # Query Transformation
38
- query_prompt = PromptTemplate(
39
- template="""
40
-
41
- <|begin_of_text|>
42
-
43
- <|start_header_id|>system<|end_header_id|>
44
-
45
- You are an expert at crafting web search queries for fact checking.
46
- More often than not, a user will provide an information that they wish to fact check, however it might not be in the best format.
47
- Reword their query to be the most effective web search string possible.
48
- Return the JSON with a single key 'query' with no premable or explanation.
49
-
50
- Information to transform: {question}
51
-
52
- <|eot_id|>
53
-
54
- <|start_header_id|>assistant<|end_header_id|>
55
-
56
- """,
57
- input_variables=["question"],
58
- )
59
-
60
- # Chain
61
- query_chain = query_prompt | llama3_json | JsonOutputParser()
62
-
63
  # Google Search and Firecrawl Setup
64
  def search_and_scrape(keyword):
65
  search_results = google_search.results(keyword, 3)
@@ -70,64 +33,6 @@ def search_and_scrape(keyword):
70
  scraped_data.append(scrape_response)
71
  return scraped_data
72
 
73
- # Summarizer
74
- summarize_prompt = PromptTemplate(
75
- template="""
76
-
77
- <|begin_of_text|>
78
-
79
- <|start_header_id|>system<|end_header_id|>
80
-
81
- You are an expert at summarizing web crawling results. The user will give you multiple web search result with different topics. Your task is to summarize all the important information
82
- from the article in a readable paragraph. It is okay if one paragraph contains multiple topics.
83
-
84
- Information to transform: {question}
85
-
86
- <|eot_id|>
87
-
88
- <|start_header_id|>assistant<|end_header_id|>
89
-
90
- """,
91
- input_variables=["question"],
92
- )
93
-
94
- # Chain
95
- summarize_chain = summarize_prompt | llama3 | StrOutputParser()
96
-
97
- # Generation prompt
98
- generate_prompt = PromptTemplate(
99
- template="""
100
-
101
- <|begin_of_text|>
102
-
103
- <|start_header_id|>system<|end_header_id|>
104
-
105
- You are a fact-checker AI assistant that receives an information from the user, synthesizes web search results for that information, and verify whether the user's information is a fact or possibly a hoax.
106
- Strictly use the following pieces of web search context to answer the question. If you don't know the answer, just give "Possibly Hoax" verdict. Only make direct references to material if provided in the context.
107
- Return a JSON output with these keys, with no premable:
108
- 1. user_information: the user's input
109
- 2. system_verdict: is the user question above a fact? choose only between "Fact" or "Possibly Hoax"
110
- 3. explanation: a short explanation on why the verdict was chosen
111
- If the context does not relate with the information provided by user, you can give "Possibly Hoax" result and tell the user that based on web search, it seems that the provided information is a false information.
112
-
113
- <|eot_id|>
114
-
115
- <|start_header_id|>user<|end_header_id|>
116
-
117
- User Information: {question}
118
- Web Search Context: {context}
119
- JSON output:
120
-
121
- <|eot_id|>
122
-
123
- <|start_header_id|>assistant<|end_header_id|>
124
- """,
125
- input_variables=["question", "context"],
126
- )
127
-
128
- # Chain
129
- generate_chain = generate_prompt | llama3_json | JsonOutputParser()
130
-
131
  # Full Flow Function
132
  def fact_check_flow(user_question):
133
  # Step 1 : Initializing
@@ -140,6 +45,90 @@ def fact_check_flow(user_question):
140
  )
141
  llama3 = ChatHuggingFace(llm=llm, temperature = 1)
142
  llama3_json = ChatHuggingFace(llm=llm, format = 'json', temperature = 0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  # Step 2: Transform question into search query keyword
145
  keyword = query_chain.invoke({"question": user_question})["query"]
 
20
  os.environ["GOOGLE_CSE_ID"] = os.getenv('GOOGLE_CSE_ID')
21
  os.environ["GOOGLE_API_KEY"] = os.getenv('GOOGLE_API_KEY')
22
 
 
 
 
 
 
 
 
 
 
 
23
  google_search = GoogleSearchAPIWrapper()
24
  firecrawl_app = FirecrawlApp(api_key=os.getenv('FIRECRAWL_KEY'))
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Google Search and Firecrawl Setup
27
  def search_and_scrape(keyword):
28
  search_results = google_search.results(keyword, 3)
 
33
  scraped_data.append(scrape_response)
34
  return scraped_data
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  # Full Flow Function
37
  def fact_check_flow(user_question):
38
  # Step 1 : Initializing
 
45
  )
46
  llama3 = ChatHuggingFace(llm=llm, temperature = 1)
47
  llama3_json = ChatHuggingFace(llm=llm, format = 'json', temperature = 0)
48
+
49
+ # Query Transformation
50
+ query_prompt = PromptTemplate(
51
+ template="""
52
+
53
+ <|begin_of_text|>
54
+
55
+ <|start_header_id|>system<|end_header_id|>
56
+
57
+ You are an expert at crafting web search queries for fact checking.
58
+ More often than not, a user will provide an information that they wish to fact check, however it might not be in the best format.
59
+ Reword their query to be the most effective web search string possible.
60
+ Return the JSON with a single key 'query' with no premable or explanation.
61
+
62
+ Information to transform: {question}
63
+
64
+ <|eot_id|>
65
+
66
+ <|start_header_id|>assistant<|end_header_id|>
67
+
68
+ """,
69
+ input_variables=["question"],
70
+ )
71
+
72
+ # Chain
73
+ query_chain = query_prompt | llama3_json | JsonOutputParser()
74
+
75
+ # Summarizer
76
+ summarize_prompt = PromptTemplate(
77
+ template="""
78
+
79
+ <|begin_of_text|>
80
+
81
+ <|start_header_id|>system<|end_header_id|>
82
+
83
+ You are an expert at summarizing web crawling results. The user will give you multiple web search result with different topics. Your task is to summarize all the important information
84
+ from the article in a readable paragraph. It is okay if one paragraph contains multiple topics.
85
+
86
+ Information to transform: {question}
87
+
88
+ <|eot_id|>
89
+
90
+ <|start_header_id|>assistant<|end_header_id|>
91
+
92
+ """,
93
+ input_variables=["question"],
94
+ )
95
+
96
+ # Chain
97
+ summarize_chain = summarize_prompt | llama3 | StrOutputParser()
98
+
99
+ # Generation prompt
100
+ generate_prompt = PromptTemplate(
101
+ template="""
102
+
103
+ <|begin_of_text|>
104
+
105
+ <|start_header_id|>system<|end_header_id|>
106
+
107
+ You are a fact-checker AI assistant that receives an information from the user, synthesizes web search results for that information, and verify whether the user's information is a fact or possibly a hoax.
108
+ Strictly use the following pieces of web search context to answer the question. If you don't know the answer, just give "Possibly Hoax" verdict. Only make direct references to material if provided in the context.
109
+ Return a JSON output with these keys, with no premable:
110
+ 1. user_information: the user's input
111
+ 2. system_verdict: is the user question above a fact? choose only between "Fact" or "Possibly Hoax"
112
+ 3. explanation: a short explanation on why the verdict was chosen
113
+ If the context does not relate with the information provided by user, you can give "Possibly Hoax" result and tell the user that based on web search, it seems that the provided information is a false information.
114
+
115
+ <|eot_id|>
116
+
117
+ <|start_header_id|>user<|end_header_id|>
118
+
119
+ User Information: {question}
120
+ Web Search Context: {context}
121
+ JSON output:
122
+
123
+ <|eot_id|>
124
+
125
+ <|start_header_id|>assistant<|end_header_id|>
126
+ """,
127
+ input_variables=["question", "context"],
128
+ )
129
+
130
+ # Chain
131
+ generate_chain = generate_prompt | llama3_json | JsonOutputParser()
132
 
133
  # Step 2: Transform question into search query keyword
134
  keyword = query_chain.invoke({"question": user_question})["query"]