Parthiban97 commited on
Commit
bad12a8
·
verified ·
1 Parent(s): ed679fa

Upload 5 files

Browse files
.streamlit/config.toml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [theme]
2
+ primaryColor="#2b271f"
3
+ backgroundColor="#e9e9f3"
4
+ secondaryBackgroundColor="#445271"
5
+ textColor="#000000"
6
+ font="serif"
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
__pycache__/htmlTemplates.cpython-310.pyc ADDED
Binary file (1.17 kB). View file
 
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from langchain_openai import ChatOpenAI
4
+ from langchain.memory import ConversationBufferMemory
5
+ from langchain_community.document_loaders import WebBaseLoader
6
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
7
+ from langchain_openai import OpenAIEmbeddings
8
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
9
+ from langchain_community.vectorstores import FAISS
10
+ from langchain import hub
11
+ from langchain.tools.retriever import create_retriever_tool
12
+ from langchain_community.tools import WikipediaQueryRun
13
+ from langchain_community.utilities import WikipediaAPIWrapper
14
+ from langchain_community.utilities import ArxivAPIWrapper
15
+ from langchain_community.tools import ArxivQueryRun
16
+ from langchain.agents import create_openai_tools_agent
17
+ from langchain.agents import AgentExecutor
18
+ from htmlTemplates import css, bot_template, user_template
19
+
20
+ st.set_page_config(page_title="Query Assistant", page_icon=":robot_face:")
21
+ st.write(css, unsafe_allow_html=True)
22
+
23
+
24
+ # Initialize session state
25
+ if "agent_executor" not in st.session_state:
26
+ st.session_state.agent_executor = None
27
+ if "memory" not in st.session_state:
28
+ st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
29
+ if "chat_history" not in st.session_state:
30
+ st.session_state.chat_history = []
31
+
32
+ # Sidebar
33
+ with st.sidebar:
34
+ st.title("Query Assistant")
35
+ st.subheader("Configuration")
36
+ openai_api_key = st.text_input("Enter your OpenAI API Key", type="password", help="Get your API key from [OpenAI Website](https://platform.openai.com/api-keys)")
37
+ os.environ["OPENAI_API_KEY"] = str(openai_api_key)
38
+
39
+ custom_urls = st.text_area("Enter URLs (optional)", placeholder="Enter URLs separated by (,)")
40
+
41
+ # Custom prompt text area
42
+ custom_prompt_template = st.text_area("User Prompts", placeholder="Enter your custom prompt here...(Optional)")
43
+
44
+ if st.button("Load Tools"):
45
+ with st.spinner("Loading tools and creating agent..."):
46
+ # Load Wikipedia tool
47
+ api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=600)
48
+ wiki_tool = WikipediaQueryRun(api_wrapper=api_wrapper)
49
+
50
+ # Load arXiv tool
51
+ arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=600)
52
+ arxiv_tool = ArxivQueryRun(api_wrapper=arxiv_wrapper)
53
+
54
+ if custom_urls:
55
+ urls = [url.strip() for url in custom_urls.split(",")]
56
+ all_documents = []
57
+ for url in urls:
58
+ loader = WebBaseLoader(url)
59
+ docs = loader.load()
60
+ documents = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(docs)
61
+ all_documents.extend(documents)
62
+
63
+ vectordb = FAISS.from_documents(all_documents, OpenAIEmbeddings())
64
+ retriever = vectordb.as_retriever()
65
+ retriever_tool = create_retriever_tool(retriever, "custom_search", "Search for information if you find any matching keywords from the provided URLs then use this tool and provide the best fit answer from that")
66
+ tools = [wiki_tool, arxiv_tool, retriever_tool]
67
+ else:
68
+ tools = [wiki_tool, arxiv_tool]
69
+
70
+ # Load language model
71
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0.4)
72
+
73
+ # Set the prompt template
74
+ if custom_prompt_template:
75
+ prompt = ChatPromptTemplate.from_messages([
76
+ ("system", custom_prompt_template),
77
+ MessagesPlaceholder("chat_history", optional=True),
78
+ ("human", "{input}"),
79
+ MessagesPlaceholder("agent_scratchpad"),
80
+ ])
81
+ else:
82
+ prompt = hub.pull("hwchase17/openai-functions-agent")
83
+
84
+ # Create the agent with memory
85
+ agent = create_openai_tools_agent(llm, tools, prompt=prompt.partial(chat_history=st.session_state.memory.buffer))
86
+ st.session_state.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
87
+ st.success("Tools loaded successfully!")
88
+
89
+ # Main app
90
+ user_query = st.chat_input("Enter your query:")
91
+
92
+ if user_query and st.session_state.agent_executor:
93
+ with st.spinner("Processing your query..."):
94
+ response = st.session_state.agent_executor.invoke({"input": user_query})
95
+ st.session_state.memory.save_context({"input": user_query}, {"chat_history": response["output"]})
96
+ st.session_state.chat_history.append({"role": "user", "content": user_query})
97
+ st.session_state.chat_history.append({"role": "assistant", "content": response["output"]})
98
+ print(st.session_state.chat_history)
99
+
100
+
101
+ for message in st.session_state.chat_history:
102
+ if message["role"] == "user":
103
+ st.write(user_template.replace(
104
+ "{{MSG}}", message["content"]), unsafe_allow_html=True)
105
+ else:
106
+ st.write(bot_template.replace(
107
+ "{{MSG}}", message["content"]), unsafe_allow_html=True)
htmlTemplates.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ css = '''
2
+ <style>
3
+ .chat-message {
4
+ padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
5
+ }
6
+ .chat-message.user {
7
+ background-color: #2b313e
8
+ }
9
+ .chat-message.bot {
10
+ background-color: #475063
11
+ }
12
+ .chat-message .avatar {
13
+ width: 20%;
14
+ }
15
+ .chat-message .avatar img {
16
+ max-width: 78px;
17
+ max-height: 78px;
18
+ border-radius: 50%;
19
+ object-fit: cover;
20
+ }
21
+ .chat-message .message {
22
+ width: 80%;
23
+ padding: 0 1.5rem;
24
+ color: #fff;
25
+ }
26
+ '''
27
+
28
+ bot_template = '''
29
+ <div class="chat-message bot">
30
+ <div class="avatar">
31
+ <img src="https://i.ibb.co/cN0nmSj/Screenshot-2023-05-28-at-02-37-21.png">
32
+ </div>
33
+ <div class="message">{{MSG}}</div>
34
+ </div>
35
+ '''
36
+
37
+ user_template = '''
38
+ <div class="chat-message user">
39
+ <div class="avatar">
40
+ <img src="https://i.ibb.co/tMd5k0j/human-face.png">
41
+ </div>
42
+ <div class="message">{{MSG}}</div>
43
+ </div>
44
+ '''
requirements.txt ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain_openai
2
+ langchain_core
3
+ python-dotenv
4
+ streamlit
5
+ langchain_community
6
+ langserve
7
+ fastapi
8
+ uvicorn
9
+ sse_starlette
10
+ bs4
11
+ pypdf
12
+ chromadb
13
+ faiss-cpu
14
+ groq
15
+ cassio
16
+ beautifulsoup4
17
+ langchain-groq
18
+ wikipedia
19
+ arxiv
20
+ langchainhub
21
+ sentence_transformers
22
+ PyPDF2
23
+ langchain-objectbox