Lee Thanh commited on
Commit
c1869f7
1 Parent(s): fe4b9a5

Upload 3 files

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