Spaces:
Sleeping
Sleeping
Jagadish Krishnamoorthy
commited on
Add Faiss db RAG
Browse filesSigned-off-by: Jagadish Krishnamoorthy <[email protected]>
- app.py +35 -12
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,17 +1,34 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
-
from langchain_openai import ChatOpenAI
|
3 |
from langchain_core.prompts import ChatPromptTemplate
|
4 |
from langchain_community.document_loaders import WebBaseLoader
|
|
|
|
|
|
|
|
|
5 |
|
6 |
default_url = "https://rocm.docs.amd.com/en/latest/what-is-rocm.html"
|
7 |
st.title("URL Loader")
|
8 |
|
|
|
|
|
9 |
url = st.text_input("Provide URL ", default_url)
|
10 |
if "url_dict" not in st.session_state:
|
11 |
st.session_state.url_dict = {}
|
12 |
if url not in st.session_state.url_dict:
|
13 |
loader = WebBaseLoader(url)
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
llm = ChatOpenAI(temperature=0.1)
|
17 |
prompt = ChatPromptTemplate.from_template("""
|
@@ -19,31 +36,37 @@ prompt = ChatPromptTemplate.from_template("""
|
|
19 |
Context: {context}
|
20 |
Question : {input}
|
21 |
""")
|
22 |
-
chain = prompt | llm
|
23 |
|
24 |
# Initialize chat history
|
25 |
if "messages" not in st.session_state:
|
26 |
st.session_state.messages = []
|
27 |
|
28 |
# Display chat messages from history on app rerun
|
29 |
-
for message in st.session_state.messages:
|
30 |
with st.chat_message(message["role"]):
|
31 |
st.markdown(message["content"])
|
32 |
|
33 |
# React to user input
|
34 |
-
if
|
35 |
# Display user message in chat message container
|
36 |
-
st.chat_message("user").markdown(
|
37 |
# Add user message to chat history
|
38 |
-
st.session_state.messages.append({"role": "user", "content":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
response = chain.invoke({
|
41 |
-
"
|
42 |
-
"input" : prompt
|
43 |
})
|
44 |
# Display assistant response in chat message container
|
45 |
with st.chat_message("assistant"):
|
46 |
-
st.markdown(response
|
47 |
-
|
48 |
-
st.session_state.messages.append({"role": "assistant", "content": response.content})
|
49 |
|
|
|
1 |
+
from collections import defaultdict
|
2 |
+
|
3 |
import streamlit as st
|
4 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
5 |
from langchain_core.prompts import ChatPromptTemplate
|
6 |
from langchain_community.document_loaders import WebBaseLoader
|
7 |
+
from langchain_text_splitters import CharacterTextSplitter
|
8 |
+
from langchain_community.vectorstores import FAISS
|
9 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
10 |
+
from langchain.chains import create_retrieval_chain
|
11 |
|
12 |
default_url = "https://rocm.docs.amd.com/en/latest/what-is-rocm.html"
|
13 |
st.title("URL Loader")
|
14 |
|
15 |
+
embeddings = OpenAIEmbeddings()
|
16 |
+
|
17 |
url = st.text_input("Provide URL ", default_url)
|
18 |
if "url_dict" not in st.session_state:
|
19 |
st.session_state.url_dict = {}
|
20 |
if url not in st.session_state.url_dict:
|
21 |
loader = WebBaseLoader(url)
|
22 |
+
documents = loader.load()
|
23 |
+
st.session_state.url_dict[url] = defaultdict(dict)
|
24 |
+
st.session_state.url_dict[url]['documents'] = documents
|
25 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
26 |
+
docs = text_splitter.split_documents(documents)
|
27 |
+
db = FAISS.from_documents(docs, embeddings)
|
28 |
+
print(db.index.ntotal)
|
29 |
+
url_hash = "faiss_index" + str(abs(hash(url)))
|
30 |
+
db.save_local(url_hash)
|
31 |
+
st.session_state.url_dict[url]['FAISS_db'] = url_hash
|
32 |
|
33 |
llm = ChatOpenAI(temperature=0.1)
|
34 |
prompt = ChatPromptTemplate.from_template("""
|
|
|
36 |
Context: {context}
|
37 |
Question : {input}
|
38 |
""")
|
|
|
39 |
|
40 |
# Initialize chat history
|
41 |
if "messages" not in st.session_state:
|
42 |
st.session_state.messages = []
|
43 |
|
44 |
# Display chat messages from history on app rerun
|
45 |
+
for message in st.session_state.messages[-2:]:
|
46 |
with st.chat_message(message["role"]):
|
47 |
st.markdown(message["content"])
|
48 |
|
49 |
# React to user input
|
50 |
+
if question := st.chat_input("Ask Question to the URL provided"):
|
51 |
# Display user message in chat message container
|
52 |
+
st.chat_message("user").markdown(question)
|
53 |
# Add user message to chat history
|
54 |
+
st.session_state.messages.append({"role": "user", "content": question})
|
55 |
+
|
56 |
+
db = FAISS.load_local(st.session_state.url_dict[url]['FAISS_db'],
|
57 |
+
embeddings, allow_dangerous_deserialization=True)
|
58 |
+
document_chain = create_stuff_documents_chain(
|
59 |
+
llm=llm,
|
60 |
+
prompt=prompt
|
61 |
+
)
|
62 |
+
retriever = db.as_retriever(search_kwargs={"k": 2})
|
63 |
+
chain = create_retrieval_chain(retriever, document_chain)
|
64 |
|
65 |
response = chain.invoke({
|
66 |
+
"input" : question
|
|
|
67 |
})
|
68 |
# Display assistant response in chat message container
|
69 |
with st.chat_message("assistant"):
|
70 |
+
st.markdown(response["answer"])
|
71 |
+
st.session_state.messages.append({"role": "assistant", "content": response["answer"]})
|
|
|
72 |
|
requirements.txt
CHANGED
@@ -13,6 +13,7 @@ charset-normalizer==3.3.2
|
|
13 |
click==8.1.7
|
14 |
dataclasses-json==0.6.6
|
15 |
distro==1.9.0
|
|
|
16 |
frozenlist==1.4.1
|
17 |
gitdb==4.0.11
|
18 |
GitPython==3.1.43
|
|
|
13 |
click==8.1.7
|
14 |
dataclasses-json==0.6.6
|
15 |
distro==1.9.0
|
16 |
+
faiss-cpu==1.8.0
|
17 |
frozenlist==1.4.1
|
18 |
gitdb==4.0.11
|
19 |
GitPython==3.1.43
|