milvus.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. from pymilvus import MilvusClient as Client
  2. from pymilvus import FieldSchema, DataType
  3. import json
  4. import logging
  5. from typing import Optional
  6. from open_webui.retrieval.vector.main import (
  7. VectorDBBase,
  8. VectorItem,
  9. SearchResult,
  10. GetResult,
  11. )
  12. from open_webui.config import (
  13. MILVUS_URI,
  14. MILVUS_DB,
  15. MILVUS_TOKEN,
  16. )
  17. from open_webui.env import SRC_LOG_LEVELS
  18. log = logging.getLogger(__name__)
  19. log.setLevel(SRC_LOG_LEVELS["RAG"])
  20. class MilvusClient(VectorDBBase):
  21. def __init__(self):
  22. self.collection_prefix = "open_webui"
  23. if MILVUS_TOKEN is None:
  24. self.client = Client(uri=MILVUS_URI, db_name=MILVUS_DB)
  25. else:
  26. self.client = Client(uri=MILVUS_URI, db_name=MILVUS_DB, token=MILVUS_TOKEN)
  27. def _result_to_get_result(self, result) -> GetResult:
  28. ids = []
  29. documents = []
  30. metadatas = []
  31. for match in result:
  32. _ids = []
  33. _documents = []
  34. _metadatas = []
  35. for item in match:
  36. _ids.append(item.get("id"))
  37. _documents.append(item.get("data", {}).get("text"))
  38. _metadatas.append(item.get("metadata"))
  39. ids.append(_ids)
  40. documents.append(_documents)
  41. metadatas.append(_metadatas)
  42. return GetResult(
  43. **{
  44. "ids": ids,
  45. "documents": documents,
  46. "metadatas": metadatas,
  47. }
  48. )
  49. def _result_to_search_result(self, result) -> SearchResult:
  50. ids = []
  51. distances = []
  52. documents = []
  53. metadatas = []
  54. for match in result:
  55. _ids = []
  56. _distances = []
  57. _documents = []
  58. _metadatas = []
  59. for item in match:
  60. _ids.append(item.get("id"))
  61. # normalize milvus score from [-1, 1] to [0, 1] range
  62. # https://milvus.io/docs/de/metric.md
  63. _dist = (item.get("distance") + 1.0) / 2.0
  64. _distances.append(_dist)
  65. _documents.append(item.get("entity", {}).get("data", {}).get("text"))
  66. _metadatas.append(item.get("entity", {}).get("metadata"))
  67. ids.append(_ids)
  68. distances.append(_distances)
  69. documents.append(_documents)
  70. metadatas.append(_metadatas)
  71. return SearchResult(
  72. **{
  73. "ids": ids,
  74. "distances": distances,
  75. "documents": documents,
  76. "metadatas": metadatas,
  77. }
  78. )
  79. def _create_collection(self, collection_name: str, dimension: int):
  80. schema = self.client.create_schema(
  81. auto_id=False,
  82. enable_dynamic_field=True,
  83. )
  84. schema.add_field(
  85. field_name="id",
  86. datatype=DataType.VARCHAR,
  87. is_primary=True,
  88. max_length=65535,
  89. )
  90. schema.add_field(
  91. field_name="vector",
  92. datatype=DataType.FLOAT_VECTOR,
  93. dim=dimension,
  94. description="vector",
  95. )
  96. schema.add_field(field_name="data", datatype=DataType.JSON, description="data")
  97. schema.add_field(
  98. field_name="metadata", datatype=DataType.JSON, description="metadata"
  99. )
  100. index_params = self.client.prepare_index_params()
  101. index_params.add_index(
  102. field_name="vector",
  103. index_type="HNSW",
  104. metric_type="COSINE",
  105. params={"M": 16, "efConstruction": 100},
  106. )
  107. self.client.create_collection(
  108. collection_name=f"{self.collection_prefix}_{collection_name}",
  109. schema=schema,
  110. index_params=index_params,
  111. )
  112. def has_collection(self, collection_name: str) -> bool:
  113. # Check if the collection exists based on the collection name.
  114. collection_name = collection_name.replace("-", "_")
  115. return self.client.has_collection(
  116. collection_name=f"{self.collection_prefix}_{collection_name}"
  117. )
  118. def delete_collection(self, collection_name: str):
  119. # Delete the collection based on the collection name.
  120. collection_name = collection_name.replace("-", "_")
  121. return self.client.drop_collection(
  122. collection_name=f"{self.collection_prefix}_{collection_name}"
  123. )
  124. def search(
  125. self, collection_name: str, vectors: list[list[float | int]], limit: int
  126. ) -> Optional[SearchResult]:
  127. # Search for the nearest neighbor items based on the vectors and return 'limit' number of results.
  128. collection_name = collection_name.replace("-", "_")
  129. result = self.client.search(
  130. collection_name=f"{self.collection_prefix}_{collection_name}",
  131. data=vectors,
  132. limit=limit,
  133. output_fields=["data", "metadata"],
  134. )
  135. return self._result_to_search_result(result)
  136. def query(self, collection_name: str, filter: dict, limit: Optional[int] = None):
  137. # Construct the filter string for querying
  138. collection_name = collection_name.replace("-", "_")
  139. if not self.has_collection(collection_name):
  140. return None
  141. filter_string = " && ".join(
  142. [
  143. f'metadata["{key}"] == {json.dumps(value)}'
  144. for key, value in filter.items()
  145. ]
  146. )
  147. max_limit = 16383 # The maximum number of records per request
  148. all_results = []
  149. if limit is None:
  150. limit = float("inf") # Use infinity as a placeholder for no limit
  151. # Initialize offset and remaining to handle pagination
  152. offset = 0
  153. remaining = limit
  154. try:
  155. # Loop until there are no more items to fetch or the desired limit is reached
  156. while remaining > 0:
  157. log.info(f"remaining: {remaining}")
  158. current_fetch = min(
  159. max_limit, remaining
  160. ) # Determine how many items to fetch in this iteration
  161. results = self.client.query(
  162. collection_name=f"{self.collection_prefix}_{collection_name}",
  163. filter=filter_string,
  164. output_fields=["*"],
  165. limit=current_fetch,
  166. offset=offset,
  167. )
  168. if not results:
  169. break
  170. all_results.extend(results)
  171. results_count = len(results)
  172. remaining -= (
  173. results_count # Decrease remaining by the number of items fetched
  174. )
  175. offset += results_count
  176. # Break the loop if the results returned are less than the requested fetch count
  177. if results_count < current_fetch:
  178. break
  179. log.debug(all_results)
  180. return self._result_to_get_result([all_results])
  181. except Exception as e:
  182. log.exception(
  183. f"Error querying collection {collection_name} with limit {limit}: {e}"
  184. )
  185. return None
  186. def get(self, collection_name: str) -> Optional[GetResult]:
  187. # Get all the items in the collection.
  188. collection_name = collection_name.replace("-", "_")
  189. result = self.client.query(
  190. collection_name=f"{self.collection_prefix}_{collection_name}",
  191. filter='id != ""',
  192. )
  193. return self._result_to_get_result([result])
  194. def insert(self, collection_name: str, items: list[VectorItem]):
  195. # Insert the items into the collection, if the collection does not exist, it will be created.
  196. collection_name = collection_name.replace("-", "_")
  197. if not self.client.has_collection(
  198. collection_name=f"{self.collection_prefix}_{collection_name}"
  199. ):
  200. self._create_collection(
  201. collection_name=collection_name, dimension=len(items[0]["vector"])
  202. )
  203. return self.client.insert(
  204. collection_name=f"{self.collection_prefix}_{collection_name}",
  205. data=[
  206. {
  207. "id": item["id"],
  208. "vector": item["vector"],
  209. "data": {"text": item["text"]},
  210. "metadata": item["metadata"],
  211. }
  212. for item in items
  213. ],
  214. )
  215. def upsert(self, collection_name: str, items: list[VectorItem]):
  216. # Update the items in the collection, if the items are not present, insert them. If the collection does not exist, it will be created.
  217. collection_name = collection_name.replace("-", "_")
  218. if not self.client.has_collection(
  219. collection_name=f"{self.collection_prefix}_{collection_name}"
  220. ):
  221. self._create_collection(
  222. collection_name=collection_name, dimension=len(items[0]["vector"])
  223. )
  224. return self.client.upsert(
  225. collection_name=f"{self.collection_prefix}_{collection_name}",
  226. data=[
  227. {
  228. "id": item["id"],
  229. "vector": item["vector"],
  230. "data": {"text": item["text"]},
  231. "metadata": item["metadata"],
  232. }
  233. for item in items
  234. ],
  235. )
  236. def delete(
  237. self,
  238. collection_name: str,
  239. ids: Optional[list[str]] = None,
  240. filter: Optional[dict] = None,
  241. ):
  242. # Delete the items from the collection based on the ids.
  243. collection_name = collection_name.replace("-", "_")
  244. if ids:
  245. return self.client.delete(
  246. collection_name=f"{self.collection_prefix}_{collection_name}",
  247. ids=ids,
  248. )
  249. elif filter:
  250. # Convert the filter dictionary to a string using JSON_CONTAINS.
  251. filter_string = " && ".join(
  252. [
  253. f'metadata["{key}"] == {json.dumps(value)}'
  254. for key, value in filter.items()
  255. ]
  256. )
  257. return self.client.delete(
  258. collection_name=f"{self.collection_prefix}_{collection_name}",
  259. filter=filter_string,
  260. )
  261. def reset(self):
  262. # Resets the database. This will delete all collections and item entries.
  263. collection_names = self.client.list_collections()
  264. for collection_name in collection_names:
  265. if collection_name.startswith(self.collection_prefix):
  266. self.client.drop_collection(collection_name=collection_name)