Skip to content

Commit b058130

Browse files
committed
RAG(RAGFlow): 混合检索源码解析-混合检索核心实现分析-search()完善
1 parent 6458954 commit b058130

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

docs/知识库/RAG/RAGFlow源码分析/检索阶段/混合检索策略.md

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ def search()
198198
```
199199

200200

201-
#### self.qryr.question
201+
#### self.qryr.question扩展问题
202202

203203
传入查询的问题文本,进行一系列的处理:
204204
- 输入预处理:`移除标点、换行符等噪声,全角转半角` + `去除中英文常见虚词和语气词`
205-
- 存在非中文词语时处理
206-
- 只存在中文词语时处理
205+
- 存在非中文词语时处理
206+
- 只存在中文词语时处理
207207

208208
##### 输入预处理
209209

@@ -424,23 +424,6 @@ result = [
424424
```
425425

426426

427-
------
428-
429-
**意图解析:**
430-
431-
432-
433-
434-
435-
436-
437-
------
438-
439-
**语义扩展:**
440-
441-
------
442-
443-
444427
##### 只存在中文时
445428

446429

@@ -579,7 +562,7 @@ def score_(self, tfts):
579562
- 然后将 `tk` 存放在 `tms`
580563
-----
581564

582-
最终进行数据的拼接:各种权重的配置拼接查询表达式
565+
最终进行数据的拼接:各种权重的配置拼接
583566

584567
```python
585568
tms = " ".join([f"({t})^{w}" for t, w in tms])
@@ -604,15 +587,36 @@ if qs:
604587
), keywords
605588
```
606589

607-
#### self.get_vector
608-
590+
#### self.get_vector转化原始问题为向量
609591

610-
#### self.dataStore.search
592+
直接使用向量模型转化问题为向量,检查维度 `shape` 是否为 1,然后将向量数据转化为 `float` 数据数组,动态生成向量列名:`q_{向量长度}_vec`
611593

594+
```python
595+
def get_vector(self, txt, emb_mdl, topk=10, similarity=0.1):
596+
qv, _ = emb_mdl.encode_queries(txt)
597+
shape = np.array(qv).shape
598+
if len(shape) > 1:
599+
raise Exception(
600+
f"Dealer.get_vector returned array's shape {shape} doesn't match expectation(exact one dimension).")
601+
embedding_data = [get_float(v) for v in qv]
602+
vector_column_name = f"q_{len(embedding_data)}_vec"
603+
return MatchDenseExpr(vector_column_name, embedding_data, 'float', 'cosine', topk, {"similarity": similarity})
604+
```
612605

613-
#### 检索逻辑总结
606+
#### self.dataStore.search使用原始问题向量和扩展问题后的数据进行检索
614607

608+
`this.dataStore`本质就是 `ES/Infinity`,直接调用了对应的 `search()` 方法进行检索
615609

610+
```python
611+
if lower_case_doc_engine == "elasticsearch":
612+
docStoreConn = rag.utils.es_conn.ESConnection()
613+
elif lower_case_doc_engine == "infinity":
614+
docStoreConn = rag.utils.infinity_conn.InfinityConnection()
615+
else:
616+
raise Exception(f"Not supported doc engine: {DOC_ENGINE}")
617+
618+
retrievaler = search.Dealer(docStoreConn)
619+
```
616620

617621
-----
618622

0 commit comments

Comments
 (0)