Created
October 23, 2023 13:57
-
-
Save FriedGil/b900a3fb6482f8a4379462ee0ab68987 to your computer and use it in GitHub Desktop.
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
#Misinformation Classication with GPT-4 | |
from langchain.agents import load_tools | |
from langchain.agents import initialize_agent | |
from langchain.llms import OpenAI | |
from langchain.chat_models import ChatOpenAI | |
from langchain import SerpAPIWrapper | |
from langchain.document_loaders import WebBaseLoader | |
from langchain.tools import tool | |
import requests | |
from bs4 import BeautifulSoup | |
#compare lora finetune to normal | |
@tool | |
def read_webpage(url: str): | |
"""for reading websites when you have a valid url""" | |
res = requests.get(url) | |
html_page = res.content | |
soup = BeautifulSoup(html_page, 'html.parser') | |
ans = "" | |
for para in soup.find_all("p"): | |
ans += para.get_text() | |
if len(ans) > 1000: break | |
return ans | |
@tool | |
def search(input: str): | |
"""for performing google searches""" | |
search = SerpAPIWrapper() | |
# res = search.run(input + " site:factcheck.org OR site:snopes.com") | |
res = search.run(input) | |
return res | |
def search_classify(text): | |
llm = ChatOpenAI(temperature=0, model="gpt-4") | |
tools = load_tools([], llm=llm) | |
# tools.append(read_webpage) | |
tools.append(search) | |
#Add Google Factcheck API as a tool | |
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) | |
from langchain.prompts import PromptTemplate | |
claim = text | |
result = agent.run(f"Rate the truthfulness of the following statement: {claim}. Provide a classification from 0 to 100, 0 meaning completely false and 100 meaning completely true. Do not provide any explanations, only respond with the numerical score. After 3 unhelpful searches, return 50.") | |
return result | |
from langchain.prompts import PromptTemplate | |
from langchain.chains import LLMChain | |
def classify(text): | |
llm = ChatOpenAI(temperature=0, model="gpt-4") | |
prompt_template = "Rate the truthfulness of the following statement: '{text}'. Provide a classification from 0 to 100, 0 meaning definitely false and 100 meaning definitely true. Do not provide any explanations, only respond with the numerical score. Return -1 if you are unable to respond. " | |
llm_chain = LLMChain( | |
llm=llm, | |
prompt=PromptTemplate.from_template(prompt_template) | |
) | |
return llm_chain(text)['text']; | |
#Add Output parser | |
#Site whitelist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment