Spaces:
Sleeping
Sleeping
File size: 5,367 Bytes
7e99c90 5d38be9 7e99c90 5d38be9 7e99c90 c80a1a0 5d38be9 c80a1a0 |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
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() |