aletheia / app.py
matthewfarant's picture
Update app.py
5d38be9 verified
raw
history blame
5.37 kB
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser, StrOutputParser
from langchain_community.chat_models import ChatOllama
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
from langchain_core.tools import Tool
from langchain_google_community import GoogleSearchAPIWrapper
from firecrawl import FirecrawlApp
import gradio as gr
import os
# Initialize LLM and Tools
# local_llm = 'llama3.1'
# llama3 = ChatOllama(model=local_llm, temperature=1)
# llama3_json = ChatOllama(model=local_llm, format='json', temperature=0)
#
os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv('HF_KEY')
llm = HuggingFaceEndpoint(
repo_id="meta-llama/Meta-Llama-3.1-8B-Instruct",
task="text-generation",
max_new_tokens=1000,
do_sample=False,
repetition_penalty=1.03,
)
llama3 = ChatHuggingFace(llm=llm, temperature = 1)
llama3_json = ChatHuggingFace(llm=llm, format = 'json', temperature = 0)
google_search = GoogleSearchAPIWrapper()
os.environ["GOOGLE_CSE_ID"] = "72150c1d158b54108"
os.environ["GOOGLE_API_KEY"] = "AIzaSyAaNoF_a27HM3C87ObELRLTeVCrke_3OJA"
firecrawl_app = FirecrawlApp(api_key='fc-beeb8af53ea6460fbe9f5c2997c7b39b')
# Query Transformation
query_prompt = PromptTemplate(
template="""
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
You are an expert at crafting web search queries for fact checking.
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.
Reword their query to be the most effective web search string possible.
Return the JSON with a single key 'query' with no premable or explanation.
Information to transform: {question}
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
""",
input_variables=["question"],
)
# Chain
query_chain = query_prompt | llama3_json | JsonOutputParser()
# Google Search and Firecrawl Setup
def search_and_scrape(keyword):
search_results = google_search.results(keyword, 3)
scraped_data = []
for result in search_results:
url = result['link']
scrape_response = firecrawl_app.scrape_url(url=url, params={'formats': ['markdown']})
scraped_data.append(scrape_response)
return scraped_data
# Summarizer
summarize_prompt = PromptTemplate(
template="""
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
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
from the article in a readable paragraph. It is okay if one paragraph contains multiple topics.
Information to transform: {question}
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
""",
input_variables=["question"],
)
# Chain
summarize_chain = summarize_prompt | llama3 | StrOutputParser()
# Generation prompt
generate_prompt = PromptTemplate(
template="""
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
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.
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.
Return a JSON output with these keys, with no premable:
1. Verdict: choose between "Fact" and "Possibly Hoax"
2. Explanation: a short explanation on why the verdict was chosen
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.
<|eot_id|>
<|start_header_id|>user<|end_header_id|>
User Information: {question}
Web Search Context: {context}
JSON Verdict and Explanation:
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
""",
input_variables=["question", "context"],
)
# Chain
generate_chain = generate_prompt | llama3_json | JsonOutputParser()
# Full Flow Function
def fact_check_flow(user_question):
# Step 2: Transform question into search query keyword
keyword = query_chain.invoke({"question": user_question})["query"]
# Step 3 & 4: Google search and scrape results
context_data = search_and_scrape(keyword)
final_markdown = []
for results in context_data:
final_markdown.append(results['markdown'])
final_markdown = ' '.join(final_markdown)
context = summarize_chain.invoke({"question": final_markdown})
# Step 5: Use scraped data as context and run generate chain
final_response = generate_chain.invoke({"question": user_question, "context": context})
return final_response
# Example Use
# user_question = "biden is not joining election in 2024"
# result = fact_check_flow(user_question)
# print(result)
demo = gr.Interface(fn=fact_check_flow, inputs="textbox", outputs="textbox")
if __name__ == "__main__":
demo.launch()