2023-12-25 19:05:59 +08:00
|
|
|
|
import re
|
2023-12-28 13:50:13 +08:00
|
|
|
|
from elasticsearch_dsl import Q, Search, A
|
|
|
|
|
from typing import List, Optional, Tuple, Dict, Union
|
2023-12-25 19:05:59 +08:00
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
from util import setup_logging, rmSpace
|
|
|
|
|
from nlp import huqie, query
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from sklearn.metrics.pairwise import cosine_similarity as CosineSimilarity
|
|
|
|
|
import numpy as np
|
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
|
|
|
|
|
def index_name(uid): return f"docgpt_{uid}"
|
|
|
|
|
|
2023-12-26 19:32:06 +08:00
|
|
|
|
|
2023-12-25 19:05:59 +08:00
|
|
|
|
class Dealer:
|
|
|
|
|
def __init__(self, es, emb_mdl):
|
|
|
|
|
self.qryr = query.EsQueryer(es)
|
2023-12-28 13:50:13 +08:00
|
|
|
|
self.qryr.flds = [
|
|
|
|
|
"title_tks^10",
|
|
|
|
|
"title_sm_tks^5",
|
|
|
|
|
"content_ltks^2",
|
|
|
|
|
"content_sm_ltks"]
|
2023-12-25 19:05:59 +08:00
|
|
|
|
self.es = es
|
|
|
|
|
self.emb_mdl = emb_mdl
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class SearchResult:
|
2023-12-28 13:50:13 +08:00
|
|
|
|
total: int
|
2023-12-25 19:05:59 +08:00
|
|
|
|
ids: List[str]
|
|
|
|
|
query_vector: List[float] = None
|
|
|
|
|
field: Optional[Dict] = None
|
|
|
|
|
highlight: Optional[Dict] = None
|
|
|
|
|
aggregation: Union[List, Dict, None] = None
|
|
|
|
|
keywords: Optional[List[str]] = None
|
|
|
|
|
group_docs: List[List] = None
|
|
|
|
|
|
|
|
|
|
def _vector(self, txt, sim=0.8, topk=10):
|
|
|
|
|
return {
|
|
|
|
|
"field": "q_vec",
|
|
|
|
|
"k": topk,
|
|
|
|
|
"similarity": sim,
|
|
|
|
|
"num_candidates": 1000,
|
|
|
|
|
"query_vector": self.emb_mdl.encode_queries(txt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def search(self, req, idxnm, tks_num=3):
|
|
|
|
|
keywords = []
|
|
|
|
|
qst = req.get("question", "")
|
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
bqry, keywords = self.qryr.question(qst)
|
|
|
|
|
if req.get("kb_ids"):
|
|
|
|
|
bqry.filter.append(Q("terms", kb_id=req["kb_ids"]))
|
2023-12-25 19:05:59 +08:00
|
|
|
|
bqry.filter.append(Q("exists", field="q_tks"))
|
|
|
|
|
bqry.boost = 0.05
|
|
|
|
|
print(bqry)
|
|
|
|
|
|
|
|
|
|
s = Search()
|
2023-12-28 13:50:13 +08:00
|
|
|
|
pg = int(req.get("page", 1)) - 1
|
2023-12-25 19:05:59 +08:00
|
|
|
|
ps = int(req.get("size", 1000))
|
|
|
|
|
src = req.get("field", ["docnm_kwd", "content_ltks", "kb_id",
|
|
|
|
|
"image_id", "doc_id", "q_vec"])
|
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
s = s.query(bqry)[pg * ps:(pg + 1) * ps]
|
2023-12-25 19:05:59 +08:00
|
|
|
|
s = s.highlight("content_ltks")
|
|
|
|
|
s = s.highlight("title_ltks")
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if not qst:
|
|
|
|
|
s = s.sort(
|
|
|
|
|
{"create_time": {"order": "desc", "unmapped_type": "date"}})
|
2023-12-25 19:05:59 +08:00
|
|
|
|
|
|
|
|
|
s = s.highlight_options(
|
2023-12-28 13:50:13 +08:00
|
|
|
|
fragment_size=120,
|
|
|
|
|
number_of_fragments=5,
|
|
|
|
|
boundary_scanner_locale="zh-CN",
|
|
|
|
|
boundary_scanner="SENTENCE",
|
|
|
|
|
boundary_chars=",./;:\\!(),。?:!……()——、"
|
|
|
|
|
)
|
2023-12-25 19:05:59 +08:00
|
|
|
|
s = s.to_dict()
|
|
|
|
|
q_vec = []
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if req.get("vector"):
|
2023-12-25 19:05:59 +08:00
|
|
|
|
s["knn"] = self._vector(qst, req.get("similarity", 0.4), ps)
|
|
|
|
|
s["knn"]["filter"] = bqry.to_dict()
|
|
|
|
|
del s["highlight"]
|
|
|
|
|
q_vec = s["knn"]["query_vector"]
|
2023-12-28 13:50:13 +08:00
|
|
|
|
res = self.es.search(s, idxnm=idxnm, timeout="600s", src=src)
|
2023-12-25 19:05:59 +08:00
|
|
|
|
print("TOTAL: ", self.es.getTotal(res))
|
|
|
|
|
if self.es.getTotal(res) == 0 and "knn" in s:
|
2023-12-28 13:50:13 +08:00
|
|
|
|
bqry, _ = self.qryr.question(qst, min_match="10%")
|
|
|
|
|
if req.get("kb_ids"):
|
|
|
|
|
bqry.filter.append(Q("terms", kb_id=req["kb_ids"]))
|
2023-12-25 19:05:59 +08:00
|
|
|
|
s["query"] = bqry.to_dict()
|
|
|
|
|
s["knn"]["filter"] = bqry.to_dict()
|
|
|
|
|
s["knn"]["similarity"] = 0.7
|
2023-12-28 13:50:13 +08:00
|
|
|
|
res = self.es.search(s, idxnm=idxnm, timeout="600s", src=src)
|
2023-12-25 19:05:59 +08:00
|
|
|
|
|
|
|
|
|
kwds = set([])
|
|
|
|
|
for k in keywords:
|
|
|
|
|
kwds.add(k)
|
|
|
|
|
for kk in huqie.qieqie(k).split(" "):
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if len(kk) < 2:
|
|
|
|
|
continue
|
|
|
|
|
if kk in kwds:
|
|
|
|
|
continue
|
2023-12-25 19:05:59 +08:00
|
|
|
|
kwds.add(kk)
|
|
|
|
|
|
|
|
|
|
aggs = self.getAggregation(res, "docnm_kwd")
|
|
|
|
|
|
|
|
|
|
return self.SearchResult(
|
2023-12-28 13:50:13 +08:00
|
|
|
|
total=self.es.getTotal(res),
|
|
|
|
|
ids=self.es.getDocIds(res),
|
|
|
|
|
query_vector=q_vec,
|
|
|
|
|
aggregation=aggs,
|
|
|
|
|
highlight=self.getHighlight(res),
|
|
|
|
|
field=self.getFields(res, ["docnm_kwd", "content_ltks",
|
|
|
|
|
"kb_id", "image_id", "doc_id", "q_vec"]),
|
|
|
|
|
keywords=list(kwds)
|
2023-12-25 19:05:59 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def getAggregation(self, res, g):
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if not "aggregations" in res or "aggs_" + g not in res["aggregations"]:
|
|
|
|
|
return
|
|
|
|
|
bkts = res["aggregations"]["aggs_" + g]["buckets"]
|
2023-12-25 19:05:59 +08:00
|
|
|
|
return [(b["key"], b["doc_count"]) for b in bkts]
|
|
|
|
|
|
|
|
|
|
def getHighlight(self, res):
|
|
|
|
|
def rmspace(line):
|
|
|
|
|
eng = set(list("qwertyuioplkjhgfdsazxcvbnm"))
|
|
|
|
|
r = []
|
|
|
|
|
for t in line.split(" "):
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if not t:
|
|
|
|
|
continue
|
|
|
|
|
if len(r) > 0 and len(
|
|
|
|
|
t) > 0 and r[-1][-1] in eng and t[0] in eng:
|
|
|
|
|
r.append(" ")
|
2023-12-25 19:05:59 +08:00
|
|
|
|
r.append(t)
|
|
|
|
|
r = "".join(r)
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
ans = {}
|
|
|
|
|
for d in res["hits"]["hits"]:
|
|
|
|
|
hlts = d.get("highlight")
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if not hlts:
|
|
|
|
|
continue
|
2023-12-25 19:05:59 +08:00
|
|
|
|
ans[d["_id"]] = "".join([a for a in list(hlts.items())[0][1]])
|
|
|
|
|
return ans
|
|
|
|
|
|
|
|
|
|
def getFields(self, sres, flds):
|
|
|
|
|
res = {}
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if not flds:
|
|
|
|
|
return {}
|
|
|
|
|
for d in self.es.getSource(sres):
|
|
|
|
|
m = {n: d.get(n) for n in flds if d.get(n) is not None}
|
|
|
|
|
for n, v in m.items():
|
|
|
|
|
if isinstance(v, type([])):
|
2023-12-25 19:05:59 +08:00
|
|
|
|
m[n] = "\t".join([str(vv) for vv in v])
|
|
|
|
|
continue
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if not isinstance(v, type("")):
|
|
|
|
|
m[n] = str(m[n])
|
2023-12-25 19:05:59 +08:00
|
|
|
|
m[n] = rmSpace(m[n])
|
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if m:
|
|
|
|
|
res[d["id"]] = m
|
2023-12-25 19:05:59 +08:00
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def trans2floats(txt):
|
|
|
|
|
return [float(t) for t in txt.split("\t")]
|
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
def insert_citations(self, ans, top_idx, sres,
|
|
|
|
|
vfield="q_vec", cfield="content_ltks"):
|
2023-12-25 19:05:59 +08:00
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
ins_embd = [Dealer.trans2floats(
|
|
|
|
|
sres.field[sres.ids[i]][vfield]) for i in top_idx]
|
|
|
|
|
ins_tw = [sres.field[sres.ids[i]][cfield].split(" ") for i in top_idx]
|
2023-12-25 19:05:59 +08:00
|
|
|
|
s = 0
|
|
|
|
|
e = 0
|
|
|
|
|
res = ""
|
2023-12-28 13:50:13 +08:00
|
|
|
|
|
2023-12-25 19:05:59 +08:00
|
|
|
|
def citeit():
|
|
|
|
|
nonlocal s, e, ans, res
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if not ins_embd:
|
|
|
|
|
return
|
2023-12-25 19:05:59 +08:00
|
|
|
|
embd = self.emb_mdl.encode(ans[s: e])
|
2023-12-28 13:50:13 +08:00
|
|
|
|
sim = self.qryr.hybrid_similarity(embd,
|
|
|
|
|
ins_embd,
|
2023-12-25 19:05:59 +08:00
|
|
|
|
huqie.qie(ans[s:e]).split(" "),
|
|
|
|
|
ins_tw)
|
|
|
|
|
print(ans[s: e], sim)
|
2023-12-28 13:50:13 +08:00
|
|
|
|
mx = np.max(sim) * 0.99
|
|
|
|
|
if mx < 0.55:
|
|
|
|
|
return
|
|
|
|
|
cita = list(set([top_idx[i]
|
|
|
|
|
for i in range(len(ins_embd)) if sim[i] > mx]))[:4]
|
|
|
|
|
for i in cita:
|
|
|
|
|
res += f"@?{i}?@"
|
2023-12-25 19:05:59 +08:00
|
|
|
|
|
|
|
|
|
return cita
|
|
|
|
|
|
|
|
|
|
punct = set(";。?!!")
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if not self.qryr.isChinese(ans):
|
2023-12-25 19:05:59 +08:00
|
|
|
|
punct.add("?")
|
|
|
|
|
punct.add(".")
|
|
|
|
|
while e < len(ans):
|
|
|
|
|
if e - s < 12 or ans[e] not in punct:
|
|
|
|
|
e += 1
|
|
|
|
|
continue
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if ans[e] == "." and e + \
|
|
|
|
|
1 < len(ans) and re.match(r"[0-9]", ans[e + 1]):
|
2023-12-25 19:05:59 +08:00
|
|
|
|
e += 1
|
|
|
|
|
continue
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if ans[e] == "." and e - 2 >= 0 and ans[e - 2] == "\n":
|
2023-12-25 19:05:59 +08:00
|
|
|
|
e += 1
|
|
|
|
|
continue
|
|
|
|
|
res += ans[s: e]
|
|
|
|
|
citeit()
|
|
|
|
|
res += ans[e]
|
|
|
|
|
e += 1
|
|
|
|
|
s = e
|
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if s < len(ans):
|
2023-12-25 19:05:59 +08:00
|
|
|
|
res += ans[s:]
|
|
|
|
|
citeit()
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
def rerank(self, sres, query, tkweight=0.3, vtweight=0.7,
|
|
|
|
|
vfield="q_vec", cfield="content_ltks"):
|
|
|
|
|
ins_embd = [
|
|
|
|
|
Dealer.trans2floats(
|
|
|
|
|
sres.field[i]["q_vec"]) for i in sres.ids]
|
|
|
|
|
if not ins_embd:
|
|
|
|
|
return []
|
|
|
|
|
ins_tw = [sres.field[i][cfield].split(" ") for i in sres.ids]
|
|
|
|
|
# return CosineSimilarity([sres.query_vector], ins_embd)[0]
|
|
|
|
|
sim = self.qryr.hybrid_similarity(sres.query_vector,
|
|
|
|
|
ins_embd,
|
2023-12-25 19:05:59 +08:00
|
|
|
|
huqie.qie(query).split(" "),
|
|
|
|
|
ins_tw, tkweight, vtweight)
|
|
|
|
|
return sim
|
|
|
|
|
|
|
|
|
|
|
2023-12-28 13:50:13 +08:00
|
|
|
|
if __name__ == "__main__":
|
2023-12-25 19:05:59 +08:00
|
|
|
|
from util import es_conn
|
|
|
|
|
SE = Dealer(es_conn.HuEs("infiniflow"))
|
|
|
|
|
qs = [
|
|
|
|
|
"胡凯",
|
|
|
|
|
""
|
|
|
|
|
]
|
2023-12-28 13:50:13 +08:00
|
|
|
|
for q in qs:
|
2023-12-25 19:05:59 +08:00
|
|
|
|
print(">>>>>>>>>>>>>>>>>>>>", q)
|
2023-12-28 13:50:13 +08:00
|
|
|
|
print(SE.search(
|
|
|
|
|
{"question": q, "kb_ids": "64f072a75f3b97c865718c4a"}, "infiniflow_*"))
|