qdrant_multitenancy.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import logging
  2. from typing import Optional, Tuple, List, Dict, Any
  3. from urllib.parse import urlparse
  4. import grpc
  5. from open_webui.config import (
  6. QDRANT_API_KEY,
  7. QDRANT_GRPC_PORT,
  8. QDRANT_ON_DISK,
  9. QDRANT_PREFER_GRPC,
  10. QDRANT_URI,
  11. QDRANT_COLLECTION_PREFIX,
  12. QDRANT_TIMEOUT,
  13. QDRANT_HNSW_M,
  14. )
  15. from open_webui.env import SRC_LOG_LEVELS
  16. from open_webui.retrieval.vector.main import (
  17. GetResult,
  18. SearchResult,
  19. VectorDBBase,
  20. VectorItem,
  21. )
  22. from qdrant_client import QdrantClient as Qclient
  23. from qdrant_client.http.exceptions import UnexpectedResponse
  24. from qdrant_client.http.models import PointStruct
  25. from qdrant_client.models import models
  26. NO_LIMIT = 999999999
  27. TENANT_ID_FIELD = "tenant_id"
  28. DEFAULT_DIMENSION = 384
  29. log = logging.getLogger(__name__)
  30. log.setLevel(SRC_LOG_LEVELS["RAG"])
  31. def _tenant_filter(tenant_id: str) -> models.FieldCondition:
  32. return models.FieldCondition(
  33. key=TENANT_ID_FIELD, match=models.MatchValue(value=tenant_id)
  34. )
  35. def _metadata_filter(key: str, value: Any) -> models.FieldCondition:
  36. return models.FieldCondition(
  37. key=f"metadata.{key}", match=models.MatchValue(value=value)
  38. )
  39. class QdrantClient(VectorDBBase):
  40. def __init__(self):
  41. self.collection_prefix = QDRANT_COLLECTION_PREFIX
  42. self.QDRANT_URI = QDRANT_URI
  43. self.QDRANT_API_KEY = QDRANT_API_KEY
  44. self.QDRANT_ON_DISK = QDRANT_ON_DISK
  45. self.PREFER_GRPC = QDRANT_PREFER_GRPC
  46. self.GRPC_PORT = QDRANT_GRPC_PORT
  47. self.QDRANT_TIMEOUT = QDRANT_TIMEOUT
  48. self.QDRANT_HNSW_M = QDRANT_HNSW_M
  49. if not self.QDRANT_URI:
  50. raise ValueError(
  51. "QDRANT_URI is not set. Please configure it in the environment variables."
  52. )
  53. # Unified handling for either scheme
  54. parsed = urlparse(self.QDRANT_URI)
  55. host = parsed.hostname or self.QDRANT_URI
  56. http_port = parsed.port or 6333 # default REST port
  57. self.client = (
  58. Qclient(
  59. host=host,
  60. port=http_port,
  61. grpc_port=self.GRPC_PORT,
  62. prefer_grpc=self.PREFER_GRPC,
  63. api_key=self.QDRANT_API_KEY,
  64. timeout=self.QDRANT_TIMEOUT,
  65. )
  66. if self.PREFER_GRPC
  67. else Qclient(
  68. url=self.QDRANT_URI,
  69. api_key=self.QDRANT_API_KEY,
  70. timeout=self.QDRANT_TIMEOUT,
  71. )
  72. )
  73. # Main collection types for multi-tenancy
  74. self.MEMORY_COLLECTION = f"{self.collection_prefix}_memories"
  75. self.KNOWLEDGE_COLLECTION = f"{self.collection_prefix}_knowledge"
  76. self.FILE_COLLECTION = f"{self.collection_prefix}_files"
  77. self.WEB_SEARCH_COLLECTION = f"{self.collection_prefix}_web-search"
  78. self.HASH_BASED_COLLECTION = f"{self.collection_prefix}_hash-based"
  79. def _result_to_get_result(self, points) -> GetResult:
  80. ids, documents, metadatas = [], [], []
  81. for point in points:
  82. payload = point.payload
  83. ids.append(point.id)
  84. documents.append(payload["text"])
  85. metadatas.append(payload["metadata"])
  86. return GetResult(ids=[ids], documents=[documents], metadatas=[metadatas])
  87. def _get_collection_and_tenant_id(self, collection_name: str) -> Tuple[str, str]:
  88. """
  89. Maps the traditional collection name to multi-tenant collection and tenant ID.
  90. Returns:
  91. tuple: (collection_name, tenant_id)
  92. WARNING: This mapping relies on current Open WebUI naming conventions for
  93. collection names. If Open WebUI changes how it generates collection names
  94. (e.g., "user-memory-" prefix, "file-" prefix, web search patterns, or hash
  95. formats), this mapping will break and route data to incorrect collections.
  96. POTENTIALLY CAUSING HUGE DATA CORRUPTION, DATA CONSISTENCY ISSUES AND INCORRECT
  97. DATA MAPPING INSIDE THE DATABASE.
  98. """
  99. # Check for user memory collections
  100. tenant_id = collection_name
  101. if collection_name.startswith("user-memory-"):
  102. return self.MEMORY_COLLECTION, tenant_id
  103. # Check for file collections
  104. elif collection_name.startswith("file-"):
  105. return self.FILE_COLLECTION, tenant_id
  106. # Check for web search collections
  107. elif collection_name.startswith("web-search-"):
  108. return self.WEB_SEARCH_COLLECTION, tenant_id
  109. # Handle hash-based collections (YouTube and web URLs)
  110. elif len(collection_name) == 63 and all(
  111. c in "0123456789abcdef" for c in collection_name
  112. ):
  113. return self.HASH_BASED_COLLECTION, tenant_id
  114. else:
  115. return self.KNOWLEDGE_COLLECTION, tenant_id
  116. def _create_multi_tenant_collection(
  117. self, mt_collection_name: str, dimension: int = DEFAULT_DIMENSION
  118. ):
  119. """
  120. Creates a collection with multi-tenancy configuration and payload indexes for tenant_id and metadata fields.
  121. """
  122. self.client.create_collection(
  123. collection_name=mt_collection_name,
  124. vectors_config=models.VectorParams(
  125. size=dimension,
  126. distance=models.Distance.COSINE,
  127. on_disk=self.QDRANT_ON_DISK,
  128. ),
  129. # Disable global index building due to multitenancy
  130. # For more details https://qdrant.tech/documentation/guides/multiple-partitions/#calibrate-performance
  131. hnsw_config=models.HnswConfigDiff(
  132. payload_m=self.QDRANT_HNSW_M,
  133. m=0,
  134. ),
  135. )
  136. log.info(
  137. f"Multi-tenant collection {mt_collection_name} created with dimension {dimension}!"
  138. )
  139. self.client.create_payload_index(
  140. collection_name=mt_collection_name,
  141. field_name=TENANT_ID_FIELD,
  142. field_schema=models.KeywordIndexParams(
  143. type=models.KeywordIndexType.KEYWORD,
  144. is_tenant=True,
  145. on_disk=self.QDRANT_ON_DISK,
  146. ),
  147. )
  148. for field in ("metadata.hash", "metadata.file_id"):
  149. self.client.create_payload_index(
  150. collection_name=mt_collection_name,
  151. field_name=field,
  152. field_schema=models.KeywordIndexParams(
  153. type=models.KeywordIndexType.KEYWORD,
  154. on_disk=self.QDRANT_ON_DISK,
  155. ),
  156. )
  157. def _create_points(
  158. self, items: List[VectorItem], tenant_id: str
  159. ) -> List[PointStruct]:
  160. """
  161. Create point structs from vector items with tenant ID.
  162. """
  163. return [
  164. PointStruct(
  165. id=item["id"],
  166. vector=item["vector"],
  167. payload={
  168. "text": item["text"],
  169. "metadata": item["metadata"],
  170. TENANT_ID_FIELD: tenant_id,
  171. },
  172. )
  173. for item in items
  174. ]
  175. def _ensure_collection(
  176. self, mt_collection_name: str, dimension: int = DEFAULT_DIMENSION
  177. ):
  178. """
  179. Ensure the collection exists and payload indexes are created for tenant_id and metadata fields.
  180. """
  181. if not self.client.collection_exists(collection_name=mt_collection_name):
  182. self._create_multi_tenant_collection(mt_collection_name, dimension)
  183. def has_collection(self, collection_name: str) -> bool:
  184. """
  185. Check if a logical collection exists by checking for any points with the tenant ID.
  186. """
  187. if not self.client:
  188. return False
  189. mt_collection, tenant_id = self._get_collection_and_tenant_id(collection_name)
  190. if not self.client.collection_exists(collection_name=mt_collection):
  191. return False
  192. tenant_filter = _tenant_filter(tenant_id)
  193. count_result = self.client.count(
  194. collection_name=mt_collection,
  195. count_filter=models.Filter(must=[tenant_filter]),
  196. )
  197. return count_result.count > 0
  198. def delete(
  199. self,
  200. collection_name: str,
  201. ids: Optional[List[str]] = None,
  202. filter: Optional[Dict[str, Any]] = None,
  203. ):
  204. """
  205. Delete vectors by ID or filter from a collection with tenant isolation.
  206. """
  207. if not self.client:
  208. return None
  209. mt_collection, tenant_id = self._get_collection_and_tenant_id(collection_name)
  210. if not self.client.collection_exists(collection_name=mt_collection):
  211. log.debug(f"Collection {mt_collection} doesn't exist, nothing to delete")
  212. return None
  213. must_conditions = [_tenant_filter(tenant_id)]
  214. should_conditions = []
  215. if ids:
  216. should_conditions = [_metadata_filter("id", id_value) for id_value in ids]
  217. elif filter:
  218. must_conditions += [_metadata_filter(k, v) for k, v in filter.items()]
  219. return self.client.delete(
  220. collection_name=mt_collection,
  221. points_selector=models.FilterSelector(
  222. filter=models.Filter(must=must_conditions, should=should_conditions)
  223. ),
  224. )
  225. def search(
  226. self, collection_name: str, vectors: List[List[float | int]], limit: int
  227. ) -> Optional[SearchResult]:
  228. """
  229. Search for the nearest neighbor items based on the vectors with tenant isolation.
  230. """
  231. if not self.client or not vectors:
  232. return None
  233. mt_collection, tenant_id = self._get_collection_and_tenant_id(collection_name)
  234. if not self.client.collection_exists(collection_name=mt_collection):
  235. log.debug(f"Collection {mt_collection} doesn't exist, search returns None")
  236. return None
  237. tenant_filter = _tenant_filter(tenant_id)
  238. query_response = self.client.query_points(
  239. collection_name=mt_collection,
  240. query=vectors[0],
  241. limit=limit,
  242. query_filter=models.Filter(must=[tenant_filter]),
  243. )
  244. get_result = self._result_to_get_result(query_response.points)
  245. return SearchResult(
  246. ids=get_result.ids,
  247. documents=get_result.documents,
  248. metadatas=get_result.metadatas,
  249. distances=[[(point.score + 1.0) / 2.0 for point in query_response.points]],
  250. )
  251. def query(
  252. self, collection_name: str, filter: Dict[str, Any], limit: Optional[int] = None
  253. ):
  254. """
  255. Query points with filters and tenant isolation.
  256. """
  257. if not self.client:
  258. return None
  259. mt_collection, tenant_id = self._get_collection_and_tenant_id(collection_name)
  260. if not self.client.collection_exists(collection_name=mt_collection):
  261. log.debug(f"Collection {mt_collection} doesn't exist, query returns None")
  262. return None
  263. if limit is None:
  264. limit = NO_LIMIT
  265. tenant_filter = _tenant_filter(tenant_id)
  266. field_conditions = [_metadata_filter(k, v) for k, v in filter.items()]
  267. combined_filter = models.Filter(must=[tenant_filter, *field_conditions])
  268. points = self.client.scroll(
  269. collection_name=mt_collection,
  270. scroll_filter=combined_filter,
  271. limit=limit,
  272. )
  273. return self._result_to_get_result(points[0])
  274. def get(self, collection_name: str) -> Optional[GetResult]:
  275. """
  276. Get all items in a collection with tenant isolation.
  277. """
  278. if not self.client:
  279. return None
  280. mt_collection, tenant_id = self._get_collection_and_tenant_id(collection_name)
  281. if not self.client.collection_exists(collection_name=mt_collection):
  282. log.debug(f"Collection {mt_collection} doesn't exist, get returns None")
  283. return None
  284. tenant_filter = _tenant_filter(tenant_id)
  285. points = self.client.scroll(
  286. collection_name=mt_collection,
  287. scroll_filter=models.Filter(must=[tenant_filter]),
  288. limit=NO_LIMIT,
  289. )
  290. return self._result_to_get_result(points[0])
  291. def upsert(self, collection_name: str, items: List[VectorItem]):
  292. """
  293. Upsert items with tenant ID.
  294. """
  295. if not self.client or not items:
  296. return None
  297. mt_collection, tenant_id = self._get_collection_and_tenant_id(collection_name)
  298. dimension = len(items[0]["vector"])
  299. self._ensure_collection(mt_collection, dimension)
  300. points = self._create_points(items, tenant_id)
  301. self.client.upload_points(mt_collection, points)
  302. return None
  303. def insert(self, collection_name: str, items: List[VectorItem]):
  304. """
  305. Insert items with tenant ID.
  306. """
  307. return self.upsert(collection_name, items)
  308. def reset(self):
  309. """
  310. Reset the database by deleting all collections.
  311. """
  312. if not self.client:
  313. return None
  314. for collection in self.client.get_collections().collections:
  315. if collection.name.startswith(self.collection_prefix):
  316. self.client.delete_collection(collection_name=collection.name)
  317. def delete_collection(self, collection_name: str):
  318. """
  319. Delete a collection.
  320. """
  321. if not self.client:
  322. return None
  323. mt_collection, tenant_id = self._get_collection_and_tenant_id(collection_name)
  324. if not self.client.collection_exists(collection_name=mt_collection):
  325. log.debug(f"Collection {mt_collection} doesn't exist, nothing to delete")
  326. return None
  327. self.client.delete(
  328. collection_name=mt_collection,
  329. points_selector=models.FilterSelector(
  330. filter=models.Filter(must=[_tenant_filter(tenant_id)])
  331. ),
  332. )