Skip to content

Commit 7afff01

Browse files
authored
docs: update docs website
1 parent 47647f8 commit 7afff01

19 files changed

Lines changed: 888 additions & 0 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Build/Publish Develop Docs
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
permissions:
8+
contents: write
9+
jobs:
10+
deploy:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0
16+
- name: Configure Git Credentials
17+
run: |
18+
git config user.name github-actions[bot]
19+
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
20+
- uses: actions/setup-python@v5
21+
with:
22+
python-version: 3.x
23+
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
24+
- uses: actions/cache@v4
25+
with:
26+
key: mkdocs-material-${{ env.cache_id }}
27+
path: .cache
28+
restore-keys: |
29+
mkdocs-material-
30+
- run: pip install mike mkdocs-material jieba mkdocs-git-revision-date-localized-plugin mkdocs-git-committers-plugin-2 mkdocs-static-i18n
31+
- run: |
32+
git fetch origin gh-pages --depth=1
33+
mkdocs build
34+
ls -la site/
35+
mike set-default main
36+
mike deploy --push --update-aliases main latest
File renamed without changes.

docs/blog/.authors.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
authors:
2+
SWHL:
3+
name: SWHL
4+
description: Creator
5+
avatar: https://avatars.githubusercontent.com/u/28639377?v=4
6+
url: https://github.com/SWHL

docs/blog/.meta.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
comments: true
2+
hide:
3+
- feedback

docs/blog/index.md

Whitespace-only changes.

