Spaces:
Paused
Paused
Update rag_engine.py
Browse files- rag_engine.py +16 -6
rag_engine.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import glob
|
| 3 |
from langchain_community.document_loaders import DirectoryLoader, TextLoader, PyPDFLoader
|
| 4 |
-
from
|
| 5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
-
from qdrant_client import QdrantClient
|
| 8 |
|
| 9 |
class RAGEngine:
|
| 10 |
def __init__(self, knowledge_base_dir="./knowledge_base"):
|
|
@@ -41,13 +41,23 @@ class RAGEngine:
|
|
| 41 |
|
| 42 |
# Check if collection exists/is empty and build if needed
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
self._build_index()
|
| 47 |
else:
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
-
print(f"⚠️ Collection check failed
|
|
|
|
| 51 |
self._build_index()
|
| 52 |
|
| 53 |
def _build_index(self):
|
|
|
|
| 1 |
import os
|
| 2 |
import glob
|
| 3 |
from langchain_community.document_loaders import DirectoryLoader, TextLoader, PyPDFLoader
|
| 4 |
+
from langchain_qdrant import Qdrant
|
| 5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
+
from qdrant_client import QdrantClient, models
|
| 8 |
|
| 9 |
class RAGEngine:
|
| 10 |
def __init__(self, knowledge_base_dir="./knowledge_base"):
|
|
|
|
| 41 |
|
| 42 |
# Check if collection exists/is empty and build if needed
|
| 43 |
try:
|
| 44 |
+
if not self.client.collection_exists(self.collection_name):
|
| 45 |
+
print(f"⚠️ Collection '{self.collection_name}' not found. Creating...")
|
| 46 |
+
self.client.create_collection(
|
| 47 |
+
collection_name=self.collection_name,
|
| 48 |
+
vectors_config=models.VectorParams(size=384, distance=models.Distance.COSINE)
|
| 49 |
+
)
|
| 50 |
+
print(f"✅ Collection '{self.collection_name}' created!")
|
| 51 |
self._build_index()
|
| 52 |
else:
|
| 53 |
+
count = self.client.count(collection_name=self.collection_name).count
|
| 54 |
+
if count == 0:
|
| 55 |
+
self._build_index()
|
| 56 |
+
else:
|
| 57 |
+
print(f"✅ Qdrant Collection '{self.collection_name}' ready with {count} vectors.")
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f"⚠️ Collection check/creation failed: {e}")
|
| 60 |
+
# Try to build anyway, maybe wrapper handles it
|
| 61 |
self._build_index()
|
| 62 |
|
| 63 |
def _build_index(self):
|