Shreemit commited on
Commit
265cb40
·
verified ·
1 Parent(s): ce75785

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -64
app.py CHANGED
@@ -2,10 +2,7 @@ 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
@@ -14,66 +11,57 @@ 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)
 
 
 
 
 
2
  from getpass import getpass
3
  from langchain_google_genai import GoogleGenerativeAI
4
  from langchain.prompts import PromptTemplate
5
+ from langchain.agents import AgentExecutor, initialize_agent, AgentType
 
 
 
6
  from langchain.agents.format_scratchpad import format_to_openai_function_messages
7
  from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
8
  from langchain.utilities.tavily_search import TavilySearchAPIWrapper
 
11
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
12
  from langchain_core.pydantic_v1 import BaseModel, Field
13
  from langchain_google_genai import ChatGoogleGenerativeAI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ def create_tools():
16
+ search = TavilySearchAPIWrapper(tavily_api_key='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')
17
+ description = """"A search engine optimized for comprehensive, accurate, \
18
+ and trusted results. Useful for when you need to answer questions \
19
+ about current events or about recent information. \
20
+ Input should be a search query. \
21
+ If the user is asking about something that you don't know about, \
22
+ you should probably use this tool to see if that can provide any information."""
23
+ tavily_tool = TavilySearchResults(api_wrapper=search, description=description)
24
+ return [tavily_tool]
25
+
26
+ def create_llm_with_tools(llm, tools):
27
+ return llm.bind(functions=tools)
28
+
29
+ def create_agent_chain(tools, llm):
30
+ return initialize_agent(
31
+ tools,
32
+ llm,
33
+ agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
34
+ verbose=True,
35
+ )
36
+
37
+ def get_user_input():
38
+ return st.text_input("Enter your question")
39
+
40
+ def display_response(response):
41
  st.write(response)
42
 
43
+ def main():
44
+ st.title('Fact-Checking Chatbot')
45
+ llm = GoogleGenerativeAI(model="gemini-pro", google_api_key="AIzaSyBNfTHLMjR9vGiomZsW9NFsUTwc2U2NuFA")
46
+ tools = create_tools()
47
+ llm_with_tools = create_llm_with_tools(llm, tools)
48
+ agent_chain = create_agent_chain(tools, llm)
49
+ user_input = get_user_input()
50
+ if user_input:
51
+ response = llm.invoke(user_input)
52
+ display_response(response)
53
+ prompt = """
54
+ You are a fact-checker. You are asked to verify the following statement based on the information you get from your tool
55
+ and your knowledge. You should provide a response that is based on the information you have and that is as accurate as possible.
56
+ Your response should be True or False. If you are not sure, you should say that you are not sure.
57
+ """
58
+ new_prompt = st.text_area(prompt)
59
+ if new_prompt:
60
+ prompt = new_prompt
61
+ answer = agent_chain.invoke(
62
+ prompt + "\n " + user_input,
63
+ )
64
+ display_response(answer)
65
+
66
+ if __name__ == "__main__":
67
+ main()