Spaces:
Sleeping
Sleeping
File size: 5,516 Bytes
45e7ae0 | 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | import os
import uuid
import httpx
import logging
from dotenv import load_dotenv
from qdrant_client import QdrantClient
from qdrant_client.models import PointStruct, Filter, FieldCondition, MatchValue
load_dotenv()
logger = logging.getLogger("uvicorn.error")
COLLECTION_NAME = "docbrain_documents"
JINA_API_KEY = os.getenv("JINA_API_KEY")
QDRANT_URL = os.getenv("QDRANT_URL")
QDRANT_API_KEY = os.getenv("QDRANT_API_KEY")
if not JINA_API_KEY:
raise ValueError("JINA_API_KEY is missing in .env file")
if not QDRANT_URL:
raise ValueError("QDRANT_URL is missing in .env file")
if not QDRANT_API_KEY:
raise ValueError("QDRANT_API_KEY is missing in .env file")
client = QdrantClient(
url=QDRANT_URL,
api_key=QDRANT_API_KEY,
check_compatibility=False
)
def get_embedding(text: str, task: str = "retrieval.query") -> list[float]:
response = httpx.post(
"https://api.jina.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {JINA_API_KEY}",
"Content-Type": "application/json",
},
json={
"model": "jina-embeddings-v3",
"task": task,
"input": [text],
},
timeout=30,
)
response.raise_for_status()
return response.json()["data"][0]["embedding"]
def get_embeddings_batch(texts: list[str], task: str = "retrieval.passage"):
response = httpx.post(
"https://api.jina.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {JINA_API_KEY}",
"Content-Type": "application/json",
},
json={
"model": "jina-embeddings-v3",
"task": task,
"input": texts,
},
timeout=30,
)
response.raise_for_status()
return [item["embedding"] for item in response.json()["data"]]
def store_chunks(chunks, filename, user_id, document_id):
points = []
embeddings = get_embeddings_batch(
chunks,
task="retrieval.passage"
)
for index, chunk in enumerate(chunks):
points.append(
PointStruct(
id=str(uuid.uuid4()),
vector=embeddings[index],
payload={
"text": chunk,
"source": filename,
"user_id": user_id,
"document_id": document_id,
"chunk_index": index,
},
)
)
client.upsert(
collection_name=COLLECTION_NAME,
points=points
)
return {
"message": "Chunks stored successfully",
"total_chunks": len(chunks)
}
def search_chunks(query: str, k: int = 3, user_id: str | None = None, document_id: str | None = None):
query_vector = get_embedding(query, task="retrieval.query")
q_filter = Filter(
must=[
FieldCondition(key="user_id", match=MatchValue(value=user_id)),
FieldCondition(key="document_id", match=MatchValue(value=document_id)),
]
)
results = client.query_points(
collection_name=COLLECTION_NAME,
query=query_vector,
query_filter=q_filter,
limit=k
)
return [
{
"page_content": point.payload.get("text", ""),
"metadata": {
"source": point.payload.get("source"),
"user_id": point.payload.get("user_id"),
"document_id": point.payload.get("document_id"),
"chunk_index": point.payload.get("chunk_index"),
"score": point.score,
}
}
for point in results.points
]
def get_document_chunks(
user_id: str,
document_id: str,
limit: int = 6,
fetch_limit: int = 128,
):
q_filter = Filter(
must=[
FieldCondition(key="user_id", match=MatchValue(value=user_id)),
FieldCondition(key="document_id", match=MatchValue(value=document_id)),
]
)
points, _ = client.scroll(
collection_name=COLLECTION_NAME,
scroll_filter=q_filter,
with_payload=True,
with_vectors=False,
limit=fetch_limit,
)
sorted_points = sorted(
points,
key=lambda point: point.payload.get("chunk_index", 0),
)
return [
{
"page_content": point.payload.get("text", ""),
"metadata": {
"source": point.payload.get("source"),
"user_id": point.payload.get("user_id"),
"document_id": point.payload.get("document_id"),
"chunk_index": point.payload.get("chunk_index"),
"score": None,
},
}
for point in sorted_points[:limit]
]
def delete_document_vectors(user_id: str, document_id: str):
q_filter = Filter(
must=[
FieldCondition(key="user_id", match=MatchValue(value=user_id)),
FieldCondition(key="document_id", match=MatchValue(value=document_id)),
]
)
try:
client.delete(
collection_name=COLLECTION_NAME,
points_selector=q_filter
)
logger.info(
"Vectors deleted successfully for document %s (user %s)",
document_id,
user_id,
)
return True
except Exception as exc:
logger.warning(
"Failed to delete vectors for document %s (user %s): %s",
document_id,
user_id,
exc,
)
return False
|