satvikjain commited on
Commit
8ab1aa8
·
1 Parent(s): 4513c5e

initial commit

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. app.py +25 -0
  3. live_search.py +87 -0
  4. requirements.txt +6 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from live_search import Search_Class
2
+ import gradio as gr
3
+
4
+ bot = Search_Class()
5
+
6
+ with gr.Blocks() as app:
7
+ with gr.Tab("ChatBot"):
8
+ gr.Markdown("""## 🤖 Real-time AI Assitant for:
9
+ - 📰 News Updates
10
+ - 💹 Stock Market Updates
11
+ - ☁️ Weather Updates
12
+ """)
13
+ chat = gr.Chatbot()
14
+ text = gr.Textbox(placeholder="➡️ Enter your query here", show_label= False)
15
+ text.submit(fn = bot.run,
16
+ inputs=text,
17
+ outputs=chat)
18
+ submit = gr.Button(value = "Submit")
19
+ submit.click(fn = bot.run,
20
+ inputs=text,
21
+ outputs=chat)
22
+ clear = gr.ClearButton([chat, text], value= "Clear and Delete Memory")
23
+ clear.click(fn = bot.setup_memory)
24
+
25
+ app.launch()
live_search.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.tools import WikipediaQueryRun
2
+ from langchain_community.utilities import WikipediaAPIWrapper
3
+ from langchain_community.document_loaders import WebBaseLoader
4
+ from langchain_community.vectorstores import FAISS
5
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
6
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
7
+ from langchain.tools.retriever import create_retriever_tool
8
+ from dotenv import load_dotenv
9
+ from langchain_google_genai import ChatGoogleGenerativeAI
10
+ from langchain_community.chat_message_histories import ChatMessageHistory
11
+ from langchain.agents import create_react_agent, create_tool_calling_agent
12
+ from langchain.agents import AgentExecutor
13
+ from langchain import hub
14
+ from langchain.agents import Tool
15
+ from langchain_community.utilities import SerpAPIWrapper
16
+ from langchain_core.runnables.history import RunnableWithMessageHistory
17
+
18
+ class Search_Class:
19
+ def __init__(self):
20
+ self.setup_env()
21
+ self.setup_llm()
22
+ self.setup_embeddings()
23
+ self.setup_vector_store()
24
+ self.setup_tools()
25
+ self.setup_memory()
26
+ self.setup_agent()
27
+
28
+ def setup_env(self):
29
+ load_dotenv()
30
+
31
+ def setup_llm(self):
32
+ self.llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0)
33
+ # self.llm = ChatGroq(model="llama3-70b-8192", temperature=0)
34
+ def setup_embeddings(self):
35
+ self.loader = WebBaseLoader("https://www.etmoney.com/stocks/list-of-stocks")
36
+ self.docs = self.loader.load()
37
+ self.embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
38
+ self.documents = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(self.docs)
39
+
40
+ def setup_vector_store(self):
41
+ self.vectordb = FAISS.from_documents(self.documents, self.embeddings)
42
+ self.retriever = self.vectordb.as_retriever()
43
+
44
+ def setup_tools(self):
45
+ self.search_tool = Tool(
46
+ name="Search",
47
+ description="A search engine. Useful for when you need to answer questions about current events. Input should be a search query.",
48
+ func=SerpAPIWrapper().run
49
+ )
50
+ api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
51
+ api_wrapper = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
52
+ self.wiki_tool = Tool(
53
+ name = "Wikipedia",
54
+ description = "A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.",
55
+ func = api_wrapper.run
56
+ )
57
+ self.retriever_tool = create_retriever_tool(
58
+ self.retriever, "stock_search", "For any information related to stock prices use this tool"
59
+ )
60
+ self.tools = [self.search_tool, self.wiki_tool]
61
+ self.names = ["Wikipedia","stock_search"]
62
+
63
+ def setup_memory(self):
64
+ self.memory = ChatMessageHistory(session_id="test-session")
65
+ self.chat_history = []
66
+
67
+ def setup_agent(self):
68
+ self.prompt = hub.pull("satvikjain/react_smaller")
69
+ self.agent = create_tool_calling_agent(self.llm, self.tools, self.prompt)
70
+ self.agent_executor = AgentExecutor(agent=self.agent, tools=self.tools, verbose=True, handle_parsing_errors=True)
71
+ self.agent_executor.return_intermediate_steps = True
72
+ self.agent_with_chat_history = RunnableWithMessageHistory(
73
+ self.agent_executor,
74
+ lambda session_id: self.memory,
75
+ input_messages_key="input",
76
+ history_messages_key="chat_history"
77
+ )
78
+
79
+ def run(self, user_input = "Hi"):
80
+ response = self.agent_with_chat_history.invoke({
81
+ "input": user_input, "tools": self.tools, "tool_names":self.names},
82
+ config={"configurable": {"session_id": "test-session"}
83
+ }
84
+ )
85
+ self.chat_history.append([user_input, response["output"]])
86
+ return self.chat_history
87
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain
2
+ langchain_community
3
+ langchain_google_genai
4
+ langchain_text_splitters
5
+ langchain_core
6
+ gradio