Spaces:
Running
Running
File size: 4,404 Bytes
e64b016 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import chromadb
from chromadb.config import Settings
from sentence_transformers import SentenceTransformer
from typing import List, Dict, Any
import uuid
from pathlib import Path
class RAGEngine:
"""RAG engine using ChromaDB for vector storage and retrieval"""
def __init__(self, persist_directory: str = "data/chroma_db"):
"""Initialize RAG engine with ChromaDB"""
Path(persist_directory).mkdir(parents=True, exist_ok=True)
# Initialize ChromaDB client
self.client = chromadb.PersistentClient(path=persist_directory)
# Get or create collection
self.collection = self.client.get_or_create_collection(
name="documents",
metadata={"hnsw:space": "cosine"}
)
# Initialize embedding model
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
async def add_document(
self,
text: str,
metadata: Dict[str, Any] = None
) -> str:
"""
Add document to RAG index
Args:
text: Document text
metadata: Document metadata
Returns:
Document ID
"""
doc_id = str(uuid.uuid4())
# Generate embedding
embedding = self.embedding_model.encode(text).tolist()
# Add to collection
self.collection.add(
ids=[doc_id],
embeddings=[embedding],
documents=[text],
metadatas=[metadata or {}]
)
return doc_id
async def add_documents(
self,
texts: List[str],
metadatas: List[Dict[str, Any]] = None
) -> List[str]:
"""Add multiple documents at once"""
doc_ids = [str(uuid.uuid4()) for _ in texts]
# Generate embeddings
embeddings = self.embedding_model.encode(texts).tolist()
# Add to collection
self.collection.add(
ids=doc_ids,
embeddings=embeddings,
documents=texts,
metadatas=metadatas or [{} for _ in texts]
)
return doc_ids
async def search(
self,
query: str,
k: int = 5,
filter_metadata: Dict[str, Any] = None
) -> List[Dict[str, Any]]:
"""
Search for relevant documents
Args:
query: Search query
k: Number of results
filter_metadata: Metadata filters
Returns:
List of matching documents with scores
"""
# Generate query embedding
query_embedding = self.embedding_model.encode(query).tolist()
# Search
results = self.collection.query(
query_embeddings=[query_embedding],
n_results=k,
where=filter_metadata
)
# Format results
documents = []
if results['documents'] and results['documents'][0]:
for i, doc in enumerate(results['documents'][0]):
documents.append({
'id': results['ids'][0][i],
'text': doc,
'metadata': results['metadatas'][0][i] if results['metadatas'] else {},
'distance': results['distances'][0][i] if results['distances'] else 0
})
return documents
async def get_document(self, doc_id: str) -> Dict[str, Any]:
"""Get document by ID"""
result = self.collection.get(ids=[doc_id])
if result['documents']:
return {
'id': doc_id,
'text': result['documents'][0],
'metadata': result['metadatas'][0] if result['metadatas'] else {}
}
return None
async def delete_document(self, doc_id: str):
"""Delete document by ID"""
self.collection.delete(ids=[doc_id])
async def count_documents(self) -> int:
"""Get total number of documents"""
return self.collection.count()
async def clear_all(self):
"""Clear all documents"""
self.client.delete_collection(name="documents")
self.collection = self.client.get_or_create_collection(
name="documents",
metadata={"hnsw:space": "cosine"}
)
|