Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from getpass import getpass
|
3 |
+
from langchain_google_genai import GoogleGenerativeAI
|
4 |
+
from langchain.prompts import PromptTemplate
|
5 |
+
|
6 |
+
from typing import List, Tuple
|
7 |
+
|
8 |
+
from langchain.agents import AgentExecutor
|
9 |
+
from langchain.agents.format_scratchpad import format_to_openai_function_messages
|
10 |
+
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
|
11 |
+
from langchain.utilities.tavily_search import TavilySearchAPIWrapper
|
12 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
13 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
14 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
15 |
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
16 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
17 |
+
from langchain.agents import initialize_agent, AgentType
|
18 |
+
|
19 |
+
# Create the tool
|
20 |
+
search = TavilySearchAPIWrapper(tavily_api_key='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')
|
21 |
+
description = """"A search engine optimized for comprehensive, accurate, \
|
22 |
+
and trusted results. Useful for when you need to answer questions \
|
23 |
+
about current events or about recent information. \
|
24 |
+
Input should be a search query. \
|
25 |
+
If the user is asking about something that you don't know about, \
|
26 |
+
you should probably use this tool to see if that can provide any information."""
|
27 |
+
tavily_tool = TavilySearchResults(api_wrapper=search, description=description)
|
28 |
+
|
29 |
+
tools = [tavily_tool]
|
30 |
+
|
31 |
+
from getpass import getpass
|
32 |
+
|
33 |
+
# api_key = getpass()
|
34 |
+
|
35 |
+
llm = GoogleGenerativeAI(model="gemini-pro", google_api_key="AIzaSyBNfTHLMjR9vGiomZsW9NFsUTwc2U2NuFA")
|
36 |
+
|
37 |
+
prompt = ChatPromptTemplate.from_messages(
|
38 |
+
[
|
39 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
40 |
+
("user", "{input}"),
|
41 |
+
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
42 |
+
]
|
43 |
+
)
|
44 |
+
|
45 |
+
llm_with_tools = llm.bind(functions=tools)
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
# Set up Streamlit
|
50 |
+
st.title('Fact-Checking Chatbot')
|
51 |
+
# Get user input
|
52 |
+
user_input = st.text_input("Enter your question")
|
53 |
+
|
54 |
+
# If user has entered a question, generate response
|
55 |
+
if user_input:
|
56 |
+
response = llm.invoke(user_input)
|
57 |
+
st.write(response)
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
agent_chain = initialize_agent(
|
62 |
+
[tavily_tool],
|
63 |
+
llm,
|
64 |
+
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
|
65 |
+
verbose=True,
|
66 |
+
)
|
67 |
+
prompt = """
|
68 |
+
You are a fact-checker. You are asked to verify the following statement based on the information you get from your tool
|
69 |
+
and your knowledge. You should provide a response that is based on the information you have and that is as accurate as possible.
|
70 |
+
Your response should be True or False. If you are not sure, you should say that you are not sure.
|
71 |
+
"""
|
72 |
+
|
73 |
+
prompt = st.text_area(prompt)
|
74 |
+
|
75 |
+
answer = agent_chain.invoke(
|
76 |
+
prompt + "\n " + user_input,
|
77 |
+
)
|
78 |
+
|
79 |
+
st.write(answer)
|