You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

infinity_conn.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import logging
  2. import os
  3. import re
  4. import json
  5. import time
  6. import infinity
  7. from infinity.common import ConflictType, InfinityException, SortType
  8. from infinity.index import IndexInfo, IndexType
  9. from infinity.connection_pool import ConnectionPool
  10. from infinity.errors import ErrorCode
  11. from rag import settings
  12. from rag.utils import singleton
  13. import polars as pl
  14. from polars.series.series import Series
  15. from api.utils.file_utils import get_project_base_directory
  16. from rag.utils.doc_store_conn import (
  17. DocStoreConnection,
  18. MatchExpr,
  19. MatchTextExpr,
  20. MatchDenseExpr,
  21. FusionExpr,
  22. OrderByExpr,
  23. )
  24. logger = logging.getLogger('ragflow.infinity_conn')
  25. def equivalent_condition_to_str(condition: dict) -> str:
  26. assert "_id" not in condition
  27. cond = list()
  28. for k, v in condition.items():
  29. if not isinstance(k, str) or not v:
  30. continue
  31. if isinstance(v, list):
  32. inCond = list()
  33. for item in v:
  34. if isinstance(item, str):
  35. inCond.append(f"'{item}'")
  36. else:
  37. inCond.append(str(item))
  38. if inCond:
  39. strInCond = ", ".join(inCond)
  40. strInCond = f"{k} IN ({strInCond})"
  41. cond.append(strInCond)
  42. elif isinstance(v, str):
  43. cond.append(f"{k}='{v}'")
  44. else:
  45. cond.append(f"{k}={str(v)}")
  46. return " AND ".join(cond)
  47. def concat_dataframes(df_list: list[pl.DataFrame], selectFields: list[str]) -> pl.DataFrame:
  48. """
  49. Concatenate multiple dataframes into one.
  50. """
  51. if df_list:
  52. return pl.concat(df_list)
  53. schema = dict()
  54. for fieldnm in selectFields:
  55. schema[fieldnm] = str
  56. return pl.DataFrame(schema=schema)
  57. @singleton
  58. class InfinityConnection(DocStoreConnection):
  59. def __init__(self):
  60. self.dbName = settings.INFINITY.get("db_name", "default_db")
  61. infinity_uri = settings.INFINITY["uri"]
  62. if ":" in infinity_uri:
  63. host, port = infinity_uri.split(":")
  64. infinity_uri = infinity.common.NetworkAddress(host, int(port))
  65. self.connPool = None
  66. logger.info(f"Use Infinity {infinity_uri} as the doc engine.")
  67. for _ in range(24):
  68. try:
  69. connPool = ConnectionPool(infinity_uri)
  70. inf_conn = connPool.get_conn()
  71. res = inf_conn.show_current_node()
  72. connPool.release_conn(inf_conn)
  73. self.connPool = connPool
  74. if res.error_code == ErrorCode.OK and res.server_status=="started":
  75. break
  76. logger.warn(f"Infinity status: {res.server_status}. Waiting Infinity {infinity_uri} to be healthy.")
  77. time.sleep(5)
  78. except Exception as e:
  79. logger.warning(f"{str(e)}. Waiting Infinity {infinity_uri} to be healthy.")
  80. time.sleep(5)
  81. if self.connPool is None:
  82. msg = f"Infinity {infinity_uri} didn't become healthy in 120s."
  83. logger.error(msg)
  84. raise Exception(msg)
  85. logger.info(f"Infinity {infinity_uri} is healthy.")
  86. """
  87. Database operations
  88. """
  89. def dbType(self) -> str:
  90. return "infinity"
  91. def health(self) -> dict:
  92. """
  93. Return the health status of the database.
  94. TODO: Infinity-sdk provides health() to wrap `show global variables` and `show tables`
  95. """
  96. inf_conn = self.connPool.get_conn()
  97. res = inf_conn.show_current_node()
  98. self.connPool.release_conn(inf_conn)
  99. res2 = {
  100. "type": "infinity",
  101. "status": "green" if res.error_code == 0 and res.server_status == "started" else "red",
  102. "error": res.error_msg,
  103. }
  104. return res2
  105. """
  106. Table operations
  107. """
  108. def createIdx(self, indexName: str, knowledgebaseId: str, vectorSize: int):
  109. table_name = f"{indexName}_{knowledgebaseId}"
  110. inf_conn = self.connPool.get_conn()
  111. inf_db = inf_conn.create_database(self.dbName, ConflictType.Ignore)
  112. fp_mapping = os.path.join(
  113. get_project_base_directory(), "conf", "infinity_mapping.json"
  114. )
  115. if not os.path.exists(fp_mapping):
  116. raise Exception(f"Mapping file not found at {fp_mapping}")
  117. schema = json.load(open(fp_mapping))
  118. vector_name = f"q_{vectorSize}_vec"
  119. schema[vector_name] = {"type": f"vector,{vectorSize},float"}
  120. inf_table = inf_db.create_table(
  121. table_name,
  122. schema,
  123. ConflictType.Ignore,
  124. )
  125. inf_table.create_index(
  126. "q_vec_idx",
  127. IndexInfo(
  128. vector_name,
  129. IndexType.Hnsw,
  130. {
  131. "M": "16",
  132. "ef_construction": "50",
  133. "metric": "cosine",
  134. "encode": "lvq",
  135. },
  136. ),
  137. ConflictType.Ignore,
  138. )
  139. text_suffix = ["_tks", "_ltks", "_kwd"]
  140. for field_name, field_info in schema.items():
  141. if field_info["type"] != "varchar":
  142. continue
  143. for suffix in text_suffix:
  144. if field_name.endswith(suffix):
  145. inf_table.create_index(
  146. f"text_idx_{field_name}",
  147. IndexInfo(
  148. field_name, IndexType.FullText, {"ANALYZER": "standard"}
  149. ),
  150. ConflictType.Ignore,
  151. )
  152. break
  153. self.connPool.release_conn(inf_conn)
  154. logger.info(
  155. f"INFINITY created table {table_name}, vector size {vectorSize}"
  156. )
  157. def deleteIdx(self, indexName: str, knowledgebaseId: str):
  158. table_name = f"{indexName}_{knowledgebaseId}"
  159. inf_conn = self.connPool.get_conn()
  160. db_instance = inf_conn.get_database(self.dbName)
  161. db_instance.drop_table(table_name, ConflictType.Ignore)
  162. self.connPool.release_conn(inf_conn)
  163. logger.info(f"INFINITY dropped table {table_name}")
  164. def indexExist(self, indexName: str, knowledgebaseId: str) -> bool:
  165. table_name = f"{indexName}_{knowledgebaseId}"
  166. try:
  167. inf_conn = self.connPool.get_conn()
  168. db_instance = inf_conn.get_database(self.dbName)
  169. _ = db_instance.get_table(table_name)
  170. self.connPool.release_conn(inf_conn)
  171. return True
  172. except Exception as e:
  173. logger.warning(f"INFINITY indexExist {str(e)}")
  174. return False
  175. """
  176. CRUD operations
  177. """
  178. def search(
  179. self,
  180. selectFields: list[str],
  181. highlightFields: list[str],
  182. condition: dict,
  183. matchExprs: list[MatchExpr],
  184. orderBy: OrderByExpr,
  185. offset: int,
  186. limit: int,
  187. indexNames: str | list[str],
  188. knowledgebaseIds: list[str],
  189. ) -> list[dict] | pl.DataFrame:
  190. """
  191. TODO: Infinity doesn't provide highlight
  192. """
  193. if isinstance(indexNames, str):
  194. indexNames = indexNames.split(",")
  195. assert isinstance(indexNames, list) and len(indexNames) > 0
  196. inf_conn = self.connPool.get_conn()
  197. db_instance = inf_conn.get_database(self.dbName)
  198. df_list = list()
  199. table_list = list()
  200. if "id" not in selectFields:
  201. selectFields.append("id")
  202. # Prepare expressions common to all tables
  203. filter_cond = ""
  204. filter_fulltext = ""
  205. if condition:
  206. filter_cond = equivalent_condition_to_str(condition)
  207. for matchExpr in matchExprs:
  208. if isinstance(matchExpr, MatchTextExpr):
  209. if len(filter_cond) != 0 and "filter" not in matchExpr.extra_options:
  210. matchExpr.extra_options.update({"filter": filter_cond})
  211. fields = ",".join(matchExpr.fields)
  212. filter_fulltext = (
  213. f"filter_fulltext('{fields}', '{matchExpr.matching_text}')"
  214. )
  215. if len(filter_cond) != 0:
  216. filter_fulltext = f"({filter_cond}) AND {filter_fulltext}"
  217. logger.debug(f"filter_fulltext: {filter_fulltext}")
  218. minimum_should_match = matchExpr.extra_options.get("minimum_should_match", 0.0)
  219. if isinstance(minimum_should_match, float):
  220. str_minimum_should_match = str(int(minimum_should_match * 100)) + "%"
  221. matchExpr.extra_options["minimum_should_match"] = str_minimum_should_match
  222. for k, v in matchExpr.extra_options.items():
  223. if not isinstance(v, str):
  224. matchExpr.extra_options[k] = str(v)
  225. elif isinstance(matchExpr, MatchDenseExpr):
  226. if len(filter_cond) != 0 and "filter" not in matchExpr.extra_options:
  227. matchExpr.extra_options.update({"filter": filter_fulltext})
  228. for k, v in matchExpr.extra_options.items():
  229. if not isinstance(v, str):
  230. matchExpr.extra_options[k] = str(v)
  231. order_by_expr_list = list()
  232. if orderBy.fields:
  233. for order_field in orderBy.fields:
  234. if order_field[1] == 0:
  235. order_by_expr_list.append((order_field[0], SortType.Asc))
  236. else:
  237. order_by_expr_list.append((order_field[0], SortType.Desc))
  238. # Scatter search tables and gather the results
  239. for indexName in indexNames:
  240. for knowledgebaseId in knowledgebaseIds:
  241. table_name = f"{indexName}_{knowledgebaseId}"
  242. try:
  243. table_instance = db_instance.get_table(table_name)
  244. except Exception:
  245. continue
  246. table_list.append(table_name)
  247. builder = table_instance.output(selectFields)
  248. if len(matchExprs) > 0:
  249. for matchExpr in matchExprs:
  250. if isinstance(matchExpr, MatchTextExpr):
  251. fields = ",".join(matchExpr.fields)
  252. builder = builder.match_text(
  253. fields,
  254. matchExpr.matching_text,
  255. matchExpr.topn,
  256. matchExpr.extra_options,
  257. )
  258. elif isinstance(matchExpr, MatchDenseExpr):
  259. builder = builder.match_dense(
  260. matchExpr.vector_column_name,
  261. matchExpr.embedding_data,
  262. matchExpr.embedding_data_type,
  263. matchExpr.distance_type,
  264. matchExpr.topn,
  265. matchExpr.extra_options,
  266. )
  267. elif isinstance(matchExpr, FusionExpr):
  268. builder = builder.fusion(
  269. matchExpr.method, matchExpr.topn, matchExpr.fusion_params
  270. )
  271. else:
  272. if len(filter_cond) > 0:
  273. builder.filter(filter_cond)
  274. if orderBy.fields:
  275. builder.sort(order_by_expr_list)
  276. builder.offset(offset).limit(limit)
  277. kb_res = builder.to_pl()
  278. df_list.append(kb_res)
  279. self.connPool.release_conn(inf_conn)
  280. res = concat_dataframes(df_list, selectFields)
  281. logger.debug("INFINITY search tables: " + str(table_list))
  282. return res
  283. def get(
  284. self, chunkId: str, indexName: str, knowledgebaseIds: list[str]
  285. ) -> dict | None:
  286. inf_conn = self.connPool.get_conn()
  287. db_instance = inf_conn.get_database(self.dbName)
  288. df_list = list()
  289. assert isinstance(knowledgebaseIds, list)
  290. for knowledgebaseId in knowledgebaseIds:
  291. table_name = f"{indexName}_{knowledgebaseId}"
  292. table_instance = db_instance.get_table(table_name)
  293. kb_res = table_instance.output(["*"]).filter(f"id = '{chunkId}'").to_pl()
  294. if len(kb_res) != 0 and kb_res.shape[0] > 0:
  295. df_list.append(kb_res)
  296. self.connPool.release_conn(inf_conn)
  297. res = concat_dataframes(df_list, ["id"])
  298. res_fields = self.getFields(res, res.columns)
  299. return res_fields.get(chunkId, None)
  300. def insert(
  301. self, documents: list[dict], indexName: str, knowledgebaseId: str
  302. ) -> list[str]:
  303. inf_conn = self.connPool.get_conn()
  304. db_instance = inf_conn.get_database(self.dbName)
  305. table_name = f"{indexName}_{knowledgebaseId}"
  306. try:
  307. table_instance = db_instance.get_table(table_name)
  308. except InfinityException as e:
  309. # src/common/status.cppm, kTableNotExist = 3022
  310. if e.error_code != ErrorCode.TABLE_NOT_EXIST:
  311. raise
  312. vector_size = 0
  313. patt = re.compile(r"q_(?P<vector_size>\d+)_vec")
  314. for k in documents[0].keys():
  315. m = patt.match(k)
  316. if m:
  317. vector_size = int(m.group("vector_size"))
  318. break
  319. if vector_size == 0:
  320. raise ValueError("Cannot infer vector size from documents")
  321. self.createIdx(indexName, knowledgebaseId, vector_size)
  322. table_instance = db_instance.get_table(table_name)
  323. for d in documents:
  324. assert "_id" not in d
  325. assert "id" in d
  326. for k, v in d.items():
  327. if k.endswith("_kwd") and isinstance(v, list):
  328. d[k] = " ".join(v)
  329. if k == 'kb_id':
  330. if isinstance(d[k], list):
  331. d[k] = d[k][0] # since d[k] is a list, but we need a str
  332. ids = ["'{}'".format(d["id"]) for d in documents]
  333. str_ids = ", ".join(ids)
  334. str_filter = f"id IN ({str_ids})"
  335. table_instance.delete(str_filter)
  336. # for doc in documents:
  337. # logger.info(f"insert position_list: {doc['position_list']}")
  338. # logger.info(f"InfinityConnection.insert {json.dumps(documents)}")
  339. table_instance.insert(documents)
  340. self.connPool.release_conn(inf_conn)
  341. logger.debug(f"inserted into {table_name} {str_ids}.")
  342. return []
  343. def update(
  344. self, condition: dict, newValue: dict, indexName: str, knowledgebaseId: str
  345. ) -> bool:
  346. # if 'position_list' in newValue:
  347. # logger.info(f"upsert position_list: {newValue['position_list']}")
  348. inf_conn = self.connPool.get_conn()
  349. db_instance = inf_conn.get_database(self.dbName)
  350. table_name = f"{indexName}_{knowledgebaseId}"
  351. table_instance = db_instance.get_table(table_name)
  352. filter = equivalent_condition_to_str(condition)
  353. for k, v in newValue.items():
  354. if k.endswith("_kwd") and isinstance(v, list):
  355. newValue[k] = " ".join(v)
  356. table_instance.update(filter, newValue)
  357. self.connPool.release_conn(inf_conn)
  358. return True
  359. def delete(self, condition: dict, indexName: str, knowledgebaseId: str) -> int:
  360. inf_conn = self.connPool.get_conn()
  361. db_instance = inf_conn.get_database(self.dbName)
  362. table_name = f"{indexName}_{knowledgebaseId}"
  363. filter = equivalent_condition_to_str(condition)
  364. try:
  365. table_instance = db_instance.get_table(table_name)
  366. except Exception:
  367. logger.warning(
  368. f"Skipped deleting `{filter}` from table {table_name} since the table doesn't exist."
  369. )
  370. return 0
  371. res = table_instance.delete(filter)
  372. self.connPool.release_conn(inf_conn)
  373. return res.deleted_rows
  374. """
  375. Helper functions for search result
  376. """
  377. def getTotal(self, res):
  378. return len(res)
  379. def getChunkIds(self, res):
  380. return list(res["id"])
  381. def getFields(self, res, fields: list[str]) -> list[str, dict]:
  382. res_fields = {}
  383. if not fields:
  384. return {}
  385. num_rows = len(res)
  386. column_id = res["id"]
  387. for i in range(num_rows):
  388. id = column_id[i]
  389. m = {"id": id}
  390. for fieldnm in fields:
  391. if fieldnm not in res:
  392. m[fieldnm] = None
  393. continue
  394. v = res[fieldnm][i]
  395. if isinstance(v, Series):
  396. v = list(v)
  397. elif fieldnm == "important_kwd":
  398. assert isinstance(v, str)
  399. v = v.split()
  400. else:
  401. if not isinstance(v, str):
  402. v = str(v)
  403. # if fieldnm.endswith("_tks"):
  404. # v = rmSpace(v)
  405. m[fieldnm] = v
  406. res_fields[id] = m
  407. return res_fields
  408. def getHighlight(self, res, keywords: list[str], fieldnm: str):
  409. ans = {}
  410. num_rows = len(res)
  411. column_id = res["id"]
  412. for i in range(num_rows):
  413. id = column_id[i]
  414. txt = res[fieldnm][i]
  415. txt = re.sub(r"[\r\n]", " ", txt, flags=re.IGNORECASE | re.MULTILINE)
  416. txts = []
  417. for t in re.split(r"[.?!;\n]", txt):
  418. for w in keywords:
  419. t = re.sub(
  420. r"(^|[ .?/'\"\(\)!,:;-])(%s)([ .?/'\"\(\)!,:;-])"
  421. % re.escape(w),
  422. r"\1<em>\2</em>\3",
  423. t,
  424. flags=re.IGNORECASE | re.MULTILINE,
  425. )
  426. if not re.search(
  427. r"<em>[^<>]+</em>", t, flags=re.IGNORECASE | re.MULTILINE
  428. ):
  429. continue
  430. txts.append(t)
  431. ans[id] = "...".join(txts)
  432. return ans
  433. def getAggregation(self, res, fieldnm: str):
  434. """
  435. TODO: Infinity doesn't provide aggregation
  436. """
  437. return list()
  438. """
  439. SQL
  440. """
  441. def sql(sql: str, fetch_size: int, format: str):
  442. raise NotImplementedError("Not implemented")