captain-awesome commited on
Commit
0ff2378
1 Parent(s): 8c67f60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -6
app.py CHANGED
@@ -1,6 +1,12 @@
1
  import streamlit as st
2
  from langchain_core.messages import AIMessage, HumanMessage
3
  from langchain_community.document_loaders import WebBaseLoader
 
 
 
 
 
 
4
 
5
 
6
  def get_response(user_input):
@@ -8,18 +14,66 @@ def get_response(user_input):
8
 
9
  def get_vector_store_from_url(url):
10
  loader = WebBaseLoader(url)
11
- documents = loader.load()
12
- return documents
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # app config
15
  st.set_page_config(page_title= "Chat with Websites", page_icon="🤖")
16
  st.title("Chat with Websites")
17
 
18
 
19
- if "chat_history" not in st.session_state:
20
- st.session_state.chat_history = [
21
- AIMessage(content = "Hello, I am a bot. How can I help you"),
22
- ]
23
 
24
 
25
  #sidebar
@@ -33,6 +87,14 @@ if (website_url is None or website_url == "") or (openai_apikey is None or opena
33
 
34
 
35
  else:
 
 
 
 
 
 
 
 
36
 
37
  documents = get_vector_store_from_url(website_url)
38
  with st.sidebar:
 
1
  import streamlit as st
2
  from langchain_core.messages import AIMessage, HumanMessage
3
  from langchain_community.document_loaders import WebBaseLoader
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain_community.vectorstores import Chroma
6
+ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
7
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
8
+ from langchain.chains import create_history_aware_retriever, create_retrieval_chain
9
+ from langchain.chains.combine_documents import create_stuff_documents_chain
10
 
11
 
12
  def get_response(user_input):
 
14
 
15
  def get_vector_store_from_url(url):
16
  loader = WebBaseLoader(url)
17
+ document = loader.load()
18
+
19
+ # split the document into chunks
20
+ text_splitter = RecursiveCharacterTextSplitter()
21
+ document_chunks = text_splitter.split_documents(document)
22
+
23
+ # create a vectorstore from the chunks
24
+ vector_store = Chroma.from_documents(document_chunks, OpenAIEmbeddings())
25
+
26
+ return vector_store
27
+
28
+
29
+ def get_context_retriever_chain(vector_store):
30
+ llm = ChatOpenAI()
31
+
32
+ retriever = vector_store.as_retriever()
33
+
34
+ prompt = ChatPromptTemplate.from_messages([
35
+ MessagesPlaceholder(variable_name="chat_history"),
36
+ ("user", "{input}"),
37
+ ("user", "Given the above conversation, generate a search query to look up in order to get information relevant to the conversation")
38
+ ])
39
+
40
+ retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
41
+
42
+ return retriever_chain
43
+
44
 
45
+ def get_conversational_rag_chain(retriever_chain):
46
+
47
+ llm = ChatOpenAI()
48
+
49
+ prompt = ChatPromptTemplate.from_messages([
50
+ ("system", "Answer the user's questions based on the below context:\n\n{context}"),
51
+ MessagesPlaceholder(variable_name="chat_history"),
52
+ ("user", "{input}"),
53
+ ])
54
+
55
+ stuff_documents_chain = create_stuff_documents_chain(llm,prompt)
56
+
57
+ return create_retrieval_chain(retriever_chain, stuff_documents_chain)
58
+
59
+ def get_response(user_input):
60
+ retriever_chain = get_context_retriever_chain(st.session_state.vector_store)
61
+ conversation_rag_chain = get_conversational_rag_chain(retriever_chain)
62
+
63
+ response = conversation_rag_chain.invoke({
64
+ "chat_history": st.session_state.chat_history,
65
+ "input": user_query
66
+ })
67
+
68
+ return response['answer']
69
+
70
+
71
  # app config
72
  st.set_page_config(page_title= "Chat with Websites", page_icon="🤖")
73
  st.title("Chat with Websites")
74
 
75
 
76
+
 
 
 
77
 
78
 
79
  #sidebar
 
87
 
88
 
89
  else:
90
+
91
+ if "chat_history" not in st.session_state:
92
+ st.session_state.chat_history = [
93
+ AIMessage(content = "Hello, I am a bot. How can I help you"),
94
+ ]
95
+
96
+ if "vector_store" not in st.session_state:
97
+ st.session_state.vector_store = get_vectorstore_from_url(website_url)
98
 
99
  documents = get_vector_store_from_url(website_url)
100
  with st.sidebar: