Spaces:
Sleeping
Sleeping
UPDATE: Hybrid Search
Browse files- functions.py +16 -7
- requirements.txt +1 -0
functions.py
CHANGED
@@ -2,6 +2,7 @@ from langchain.retrievers.contextual_compression import ContextualCompressionRet
|
|
2 |
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
|
3 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
4 |
from langchain_qdrant import QdrantVectorStore
|
|
|
5 |
from langchain_core.prompts.chat import ChatPromptTemplate
|
6 |
from langchain_core.output_parsers import StrOutputParser
|
7 |
from langchain.retrievers import ParentDocumentRetriever
|
@@ -12,6 +13,7 @@ from langchain.storage import InMemoryStore
|
|
12 |
from langchain.docstore.document import Document
|
13 |
from langchain_huggingface import HuggingFaceEmbeddings
|
14 |
from langchain.retrievers import ContextualCompressionRetriever
|
|
|
15 |
from langchain.retrievers.document_compressors import FlashrankRerank
|
16 |
from supabase.client import create_client
|
17 |
from qdrant_client import QdrantClient
|
@@ -25,11 +27,12 @@ client = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_KEY"])
|
|
25 |
qdrantClient = QdrantClient(url=os.environ["QDRANT_URL"], api_key=os.environ["QDRANT_API_KEY"])
|
26 |
model_kwargs = {"device": "cuda"}
|
27 |
encode_kwargs = {"normalize_embeddings": True}
|
28 |
-
|
29 |
model_name = "BAAI/bge-m3",
|
30 |
model_kwargs = model_kwargs,
|
31 |
encode_kwargs = encode_kwargs
|
32 |
)
|
|
|
33 |
prompt = """
|
34 |
### Role
|
35 |
- **Primary Function**: You are an AI chatbot dedicated to assisting users with their inquiries, issues, and requests. Your goal is to deliver excellent, friendly, and efficient responses at all times. Listen attentively, understand user needs, and provide the best assistance possible or direct them to appropriate resources. If a question is unclear, ask for clarification. Always conclude your replies on a positive note.
|
@@ -116,7 +119,8 @@ def createTable(tablename: str):
|
|
116 |
|
117 |
def addDocuments(text: str, vectorstore: str):
|
118 |
try:
|
119 |
-
global
|
|
|
120 |
global store
|
121 |
parentSplitter = RecursiveCharacterTextSplitter(
|
122 |
chunk_size = 2100,
|
@@ -128,10 +132,12 @@ def addDocuments(text: str, vectorstore: str):
|
|
128 |
)
|
129 |
texts = [Document(page_content = text)]
|
130 |
vectorstore = QdrantVectorStore.from_existing_collection(
|
131 |
-
embedding =
|
|
|
132 |
collection_name=vectorstore,
|
133 |
url=os.environ["QDRANT_URL"],
|
134 |
-
api_key=os.environ["QDRANT_API_KEY"]
|
|
|
135 |
)
|
136 |
retriever = ParentDocumentRetriever(
|
137 |
vectorstore=vectorstore,
|
@@ -178,13 +184,16 @@ def trimMessages(chain_input):
|
|
178 |
def answerQuery(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192") -> str:
|
179 |
global prompt
|
180 |
global client
|
181 |
-
global
|
|
|
182 |
vectorStoreName = vectorstore
|
183 |
vectorstore = QdrantVectorStore.from_existing_collection(
|
184 |
-
embedding =
|
|
|
185 |
collection_name=vectorstore,
|
186 |
url=os.environ["QDRANT_URL"],
|
187 |
-
api_key=os.environ["QDRANT_API_KEY"]
|
|
|
188 |
)
|
189 |
retriever = ParentDocumentRetriever(
|
190 |
vectorstore=vectorstore,
|
|
|
2 |
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
|
3 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
4 |
from langchain_qdrant import QdrantVectorStore
|
5 |
+
from langchain_qdrant import RetrievalMode
|
6 |
from langchain_core.prompts.chat import ChatPromptTemplate
|
7 |
from langchain_core.output_parsers import StrOutputParser
|
8 |
from langchain.retrievers import ParentDocumentRetriever
|
|
|
13 |
from langchain.docstore.document import Document
|
14 |
from langchain_huggingface import HuggingFaceEmbeddings
|
15 |
from langchain.retrievers import ContextualCompressionRetriever
|
16 |
+
from langchain_qdrant import FastEmbedSparse
|
17 |
from langchain.retrievers.document_compressors import FlashrankRerank
|
18 |
from supabase.client import create_client
|
19 |
from qdrant_client import QdrantClient
|
|
|
27 |
qdrantClient = QdrantClient(url=os.environ["QDRANT_URL"], api_key=os.environ["QDRANT_API_KEY"])
|
28 |
model_kwargs = {"device": "cuda"}
|
29 |
encode_kwargs = {"normalize_embeddings": True}
|
30 |
+
vectorEmbeddings = HuggingFaceEmbeddings(
|
31 |
model_name = "BAAI/bge-m3",
|
32 |
model_kwargs = model_kwargs,
|
33 |
encode_kwargs = encode_kwargs
|
34 |
)
|
35 |
+
sparseEmbeddings = FastEmbedSparse(model = "Qdrant/BM25", threads = 100, parallel = 0)
|
36 |
prompt = """
|
37 |
### Role
|
38 |
- **Primary Function**: You are an AI chatbot dedicated to assisting users with their inquiries, issues, and requests. Your goal is to deliver excellent, friendly, and efficient responses at all times. Listen attentively, understand user needs, and provide the best assistance possible or direct them to appropriate resources. If a question is unclear, ask for clarification. Always conclude your replies on a positive note.
|
|
|
119 |
|
120 |
def addDocuments(text: str, vectorstore: str):
|
121 |
try:
|
122 |
+
global vectorEmbeddings
|
123 |
+
global sparseEmbeddings
|
124 |
global store
|
125 |
parentSplitter = RecursiveCharacterTextSplitter(
|
126 |
chunk_size = 2100,
|
|
|
132 |
)
|
133 |
texts = [Document(page_content = text)]
|
134 |
vectorstore = QdrantVectorStore.from_existing_collection(
|
135 |
+
embedding = vectorEmbeddings,
|
136 |
+
sparse_embedding=sparseEmbeddings,
|
137 |
collection_name=vectorstore,
|
138 |
url=os.environ["QDRANT_URL"],
|
139 |
+
api_key=os.environ["QDRANT_API_KEY"],
|
140 |
+
retrieval_mode=RetrievalMode.HYBRID
|
141 |
)
|
142 |
retriever = ParentDocumentRetriever(
|
143 |
vectorstore=vectorstore,
|
|
|
184 |
def answerQuery(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192") -> str:
|
185 |
global prompt
|
186 |
global client
|
187 |
+
global vectorEmbeddings
|
188 |
+
global sparseEmbeddings
|
189 |
vectorStoreName = vectorstore
|
190 |
vectorstore = QdrantVectorStore.from_existing_collection(
|
191 |
+
embedding = vectorEmbeddings,
|
192 |
+
sparse_embedding=sparseEmbeddings,
|
193 |
collection_name=vectorstore,
|
194 |
url=os.environ["QDRANT_URL"],
|
195 |
+
api_key=os.environ["QDRANT_API_KEY"],
|
196 |
+
retrieval_mode=RetrievalMode.HYBRID
|
197 |
)
|
198 |
retriever = ParentDocumentRetriever(
|
199 |
vectorstore=vectorstore,
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
huggingface-hub
|
2 |
fastapi
|
|
|
3 |
flashrank
|
4 |
gradio
|
5 |
langchain
|
|
|
1 |
huggingface-hub
|
2 |
fastapi
|
3 |
+
fastembed-gpu
|
4 |
flashrank
|
5 |
gradio
|
6 |
langchain
|