docs/blog/posts/custom_llm_api.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
title: 自定义LLM API
3+
date:
4+
created: 2023-09-11
5+
authors: [SWHL]
6+
categories:
7+
- General
8+
comments: true
9+
---
10+
11+
### 引言
12+
13+
{{% alert context="info" %}}该项目的LLM部分是独立的,用户可在 **knowledge_qa_llm/llm** 自定义配置所需的LLM接口。{{% /alert %}}
14+
15+
下面以自定义支持InterLM-7b大模型为例,说明如何支持的。前提是本地满足部署LLM的推理条件。
16+
17+
### 步骤如下
18+
19+
#### 1. 部署LLM模型到本地
20+
21+
具体如何下载,参见Hugging Face中[internlm-7b](https://huggingface.co/internlm/internlm-7b)
22+
23+
#### 2. 编写模型的部署推理代码
24+
25+
这一点可以参考[ChatGLM](https://github.com/THUDM/ChatGLM-6B/blob/main/api.py)API的实现。只需要替换模型加载部分为InternLM的即可。具体如下:
26+
27+
<details>
28+
29+
```python {linenos=table}
30+
from fastapi import FastAPI, Request
31+
from transformers import AutoTokenizer, AutoModel
32+
import uvicorn, json, datetime
33+
import torch
34+
35+
DEVICE = "cuda"
36+
DEVICE_ID = "0"
37+
CUDA_DEVICE = f"{DEVICE}:{DEVICE_ID}" if DEVICE_ID else DEVICE
38+
39+
40+
def torch_gc():
41+
if torch.cuda.is_available():
42+
with torch.cuda.device(CUDA_DEVICE):
43+
torch.cuda.empty_cache()
44+
torch.cuda.ipc_collect()
45+
46+
47+
app = FastAPI()
48+
49+
50+
@app.post("/")
51+
async def create_item(request: Request):
52+
global model, tokenizer
53+
json_post_raw = await request.json()
54+
json_post = json.dumps(json_post_raw)
55+
json_post_list = json.loads(json_post)
56+
prompt = json_post_list.get('prompt')
57+
history = json_post_list.get('history')
58+
max_length = json_post_list.get('max_length')
59+
top_p = json_post_list.get('top_p')
60+
temperature = json_post_list.get('temperature')
61+
response, history = model.chat(tokenizer,
62+
prompt,
63+
history=history,
64+
max_new_tokens=max_length if max_length else 2048,
65+
top_p=top_p if top_p else 0.7,
66+
temperature=temperature if temperature else 0.95)
67+
now = datetime.datetime.now()
68+
time = now.strftime("%Y-%m-%d %H:%M:%S")
69+
answer = {
70+
"response": response,
71+
"history": history,
72+
"status": 200,
73+
"time": time
74+
}
75+
log = "[" + time + "] " + '", prompt:"' + prompt + '", response:"' + repr(response) + '"'
76+
print(log)
77+
torch_gc()
78+
return answer
79+
80+
81+
if __name__ == '__main__':
82+
tokenizer = AutoTokenizer.from_pretrained("internlm/internlm-chat-7b-v1_1", trust_remote_code=True)
83+
model = AutoModel.from_pretrained("internlm/internlm-chat-7b-v1_1", trust_remote_code=True).half().cuda()
84+
model.eval()
85+
uvicorn.run(app, host='0.0.0.0', port=8000, workers=1)
86+
```
87+
88+
</details>
89+
90+
#### 3. 编写调用接口部分代码
91+
92+
在以下项目`knowledge_qa_llm/llm/`目录下创建`internlm_7b.py`文件,具体代码如下:
93+
94+
<details>
95+
96+
```python {linenos=table}
97+
import json
98+
from typing import List, Optional
99+
100+
import requests
101+
102+
103+
class InternLM_7B:
104+
def __init__(self, api_url: str = None):
105+
self.api_url = api_url
106+
107+
def __call__(self, prompt: str, history: Optional[List] = None, **kwargs):
108+
if not history:
109+
history = []
110+
111+
data = {"prompt": prompt, "history": history}
112+
if kwargs:
113+
temperature = kwargs.get("temperature", 0.1)
114+
top_p = kwargs.get("top_p", 0.7)
115+
max_length = kwargs.get("max_length", 4096)
116+
117+
data.update(
118+
{"temperature": temperature, "top_p": top_p, "max_length": max_length}
119+
)
120+
req = requests.post(self.api_url, data=json.dumps(data), timeout=60)
121+
try:
122+
rdata = req.json()
123+
if rdata["status"] == 200:
124+
return rdata["response"]
125+
return "Network error"
126+
except Exception as e:
127+
return f"Network error:{e}"
128+
```
129+
130+
</details>
131+
132+
#### 4. 添加导入声明
133+
134+
`knowledge_qa_llm/llm/__init__.py`中添加对应的`import`部分代码,示例如下:
135+
136+
```python {linenos=table}
137+
from .baichuan_7b import BaiChuan7B
138+
from .chatglm2_6b import ChatGLM2_6B
139+
from .ernie_bot_turbo import ERNIEBotTurbo
140+
from .qwen7b_chat import Qwen7B_Chat
141+
from .internlm_7b import InternLM_7B
142+
143+
__all__ = ["BaiChuan7B", "ChatGLM2_6B", "ERNIEBotTurbo", "Qwen7B_Chat", "InternLM_7B"]
144+
```
145+
146+
#### 5. 更改配置文件
147+
148+
更改`knowledge_qa_llm/config.yaml`
149+
150+
```yaml {linenos=table}
151+
LLM_API:
152+
InternLM_7B: your_api
153+
Qwen7B_Chat: your_api
154+
ChatGLM2_6B: your_api
155+
BaiChuan7B: your_api
156+
```
157+
158+
#### 6. 启动
159+
160+
```bash {linenos=table}
161+
streamlit run web_ui.py
162+
```

docs/blog/posts/supported_llm.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: 支持的LLM
3+
date:
4+
created: 2023-09-11
5+
authors: [SWHL]
6+
categories:
7+
- General
8+
comments: true
9+
---
10+
11+
[ChatGLM2-6B](https://huggingface.co/THUDM/chatglm2-6b)
12+
13+
[BaiChuan-7B](https://huggingface.co/baichuan-inc/Baichuan-7B)
14+
15+
[Qwen-7B](https://huggingface.co/Qwen/Qwen-7B)
16+
17+
[llama2](https://github.com/facebookresearch/llama)
18+
19+
[InternLM-7b](https://huggingface.co/internlm/internlm-7b)

docs/changelog.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
comments: true
3+
hide:
4+
- toc
5+
---
6+
7+
8+
#### 2023-10-15 v0.0.10 update
9+
10+
- 当不能从文档中搜索到任何有效信息时,会直接调用模型本身的能力。
11+
- 完善文档,添加超参数的解释
12+
- 基于erniebot库,统一文心一言版本和仓库主分支版本
13+
14+
#### 2023-09-07 v0.0.9 update
15+
16+
- 解决多人上传的文档,会被其他人搜到的问题
17+
- 优化UI界面
18+
19+
#### 2023-08-11 v0.0.7 update
20+
21+
- 优化布局,去掉插件选项,将提取向量模型选项放到主页部分
22+
- 将提示语英语化,便于交流使用。
23+
- 添加项目logo: 🧐
24+
- 更新CLI使用代码
25+
26+
#### 2023-08-05 v0.0.6 update
27+
28+
- 适配更多模型接口,包括在线大模型接口,例如文心一言
29+
- 添加提取特征向量的状态提示
30+
31+
#### 2023-08-04 v0.0.5 update
32+
33+
- 修复了插入数据库数据重复的问题。
34+
35+
#### 2023-07-29 v0.0.4 update
36+
37+
- 基于`streamlit==1.25.0`优化UI
38+
- 优化代码
39+
- 录制UI GIF demo
40+
41+
#### 2023-07-28 v0.0.3 update
42+
43+
- 完成文件解析部分
44+
45+
#### 2023-07-25 v0.0.2 update
46+
47+
- 规范现有目录结构,更加紧凑,提取部分变量到`config.yaml`
48+
- 完善说明文档

docs/index.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
comments: true
3+
hide:
4+
- navigation
5+
- toc
6+
---
7+
8+
<div align="center">
9+
<div>&nbsp;</div>
10+
<div align="center">
11+
<b><font size="6">🧐 Knowledge QA LLM</font></b>
12+
</div>
13+
<div>&nbsp;</div>
14+
<a href=""><img src="https://img.shields.io/badge/Python->=3.8,<3.12-aff.svg"></a>
15+
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
16+
<a href=""><img src="https://img.shields.io/github/v/release/RapidAI/QA-LocalKnowledge-LLM?logo=github"></a>
17+
<a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
18+
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
19+
<a href="https://choosealicense.com/licenses/apache-2.0/"><img alt="GitHub" src="https://img.shields.io/github/license/RapidAI/Knowledge-QA-LLM"></a>
20+
<a href="https://github.com/RapidAI/Knowledge-QA-LLM"><img src="https://img.shields.io/badge/Github-KnowledgeQALLM-brightgreen"></a>
21+
22+
</div>
23+
24+
### 简介
25+
26+
基于本地知识库+LLM的问答系统。该项目的思路是由[langchain-ChatGLM](https://github.com/imClumsyPanda/langchain-ChatGLM)启发而来。
27+
28+
- 缘由:
29+
- 之前使用过这个项目,感觉不是太灵活,部署不太友好。
30+
- 借鉴[如何用大语言模型构建一个知识问答系统](https://mp.weixin.qq.com/s/movaNCWjJGBaes6KxhpYpg)中思路,尝试以此作为实践。
31+
- 优势:
32+
- 整个项目为模块化配置,不依赖`lanchain`库,各部分可轻易替换,代码简单易懂。
33+
- 除需要单独部署大模型接口外,其他部分用CPU即可。
34+
- 支持常见格式文档,包括txt、md、pdf, docx, pptx, excel等等。当然,也可自定义支持其他类型文档。
35+
36+
### 整体流程
37+
38+
#### 解析文档并存储在数据库
39+
40+
```mermaid
41+
flowchart LR
42+
43+
A([Documents]) --ExtractText--> B([sentences])
44+
B --Embeddings--> C([Embeddings])
45+
C --Store--> D[(DataBase)]
46+
```
47+
48+
#### 检索并回答问题
49+
50+
```mermaid
51+
flowchart LR
52+
E([Query]) --Embedding--> F([Embeddings]) --> H[(Database)] --Search--> G([Context])
53+
E --> I([Prompt])
54+
G --> I --> J([LLM]) --> K([Answer])
55+
```
56+
57+
### 使用的工具
58+
59+
- 文档分析: [`extract_office_content`](https://github.com/SWHL/ExtractOfficeContent), [`rapidocr_pdf`](https://github.com/RapidAI/RapidOCRPDF), [`rapidocr_onnxruntime`](https://github.com/RapidAI/RapidOCR)
60+
- 提取语义向量: [`moka-ai/m3e-small`](https://huggingface.co/moka-ai/m3e-base)
61+
- 向量存储: `sqlite`
62+
- 向量检索: [`faiss`](https://github.com/facebookresearch/faiss)
63+
- UI搭建: [`streamlit>=1.25.0`](https://github.com/streamlit/streamlit)

docs/online_demo.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
comments: true
3+
hide:
4+
- navigation
5+
- toc
6+
---
7+
8+
9+
#### 简介
10+
11+
在线demo是基于百度的AI Studio平台搭建,基于文心一言大模型的接口搭建。
12+
13+
因为该项目核心在于利用大模型的总结和提取能力,主打离线私有部署,但是一直没有一个在线demo供大家查看效果。因此有了基于文心一言版的 **🧐 Knowledge QA LLM**
14+
15+
#### Demo源码
16+
17+
基于`erniebot`库来搭建的,如需使用,需要鉴权,提供**Access Token**,具体教程,参见:[link](https://github.com/PaddlePaddle/ERNIE-Bot-SDK/blob/develop/docs/authentication.md)
18+
19+
地址: <https://aistudio.baidu.com/projectdetail/6675380?contributionType=1>
20+
21+
#### 在线Demo
22+
23+
{{< alert text="该Demo主要侧重查看效果,至于工程化则差一些。" />}}
24+
25+
基于文心一言API的文档知识问答系统: <https://aistudio.baidu.com/application/detail/8138>
26+
27+
<div align="center">
28+
<img src="https://github.com/RapidAI/Knowledge-QA-LLM/releases/download/v0.0.1/UIDemo.gif" width="100%" height="100%">
29+
</div>

0 commit comments

Comments
 (0)