Skip to content

Commit 3b40fa4

Browse files
committed
Merge branch 'main' of github.com:codefuse-ai/ModelCache into support_docker_compose_cjn
2 parents c91934c + 3dad91c commit 3b40fa4

21 files changed

+920
-140
lines changed

Diff for: README.md

+142-85
Large diffs are not rendered by default.

Diff for: README_CN.md

+95-48
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,98 @@ ModelCache
1515
</div>
1616

1717
## Contents
18+
19+
- [Contents](#contents)
1820
- [新闻](#新闻)
1921
- [项目简介](#项目简介)
22+
- [架构大图](#架构大图)
2023
- [快速部署](#快速部署)
24+
- [环境依赖](#环境依赖)
25+
- [启动服务](#启动服务)
26+
- [启动 Demo](#启动-demo)
27+
- [启动标准服务](#启动标准服务)
2128
- [服务访问](#服务访问)
29+
- [写入 cache](#写入-cache)
30+
- [查询 cache](#查询-cache)
31+
- [清空 cache](#清空-cache)
2232
- [文章](#文章)
23-
- [架构大图](#架构大图)
33+
- [功能对比](#功能对比)
2434
- [核心功能](#核心功能)
35+
- [Todo List](#todo-list)
36+
- [Adapter](#adapter)
37+
- [Embedding model\&inference](#embedding-modelinference)
38+
- [Scalar Storage](#scalar-storage)
39+
- [Vector Storage](#vector-storage)
40+
- [Ranking](#ranking)
41+
- [Service](#service)
2542
- [致谢](#致谢)
26-
- [Contributing](#Contributing)
43+
- [Contributing](#contributing)
44+
2745
## 新闻
28-
- 🔥🔥[2024.10.22] 增加1024程序员节任务。
46+
47+
- 🔥🔥[2024.10.22] 增加1024程序员节任务。
2948
- 🔥🔥[2024.04.09] 增加了多租户场景中Redis Search存储和检索embedding的能力,可以将Cache和向量数据库的交互耗时降低至10ms内。
3049
- 🔥🔥[2023.12.10] 增加llmEmb、onnx、paddlenlp、fasttext等LLM embedding框架,并增加timm 图片embedding框架,用于提供更丰富的embedding能力。
3150
- 🔥🔥[2023.11.20] codefuse-ModelCache增加本地存储能力, 适配了嵌入式数据库sqlite、faiss,方便用户快速启动测试。
3251
- [2023.10.31] codefuse-ModelCache...
52+
3353
## 项目简介
54+
3455
Codefuse-ModelCache 是一个开源的大模型语义缓存系统,通过缓存已生成的模型结果,降低类似请求的响应时间,提升用户体验。该项目从服务优化角度出发,引入缓存机制,在资源有限和对实时性要求较高的场景下,帮助企业和研究机构降低推理部署成本、提升模型性能和效率、提供规模化大模型服务。我们希望通过开源,分享交流大模型语义Cache的相关技术。
56+
57+
## 架构大图
58+
59+
![modelcache modules](docs/modelcache_modules_20240409.png)
60+
3561
## 快速部署
36-
项目中启动服务脚本分为flask4modelcache.py 和 flask4modelcache_demo.py,其中:
3762

38-
- flask4modelcache_demo.py 为快速测试服务,内嵌了sqlite和faiss,用户无需关心数据库相关事宜。
39-
- flask4modelcache.py 为正常服务,需用户具备mysql和milvus等数据库服务。
63+
项目中启动服务脚本分为 `flask4modelcache.py``flask4modelcache_demo.py`,其中:
64+
65+
- `flask4modelcache_demo.py` 为快速测试服务,内嵌了 SQLite 和 FAISS,用户无需关心数据库相关事宜。
66+
- `flask4modelcache.py` 为正常服务,需用户具备 MySQL 和 Milvus 等数据库服务。
67+
4068
### 环境依赖
41-
- python版本: 3.8及以上
69+
70+
- python版本: 3.8 及以上
4271
- 依赖包安装:
43-
```shell
44-
pip install -r requirements.txt
45-
```
46-
### 服务启动
47-
#### 方式一:Demo服务启动
48-
- 离线模型bin文件下载, 参考地址:[https://huggingface.co/shibing624/text2vec-base-chinese/tree/main](https://huggingface.co/shibing624/text2vec-base-chinese/tree/main),并将下载的bin文件,放到 model/text2vec-base-chinese 文件夹中。
49-
- 执行flask4modelcache_demo.py启动服务。
50-
```shell
51-
cd CodeFuse-ModelCache
52-
```
53-
```shell
54-
python flask4modelcache_demo.py
55-
```
5672

57-
#### 方式二:通过 docker-compose 启动服务
58-
- 离线模型bin文件下载, 参考地址:[https://huggingface.co/shibing624/text2vec-base-chinese/tree/main](https://huggingface.co/shibing624/text2vec-base-chinese/tree/main),并将下载的bin文件,放到 model/text2vec-base-chinese 文件夹中。
73+
```shell
74+
pip install -r requirements.txt
75+
```
5976

60-
- 配置 docker network,只需执行一次
61-
```shell
62-
docker network create modelcache
63-
```
64-
- 执行 docker-compose 命令
65-
```shell
66-
# 首次运行本地不存在 modelcache 镜像、或 Dockerfile 变更时
67-
docker-compose up --build
77+
### 启动服务
6878

69-
# 非首次运行,且 Dockerfile 无变更
70-
docker-compose up
71-
```
72-
#### 方式三:不通过 docker-compose 启动服务
73-
在启动服务前,应该进行如下环境配置:
74-
1. 安装关系数据库 mysql, 导入sql创建数据表,sql文件:```reference_doc/create_table.sql```
75-
2. 安装向量数据库milvus
79+
#### 启动 Demo
80+
81+
- 离线模型 bin 文件下载, 参考地址:[Hugging Face](https://huggingface.co/shibing624/text2vec-base-chinese/tree/main),并将下载的 bin 文件,放到 `model/text2vec-base-chinese` 文件夹中。
82+
- 执行 `flask4modelcache_demo.py` 启动服务。
83+
84+
```shell
85+
cd CodeFuse-ModelCache
86+
```
87+
88+
```shell
89+
python flask4modelcache_demo.py
90+
```
91+
92+
#### 启动标准服务
93+
94+
在启动标准服务前,应该进行如下环境配置:
95+
96+
1. 安装关系数据库 MySQL, 导入 SQL 创建数据表,MySQL 文件:```reference_doc/create_table.sql```
97+
2. 安装向量数据库 Milvus。
7698
3. 在配置文件中添加数据库访问信息,配置文件为:
7799
1. ```modelcache/config/milvus_config.ini```
78100
2. ```modelcache/config/mysql_config.ini```
79-
4. 离线模型bin文件下载, 参考地址:[https://huggingface.co/shibing624/text2vec-base-chinese/tree/main](https://huggingface.co/shibing624/text2vec-base-chinese/tree/main)并将下载的bin文件,放到 model/text2vec-base-chinese 文件夹中
101+
4. 离线模型 bin 文件下载, 参考地址:[Hugging Face](https://huggingface.co/shibing624/text2vec-base-chinese/tree/main)并将下载的 bin 文件,放到 `model/text2vec-base-chinese` 文件夹中
80102
5. 通过flask4modelcache.py脚本启动后端服务。
103+
81104
## 服务访问
82-
当前服务以restful API方式提供3个核心功能:数据写入,cache查询和cache数据清空。请求demo 如下:
83-
### cache写入
105+
106+
当前服务以 restful API 方式提供 3 个核心功能:数据写入,cache 查询和 cache 数据清空。请求 demo 如下:
107+
108+
### 写入 cache
109+
84110
```python
85111
import json
86112
import requests
@@ -93,7 +119,9 @@ data = {'type': type, 'scope': scope, 'chat_info': chat_info}
93119
headers = {"Content-Type": "application/json"}
94120
res = requests.post(url, headers=headers, json=json.dumps(data))
95121
```
96-
### cache查询
122+
123+
### 查询 cache
124+
97125
```python
98126
import json
99127
import requests
@@ -106,7 +134,9 @@ data = {'type': type, 'scope': scope, 'query': query}
106134
headers = {"Content-Type": "application/json"}
107135
res = requests.post(url, headers=headers, json=json.dumps(data))
108136
```
109-
### cache清空
137+
138+
### 清空 cache
139+
110140
```python
111141
import json
112142
import requests
@@ -119,12 +149,14 @@ data = {'type': type, 'scope': scope, 'remove_type': remove_type}
119149
headers = {"Content-Type": "application/json"}
120150
res = requests.post(url, headers=headers, json=json.dumps(data))
121151
```
152+
122153
## 文章
154+
123155
https://mp.weixin.qq.com/s/ExIRu2o7yvXa6nNLZcCfhQ
124-
## 架构大图
125-
![modelcache modules](docs/modelcache_modules_20240409.png)
156+
126157
## 功能对比
127-
功能方面,为了解决huggingface网络问题并提升推理速度,增加了embedding本地推理能力。鉴于SqlAlchemy框架存在一些限制,我们对关系数据库交互模块进行了重写,以更灵活地实现数据库操作。在实践中,大型模型产品需要与多个用户和多个模型对接,因此在ModelCache中增加了对多租户的支持,同时也初步兼容了系统指令和多轮会话。
158+
159+
功能方面,为了解决 Hugging Face 网络问题并提升推理速度,增加了 embedding 本地推理能力。鉴于 SqlAlchemy 框架存在一些限制,我们对关系数据库交互模块进行了重写,以更灵活地实现数据库操作。在实践中,大型模型产品需要与多个用户和多个模型对接,因此在 ModelCache 中增加了对多租户的支持,同时也初步兼容了系统指令和多轮会话。
128160

129161
<table>
130162
<tr>
@@ -248,7 +280,8 @@ https://mp.weixin.qq.com/s/ExIRu2o7yvXa6nNLZcCfhQ
248280
</table>
249281

250282
## 核心功能
251-
在ModelCache中,沿用了GPTCache的主要思想,包含了一系列核心模块:adapter、embedding、similarity和data_manager。adapter模块主要功能是处理各种任务的业务逻辑,并且能够将embedding、similarity、data_manager等模块串联起来;embedding模块主要负责将文本转换为语义向量表示,它将用户的查询转换为向量形式,并用于后续的召回或存储操作;rank模块用于对召回的向量进行相似度排序和评估;data_manager模块主要用于管理数据库。同时,为了更好的在工业界落地,我们做了架构和功能上的升级,如下:
283+
284+
在ModelCache 中,沿用了 GPTCache 的主要思想,包含了一系列核心模块:adapter、embedding、similarity 和 data_manager。adapter模块主要功能是处理各种任务的业务逻辑,并且能够将 embedding、similarity、data_manager等模块串联起来;embedding 模块主要负责将文本转换为语义向量表示,它将用户的查询转换为向量形式,并用于后续的召回或存储操作;rank 模块用于对召回的向量进行相似度排序和评估;data_manager 模块主要用于管理数据库。同时,为了更好的在工业界落地,我们做了架构和功能上的升级,如下:
252285

253286
- [x] 架构调整(轻量化集成):以类redis的缓存模式嵌入到大模型产品中,提供语义缓存能力,不会干扰LLM调用和安全审核等功能,适配所有大模型服务。
254287
- [x] 多种模型加载方案:
@@ -267,24 +300,38 @@ https://mp.weixin.qq.com/s/ExIRu2o7yvXa6nNLZcCfhQ
267300
- 增加model字段和数据统计字段,用于功能拓展。
268301

269302
## Todo List
303+
270304
### Adapter
271-
- [ ] register adapter for Milvus:根据scope中的model参数,初始化对应Collection 并且执行load操作。
305+
306+
- [ ] register adapter for Milvus:根据 scope 中的 model 参数,初始化对应 Collection 并且执行 load 操作。
307+
272308
### Embedding model&inference
273-
- [ ] inference优化:优化embedding推理速度,适配fastertransformer, TurboTransformers, ByteTransformer等推理引擎。
309+
310+
- [ ] inference 优化:优化 embedding 推理速度,适配fastertransformer、TurboTransformers 和 ByteTransformer 等推理引擎。
274311
- [ ] 兼容huggingface模型和modelscope模型,提供更多模型加载方式。
312+
275313
### Scalar Storage
314+
276315
- [ ] Support MongoDB。
277316
- [ ] Support ElasticSearch。
317+
278318
### Vector Storage
319+
279320
- [ ] 在多模态场景中适配faiss存储。
321+
280322
### Ranking
323+
281324
- [ ] 增加Rank模型,对embedding召回后的数据,进行精排。
325+
282326
### Service
327+
283328
- [ ] 支持fastapi。
284329
- [ ] 增加前端界面,用于测试。
285330

286331
## 致谢
332+
287333
本项目参考了以下开源项目,在此对相关项目和研究开发人员表示感谢。<br />[GPTCache](https://github.com/zilliztech/GPTCache)
288334

289335
## Contributing
336+
290337
ModelCache是一个非常有趣且有用的项目,我们相信这个项目有很大的潜力,无论你是经验丰富的开发者,还是刚刚入门的新手,都欢迎你为这个项目做出一些贡献,包括但不限于:提交问题和建议,参与代码编写,完善文档和示例。你的参与将会使这个项目变得更好,同时也会为开源社区做出贡献。

Diff for: docs/1.what-is-model-cache.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# What is ModelCache
2+
3+
In ModelCache, we adopted the main idea of GPTCache, includes core modules: adapter, embedding, similarity, and data_manager. The adapter module is responsible for handling the business logic of various tasks and can connect the embedding, similarity, and data_manager modules. The embedding module is mainly responsible for converting text into semantic vector representations, it transforms user queries into vector form.The rank module is used for sorting and evaluating the similarity of the recalled vectors. The data_manager module is primarily used for managing the database. In order to better facilitate industrial applications, we have made architectural and functional upgrades as follows:
4+
5+
## Architecture
6+
7+
![modelcache modules](modelcache_modules_20240409.png)
8+
9+
## Function comparison
10+
11+
We've implemented several key updates to our repository. We've resolved network issues with Hugging Face and improved inference speed by introducing local embedding capabilities. Due to limitations in SqlAlchemy, we've redesigned our relational database interaction module for more flexible operations. We've added multi-tenancy support to ModelCache, recognizing the need for multiple users and models in LLM products. Lastly, we've made initial adjustments for better compatibility with system commands and multi-turn dialogues.
12+
13+
<table>
14+
<tr>
15+
<th rowspan="2">Module</th>
16+
<th rowspan="2">Function</th>
17+
18+
</tr>
19+
<tr>
20+
<th>ModelCache</th>
21+
<th>GPTCache</th>
22+
</tr>
23+
<tr>
24+
<td rowspan="2">Basic Interface</td>
25+
<td>Data query interface</td>
26+
<td class="checkmark">&#9745; </td>
27+
<td class="checkmark">&#9745; </td>
28+
</tr>
29+
<tr>
30+
<td>Data writing interface</td>
31+
<td class="checkmark">&#9745; </td>
32+
<td class="checkmark">&#9745; </td>
33+
</tr>
34+
<tr>
35+
<td rowspan="3">Embedding</td>
36+
<td>Embedding model configuration</td>
37+
<td class="checkmark">&#9745; </td>
38+
<td class="checkmark">&#9745; </td>
39+
</tr>
40+
<tr>
41+
<td>Large model embedding layer</td>
42+
<td class="checkmark">&#9745; </td>
43+
<td></td>
44+
</tr>
45+
<tr>
46+
<td>BERT model long text processing</td>
47+
<td class="checkmark">&#9745; </td>
48+
<td></td>
49+
</tr>
50+
<tr>
51+
<td rowspan="2">Large model invocation</td>
52+
<td>Decoupling from large models</td>
53+
<td class="checkmark">&#9745; </td>
54+
<td></td>
55+
</tr>
56+
<tr>
57+
<td>Local loading of embedding model</td>
58+
<td class="checkmark">&#9745; </td>
59+
<td></td>
60+
</tr>
61+
<tr>
62+
<td rowspan="2">Data isolation</td>
63+
<td>Model data isolation</td>
64+
<td class="checkmark">&#9745; </td>
65+
<td class="checkmark">&#9745; </td>
66+
</tr>
67+
<tr>
68+
<td>Hyperparameter isolation</td>
69+
<td></td>
70+
<td></td>
71+
</tr>
72+
<tr>
73+
<td rowspan="3">Databases</td>
74+
<td>MySQL</td>
75+
<td class="checkmark">&#9745; </td>
76+
<td class="checkmark">&#9745; </td>
77+
</tr>
78+
<tr>
79+
<td>Milvus</td>
80+
<td class="checkmark">&#9745; </td>
81+
<td class="checkmark">&#9745; </td>
82+
</tr>
83+
<tr>
84+
<td>OceanBase</td>
85+
<td class="checkmark">&#9745; </td>
86+
<td></td>
87+
</tr>
88+
<tr>
89+
<td rowspan="3">Session management</td>
90+
<td>Single-turn dialogue</td>
91+
<td class="checkmark">&#9745; </td>
92+
<td class="checkmark">&#9745; </td>
93+
</tr>
94+
<tr>
95+
<td>System commands</td>
96+
<td class="checkmark">&#9745; </td>
97+
<td></td>
98+
</tr>
99+
<tr>
100+
<td>Multi-turn dialogue</td>
101+
<td class="checkmark">&#9745; </td>
102+
<td></td>
103+
</tr>
104+
<tr>
105+
<td rowspan="2">Data management</td>
106+
<td>Data persistence</td>
107+
<td class="checkmark">&#9745; </td>
108+
<td class="checkmark">&#9745; </td>
109+
</tr>
110+
<tr>
111+
<td>One-click cache clearance</td>
112+
<td class="checkmark">&#9745; </td>
113+
<td></td>
114+
</tr>
115+
<tr>
116+
<td rowspan="2">Tenant management</td>
117+
<td>Support for multi-tenancy</td>
118+
<td class="checkmark">&#9745; </td>
119+
<td></td>
120+
</tr>
121+
<tr>
122+
<td>Milvus multi-collection capability</td>
123+
<td class="checkmark">&#9745; </td>
124+
<td></td>
125+
</tr>
126+
<tr>
127+
<td>Other</td>
128+
<td>Long-short dialogue distinction</td>
129+
<td class="checkmark">&#9745; </td>
130+
<td></td>
131+
</tr>
132+
</table>

Diff for: docs/2.model-cache-features.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ModelCache features
2+
3+
This topic describes ModelCache features. In ModelCache, we incorporated the core principles of GPTCache. ModelCache has four modules: adapter, embedding, similarity, and data_manager.
4+
5+
- The adapter module orchestrates the business logic for various tasks, integrate the embedding, similarity, and data_manager modules.
6+
- The embedding module converts text into semantic vector representations, and transforms user queries into vectors.
7+
- The rank module ranks and evaluate the similarity of recalled vectors.
8+
- The data_manager module manages the databases.
9+
10+
To make ModelCache more suitable for industrial use, we made several improvements to its architecture and functionality:
11+
12+
- [x] Architectural adjustment (lightweight integration):
13+
- Embedded into LLM products using a Redis-like caching mode.
14+
- Provided semantic caching without interfering with LLM calls, security audits, and other functions.
15+
- Compatible with all LLM services.
16+
- [x] Multiple model loading:
17+
- Supported local embedding model loading, and resolved Hugging Face network connectivity issues.
18+
- Supported loading embedding layers from various pre-trained models.
19+
- [x] Data isolation
20+
- Environment isolation: Read different database configurations based on the environment. Isolate development, staging, and production environments.
21+
- Multi-tenant data isolation: Dynamically create collections based on models for data isolation, addressing data separation issues in multi-model/service scenarios within large language model products.
22+
- [x] Supported system instruction: Adopted a concatenation approach to resolve issues with system instructions in the prompt paradigm.
23+
- [x] Long and short text differentiation: Long texts bring more challenges for similarity assessment. Added differentiation between long and short texts, allowing for separate threshold configurations.
24+
- [x] Milvus performance optimization: Adjusted Milvus consistency level to "Session" level for better performance.
25+
- [x] Data management:
26+
- One-click cache clearing to enable easy data management after model upgrades.
27+
- Recall of hit queries for subsequent data analysis and model iteration reference.
28+
- Asynchronous log write-back for data analysis and statistics.
29+
- Added model field and data statistics field to enhance features.

0 commit comments

Comments
 (0)