Skip to content

Commit

Permalink
4.6.8-production (#822)
Browse files Browse the repository at this point in the history
* Json completion (#16)

* json-completion

* fix duplicate

* fix

* fix: config json

* feat: query extension

* perf: i18n

* 468 doc

* json editor

* perf: doc

* perf: default extension model

* docker file

* doc

* perf: token count

* perf: search extension

* format

* perf: some constants data

---------

Co-authored-by: heheer <[email protected]>
  • Loading branch information
c121914yu and newfish-cmyk authored Feb 4, 2024
1 parent ec8e251 commit 51bbdf2
Show file tree
Hide file tree
Showing 68 changed files with 4,194 additions and 3,863 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"editor.formatOnSave": true,
"editor.mouseWheelZoom": true,
"typescript.tsdk": "node_modules/typescript/lib",
"prettier.prettierPath": "./node_modules/prettier",
"prettier.prettierPath": "",
"i18n-ally.localesPaths": [
"projects/app/public/locales",
],
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ARG proxy
RUN [ -z "$proxy" ] || sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache libc6-compat && npm install -g [email protected]
# if proxy exists, set proxy
RUN [ -z "$proxy" ] || pnpm config set registry https://registry.npm.taobao.org
RUN [ -z "$proxy" ] || pnpm config set registry https://registry.npmmirror.com

# copy packages and one project
COPY pnpm-lock.yaml pnpm-workspace.yaml ./
Expand All @@ -28,7 +28,7 @@ ARG proxy
RUN [ -z "$proxy" ] || sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache libc6-compat && npm install -g [email protected]
# if proxy exists, set proxy
RUN [ -z "$proxy" ] || pnpm config set registry https://registry.npm.taobao.org
RUN [ -z "$proxy" ] || pnpm config set registry https://registry.npmmirror.com

COPY ./worker /app/worker
RUN cd /app/worker && pnpm i --production --ignore-workspace
Expand Down
Binary file modified docSite/assets/imgs/dataset_search_params1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docSite/assets/imgs/dataset_search_params2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docSite/assets/imgs/dataset_search_params3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docSite/assets/imgs/dataset_search_process.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docSite/assets/imgs/dataset_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 88 additions & 13 deletions docSite/content/docs/course/data_search.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
---
title: '知识库搜索参数'
description: '知识库搜索原理'
title: '知识库搜索介绍'
description: '本节会详细介绍 FastGPT 知识库结构设计,理解其 QA 的存储格式和多向量映射,以便更好的构建知识库。同时会介绍每个搜索参数的功能。这篇介绍主要以使用为主,详细原理不多介绍。'
icon: 'language'
draft: false
toc: true
weight: 106
---

在知识库搜索的方式上,FastGPT提供了三种方式,分别为“语义检索”“增强语义检索”“混合检索”。
## 理解向量

![](/imgs/dataset_search_params1.png)
FastGPT 采用了 RAG 中的 Embedding 方案构建知识库,要使用好 FastGPT 需要简单的理解`Embedding`向量是如何工作的及其特点。

## 搜索模式
人类的文字、图片、视频等媒介是无法直接被计算机理解的,要想让计算机理解两段文字是否有相似性、相关性,通常需要将它们转成计算机可以理解的语言,向量是其中的一种方式。

### 语义检索
向量可以简单理解为一个数字数组,两个向量之间可以通过数学公式得出一个`距离`,距离越小代表两个向量的相似度越大。从而映射到文字、图片、视频等媒介上,可以用来判断两个媒介之间的相似度。向量搜索便是利用了这个原理。

而由于文字是有多种类型,并且拥有成千上万种组合方式,因此在转成向量进行相似度匹配时,很难保障其精确性。在向量方案构建的知识库中,通常使用`topk`召回的方式,也就是查找前`k`个最相似的内容,丢给大模型去做更进一步的`语义判断``逻辑推理``归纳总结`,从而实现知识库问答。因此,在知识库问答中,向量搜索的环节是最为重要的。

影响向量搜索精度的因素非常多,主要包括:向量模型的质量、数据的质量(长度,完整性,多样性)、检索器的精度(速度与精度之间的取舍)。与数据质量对应的就是检索词的质量。

检索器的精度比较容易解决,向量模型的训练略复杂,因此数据和检索词质量优化成了一个重要的环节。


### 提高向量搜索精度的方法

1. 更好分词分段:当一段话的结构和语义是完整的,并且是单一的,精度也会提高。因此,许多系统都会优化分词器,尽可能的保障每组数据的完整性。
2. 精简`index`的内容,减少向量内容的长度:当`index`的内容更少,更准确时,检索精度自然会提高。但与此同时,会牺牲一定的检索范围,适合答案较为严格的场景。
3. 丰富`index`的数量,可以为同一个`chunk`内容增加多组`index`
4. 优化检索词:在实际使用过程中,用户的问题通常是模糊的或是缺失的,并不一定是完整清晰的问题。因此优化用户的问题(检索词)很大程度上也可以提高精度。
5. 微调向量模型:由于市面上直接使用的向量模型都是通用型模型,在特定领域的检索精度并不高,因此微调向量模型可以很大程度上提高专业领域的检索效果。

## FastGPT 构建知识库方案

### 数据存储结构

在 FastGPT 中,整个知识库由库、集合和数据 3 部分组成。集合可以简单理解为一个`文件`。一个``中可以包含多个`集合`,一个`集合`中可以包含多组`数据`。最小的搜索单位是``,也就是说,知识库搜索时,是对整个``进行搜索,而集合仅是为了对数据进行分类管理,与搜索效果无关。(起码目前还是)

![](/imgs/dataset_tree.png)

### 向量存储结构

FastGPT 采用了`PostgresSQL``PG Vector`插件作为向量检索器,索引为`HNSW`。且`PostgresSQL`仅用于向量检索(该引擎可以替换成其它数据库),`MongoDB`用于其他数据的存取。

`MongoDB``dataset.datas`表中,会存储向量原数据的信息,同时有一个`indexes`字段,会记录其对应的向量ID,这是一个数组,也就是说,一组向量可以对应多组数据。

`PostgresSQL`的表中,设置一个`vector`字段用于存储向量。在检索时,会先召回向量,再根据向量的ID,去`MongoDB`中寻找原数据内容,如果对应了同一组原数据,则进行合并,向量得分取最高得分。

![](/imgs/datasetSetting1.png)

### 多向量的目的和使用方式

在一组向量中,内容的长度和语义的丰富度通常是矛盾的,无法兼得。因此,FastGPT 采用了多向量映射的方式,将一组数据映射到多组向量中,从而保障数据的完整性和语义的丰富度。

你可以为一组较长的文本,添加多组向量,从而在检索时,只要其中一组向量被检索到,该数据也将被召回。

### 检索方案

1. 通过`问题补全`实现指代消除和问题扩展,从而增加连续对话的检索能力以及语义丰富度。
2. 通过`Concat query`来增加`Rerank`连续对话的时,排序的准确性。
3. 通过`RRF`合并方式,综合多个渠道的检索效果。
4. 通过`Rerank`来二次排序,提高精度。

![](/imgs/dataset_search_process.png)


## 搜索参数
| | | |
| --- |---| --- |
|![](/imgs/dataset_search_params1.png)| ![](/imgs/dataset_search_params2.png) | ![](/imgs/dataset_search_params3.png) |

### 搜索模式

#### 语义检索

语义检索是通过向量距离,计算用户问题与知识库内容的距离,从而得出“相似度”,当然这并不是语文上的相似度,而是数学上的。

Expand All @@ -27,32 +85,49 @@ weight: 106
- 精度不稳定
- 受关键词和句子完整度影响

### 全文检索
#### 全文检索

采用传统的全文检索方式。适合查找关键的主谓语等。

### 混合检索
#### 混合检索

同时使用向量检索和全文检索,并通过 RRF 公式进行两个搜索结果合并,一般情况下搜索结果会更加丰富准确。

由于混合检索后的查找范围很大,并且无法直接进行相似度过滤,通常需要进行利用重排模型进行一次结果重新排序,并利用重排的得分进行过滤。



## 结果重排
#### 结果重排

利用`ReRank`模型对搜索结果进行重排,绝大多数情况下,可以有效提高搜索结果的准确率。不过,重排模型与问题的完整度(主谓语齐全)有一些关系,通常会先走问题补全后再进行搜索-重排。重排后可以得到一个`0-1`的得分,代表着搜索内容与问题的相关度,该分数通常比向量的得分更加精确,可以根据得分进行过滤。

FastGPT 会使用 `RRF` 对重排结果、向量搜索结果、全文检索结果进行合并,得到最终的搜索结果。

## 引用上限
### 搜索过滤
#### 引用上限

每次搜索最多引用`n``tokens`的内容。

之所以不采用`top k`,是发现在混合知识库(问答库、文档库)时,不同`chunk`的长度差距很大,会导致`top k`的结果不稳定,因此采用了`tokens`的方式进行引用上限的控制。

## 最低相关度
#### 最低相关度

一个`0-1`的数值,会过滤掉一些低相关度的搜索结果。

该值仅在`语义检索`或使用`结果重排`时生效。

### 问题补全

#### 背景

在 RAG 中,我们需要根据输入的问题去数据库里执行 embedding 搜索,查找相关的内容,从而查找到相似的内容(简称知识库搜索)。

在搜索的过程中,尤其是连续对话的搜索,我们通常会发现后续的问题难以搜索到合适的内容,其中一个原因是知识库搜索只会使用“当前”的问题去执行。看下面的例子:

![](/imgs/coreferenceResolution2.jpg)

用户在提问“第二点是什么”的时候,只会去知识库里查找“第二点是什么”,压根查不到内容。实际上需要查询的是“QA结构是什么”。因此我们需要引入一个【问题补全】模块,来对用户当前的问题进行补全,从而使得知识库搜索能够搜索到合适的内容。使用补全后效果如下:

![](/imgs/coreferenceResolution3.jpg)

#### 实现方式

在进行`数据检索`前,会先让模型进行`指代消除``问题扩展`,一方面可以可以解决指代对象不明确问题,同时可以扩展问题的语义丰富度。
4 changes: 2 additions & 2 deletions docSite/content/docs/development/custom-models/m3e.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ Authorization 为 sk-key。model 为刚刚在 One API 填写的自定义模型

## 接入 FastGPT

修改 config.json 配置文件,在 VectorModels 中加入 M3E 模型:
修改 config.json 配置文件,在 vectorModels 中加入 M3E 模型:

```json
"VectorModels": [
"vectorModels": [
{
"model": "text-embedding-ada-002",
"name": "Embedding-2",
Expand Down
24 changes: 12 additions & 12 deletions docSite/content/docs/development/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,21 @@ curl -O https://raw.githubusercontent.com/labring/FastGPT/main/projects/app/data

## 四、启动容器

在 docker-compose.yml 同级目录下执行

```bash
# 在 docker-compose.yml 同级目录下执行
# 进入项目目录
cd 项目目录
# 创建 mongo 密钥
openssl rand -base64 756 > ./mongodb.key
chmod 600 ./mongodb.key

# 启动容器
docker-compose pull
docker-compose up -d
```

## 、初始化 Mongo 副本集(4.6.8以前可忽略)
## 、初始化 Mongo 副本集(4.6.8以前可忽略)

FastGPT 4.6.8 后使用了 MongoDB 的事务,需要运行在副本集上。副本集没法自动化初始化,需手动操作。

Expand All @@ -120,9 +128,9 @@ docker ps
docker exec -it mongo bash

# 连接数据库
mongo
mongo -u myname -p mypassword --authenticationDatabase admin

# 初始化副本集。
# 初始化副本集。如果需要外网访问,mongo:27017 可以改成 ip:27017。但是需要同时修改 FastGPT 连接的参数(MONGODB_URI=mongodb://myname:mypassword@mongo:27017/fastgpt?authSource=admin => MONGODB_URI=mongodb://myname:mypassword@ip:27017/fastgpt?authSource=admin)
rs.initiate({
_id: "rs0",
members: [
Expand All @@ -131,14 +139,6 @@ rs.initiate({
})
# 检查状态。如果提示 rs0 状态,则代表运行成功
rs.status()

# 初始化用户
use admin
db.createUser({
user: "admin",
pwd: "password",
roles: [{ role: "root", db: "admin" }]
});
```
## 五、访问 FastGPT
Expand Down
78 changes: 70 additions & 8 deletions docSite/content/docs/development/upgrading/468.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 'V4.6.8(进行中'
description: 'FastGPT V4.6.7'
title: 'V4.6.8(需要初始化'
description: 'FastGPT V4.6.8更新说明'
icon: 'upgrade'
draft: false
toc: true
Expand All @@ -9,19 +9,81 @@ weight: 828

## docker 部署 - 更新 Mongo

开启 Mongo 副本集模式。需要进入 mongo 执行一次 init,参考[初始化Mongo副本集](/docs/development/docker/#四初始化-mongo-副本集),这个比较麻烦,初始化后可以用 mongoshell 之类的连接试试,看能不能连接上。
1. 修改 docker-compose.yml 的mongo部分,补上`command``mongodb.key`

```yml
mongo:
image: mongo:5.0.18
# image: registry.cn-hangzhou.aliyuncs.com/fastgpt/mongo:5.0.18 # 阿里云
container_name: mongo
ports:
- 27017:27017
networks:
- fastgpt
command: mongod --keyFile /data/mongodb.key --replSet rs0
environment:
# 这里密码不用变。
- MONGO_INITDB_ROOT_USERNAME=myname
- MONGO_INITDB_ROOT_PASSWORD=mypassword
volumes:
- ./mongo/data:/data/db
- ./mongodb.key:/data/mongodb.key
```
2. 创建 mongo 密钥
```bash
cd 项目目录
# 创建 mongo 密钥
openssl rand -base64 756 > ./mongodb.key
chmod 600 ./mongodb.key
# 重启 Mongo
docker-compose down
docker-compose up -d
```

3. 进入容器初始化部分集合

```bash
docker exec -it mongo bash
mongo -u myname -p mypassword --authenticationDatabase admin
# 初始化副本集。如果需要外网访问,mongo:27017 可以改成 ip:27017。但是需要同时修改 FastGPT 连接的参数(MONGODB_URI=mongodb://myname:mypassword@mongo:27017/fastgpt?authSource=admin => MONGODB_URI=mongodb://myname:mypassword@ip:27017/fastgpt?authSource=admin)
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo:27017" }
]
})
# 检查状态。如果提示 rs0 状态,则代表运行成功
rs.status()
```
## Sealos 部署 - 无需更新 Mongo
## 修改配置文件
去除了重复的模型配置,LLM模型都合并到一个属性中:[点击查看最新的配置文件](/docs/development/configuration/)
## 商业版初始化
商业版用户需要执行一个初始化,格式化团队信息。
发起 1 个 HTTP 请求 ({{rootkey}} 替换成环境变量里的 `rootkey`,{{host}} 替换成自己域名)
```bash
curl --location --request POST 'https://{{host}}/api/init/v468' \
--header 'rootkey: {{rootkey}}' \
--header 'Content-Type: application/json'
```
会初始化计费系统,内部使用可把免费的存储拉大。
## V4.6.8 更新说明
1. 新增 - 知识库搜索合并模块。
1. 优化 - LLM 模型配置,不再区分对话、分类、提取模型。同时支持模型的默认参数,避免不同模型参数冲突,可通过`defaultConfig`传入默认的配置。
2. 优化 - HTTP 模块,支持输出字符串自动序列化(JSON可自动转成字符串)
3. 优化 - 流响应,参考了`ChatNextWeb`的流,更加丝滑。此外,之前提到的乱码、中断,刷新后又正常了,可能会修复)
4. 修复 - 语音输入文件无法上传。
5. 修复 - 对话框重新生成无法使用。
2. **优化 - 内容补全。将内容补全内置到【知识库搜索】中,并实现了一次内容补全,即可完成“指代消除”和“问题扩展”。**FastGPT知识库搜索详细流程可查看:[知识库搜索介绍](/docs/course/data_search/)
3. 优化 - LLM 模型配置,不再区分对话、分类、提取模型。同时支持模型的默认参数,避免不同模型参数冲突,可通过`defaultConfig`传入默认的配置。
4. 优化 - HTTP 模块,支持输出字符串自动序列化(JSON可自动转成字符串)
5. 优化 - 流响应,参考了`ChatNextWeb`的流,更加丝滑。此外,之前提到的乱码、中断,刷新后又正常了,可能会修复)
6. 修复 - 语音输入文件无法上传。
7. 修复 - 对话框重新生成无法使用。
2 changes: 1 addition & 1 deletion docSite/content/docs/workflow/examples/google_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ export default async function (ctx: FunctionContext) {
},
{
"moduleId": "p9h459",
"name": "core.module.template.cfr",
"name": "问题补全",
"avatar": "/imgs/module/cfr.svg",
"flowType": "cfr",
"showStatus": true,
Expand Down
9 changes: 0 additions & 9 deletions docSite/content/docs/workflow/examples/lab_appointment.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,15 +638,6 @@ HTTP 模块允许你调用任意 GET/POST 类型的 HTTP 接口,从而实现
"value": "embedding",
"connected": false
},
{
"key": "datasetParamsModal",
"type": "selectDatasetParamsModal",
"label": "",
"valueType": "any",
"showTargetInApp": false,
"showTargetInPlugin": false,
"connected": false
},
{
"key": "userChatInput",
"type": "target",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "问题补全"
title: "问题补全(已合并到知识库搜索)"
description: "问题补全模块介绍和使用"
icon: "input"
draft: false
Expand Down
2 changes: 2 additions & 0 deletions docSite/content/docs/workflow/modules/dataset_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ toc: true
weight: 357
---

知识库搜索具体参数说明,以及内部逻辑请移步:[FastGPT知识库搜索方案](/docs/course/data_search/)

## 特点

- 可重复添加(复杂编排时防止线太乱,可以更美观)
Expand Down
Loading

0 comments on commit 51bbdf2

Please sign in to comment.