您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import logging
  17. import re
  18. from collections import OrderedDict
  19. from dataclasses import dataclass
  20. from rag.settings import TAG_FLD, PAGERANK_FLD
  21. from rag.utils import rmSpace, get_float
  22. from rag.nlp import rag_tokenizer, query
  23. import numpy as np
  24. from rag.utils.doc_store_conn import DocStoreConnection, MatchDenseExpr, FusionExpr, OrderByExpr
  25. def index_name(uid): return f"ragflow_{uid}"
  26. class Dealer:
  27. def __init__(self, dataStore: DocStoreConnection):
  28. self.qryr = query.FulltextQueryer()
  29. self.dataStore = dataStore
  30. @dataclass
  31. class SearchResult:
  32. total: int
  33. ids: list[str]
  34. query_vector: list[float] | None = None
  35. field: dict | None = None
  36. highlight: dict | None = None
  37. aggregation: list | dict | None = None
  38. keywords: list[str] | None = None
  39. group_docs: list[list] | None = None
  40. def get_vector(self, txt, emb_mdl, topk=10, similarity=0.1):
  41. qv, _ = emb_mdl.encode_queries(txt)
  42. shape = np.array(qv).shape
  43. if len(shape) > 1:
  44. raise Exception(
  45. f"Dealer.get_vector returned array's shape {shape} doesn't match expectation(exact one dimension).")
  46. embedding_data = [get_float(v) for v in qv]
  47. vector_column_name = f"q_{len(embedding_data)}_vec"
  48. return MatchDenseExpr(vector_column_name, embedding_data, 'float', 'cosine', topk, {"similarity": similarity})
  49. def get_filters(self, req):
  50. condition = dict()
  51. for key, field in {"kb_ids": "kb_id", "doc_ids": "doc_id"}.items():
  52. if key in req and req[key] is not None:
  53. condition[field] = req[key]
  54. # TODO(yzc): `available_int` is nullable however infinity doesn't support nullable columns.
  55. for key in ["knowledge_graph_kwd", "available_int", "entity_kwd", "from_entity_kwd", "to_entity_kwd", "removed_kwd"]:
  56. if key in req and req[key] is not None:
  57. condition[key] = req[key]
  58. return condition
  59. def search(self, req, idx_names: str | list[str],
  60. kb_ids: list[str],
  61. emb_mdl=None,
  62. highlight=False,
  63. rank_feature: dict | None = None
  64. ):
  65. filters = self.get_filters(req)
  66. orderBy = OrderByExpr()
  67. pg = int(req.get("page", 1)) - 1
  68. topk = int(req.get("topk", 1024))
  69. ps = int(req.get("size", topk))
  70. offset, limit = pg * ps, ps
  71. src = req.get("fields",
  72. ["docnm_kwd", "content_ltks", "kb_id", "img_id", "title_tks", "important_kwd", "position_int",
  73. "doc_id", "page_num_int", "top_int", "create_timestamp_flt", "knowledge_graph_kwd",
  74. "question_kwd", "question_tks",
  75. "available_int", "content_with_weight", PAGERANK_FLD, TAG_FLD])
  76. kwds = set([])
  77. qst = req.get("question", "")
  78. q_vec = []
  79. if not qst:
  80. if req.get("sort"):
  81. orderBy.asc("page_num_int")
  82. orderBy.asc("top_int")
  83. orderBy.desc("create_timestamp_flt")
  84. res = self.dataStore.search(src, [], filters, [], orderBy, offset, limit, idx_names, kb_ids)
  85. total = self.dataStore.getTotal(res)
  86. logging.debug("Dealer.search TOTAL: {}".format(total))
  87. else:
  88. highlightFields = ["content_ltks", "title_tks"] if highlight else []
  89. matchText, keywords = self.qryr.question(qst, min_match=0.3)
  90. if emb_mdl is None:
  91. matchExprs = [matchText]
  92. res = self.dataStore.search(src, highlightFields, filters, matchExprs, orderBy, offset, limit,
  93. idx_names, kb_ids, rank_feature=rank_feature)
  94. total = self.dataStore.getTotal(res)
  95. logging.debug("Dealer.search TOTAL: {}".format(total))
  96. else:
  97. matchDense = self.get_vector(qst, emb_mdl, topk, req.get("similarity", 0.1))
  98. q_vec = matchDense.embedding_data
  99. src.append(f"q_{len(q_vec)}_vec")
  100. fusionExpr = FusionExpr("weighted_sum", topk, {"weights": "0.05, 0.95"})
  101. matchExprs = [matchText, matchDense, fusionExpr]
  102. res = self.dataStore.search(src, highlightFields, filters, matchExprs, orderBy, offset, limit,
  103. idx_names, kb_ids, rank_feature=rank_feature)
  104. total = self.dataStore.getTotal(res)
  105. logging.debug("Dealer.search TOTAL: {}".format(total))
  106. # If result is empty, try again with lower min_match
  107. if total == 0:
  108. if filters.get("doc_id"):
  109. res = self.dataStore.search(src, [], filters, [], orderBy, offset, limit, idx_names, kb_ids)
  110. total = self.dataStore.getTotal(res)
  111. else:
  112. matchText, _ = self.qryr.question(qst, min_match=0.1)
  113. filters.pop("doc_id", None)
  114. matchDense.extra_options["similarity"] = 0.17
  115. res = self.dataStore.search(src, highlightFields, filters, [matchText, matchDense, fusionExpr],
  116. orderBy, offset, limit, idx_names, kb_ids, rank_feature=rank_feature)
  117. total = self.dataStore.getTotal(res)
  118. logging.debug("Dealer.search 2 TOTAL: {}".format(total))
  119. for k in keywords:
  120. kwds.add(k)
  121. for kk in rag_tokenizer.fine_grained_tokenize(k).split():
  122. if len(kk) < 2:
  123. continue
  124. if kk in kwds:
  125. continue
  126. kwds.add(kk)
  127. logging.debug(f"TOTAL: {total}")
  128. ids = self.dataStore.getChunkIds(res)
  129. keywords = list(kwds)
  130. highlight = self.dataStore.getHighlight(res, keywords, "content_with_weight")
  131. aggs = self.dataStore.getAggregation(res, "docnm_kwd")
  132. return self.SearchResult(
  133. total=total,
  134. ids=ids,
  135. query_vector=q_vec,
  136. aggregation=aggs,
  137. highlight=highlight,
  138. field=self.dataStore.getFields(res, src),
  139. keywords=keywords
  140. )
  141. @staticmethod
  142. def trans2floats(txt):
  143. return [get_float(t) for t in txt.split("\t")]
  144. def insert_citations(self, answer, chunks, chunk_v,
  145. embd_mdl, tkweight=0.1, vtweight=0.9):
  146. assert len(chunks) == len(chunk_v)
  147. if not chunks:
  148. return answer, set([])
  149. pieces = re.split(r"(```)", answer)
  150. if len(pieces) >= 3:
  151. i = 0
  152. pieces_ = []
  153. while i < len(pieces):
  154. if pieces[i] == "```":
  155. st = i
  156. i += 1
  157. while i < len(pieces) and pieces[i] != "```":
  158. i += 1
  159. if i < len(pieces):
  160. i += 1
  161. pieces_.append("".join(pieces[st: i]) + "\n")
  162. else:
  163. pieces_.extend(
  164. re.split(
  165. r"([^\|][;。?!!\n]|[a-z][.?;!][ \n])",
  166. pieces[i]))
  167. i += 1
  168. pieces = pieces_
  169. else:
  170. pieces = re.split(r"([^\|][;。?!!\n]|[a-z][.?;!][ \n])", answer)
  171. for i in range(1, len(pieces)):
  172. if re.match(r"([^\|][;。?!!\n]|[a-z][.?;!][ \n])", pieces[i]):
  173. pieces[i - 1] += pieces[i][0]
  174. pieces[i] = pieces[i][1:]
  175. idx = []
  176. pieces_ = []
  177. for i, t in enumerate(pieces):
  178. if len(t) < 5:
  179. continue
  180. idx.append(i)
  181. pieces_.append(t)
  182. logging.debug("{} => {}".format(answer, pieces_))
  183. if not pieces_:
  184. return answer, set([])
  185. ans_v, _ = embd_mdl.encode(pieces_)
  186. for i in range(len(chunk_v)):
  187. if len(ans_v[0]) != len(chunk_v[i]):
  188. chunk_v[i] = [0.0]*len(ans_v[0])
  189. logging.warning("The dimension of query and chunk do not match: {} vs. {}".format(len(ans_v[0]), len(chunk_v[i])))
  190. assert len(ans_v[0]) == len(chunk_v[0]), "The dimension of query and chunk do not match: {} vs. {}".format(
  191. len(ans_v[0]), len(chunk_v[0]))
  192. chunks_tks = [rag_tokenizer.tokenize(self.qryr.rmWWW(ck)).split()
  193. for ck in chunks]
  194. cites = {}
  195. thr = 0.63
  196. while thr > 0.3 and len(cites.keys()) == 0 and pieces_ and chunks_tks:
  197. for i, a in enumerate(pieces_):
  198. sim, tksim, vtsim = self.qryr.hybrid_similarity(ans_v[i],
  199. chunk_v,
  200. rag_tokenizer.tokenize(
  201. self.qryr.rmWWW(pieces_[i])).split(),
  202. chunks_tks,
  203. tkweight, vtweight)
  204. mx = np.max(sim) * 0.99
  205. logging.debug("{} SIM: {}".format(pieces_[i], mx))
  206. if mx < thr:
  207. continue
  208. cites[idx[i]] = list(
  209. set([str(ii) for ii in range(len(chunk_v)) if sim[ii] > mx]))[:4]
  210. thr *= 0.8
  211. res = ""
  212. seted = set([])
  213. for i, p in enumerate(pieces):
  214. res += p
  215. if i not in idx:
  216. continue
  217. if i not in cites:
  218. continue
  219. for c in cites[i]:
  220. assert int(c) < len(chunk_v)
  221. for c in cites[i]:
  222. if c in seted:
  223. continue
  224. res += f" ##{c}$$"
  225. seted.add(c)
  226. return res, seted
  227. def _rank_feature_scores(self, query_rfea, search_res):
  228. ## For rank feature(tag_fea) scores.
  229. rank_fea = []
  230. pageranks = []
  231. for chunk_id in search_res.ids:
  232. pageranks.append(search_res.field[chunk_id].get(PAGERANK_FLD, 0))
  233. pageranks = np.array(pageranks, dtype=float)
  234. if not query_rfea:
  235. return np.array([0 for _ in range(len(search_res.ids))]) + pageranks
  236. q_denor = np.sqrt(np.sum([s*s for t,s in query_rfea.items() if t != PAGERANK_FLD]))
  237. for i in search_res.ids:
  238. nor, denor = 0, 0
  239. if not search_res.field[i].get(TAG_FLD):
  240. rank_fea.append(0)
  241. continue
  242. for t, sc in eval(search_res.field[i].get(TAG_FLD, "{}")).items():
  243. if t in query_rfea:
  244. nor += query_rfea[t] * sc
  245. denor += sc * sc
  246. if denor == 0:
  247. rank_fea.append(0)
  248. else:
  249. rank_fea.append(nor/np.sqrt(denor)/q_denor)
  250. return np.array(rank_fea)*10. + pageranks
  251. def rerank(self, sres, query, tkweight=0.3,
  252. vtweight=0.7, cfield="content_ltks",
  253. rank_feature: dict | None = None
  254. ):
  255. _, keywords = self.qryr.question(query)
  256. vector_size = len(sres.query_vector)
  257. vector_column = f"q_{vector_size}_vec"
  258. zero_vector = [0.0] * vector_size
  259. ins_embd = []
  260. for chunk_id in sres.ids:
  261. vector = sres.field[chunk_id].get(vector_column, zero_vector)
  262. if isinstance(vector, str):
  263. vector = [get_float(v) for v in vector.split("\t")]
  264. ins_embd.append(vector)
  265. if not ins_embd:
  266. return [], [], []
  267. for i in sres.ids:
  268. if isinstance(sres.field[i].get("important_kwd", []), str):
  269. sres.field[i]["important_kwd"] = [sres.field[i]["important_kwd"]]
  270. ins_tw = []
  271. for i in sres.ids:
  272. content_ltks = list(OrderedDict.fromkeys(sres.field[i][cfield].split()))
  273. title_tks = [t for t in sres.field[i].get("title_tks", "").split() if t]
  274. question_tks = [t for t in sres.field[i].get("question_tks", "").split() if t]
  275. important_kwd = sres.field[i].get("important_kwd", [])
  276. tks = content_ltks + title_tks * 2 + important_kwd * 5 + question_tks * 6
  277. ins_tw.append(tks)
  278. ## For rank feature(tag_fea) scores.
  279. rank_fea = self._rank_feature_scores(rank_feature, sres)
  280. sim, tksim, vtsim = self.qryr.hybrid_similarity(sres.query_vector,
  281. ins_embd,
  282. keywords,
  283. ins_tw, tkweight, vtweight)
  284. return sim + rank_fea, tksim, vtsim
  285. def rerank_by_model(self, rerank_mdl, sres, query, tkweight=0.3,
  286. vtweight=0.7, cfield="content_ltks",
  287. rank_feature: dict | None = None):
  288. _, keywords = self.qryr.question(query)
  289. for i in sres.ids:
  290. if isinstance(sres.field[i].get("important_kwd", []), str):
  291. sres.field[i]["important_kwd"] = [sres.field[i]["important_kwd"]]
  292. ins_tw = []
  293. for i in sres.ids:
  294. content_ltks = sres.field[i][cfield].split()
  295. title_tks = [t for t in sres.field[i].get("title_tks", "").split() if t]
  296. important_kwd = sres.field[i].get("important_kwd", [])
  297. tks = content_ltks + title_tks + important_kwd
  298. ins_tw.append(tks)
  299. tksim = self.qryr.token_similarity(keywords, ins_tw)
  300. vtsim, _ = rerank_mdl.similarity(query, [rmSpace(" ".join(tks)) for tks in ins_tw])
  301. ## For rank feature(tag_fea) scores.
  302. rank_fea = self._rank_feature_scores(rank_feature, sres)
  303. return tkweight * (np.array(tksim)+rank_fea) + vtweight * vtsim, tksim, vtsim
  304. def hybrid_similarity(self, ans_embd, ins_embd, ans, inst):
  305. return self.qryr.hybrid_similarity(ans_embd,
  306. ins_embd,
  307. rag_tokenizer.tokenize(ans).split(),
  308. rag_tokenizer.tokenize(inst).split())
  309. def retrieval(self, question, embd_mdl, tenant_ids, kb_ids, page, page_size, similarity_threshold=0.2,
  310. vector_similarity_weight=0.3, top=1024, doc_ids=None, aggs=True,
  311. rerank_mdl=None, highlight=False,
  312. rank_feature: dict | None = {PAGERANK_FLD: 10}):
  313. ranks = {"total": 0, "chunks": [], "doc_aggs": {}}
  314. if not question:
  315. return ranks
  316. RERANK_LIMIT = 64
  317. req = {"kb_ids": kb_ids, "doc_ids": doc_ids, "page": page, "size": RERANK_LIMIT,
  318. "question": question, "vector": True, "topk": top,
  319. "similarity": similarity_threshold,
  320. "available_int": 1}
  321. if isinstance(tenant_ids, str):
  322. tenant_ids = tenant_ids.split(",")
  323. sres = self.search(req, [index_name(tid) for tid in tenant_ids],
  324. kb_ids, embd_mdl, highlight, rank_feature=rank_feature)
  325. ranks["total"] = sres.total
  326. if rerank_mdl and sres.total > 0:
  327. sim, tsim, vsim = self.rerank_by_model(rerank_mdl,
  328. sres, question, 1 - vector_similarity_weight,
  329. vector_similarity_weight,
  330. rank_feature=rank_feature)
  331. else:
  332. sim, tsim, vsim = self.rerank(
  333. sres, question, 1 - vector_similarity_weight, vector_similarity_weight,
  334. rank_feature=rank_feature)
  335. idx = np.argsort(sim * -1)[(page - 1) * page_size:page * page_size]
  336. dim = len(sres.query_vector)
  337. vector_column = f"q_{dim}_vec"
  338. zero_vector = [0.0] * dim
  339. if doc_ids:
  340. similarity_threshold = 0
  341. page_size = 30
  342. for i in idx:
  343. if sim[i] < similarity_threshold:
  344. break
  345. if len(ranks["chunks"]) >= page_size:
  346. if aggs:
  347. continue
  348. break
  349. id = sres.ids[i]
  350. chunk = sres.field[id]
  351. dnm = chunk.get("docnm_kwd", "")
  352. did = chunk.get("doc_id", "")
  353. position_int = chunk.get("position_int", [])
  354. d = {
  355. "chunk_id": id,
  356. "content_ltks": chunk["content_ltks"],
  357. "content_with_weight": chunk["content_with_weight"],
  358. "doc_id": did,
  359. "docnm_kwd": dnm,
  360. "kb_id": chunk["kb_id"],
  361. "important_kwd": chunk.get("important_kwd", []),
  362. "image_id": chunk.get("img_id", ""),
  363. "similarity": sim[i],
  364. "vector_similarity": vsim[i],
  365. "term_similarity": tsim[i],
  366. "vector": chunk.get(vector_column, zero_vector),
  367. "positions": position_int,
  368. }
  369. if highlight and sres.highlight:
  370. if id in sres.highlight:
  371. d["highlight"] = rmSpace(sres.highlight[id])
  372. else:
  373. d["highlight"] = d["content_with_weight"]
  374. ranks["chunks"].append(d)
  375. if dnm not in ranks["doc_aggs"]:
  376. ranks["doc_aggs"][dnm] = {"doc_id": did, "count": 0}
  377. ranks["doc_aggs"][dnm]["count"] += 1
  378. ranks["doc_aggs"] = [{"doc_name": k,
  379. "doc_id": v["doc_id"],
  380. "count": v["count"]} for k,
  381. v in sorted(ranks["doc_aggs"].items(),
  382. key=lambda x: x[1]["count"] * -1)]
  383. ranks["chunks"] = ranks["chunks"][:page_size]
  384. return ranks
  385. def sql_retrieval(self, sql, fetch_size=128, format="json"):
  386. tbl = self.dataStore.sql(sql, fetch_size, format)
  387. return tbl
  388. def chunk_list(self, doc_id: str, tenant_id: str,
  389. kb_ids: list[str], max_count=1024,
  390. offset=0,
  391. fields=["docnm_kwd", "content_with_weight", "img_id"]):
  392. condition = {"doc_id": doc_id}
  393. res = []
  394. bs = 128
  395. for p in range(offset, max_count, bs):
  396. es_res = self.dataStore.search(fields, [], condition, [], OrderByExpr(), p, bs, index_name(tenant_id),
  397. kb_ids)
  398. dict_chunks = self.dataStore.getFields(es_res, fields)
  399. for id, doc in dict_chunks.items():
  400. doc["id"] = id
  401. if dict_chunks:
  402. res.extend(dict_chunks.values())
  403. if len(dict_chunks.values()) < bs:
  404. break
  405. return res
  406. def all_tags(self, tenant_id: str, kb_ids: list[str], S=1000):
  407. if not self.dataStore.indexExist(index_name(tenant_id), kb_ids[0]):
  408. return []
  409. res = self.dataStore.search([], [], {}, [], OrderByExpr(), 0, 0, index_name(tenant_id), kb_ids, ["tag_kwd"])
  410. return self.dataStore.getAggregation(res, "tag_kwd")
  411. def all_tags_in_portion(self, tenant_id: str, kb_ids: list[str], S=1000):
  412. res = self.dataStore.search([], [], {}, [], OrderByExpr(), 0, 0, index_name(tenant_id), kb_ids, ["tag_kwd"])
  413. res = self.dataStore.getAggregation(res, "tag_kwd")
  414. total = np.sum([c for _, c in res])
  415. return {t: (c + 1) / (total + S) for t, c in res}
  416. def tag_content(self, tenant_id: str, kb_ids: list[str], doc, all_tags, topn_tags=3, keywords_topn=30, S=1000):
  417. idx_nm = index_name(tenant_id)
  418. match_txt = self.qryr.paragraph(doc["title_tks"] + " " + doc["content_ltks"], doc.get("important_kwd", []), keywords_topn)
  419. res = self.dataStore.search([], [], {}, [match_txt], OrderByExpr(), 0, 0, idx_nm, kb_ids, ["tag_kwd"])
  420. aggs = self.dataStore.getAggregation(res, "tag_kwd")
  421. if not aggs:
  422. return False
  423. cnt = np.sum([c for _, c in aggs])
  424. tag_fea = sorted([(a, round(0.1*(c + 1) / (cnt + S) / max(1e-6, all_tags.get(a, 0.0001)))) for a, c in aggs],
  425. key=lambda x: x[1] * -1)[:topn_tags]
  426. doc[TAG_FLD] = {a.replace(".", "_"): c for a, c in tag_fea if c > 0}
  427. return True
  428. def tag_query(self, question: str, tenant_ids: str | list[str], kb_ids: list[str], all_tags, topn_tags=3, S=1000):
  429. if isinstance(tenant_ids, str):
  430. idx_nms = index_name(tenant_ids)
  431. else:
  432. idx_nms = [index_name(tid) for tid in tenant_ids]
  433. match_txt, _ = self.qryr.question(question, min_match=0.0)
  434. res = self.dataStore.search([], [], {}, [match_txt], OrderByExpr(), 0, 0, idx_nms, kb_ids, ["tag_kwd"])
  435. aggs = self.dataStore.getAggregation(res, "tag_kwd")
  436. if not aggs:
  437. return {}
  438. cnt = np.sum([c for _, c in aggs])
  439. tag_fea = sorted([(a, round(0.1*(c + 1) / (cnt + S) / max(1e-6, all_tags.get(a, 0.0001)))) for a, c in aggs],
  440. key=lambda x: x[1] * -1)[:topn_tags]
  441. return {a.replace(".", "_"): max(1, c) for a, c in tag_fea}