Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
-
from langchain.vectorstores import
|
5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
from langchain.chat_models import ChatOpenAI
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
@@ -13,8 +13,6 @@ if "messages" not in st.session_state:
|
|
13 |
st.session_state.messages = []
|
14 |
if "chain" not in st.session_state:
|
15 |
st.session_state.chain = None
|
16 |
-
if "vectorstore" not in st.session_state: # Added vectorstore to session state
|
17 |
-
st.session_state.vectorstore = None
|
18 |
|
19 |
def create_sidebar():
|
20 |
with st.sidebar:
|
@@ -25,7 +23,7 @@ def create_sidebar():
|
|
25 |
### Tools Used
|
26 |
- OpenAI
|
27 |
- LangChain
|
28 |
-
-
|
29 |
|
30 |
### Steps
|
31 |
1. Add API key
|
@@ -66,23 +64,20 @@ def process_pdfs(papers, api_key):
|
|
66 |
# Cleanup
|
67 |
os.remove(file_path)
|
68 |
|
69 |
-
# Create
|
70 |
-
|
71 |
-
documents=all_texts,
|
72 |
-
embedding=embeddings,
|
73 |
-
)
|
74 |
|
75 |
# Create chain
|
76 |
st.session_state.chain = ConversationalRetrievalChain.from_llm(
|
77 |
llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", openai_api_key=api_key),
|
78 |
-
retriever=
|
79 |
-
search_kwargs={"k": 3}
|
80 |
),
|
81 |
memory=ConversationBufferMemory(
|
82 |
memory_key="chat_history",
|
83 |
return_messages=True,
|
84 |
),
|
85 |
-
return_source_documents=True,
|
86 |
)
|
87 |
|
88 |
st.success(f"Processed {len(papers)} PDF(s) successfully!")
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
+
from langchain.vectorstores import FAISS # Changed to FAISS for in-memory storage
|
5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
from langchain.chat_models import ChatOpenAI
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
13 |
st.session_state.messages = []
|
14 |
if "chain" not in st.session_state:
|
15 |
st.session_state.chain = None
|
|
|
|
|
16 |
|
17 |
def create_sidebar():
|
18 |
with st.sidebar:
|
|
|
23 |
### Tools Used
|
24 |
- OpenAI
|
25 |
- LangChain
|
26 |
+
- FAISS
|
27 |
|
28 |
### Steps
|
29 |
1. Add API key
|
|
|
64 |
# Cleanup
|
65 |
os.remove(file_path)
|
66 |
|
67 |
+
# Create vectorstore
|
68 |
+
vectorstore = FAISS.from_documents(all_texts, embeddings)
|
|
|
|
|
|
|
69 |
|
70 |
# Create chain
|
71 |
st.session_state.chain = ConversationalRetrievalChain.from_llm(
|
72 |
llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", openai_api_key=api_key),
|
73 |
+
retriever=vectorstore.as_retriever(
|
74 |
+
search_kwargs={"k": 3}
|
75 |
),
|
76 |
memory=ConversationBufferMemory(
|
77 |
memory_key="chat_history",
|
78 |
return_messages=True,
|
79 |
),
|
80 |
+
return_source_documents=True,
|
81 |
)
|
82 |
|
83 |
st.success(f"Processed {len(papers)} PDF(s) successfully!")
|