Added Qdrant support
Browse files- app.py +73 -22
- requirements.txt +2 -1
app.py
CHANGED
@@ -7,12 +7,15 @@ from langchain_community.document_loaders import TextLoader
|
|
7 |
from langchain_community.document_loaders import PyMuPDFLoader
|
8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
9 |
from langchain_community.vectorstores import FAISS
|
|
|
|
|
10 |
from langchain_huggingface import HuggingFaceEndpointEmbeddings
|
11 |
from langchain_core.prompts import PromptTemplate
|
12 |
from langchain.schema.output_parser import StrOutputParser
|
13 |
from langchain.schema.runnable import RunnablePassthrough
|
14 |
from langchain.schema.runnable.config import RunnableConfig
|
15 |
from langchain.globals import set_debug
|
|
|
16 |
|
17 |
set_debug(False)
|
18 |
|
@@ -56,33 +59,80 @@ hf_embeddings = HuggingFaceEndpointEmbeddings(
|
|
56 |
task="feature-extraction",
|
57 |
huggingfacehub_api_token=HF_TOKEN,
|
58 |
)
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
hf_retriever = vectorstore.as_retriever(search_kwargs={"k": FAISS_MAX_FETCH_SIZE, "fetch_k": FAISS_MAX_FETCH_SIZE})
|
72 |
-
print("Loaded Vectorstore at " + VECTOR_STORE_DIR)
|
73 |
else:
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
for i in range(0, len(split_documents),
|
79 |
if i==0:
|
80 |
-
vectorstore =
|
|
|
|
|
|
|
|
|
|
|
81 |
continue
|
82 |
-
vectorstore.add_documents(split_documents[i:i+
|
83 |
-
|
|
|
84 |
|
85 |
-
hf_retriever = vectorstore.as_retriever(search_kwargs={"k":
|
86 |
|
87 |
# -- AUGMENTED -- #
|
88 |
"""
|
@@ -111,6 +161,7 @@ rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
|
111 |
"""
|
112 |
1. Create a HuggingFaceEndpoint for the LLM
|
113 |
"""
|
|
|
114 |
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
|
115 |
hf_llm = HuggingFaceEndpoint(
|
116 |
endpoint_url=HF_LLM_ENDPOINT,
|
|
|
7 |
from langchain_community.document_loaders import PyMuPDFLoader
|
8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
9 |
from langchain_community.vectorstores import FAISS
|
10 |
+
from langchain_community.vectorstores import Qdrant
|
11 |
+
from langchain_openai import ChatOpenAI
|
12 |
from langchain_huggingface import HuggingFaceEndpointEmbeddings
|
13 |
from langchain_core.prompts import PromptTemplate
|
14 |
from langchain.schema.output_parser import StrOutputParser
|
15 |
from langchain.schema.runnable import RunnablePassthrough
|
16 |
from langchain.schema.runnable.config import RunnableConfig
|
17 |
from langchain.globals import set_debug
|
18 |
+
from langchain_core.messages.ai import AIMessageChunk
|
19 |
|
20 |
set_debug(False)
|
21 |
|
|
|
59 |
task="feature-extraction",
|
60 |
huggingfacehub_api_token=HF_TOKEN,
|
61 |
)
|
62 |
+
|
63 |
+
# Step 6: Create a custom retriever
|
64 |
+
class CustomQdrantRetriever:
|
65 |
+
def __init__(self, vectorstore, top_k=5):
|
66 |
+
self.vectorstore = vectorstore
|
67 |
+
self.top_k = top_k
|
68 |
+
|
69 |
+
def __call__(self, query):
|
70 |
+
embedded_query = self.vectorstore.embedding_function(query)
|
71 |
+
search_result = vectorstore.search(
|
72 |
+
# collection_name=collection_name,
|
73 |
+
query_vector=embedded_query,
|
74 |
+
limit=self.top_k
|
75 |
+
)
|
76 |
+
documents = [
|
77 |
+
{"page_content": hit.payload["text"], "metadata": hit.payload}
|
78 |
+
for hit in search_result
|
79 |
+
]
|
80 |
+
return documents
|
81 |
+
|
82 |
+
FAISS_VECTOR_STORE = "FAISS"
|
83 |
+
QDRANT_VECTOR_STORE = "QDRANT"
|
84 |
+
|
85 |
+
VECTOR_STORE = QDRANT_VECTOR_STORE
|
86 |
+
|
87 |
+
hf_retriever = ""
|
88 |
+
|
89 |
+
if VECTOR_STORE == FAISS_VECTOR_STORE:
|
90 |
+
DATA_DIR = "./data"
|
91 |
+
VECTOR_STORE_DIR = os.path.join(DATA_DIR, "vectorstore")
|
92 |
+
VECTOR_STORE_PATH = os.path.join(VECTOR_STORE_DIR, "index.faiss")
|
93 |
+
|
94 |
+
FAISS_MAX_FETCH_SIZE = 2
|
95 |
+
FAISS_MAX_BATCH_SIZE = 32
|
96 |
+
if os.path.exists(VECTOR_STORE_PATH):
|
97 |
+
vectorstore = FAISS.load_local(
|
98 |
+
VECTOR_STORE_DIR,
|
99 |
+
hf_embeddings,
|
100 |
+
allow_dangerous_deserialization=True # this is necessary to load the vectorstore from disk as it's stored as a `.pkl` file.
|
101 |
+
)
|
102 |
+
hf_retriever = vectorstore.as_retriever(search_kwargs={"k": FAISS_MAX_FETCH_SIZE, "fetch_k": FAISS_MAX_FETCH_SIZE})
|
103 |
+
print("Loaded Vectorstore at " + VECTOR_STORE_DIR)
|
104 |
+
else:
|
105 |
+
print("Indexing Files")
|
106 |
+
os.makedirs(VECTOR_STORE_DIR, exist_ok=True)
|
107 |
+
### 4. INDEX FILES
|
108 |
+
### NOTE: REMEMBER TO BATCH THE DOCUMENTS WITH MAXIMUM BATCH SIZE = 32
|
109 |
+
for i in range(0, len(split_documents), FAISS_MAX_BATCH_SIZE):
|
110 |
+
if i==0:
|
111 |
+
vectorstore = FAISS.from_documents(split_documents[i:i+FAISS_MAX_BATCH_SIZE], hf_embeddings)
|
112 |
+
continue
|
113 |
+
vectorstore.add_documents(split_documents[i:i+FAISS_MAX_BATCH_SIZE])
|
114 |
+
vectorstore.save_local(VECTOR_STORE_DIR)
|
115 |
+
|
116 |
hf_retriever = vectorstore.as_retriever(search_kwargs={"k": FAISS_MAX_FETCH_SIZE, "fetch_k": FAISS_MAX_FETCH_SIZE})
|
|
|
117 |
else:
|
118 |
+
QDRANT_MAX_FETCH_SIZE = 2
|
119 |
+
QDRANT_MAX_BATCH_SIZE = 32
|
120 |
+
|
121 |
+
vectorstore = ""
|
122 |
+
for i in range(0, len(split_documents), QDRANT_MAX_BATCH_SIZE):
|
123 |
if i==0:
|
124 |
+
vectorstore = Qdrant.from_documents(
|
125 |
+
split_documents[i:i+QDRANT_MAX_BATCH_SIZE],
|
126 |
+
hf_embeddings,
|
127 |
+
location=":memory:",
|
128 |
+
collection_name="10Q_ABNB"
|
129 |
+
)
|
130 |
continue
|
131 |
+
vectorstore.add_documents(split_documents[i:i+QDRANT_MAX_BATCH_SIZE])
|
132 |
+
|
133 |
+
# hf_retriever = CustomQdrantRetriever(vectorstore=vectorstore, top_k=QDRANT_MAX_FETCH_SIZE)
|
134 |
|
135 |
+
hf_retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
|
136 |
|
137 |
# -- AUGMENTED -- #
|
138 |
"""
|
|
|
161 |
"""
|
162 |
1. Create a HuggingFaceEndpoint for the LLM
|
163 |
"""
|
164 |
+
|
165 |
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
|
166 |
hf_llm = HuggingFaceEndpoint(
|
167 |
endpoint_url=HF_LLM_ENDPOINT,
|
requirements.txt
CHANGED
@@ -6,4 +6,5 @@ langchain_huggingface==0.0.3
|
|
6 |
langchain_text_splitters==0.2.1
|
7 |
python-dotenv==1.0.1
|
8 |
faiss-cpu
|
9 |
-
pymupdf
|
|
|
|
6 |
langchain_text_splitters==0.2.1
|
7 |
python-dotenv==1.0.1
|
8 |
faiss-cpu
|
9 |
+
pymupdf
|
10 |
+
qdrant-client
|