shivam12323 commited on
Commit
15a4c90
1 Parent(s): 5da5454

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # pip install streamlit langchain lanchain-openai beautifulsoup4 python-dotenv chromadb
2
+ import os
3
+ import streamlit as st
4
+ from langchain_core.messages import AIMessage, HumanMessage
5
+ from langchain_community.document_loaders import WebBaseLoader
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ from langchain_community.vectorstores import Chroma
8
+ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
9
+ #from dotenv import load_dotenv
10
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
11
+ from langchain.chains import create_history_aware_retriever, create_retrieval_chain
12
+ from langchain.chains.combine_documents import create_stuff_documents_chain
13
+ from constants import openai_key
14
+
15
+ os.environ["OPENAI_API_KEY"]=openai_key
16
+
17
+
18
+ #load_dotenv()
19
+
20
+ def get_vectorstore_from_url(url):
21
+ # get the text in document form
22
+ loader = WebBaseLoader(url)
23
+ document = loader.load()
24
+
25
+ # split the document into chunks
26
+ text_splitter = RecursiveCharacterTextSplitter()
27
+ document_chunks = text_splitter.split_documents(document)
28
+
29
+ # create a vectorstore from the chunks
30
+ vector_store = Chroma.from_documents(document_chunks, OpenAIEmbeddings())
31
+
32
+ return vector_store
33
+
34
+ def get_context_retriever_chain(vector_store):
35
+ llm = ChatOpenAI()
36
+
37
+ retriever = vector_store.as_retriever()
38
+
39
+ prompt = ChatPromptTemplate.from_messages([
40
+ MessagesPlaceholder(variable_name="chat_history"),
41
+ ("user", "{input}"),
42
+ ("user", "Given the above conversation, generate a search query to look up in order to get information relevant to the conversation")
43
+ ])
44
+
45
+ retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
46
+
47
+ return retriever_chain
48
+
49
+ def get_conversational_rag_chain(retriever_chain):
50
+
51
+ llm = ChatOpenAI()
52
+
53
+ prompt = ChatPromptTemplate.from_messages([
54
+ ("system", "Answer the user's questions based on the below context:\n\n{context}"),
55
+ MessagesPlaceholder(variable_name="chat_history"),
56
+ ("user", "{input}"),
57
+ ])
58
+
59
+ stuff_documents_chain = create_stuff_documents_chain(llm,prompt)
60
+
61
+ return create_retrieval_chain(retriever_chain, stuff_documents_chain)
62
+
63
+ def get_response(user_input):
64
+ retriever_chain = get_context_retriever_chain(st.session_state.vector_store)
65
+ conversation_rag_chain = get_conversational_rag_chain(retriever_chain)
66
+
67
+ response = conversation_rag_chain.invoke({
68
+ "chat_history": st.session_state.chat_history,
69
+ "input": user_input
70
+ })
71
+
72
+ return response['answer']
73
+
74
+ # app config
75
+ st.set_page_config(page_title="Chat with websites", page_icon="🤖")
76
+ st.title("Chat with websites")
77
+
78
+ # sidebar
79
+ with st.sidebar:
80
+ st.header("Settings")
81
+ website_url = st.text_input("Website URL")
82
+
83
+ if website_url is None or website_url == "":
84
+ st.info("Please enter a website URL")
85
+
86
+ else:
87
+ # session state
88
+ if "chat_history" not in st.session_state:
89
+ st.session_state.chat_history = [
90
+ AIMessage(content="Hello, I am a bot. How can I help you?"),
91
+ ]
92
+ if "vector_store" not in st.session_state:
93
+ st.session_state.vector_store = get_vectorstore_from_url(website_url)
94
+
95
+ # user input
96
+ user_query = st.chat_input("Type your message here...")
97
+ if user_query is not None and user_query != "":
98
+ response = get_response(user_query)
99
+ st.session_state.chat_history.append(HumanMessage(content=user_query))
100
+ st.session_state.chat_history.append(AIMessage(content=response))
101
+
102
+
103
+
104
+ # conversation
105
+ for message in st.session_state.chat_history:
106
+ if isinstance(message, AIMessage):
107
+ with st.chat_message("AI"):
108
+ st.write(message.content)
109
+ elif isinstance(message, HumanMessage):
110
+ with st.chat_message("Human"):
111
+ st.write(message.content)