Compare commits
128 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbbc32361b | ||
|
|
dc329041f3 | ||
|
|
5feb2e19bf | ||
|
|
ec22cd8320 | ||
|
|
8c7efcbd1a | ||
|
|
afc5947bfb | ||
|
|
40189a6899 | ||
|
|
b73829a25c | ||
|
|
a7c5d3cc05 | ||
|
|
cc36a13f17 | ||
|
|
943abbe0fb | ||
|
|
b13c3c4da5 | ||
|
|
c12aa7fdf7 | ||
|
|
e08e8aa00b | ||
|
|
85e11abc0a | ||
|
|
becee69d6a | ||
|
|
042b0c535a | ||
|
|
f97c29b41e | ||
|
|
4d6616cbfa | ||
|
|
cf37992b5c | ||
|
|
6c4026ccef | ||
|
|
caf31faf31 | ||
|
|
a0832af14b | ||
|
|
677e61416d | ||
|
|
56ba6fa5f7 | ||
|
|
16a31de1c7 | ||
|
|
05b2e9e99c | ||
|
|
ae4243b522 | ||
|
|
5759cbeae0 | ||
|
|
a3d74ec4a6 | ||
|
|
df9ac99ef2 | ||
|
|
ef1e8aef5c | ||
|
|
56dab7abba | ||
|
|
ed1f93d836 | ||
|
|
5ec8aac3ac | ||
|
|
837c132d24 | ||
|
|
8239c58494 | ||
|
|
456686f3d0 | ||
|
|
2099a87908 | ||
|
|
2b2c70e53d | ||
|
|
f32c557bdd | ||
|
|
713332522f | ||
|
|
586607a9ce | ||
|
|
8c70205940 | ||
|
|
c3ccbcb7f6 | ||
|
|
7a6d0ea650 | ||
|
|
7fb6f62cf6 | ||
|
|
af385b1b42 | ||
|
|
5249297cb1 | ||
|
|
9280a21d12 | ||
|
|
650a9dd651 | ||
|
|
42e12d7db1 | ||
|
|
249ed18d15 | ||
|
|
3e4487ad9a | ||
|
|
888642f154 | ||
|
|
963e590dfd | ||
|
|
e547893ac1 | ||
|
|
9125910cfe | ||
|
|
98c458dcf8 | ||
|
|
41b6401c13 | ||
|
|
936e36205e | ||
|
|
e289c4ec53 | ||
|
|
1cbcc62494 | ||
|
|
da31ef286b | ||
|
|
60f62507bd | ||
|
|
75cf3d1e9f | ||
|
|
02cee35a45 | ||
|
|
0cee404c7f | ||
|
|
274ece1d91 | ||
|
|
6bba859060 | ||
|
|
4eaf3a1be0 | ||
|
|
3db690773f | ||
|
|
8a9f1ed29b | ||
|
|
c0dc5a74c9 | ||
|
|
a35cda6873 | ||
|
|
30678d8ebf | ||
|
|
6ce727f9ea | ||
|
|
81e6821174 | ||
|
|
d8290f0809 | ||
|
|
8b72dca533 | ||
|
|
4d64068591 | ||
|
|
af35e17fdb | ||
|
|
5ec303610c | ||
|
|
984baf60f0 | ||
|
|
d065539707 | ||
|
|
129f3a2a30 | ||
|
|
42c26bd155 | ||
|
|
dc467c26b5 | ||
|
|
3aeb510f43 | ||
|
|
405a75e23b | ||
|
|
be47169fa8 | ||
|
|
58a010c12c | ||
|
|
7ba14d2c14 | ||
|
|
00b90f071d | ||
|
|
1c364eca35 | ||
|
|
9384419c9d | ||
|
|
38c093d9ae | ||
|
|
e6c9ca540a | ||
|
|
d85b4c0945 | ||
|
|
1e770088d0 | ||
|
|
7529f51e72 | ||
|
|
be69cfb966 | ||
|
|
6244f6c1fb | ||
|
|
e12f97a73b | ||
|
|
7f96c4ff9b | ||
|
|
138b607ac7 | ||
|
|
b204c55bd1 | ||
|
|
17cbfa05d3 | ||
|
|
a7b9940d7a | ||
|
|
fd8135f50c | ||
|
|
9f96593136 | ||
|
|
ed9e72ec9a | ||
|
|
7fb76cde0b | ||
|
|
38d49ea05f | ||
|
|
7db87c2d09 | ||
|
|
453f3be8ce | ||
|
|
f023f63103 | ||
|
|
65da4653bc | ||
|
|
26888e855b | ||
|
|
e132c622a6 | ||
|
|
17364e9da3 | ||
|
|
e5fe670a6e | ||
|
|
7807b26707 | ||
|
|
16775430ea | ||
|
|
dd5217d8a5 | ||
|
|
9f8d696bbe | ||
|
|
bf81d23de4 | ||
|
|
2390823282 |
@@ -7,4 +7,4 @@ README.md
|
|||||||
.git
|
.git
|
||||||
|
|
||||||
.yalc/
|
.yalc/
|
||||||
yalc.lock
|
yalc.lock
|
||||||
|
|||||||
@@ -3,4 +3,6 @@ AXIOS_PROXY_PORT=33210
|
|||||||
MONGODB_URI=
|
MONGODB_URI=
|
||||||
MY_MAIL=
|
MY_MAIL=
|
||||||
MAILE_CODE=
|
MAILE_CODE=
|
||||||
TOKEN_KEY=
|
TOKEN_KEY=
|
||||||
|
OPENAIKEY=
|
||||||
|
REDIS_URL=
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
{
|
{
|
||||||
"extends": "next/core-web-vitals"
|
"extends": "next/core-web-vitals",
|
||||||
|
"rules": {
|
||||||
|
"react-hooks/rules-of-hooks": 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
.gitignore
vendored
@@ -34,6 +34,6 @@ yarn-error.log*
|
|||||||
# typescript
|
# typescript
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
next-env.d.ts
|
next-env.d.ts
|
||||||
public/trainData/
|
/public/trainData/
|
||||||
.vscode/
|
/.vscode/
|
||||||
platform.json
|
platform.json
|
||||||
10
.husky/pre-commit
Normal file → Executable file
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
. "$(dirname -- "$0")/_/husky.sh"
|
. "$(dirname -- "$0")/_/husky.sh"
|
||||||
|
|
||||||
if command -v npx >/dev/null 2>&1; then
|
if command -v npx >/dev/null 2>&1; then
|
||||||
npx lint-staged
|
npx lint-staged
|
||||||
fi
|
fi
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
dist
|
dist
|
||||||
.vscode
|
.vscode
|
||||||
**/.DS_Store
|
**/.DS_Store
|
||||||
node_modules
|
node_modules
|
||||||
@@ -1,21 +1,20 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
printWidth: 100,
|
printWidth: 100,
|
||||||
tabWidth: 2,
|
tabWidth: 2,
|
||||||
useTabs: false,
|
useTabs: false,
|
||||||
semi: true,
|
semi: true,
|
||||||
singleQuote: true,
|
singleQuote: true,
|
||||||
quoteProps: 'as-needed',
|
quoteProps: 'as-needed',
|
||||||
jsxSingleQuote: false,
|
jsxSingleQuote: false,
|
||||||
trailingComma: 'none',
|
trailingComma: 'none',
|
||||||
bracketSpacing: true,
|
bracketSpacing: true,
|
||||||
jsxBracketSameLine: false,
|
jsxBracketSameLine: false,
|
||||||
arrowParens: 'always',
|
arrowParens: 'always',
|
||||||
rangeStart: 0,
|
rangeStart: 0,
|
||||||
rangeEnd: Infinity,
|
rangeEnd: Infinity,
|
||||||
requirePragma: false,
|
requirePragma: false,
|
||||||
insertPragma: false,
|
insertPragma: false,
|
||||||
proseWrap: 'preserve',
|
proseWrap: 'preserve',
|
||||||
htmlWhitespaceSensitivity: 'css',
|
htmlWhitespaceSensitivity: 'css',
|
||||||
endOfLine: 'lf'
|
endOfLine: 'lf'
|
||||||
};
|
};
|
||||||
|
|
||||||
5
.vscode/settings.json
vendored
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"editor.formatOnType": true,
|
|
||||||
"editor.formatOnSave": true ,
|
|
||||||
"prettier.tabWidth": 2
|
|
||||||
}
|
|
||||||
6
Makefile
@@ -1,4 +1,4 @@
|
|||||||
SERVICE_NAME=doc-gpt
|
SERVICE_NAME=fast-gpt
|
||||||
# Image URL to use all building/pushing image targets
|
# Image URL to use all building/pushing image targets
|
||||||
IMG ?= $(SERVICE_NAME):latest
|
IMG ?= $(SERVICE_NAME):latest
|
||||||
|
|
||||||
@@ -34,13 +34,13 @@ run: ## Run a dev service from host.
|
|||||||
|
|
||||||
.PHONY: docker-build
|
.PHONY: docker-build
|
||||||
docker-build: ## Build docker image with the desktop-frontend.
|
docker-build: ## Build docker image with the desktop-frontend.
|
||||||
docker build -t c121914yu/doc-gpt:latest .
|
docker build -t c121914yu/fast-gpt:latest .
|
||||||
|
|
||||||
##@ Deployment
|
##@ Deployment
|
||||||
|
|
||||||
.PHONY: docker-run
|
.PHONY: docker-run
|
||||||
docker-run: ## Push docker image.
|
docker-run: ## Push docker image.
|
||||||
docker run -d -p 8008:3000 --name doc-gpt -v /web_project/yjl/doc-gpt/logs:/app/.next/logs c121914yu/doc-gpt:latest
|
docker run -d -p 8008:3000 --name fast-gpt -v /web_project/yjl/fast-gpt/logs:/app/.next/logs c121914yu/fast-gpt:latest
|
||||||
|
|
||||||
#TODO: add support of docker push
|
#TODO: add support of docker push
|
||||||
|
|
||||||
|
|||||||
135
README.md
@@ -1,4 +1,6 @@
|
|||||||
# Doc GPT
|
# Fast GPT
|
||||||
|
|
||||||
|
Fast GPT 允许你使用自己的 openai API KEY 来快速的调用 openai 接口,包括 GPT3 及其微调方法,以及最新的 gpt3.5 接口。
|
||||||
|
|
||||||
## 初始化
|
## 初始化
|
||||||
复制 .env.template 成 .env.local ,填写核心参数
|
复制 .env.template 成 .env.local ,填写核心参数
|
||||||
@@ -6,68 +8,105 @@
|
|||||||
```
|
```
|
||||||
AXIOS_PROXY_HOST=axios代理地址,目前 openai 接口都需要走代理,本机的话就填 127.0.0.1
|
AXIOS_PROXY_HOST=axios代理地址,目前 openai 接口都需要走代理,本机的话就填 127.0.0.1
|
||||||
AXIOS_PROXY_PORT=代理端口
|
AXIOS_PROXY_PORT=代理端口
|
||||||
MONGODB_URI=mongo数据库地址
|
MONGODB_URI=mongo数据库地址(例如:mongodb://username:password@ip:27017/?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false)
|
||||||
MY_MAIL=发送验证码邮箱
|
MY_MAIL=发送验证码邮箱
|
||||||
MAILE_CODE=邮箱秘钥
|
MAILE_CODE=邮箱秘钥(代理里设置的是QQ邮箱,不知道怎么找这个 code 的,可以百度搜"nodemailer发送邮件")
|
||||||
TOKEN_KEY=随便填一个,用于生成和校验token
|
TOKEN_KEY=随便填一个,用于生成和校验 token
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm dev
|
pnpm dev
|
||||||
```
|
```
|
||||||
|
|
||||||
## 部署
|
|
||||||
```bash
|
|
||||||
# 本地 docker 打包
|
|
||||||
docker build -t imageName .
|
|
||||||
docker push imageName
|
|
||||||
|
|
||||||
# 服务器拉取部署
|
|
||||||
docker pull imageName
|
|
||||||
docker stop doc-gpt || true
|
|
||||||
docker rm doc-gpt || true
|
|
||||||
# 运行时才把参数写入
|
|
||||||
docker run -d --network=host --name doc-gpt -e AXIOS_PROXY_HOST= -e AXIOS_PROXY_PORT= -e MAILE_CODE= -e TOKEN_KEY= -e MONGODB_URI= imageName
|
|
||||||
```
|
|
||||||
|
|
||||||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
||||||
|
|
||||||
# 介绍页
|
## 部署
|
||||||
|
|
||||||
## 欢迎使用 Doc GPT
|
### docker 模式
|
||||||
|
请准备好 docker, mongo,代理, 和nginx。 镜像走本机的代理,所以用 network=host,port 改成代理的端口,clash 一般都是 7890。
|
||||||
|
|
||||||
时间比较赶,介绍没来得及完善,先直接上怎么使用:
|
#### docker 打包
|
||||||
|
```bash
|
||||||
|
docker build -t imageName:tag .
|
||||||
|
docker push imageName:tag
|
||||||
|
```
|
||||||
|
|
||||||
1. 使用邮箱注册账号。
|
#### 服务器拉取镜像和运行
|
||||||
2. 进入账号页面,添加关联账号,目前只有 openai 的账号可以添加,直接去 openai 官网,把 API Key 粘贴过来。
|
```bash
|
||||||
3. 进入模型页,创建一个模型,建议直接用 ChatGPT。
|
# 服务器拉取部署, imageName 替换成镜像名
|
||||||
4. 在模型列表点击【对话】,即可使用 API 进行聊天。
|
docker pull imageName:tag
|
||||||
|
docker stop fast-gpt || true
|
||||||
|
docker rm fast-gpt || true
|
||||||
|
docker run -d --network=host --name fast-gpt \
|
||||||
|
-e AXIOS_PROXY_HOST=127.0.0.1 \
|
||||||
|
-e AXIOS_PROXY_PORT=7890 \
|
||||||
|
-e MY_MAIL=your email\
|
||||||
|
-e MAILE_CODE=your email code \
|
||||||
|
-e TOKEN_KEY=任意一个内容 \
|
||||||
|
-e MONGODB_URI="mongodb://user:password@127.0.0.0:27017/?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&ssl=false" \
|
||||||
|
imageName:tag
|
||||||
|
```
|
||||||
|
|
||||||
### 模型配置
|
#### 软件教程:docker 安装
|
||||||
|
```bash
|
||||||
|
# 安装docker
|
||||||
|
curl -sSL https://get.daocloud.io/docker | sh
|
||||||
|
sudo systemctl start docker
|
||||||
|
```
|
||||||
|
|
||||||
1. **提示语**:会在每个对话框的第一句自动加入,用于限定该模型的对话内容。
|
#### 软件教程:mongo 安装
|
||||||
|
```bash
|
||||||
|
docker pull mongo:6.0.4
|
||||||
|
docker stop mongo
|
||||||
|
docker rm mongo
|
||||||
|
docker run -d --name mongo \
|
||||||
|
-e MONGO_INITDB_ROOT_USERNAME= \
|
||||||
|
-e MONGO_INITDB_ROOT_PASSWORD= \
|
||||||
|
-v /root/service/mongo:/data/db \
|
||||||
|
mongo:6.0.4
|
||||||
|
|
||||||
|
# 检查 mongo 运行情况, 有成功的 logs 代表访问成功
|
||||||
|
docker logs mongo
|
||||||
|
```
|
||||||
|
#### 软件教程: clash 代理
|
||||||
|
```bash
|
||||||
|
# 下载包
|
||||||
|
curl https://glados.rocks/tools/clash-linux.zip -o clash.zip
|
||||||
|
# 解压
|
||||||
|
unzip clash.zip
|
||||||
|
# 下载终端配置⽂件(改成自己配置文件路径)
|
||||||
|
curl https://update.glados-config.com/clash/98980/8f30944/70870/glados-terminal.yaml > config.yaml
|
||||||
|
# 赋予运行权限
|
||||||
|
chmod +x ./clash-linux-amd64-v1.10.0
|
||||||
|
# 记得配置端口变量:
|
||||||
|
export ALL_PROXY=socks5://127.0.0.1:7891
|
||||||
|
export http_proxy=http://127.0.0.1:7890
|
||||||
|
export https_proxy=http://127.0.0.1:7890
|
||||||
|
export HTTP_PROXY=http://127.0.0.1:7890
|
||||||
|
export HTTPS_PROXY=http://127.0.0.1:7890
|
||||||
|
|
||||||
2. **单句最大长度**:每个聊天,单次输入内容的最大长度。
|
# 运行脚本: 删除clash - 到 clash 目录 - 删除缓存 - 执行运行
|
||||||
|
# 会生成一个 nohup.out 文件,可以看到 clash 的 logs
|
||||||
|
OLD_PROCESS=$(pgrep clash)
|
||||||
|
if [ ! -z "$OLD_PROCESS" ]; then
|
||||||
|
echo "Killing old process: $OLD_PROCESS"
|
||||||
|
kill $OLD_PROCESS
|
||||||
|
fi
|
||||||
|
sleep 2
|
||||||
|
cd **/clash
|
||||||
|
rm -f ./nohup.out || true
|
||||||
|
rm -f ./cache.db || true
|
||||||
|
nohup ./clash-linux-amd64-v1.10.0 -d ./ &
|
||||||
|
echo "Restart clash"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 软件教程:Nginx
|
||||||
|
...没写,这个百度吧。
|
||||||
|
|
||||||
3. **上下文最大长度**:每个聊天,最多的轮数除以2,建议设置为偶数。可以持续聊天,但是旧的聊天内容会被截断,AI 就不会知道被截取的内容。
|
#### redis
|
||||||
例如:上下文最大长度为6。在第 4 轮对话时,第一轮对话的内容不会被计入。
|
|
||||||
|
|
||||||
4. **过期时间**:生成对话框后,这个对话框多久过期。
|
```bash
|
||||||
|
# 索引
|
||||||
5. **聊天最大加载次数**:单个对话框最多被加载几次,设置为-1代表不限制,正数代表只能加载 n 次,防止被盗刷。
|
# FT.CREATE idx:model:data ON JSON PREFIX 1 model:data: SCHEMA $.modelId AS modelId TAG $.dataId AS dataId TAG $.vector AS vector VECTOR FLAT 6 DIM 1536 DISTANCE_METRIC COSINE TYPE FLOAT32
|
||||||
|
# FT.CREATE idx:model:data:hash ON HASH PREFIX 1 model:data: SCHEMA modelId TAG dataId TAG vector VECTOR FLAT 6 DIM 1536 DISTANCE_METRIC COSINE TYPE FLOAT32
|
||||||
### 对话框介绍
|
FT.CREATE idx:model:data ON HASH PREFIX 1 model:data: SCHEMA modelId TAG userId TAG q TEXT text TEXT vector VECTOR FLAT 6 DIM 1536 DISTANCE_METRIC COSINE TYPE FLOAT32
|
||||||
|
```
|
||||||
1. 每个对话框以 windowId 作为标识。
|
|
||||||
2. 每次点击【对话】,都会生成新的对话框,无法回到旧的对话框。对话框内刷新,会恢复对话内容。
|
|
||||||
3. 直接分享对话框(网页)的链接给朋友,会共享同一个对话内容。但是!!!千万不要两个人同时用一个链接,会串味,还没解决这个问题。
|
|
||||||
4. 如果想分享一个纯的对话框,可以把链接里 windowId 参数去掉。例如:
|
|
||||||
|
|
||||||
* 当前网页链接:http://docgpt.ahapocket.cn/chat?chatId=6402c9f64cb5d6283f764&windowId=6402c94cb5d6283f76fb49
|
|
||||||
* 分享链接应为:http://docgpt.ahapocket.cn/chat?chatId=6402c9f64cb5d6283f764
|
|
||||||
|
|
||||||
### 其他问题
|
|
||||||
还有其他问题,可以加我 wx,拉个交流群大家一起聊聊。
|
|
||||||

|
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
{"prompt": "sealos的介绍", "completion": "sealos 是以 kubernetes 为内核的云操作系统发行版"}
|
|
||||||
{"prompt": "sealos是什么", "completion": "sealos 是以 kubernetes 为内核的云操作系统发行版"}
|
|
||||||
{"prompt": "sealos安装的先决条件", "completion": "sealos 是一个简单的 go 二进制文件,可以安装在大多数 Linux 操作系统中。"}
|
|
||||||
{"prompt": "sealos的CPU架构", "completion": "目前支持 amd64 和 arm64 架构。"}
|
|
||||||
@@ -6,7 +6,18 @@ const isDev = process.env.NODE_ENV === 'development';
|
|||||||
const nextConfig = {
|
const nextConfig = {
|
||||||
output: 'standalone',
|
output: 'standalone',
|
||||||
reactStrictMode: false,
|
reactStrictMode: false,
|
||||||
compress: true
|
compress: true,
|
||||||
|
webpack(config) {
|
||||||
|
config.module.rules = config.module.rules.concat([
|
||||||
|
{
|
||||||
|
test: /\.svg$/i,
|
||||||
|
issuer: /\.[jt]sx?$/,
|
||||||
|
use: ['@svgr/webpack']
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = nextConfig;
|
module.exports = nextConfig;
|
||||||
|
|||||||
23
package.json
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "docgpt",
|
"name": "fastgpt",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -13,24 +13,25 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@chakra-ui/icons": "^2.0.17",
|
"@chakra-ui/icons": "^2.0.17",
|
||||||
"@chakra-ui/react": "^2.5.1",
|
"@chakra-ui/react": "^2.5.1",
|
||||||
"@chakra-ui/system": "^2.5.1",
|
|
||||||
"@emotion/react": "^11.10.6",
|
"@emotion/react": "^11.10.6",
|
||||||
"@emotion/styled": "^11.10.6",
|
"@emotion/styled": "^11.10.6",
|
||||||
"@next/font": "13.1.6",
|
"@next/font": "13.1.6",
|
||||||
"@reduxjs/toolkit": "^1.9.3",
|
|
||||||
"@tanstack/react-query": "^4.24.10",
|
"@tanstack/react-query": "^4.24.10",
|
||||||
"@types/nprogress": "^0.2.0",
|
"@types/nprogress": "^0.2.0",
|
||||||
"axios": "^1.3.3",
|
"axios": "^1.3.3",
|
||||||
"crypto": "^1.0.1",
|
"crypto": "^1.0.1",
|
||||||
"dayjs": "^1.11.7",
|
"dayjs": "^1.11.7",
|
||||||
"eslint": "8.34.0",
|
"eventsource-parser": "^0.1.0",
|
||||||
"eslint-config-next": "13.1.6",
|
|
||||||
"formidable": "^2.1.1",
|
"formidable": "^2.1.1",
|
||||||
"framer-motion": "^9.0.6",
|
"framer-motion": "^9.0.6",
|
||||||
|
"gpt-token-utils": "^1.2.0",
|
||||||
"hyperdown": "^2.4.29",
|
"hyperdown": "^2.4.29",
|
||||||
"immer": "^9.0.19",
|
"immer": "^9.0.19",
|
||||||
"jsonwebtoken": "^9.0.0",
|
"jsonwebtoken": "^9.0.0",
|
||||||
|
"lodash": "^4.17.21",
|
||||||
|
"mammoth": "^1.5.1",
|
||||||
"mongoose": "^6.10.0",
|
"mongoose": "^6.10.0",
|
||||||
|
"nanoid": "^4.0.1",
|
||||||
"next": "13.1.6",
|
"next": "13.1.6",
|
||||||
"nodemailer": "^6.9.1",
|
"nodemailer": "^6.9.1",
|
||||||
"nprogress": "^0.2.0",
|
"nprogress": "^0.2.0",
|
||||||
@@ -40,17 +41,20 @@
|
|||||||
"react-hook-form": "^7.43.1",
|
"react-hook-form": "^7.43.1",
|
||||||
"react-markdown": "^8.0.5",
|
"react-markdown": "^8.0.5",
|
||||||
"react-syntax-highlighter": "^15.5.0",
|
"react-syntax-highlighter": "^15.5.0",
|
||||||
|
"redis": "^4.6.5",
|
||||||
|
"rehype-katex": "^6.0.2",
|
||||||
"remark-gfm": "^3.0.1",
|
"remark-gfm": "^3.0.1",
|
||||||
|
"remark-math": "^5.1.1",
|
||||||
"sass": "^1.58.3",
|
"sass": "^1.58.3",
|
||||||
"sharp": "^0.31.3",
|
"sharp": "^0.31.3",
|
||||||
"tunnel": "^0.0.6",
|
"tunnel": "^0.0.6",
|
||||||
"typescript": "4.9.5",
|
|
||||||
"uuid": "^9.0.0",
|
|
||||||
"zustand": "^4.3.5"
|
"zustand": "^4.3.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@svgr/webpack": "^6.5.1",
|
||||||
"@types/formidable": "^2.0.5",
|
"@types/formidable": "^2.0.5",
|
||||||
"@types/jsonwebtoken": "^9.0.1",
|
"@types/jsonwebtoken": "^9.0.1",
|
||||||
|
"@types/lodash": "^4.14.191",
|
||||||
"@types/node": "18.14.0",
|
"@types/node": "18.14.0",
|
||||||
"@types/nodemailer": "^6.4.7",
|
"@types/nodemailer": "^6.4.7",
|
||||||
"@types/react": "18.0.28",
|
"@types/react": "18.0.28",
|
||||||
@@ -58,9 +62,12 @@
|
|||||||
"@types/react-syntax-highlighter": "^15.5.6",
|
"@types/react-syntax-highlighter": "^15.5.6",
|
||||||
"@types/tunnel": "^0.0.3",
|
"@types/tunnel": "^0.0.3",
|
||||||
"@types/uuid": "^9.0.1",
|
"@types/uuid": "^9.0.1",
|
||||||
|
"eslint": "8.34.0",
|
||||||
|
"eslint-config-next": "13.1.6",
|
||||||
"husky": "^8.0.3",
|
"husky": "^8.0.3",
|
||||||
"lint-staged": "^13.1.2",
|
"lint-staged": "^13.1.2",
|
||||||
"prettier": "^2.8.4"
|
"prettier": "^2.8.4",
|
||||||
|
"typescript": "4.9.5"
|
||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
"./src/**/*.{ts,tsx,scss}": "npm run format"
|
"./src/**/*.{ts,tsx,scss}": "npm run format"
|
||||||
|
|||||||
3143
pnpm-lock.yaml
generated
@@ -1,21 +1,15 @@
|
|||||||
{"prompt":"什么是死锁?如何避免死锁?\n\n###\n\n","completion":" 死锁是指两个或多个进程在执行过程中,由于互相等待对方释放资源而造成的一种僵局。避免死锁的常用方法包括:破坏互斥条件、破坏请求和保持条件、破坏不剥夺条件、破坏循环等待条件。###"}
|
{"prompt":"什么是百度plato?</s>","completion":" 百度PLATO是一种聊天机器人,发布于2021年10月,参数量为百亿,是GPT-3的1/17。</s>"}
|
||||||
{"prompt":"什么是虚拟内存?\n\n###\n\n","completion":" 虚拟内存是指一种计算机内存管理技术,它使得操作系统可以将进程所使用的虚拟地址空间映射到物理内存中,从而可以让多个进程共享物理内存。当物理内存不足时,操作系统会将一部分不常用的内存页面暂时存储到硬盘上,这样就可以释放出更多的物理内存供其他进程使用。###"}
|
{"prompt":"与百度plato相比,chatgpt有哪些优点?</s>","completion":" 与百度PLATO相比,ChatGPT体现出耐心、体贴和妥当,被网友们普遍称赞。ChatGPT可以用于个性化聊天机器人,例如生成一个很相似的数字人,提供一位温柔健谈的朋友,指导语言学习等。</s>"}
|
||||||
{"prompt":"什么是分页和分段?有什么区别?\n\n###\n\n","completion":" 分页和分段是虚拟内存管理中常用的两种技术。分页是将虚拟地址空间划分为固定大小的页面,而分段则是将虚拟地址空间划分为不同大小的段。区别在于,分页的页面大小是固定的,而分段的段大小是可变的;另外,分页是为了实现虚拟内存,而分段则是为了实现逻辑地址空间的管理。###"}
|
{"prompt":"replika是什么?</s>","completion":" Replika是一个产品,使用ChatGPT的技术,可以根据自己的需求生成一个虚拟女友、男友或单纯的朋友。目前还引起了不少有关性骚扰、家暴、隐私等问题的争议。</s>"}
|
||||||
{"prompt":"什么是lru算法?\n\n###\n\n","completion":" lru算法是一种页面置换算法,它的全称是least recently used,即最近最少使用算法。lru算法的基本思想是在物理内存中维护一个页面访问历史记录,当需要替换一页时,选择最近最少被访问的页面进行替换。###"}
|
{"prompt":"聊天机器人可以用于哪些场景?</s>","completion":" 聊天机器人可以用于很多陪伴的场景,例如亲人去世、抑郁症或自闭症患者、语言学习、小朋友、养老院或独居在家的老人等。</s>"}
|
||||||
{"prompt":"什么是文件系统?\n\n###\n\n","completion":" 文件系统是一种用于管理计算机存储设备上文件和目录的组织方式。文件系统通常包括存储管理、目录管理、文件访问控制、文件系统维护等功能。常见的文件系统包括fat、ntfs、ext等。###"}
|
{"prompt":"有人反对使用聊天机器人解决孤独困境,为什么?</s>","completion":" 有人反对使用聊天机器人解决孤独困境,认为孤独困境应该用人与人真正的交往解决,机器是冰冷的。</s>"}
|
||||||
{"prompt":"什么是并发和并行?\n\n###\n\n","completion":" 并发是指多个任务交替执行的情况,而并行是指多个任务同时执行的情况。并发的实现方式是通过时间分片技术,而并行的实现方式是通过多核处理器等硬件设备。###"}
|
{"prompt":"为什么聊天机器人仍然需要解决当下的需求?</s>","completion":" 在社会结构性的困局还没有解开时,长夜漫漫的独身打工人、养老院里卧床的老人、父母在外留守在家的儿童,当下的需求仍需要解决。</s>"}
|
||||||
{"prompt":"什么是系统调用?\n\n###\n\n","completion":" 系统调用是操作系统中的一种机制,它允许用户空间程序请求操作系统内核提供一些服务,如读写文件、创建进程等。系统调用通常是通过软中断实现的###"}
|
{"prompt":"ai在未来会被戴上什么样的帽子?</s>","completion":" AI在未来各个场景下都会被戴上一个很虚无的帽子:“机器是冰冷的”、“没有灵魂”、“它并不是真正在思考”。</s>"}
|
||||||
{"prompt":"什么是进程间通信?\n\n###\n\n","completion":" 进程间通信是指不同进程之间进行数据交换和通信的机制,常见的方式有管道、共享内存、消息队列、信号量等。###"}
|
{"prompt":"怎么回答“ai是否真正思考”的问题?</s>","completion":" 引用乔姆斯基老爷子的那句名言:“思考是人类特性。AI有天会思考吗?那就像是在问潜水艇会不会游泳一样。”</s>"}
|
||||||
{"prompt":"什么是文件描述符?\n\n###\n\n","completion":" 文件描述符是一个非负整数,它是操作系统内核为了管理打开的文件而维护的一种抽象概念。应用程序使用文件描述符来访问文件或其他i\/o设备###"}
|
{"prompt":"聊天机器人与人类完成任务的方式有何不同?</s>","completion":" 聊天机器人与人类完成任务的方式不同,但并不重要。它们能跟人一样完成任务,这是最重要的。</s>"}
|
||||||
{"prompt":"什么是中断? \n\n###\n\n","completion":" 中断是指计算机硬件或软件在执行程序时,暂停当前程序的执行,转而去执行另一个程序或处理器所需的其他任务的过程。中断可以是外部中断、内部中断或软中断。###"}
|
{"prompt":"聊天机器人的发展前景如何?</s>","completion":" 聊天机器人的发展前景广阔,可以应用于很多领域,例如客服、教育、医疗等。</s>"}
|
||||||
{"prompt":"什么是页表?\n\n###\n\n","completion":" 页表是一种数据结构,它用于将虚拟地址转换为物理地址。在虚拟内存中,每个进程都有自己的页表,它描述了虚拟地址空间中每个页面对应的物理地址。 ###"}
|
{"prompt":"什么是百度plato?</s>","completion":" 百度PLATO是一种聊天机器人,发布于2021年10月,参数量为百亿,是GPT-3的1/17。</s>"}
|
||||||
{"prompt":"什么是僵尸进程?\n\n###\n\n","completion":" 僵尸进程是指一个已经完成执行但其父进程还没有对其进行善后处理的进程,它会一直占用系统资源,直到其父进程将其处理为止。###"}
|
{"prompt":"与百度plato相比,chatgpt有哪些优点?</s>","completion":" 与百度PLATO相比,ChatGPT体现出耐心、体贴和妥当,被网友们普遍称赞。ChatGPT还可以用于很多陪伴的场景,如亲人去世、抑郁症或自闭症患者、语言学习、小朋友、养老院或独居在家的老人等。</s>"}
|
||||||
{"prompt":"什么是守护进程? \n\n###\n\n","completion":" 守护进程是一种在后台运行的特殊进程,它通常用于提供某种服务或执行某种系统任务。守护进程通常不与任何用户界面交互,也不会终止,直到操作系统关机或重启。###"}
|
{"prompt":"replika是什么产品?</s>","completion":" Replika是一个产品,用的是ChatGPT的技术。用户可以根据自己的需求生成一个虚拟女友、男友或单纯的朋友。目前还引起了不少有关性骚扰、家暴、隐私等问题的争议。</s>"}
|
||||||
{"prompt":"计算机操作系统是什么?\n\n###\n\n","completion":" 计算机操作系统是一种控制计算机硬件和协调应用程序运行的系统软件。###"}
|
{"prompt":"聊天机器人能够解决哪些现实问题?</s>","completion":" 聊天机器人能够解决现代人面临的孤独困境,如长夜漫漫的独身打工人、养老院里卧床的老人、父母在外留守在家的儿童等。</s>"}
|
||||||
{"prompt":"什么是进程?\n\n###\n\n","completion":" 进程是一个正在执行中的程序实例,它包括了程序计数器、堆栈、数据区等信息。###"}
|
{"prompt":"有人反对使用聊天机器人解决孤独困境,为什么?</s>","completion":" 有人反对使用聊天机器人解决孤独困境,认为孤独困境应该用人与人真正的交往解决,机器是冰冷的。</s>"}
|
||||||
{"prompt":"什么是线程?\n\n###\n\n","completion":" 线程是操作系统能够进行运算调度的最小单位,它是进程的一个执行流程,共享进程的地址空间。###"}
|
|
||||||
{"prompt":"什么是虚拟内存?\n\n###\n\n","completion":" 虚拟内存是一种计算机系统内存管理技术,它使得应用程序认为它拥有连续的可用的内存,而实际上它通常是被分成多个物理内存碎片。###"}
|
|
||||||
{"prompt":"什么是死锁?\n\n###\n\n","completion":" 死锁是指两个或多个进程在执行过程中,因争夺资源而造成的一种互相等待的现象,导致所有进程都无法继续执行。###"}
|
|
||||||
{"prompt":"什么是缓存?\n\n###\n\n","completion":" 缓存是指在计算机中暂存数据的高速存储器,它可以提高计算机对数据的访问速度,避免频繁地访问较慢的主存储器。###"}
|
|
||||||
{"prompt":"什么是文件系统?\n\n###\n\n","completion":" 文件系统是计算机中用来管理和组织文件的一种机制,它通过一系列的数据结构来描述文件和目录的组织方式,以及文件如何存储和访问。###"}
|
|
||||||
{"prompt":"什么是调度算法?\n\n###\n\n","completion":" 调度算法是指操作系统中用来决定进程或线程在cpu上执行顺序的一种算法,它的目标是最大化系统吞吐量、最小化响应时间或最大化资源利用率等。###"}
|
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
BIN
public/icon/logo.png
Normal file
|
After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 117 KiB |
BIN
public/imgs/wxcode300.jpg
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
public/imgs/wxerweima300.jpg
Normal file
|
After Width: | Height: | Size: 50 KiB |
15792
public/js/pdf.js
Normal file
64521
public/js/pdf.worker.js
vendored
Normal file
1
public/js/qrcode.min.js
vendored
Normal file
BIN
public/logo.png
|
Before Width: | Height: | Size: 117 KiB |
@@ -1,20 +1,18 @@
|
|||||||
import { GET, POST, DELETE } from './request';
|
import { GET, POST, DELETE } from './request';
|
||||||
import { ChatItemType, ChatSiteType, ChatSiteItemType } from '@/types/chat';
|
import type { ChatItemType, ChatSiteItemType } from '@/types/chat';
|
||||||
|
import type { InitChatResponse } from './response/chat';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取一个聊天框的ID
|
* 获取一个聊天框的ID
|
||||||
*/
|
*/
|
||||||
export const getChatSiteId = (modelId: string) => GET<string>(`/chat/generate?modelId=${modelId}`);
|
export const getChatSiteId = (modelId: string, isShare = false) =>
|
||||||
|
GET<string>(`/chat/generate?modelId=${modelId}&isShare=${isShare ? 'true' : 'false'}`);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取初始化聊天内容
|
* 获取初始化聊天内容
|
||||||
*/
|
*/
|
||||||
export const getInitChatSiteInfo = (chatId: string, windowId: string = '') =>
|
export const getInitChatSiteInfo = (chatId: string) =>
|
||||||
GET<{
|
GET<InitChatResponse>(`/chat/init?chatId=${chatId}`);
|
||||||
windowId: string;
|
|
||||||
chatSite: ChatSiteType;
|
|
||||||
history: ChatItemType[];
|
|
||||||
}>(`/chat/init?chatId=${chatId}&windowId=${windowId}`);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 GPT3 prompt
|
* 发送 GPT3 prompt
|
||||||
@@ -35,31 +33,13 @@ export const postGPT3SendPrompt = ({
|
|||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 预发 prompt 进行存储
|
* 存储一轮对话
|
||||||
*/
|
*/
|
||||||
export const postChatGptPrompt = ({
|
export const postSaveChat = (data: { chatId: string; prompts: ChatItemType[] }) =>
|
||||||
prompt,
|
POST('/chat/saveChat', data);
|
||||||
windowId,
|
|
||||||
chatId
|
|
||||||
}: {
|
|
||||||
prompt: ChatSiteItemType;
|
|
||||||
windowId: string;
|
|
||||||
chatId: string;
|
|
||||||
}) =>
|
|
||||||
POST<string>(`/chat/preChat`, {
|
|
||||||
windowId,
|
|
||||||
prompt: {
|
|
||||||
obj: prompt.obj,
|
|
||||||
value: prompt.value
|
|
||||||
},
|
|
||||||
chatId
|
|
||||||
});
|
|
||||||
/* 获取 Chat 的 Event 对象,进行持续通信 */
|
|
||||||
export const getChatGPTSendEvent = (chatId: string, windowId: string) =>
|
|
||||||
new EventSource(`/api/chat/chatGpt?chatId=${chatId}&windowId=${windowId}`);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除最后一句
|
* 删除一句对话
|
||||||
*/
|
*/
|
||||||
export const delLastMessage = (windowId?: string) =>
|
export const delChatRecordByIndex = (chatId: string, index: number) =>
|
||||||
windowId ? DELETE(`/chat/delLastMessage?windowId=${windowId}`) : null;
|
DELETE(`/chat/delChatRecordByIndex?chatId=${chatId}&index=${index}`);
|
||||||
|
|||||||
25
src/api/data.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { GET, POST, DELETE, PUT } from './request';
|
||||||
|
import { RequestPaging } from '../types/index';
|
||||||
|
import { Obj2Query } from '@/utils/tools';
|
||||||
|
import type { DataListItem } from '@/types/data';
|
||||||
|
import type { PagingData } from '../types/index';
|
||||||
|
import type { DataItemSchema } from '@/types/mongoSchema';
|
||||||
|
import type { CreateDataProps } from '@/pages/data/components/CreateDataModal';
|
||||||
|
|
||||||
|
export const getDataList = () => GET<DataListItem[]>(`/data/getDataList`);
|
||||||
|
|
||||||
|
export const postData = (data: CreateDataProps) => POST<string>(`/data/postData`, data);
|
||||||
|
|
||||||
|
export const postSplitData = (dataId: string, text: string) =>
|
||||||
|
POST(`/data/splitData`, { dataId, text });
|
||||||
|
|
||||||
|
export const updateDataName = (dataId: string, name: string) =>
|
||||||
|
PUT(`/data/putDataName?dataId=${dataId}&name=${name}`);
|
||||||
|
|
||||||
|
export const delData = (dataId: string) => DELETE(`/data/delData?dataId=${dataId}`);
|
||||||
|
|
||||||
|
type GetDataItemsProps = RequestPaging & {
|
||||||
|
dataId: string;
|
||||||
|
};
|
||||||
|
export const getDataItems = (data: GetDataItemsProps) =>
|
||||||
|
GET<PagingData<DataItemSchema>>(`/data/getDataItems?${Obj2Query(data)}`);
|
||||||
51
src/api/fetch.ts
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import { getToken } from '../utils/user';
|
||||||
|
interface StreamFetchProps {
|
||||||
|
url: string;
|
||||||
|
data: any;
|
||||||
|
onMessage: (text: string) => void;
|
||||||
|
abortSignal: AbortController;
|
||||||
|
}
|
||||||
|
export const streamFetch = ({ url, data, onMessage, abortSignal }: StreamFetchProps) =>
|
||||||
|
new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const res = await fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: getToken() || ''
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
signal: abortSignal.signal
|
||||||
|
});
|
||||||
|
const reader = res.body?.getReader();
|
||||||
|
if (!reader) return;
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
let responseText = '';
|
||||||
|
|
||||||
|
const read = async () => {
|
||||||
|
const { done, value } = await reader?.read();
|
||||||
|
if (done) {
|
||||||
|
if (res.status === 200) {
|
||||||
|
resolve(responseText);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const parseError = JSON.parse(responseText);
|
||||||
|
reject(parseError?.message || '请求异常');
|
||||||
|
} catch (err) {
|
||||||
|
reject('请求异常');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const text = decoder.decode(value).replace(/<br\/>/g, '\n');
|
||||||
|
res.status === 200 && onMessage(text);
|
||||||
|
responseText += text;
|
||||||
|
read();
|
||||||
|
};
|
||||||
|
read();
|
||||||
|
} catch (err: any) {
|
||||||
|
console.log(err, '====');
|
||||||
|
reject(typeof err === 'string' ? err : err?.message || '请求异常');
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -1,28 +1,107 @@
|
|||||||
import { GET, POST, DELETE, PUT } from './request';
|
import { GET, POST, DELETE, PUT } from './request';
|
||||||
import type { ModelType } from '@/types/model';
|
import type { ModelSchema, ModelDataSchema, ModelSplitDataSchema } from '@/types/mongoSchema';
|
||||||
import { ModelUpdateParams } from '@/types/model';
|
import { ModelUpdateParams } from '@/types/model';
|
||||||
import { TrainingItemType } from '../types/training';
|
import { TrainingItemType } from '../types/training';
|
||||||
|
import { RequestPaging } from '../types/index';
|
||||||
|
import { Obj2Query } from '@/utils/tools';
|
||||||
|
|
||||||
export const getMyModels = () => GET<ModelType[]>('/model/list');
|
/**
|
||||||
|
* 获取模型列表
|
||||||
|
*/
|
||||||
|
export const getMyModels = () => GET<ModelSchema[]>('/model/list');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个模型
|
||||||
|
*/
|
||||||
export const postCreateModel = (data: { name: string; serviceModelName: string }) =>
|
export const postCreateModel = (data: { name: string; serviceModelName: string }) =>
|
||||||
POST<ModelType>('/model/create', data);
|
POST<ModelSchema>('/model/create', data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 ID 删除模型
|
||||||
|
*/
|
||||||
export const delModelById = (id: string) => DELETE(`/model/del?modelId=${id}`);
|
export const delModelById = (id: string) => DELETE(`/model/del?modelId=${id}`);
|
||||||
|
|
||||||
export const getModelById = (id: string) => GET<ModelType>(`/model/detail?modelId=${id}`);
|
/**
|
||||||
|
* 根据 ID 获取模型
|
||||||
|
*/
|
||||||
|
export const getModelById = (id: string) => GET<ModelSchema>(`/model/detail?modelId=${id}`);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 ID 更新模型
|
||||||
|
*/
|
||||||
export const putModelById = (id: string, data: ModelUpdateParams) =>
|
export const putModelById = (id: string, data: ModelUpdateParams) =>
|
||||||
PUT(`/model/update?modelId=${id}`, data);
|
PUT(`/model/update?modelId=${id}`, data);
|
||||||
|
|
||||||
export const postTrainModel = (id: string, form: FormData) =>
|
export const postTrainModel = (id: string, form: FormData) =>
|
||||||
POST(`/model/train?modelId=${id}`, form, {
|
POST(`/model/train/train?modelId=${id}`, form, {
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'multipart/form-data'
|
'content-type': 'multipart/form-data'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const putModelTrainingStatus = (id: string) => PUT(`/model/putTrainStatus?modelId=${id}`);
|
export const putModelTrainingStatus = (id: string) =>
|
||||||
|
PUT(`/model/train/putTrainStatus?modelId=${id}`);
|
||||||
|
|
||||||
export const getModelTrainings = (id: string) =>
|
export const getModelTrainings = (id: string) =>
|
||||||
GET<TrainingItemType[]>(`/model/getTrainings?modelId=${id}`);
|
GET<TrainingItemType[]>(`/model/train/getTrainings?modelId=${id}`);
|
||||||
|
|
||||||
|
/* 模型 data */
|
||||||
|
|
||||||
|
type GetModelDataListProps = RequestPaging & {
|
||||||
|
modelId: string;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* 获取模型的知识库数据
|
||||||
|
*/
|
||||||
|
export const getModelDataList = (props: GetModelDataListProps) =>
|
||||||
|
GET(`/model/data/getModelData?${Obj2Query(props)}`);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取导出数据(不分页)
|
||||||
|
*/
|
||||||
|
export const getExportDataList = (modelId: string) =>
|
||||||
|
GET<string>(`/model/data/exportModelData?modelId=${modelId}`);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模型正在拆分数据的数量
|
||||||
|
*/
|
||||||
|
export const getModelSplitDataListLen = (modelId: string) =>
|
||||||
|
GET<number>(`/model/data/getSplitData?modelId=${modelId}`);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 web 页面内容
|
||||||
|
*/
|
||||||
|
export const getWebContent = (url: string) => POST<string>(`/model/data/fetchingUrlData`, { url });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手动输入数据
|
||||||
|
*/
|
||||||
|
export const postModelDataInput = (data: {
|
||||||
|
modelId: string;
|
||||||
|
data: { text: ModelDataSchema['text']; q: ModelDataSchema['q'] }[];
|
||||||
|
}) => POST<number>(`/model/data/pushModelDataInput`, data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拆分数据
|
||||||
|
*/
|
||||||
|
export const postModelDataSplitData = (data: { modelId: string; text: string; prompt: string }) =>
|
||||||
|
POST(`/model/data/splitData`, data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json导入数据
|
||||||
|
*/
|
||||||
|
export const postModelDataJsonData = (
|
||||||
|
modelId: string,
|
||||||
|
jsonData: { prompt: string; completion: string; vector?: number[] }[]
|
||||||
|
) => POST(`/model/data/pushModelDataJson`, { modelId, data: jsonData });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新模型数据
|
||||||
|
*/
|
||||||
|
export const putModelDataById = (data: { dataId: string; text: string; q?: string }) =>
|
||||||
|
PUT('/model/data/putModelData', data);
|
||||||
|
/**
|
||||||
|
* 删除一条模型数据
|
||||||
|
*/
|
||||||
|
export const delOneModelData = (dataId: string) =>
|
||||||
|
DELETE(`/model/data/delModelDataById?dataId=${dataId}`);
|
||||||
|
|||||||
@@ -34,10 +34,10 @@ function responseSuccess(response: AxiosResponse<ResponseDataType>) {
|
|||||||
*/
|
*/
|
||||||
function checkRes(data: ResponseDataType) {
|
function checkRes(data: ResponseDataType) {
|
||||||
if (data === undefined) {
|
if (data === undefined) {
|
||||||
console.log(data, 'data is empty');
|
console.log('error->', data, 'data is empty');
|
||||||
return Promise.reject('服务器异常');
|
return Promise.reject('服务器异常');
|
||||||
} else if (data.code < 200 || data.code >= 400) {
|
} else if (data.code < 200 || data.code >= 400) {
|
||||||
return Promise.reject(data.message);
|
return Promise.reject(data);
|
||||||
}
|
}
|
||||||
return data.data;
|
return data.data;
|
||||||
}
|
}
|
||||||
@@ -46,24 +46,23 @@ function checkRes(data: ResponseDataType) {
|
|||||||
* 响应错误
|
* 响应错误
|
||||||
*/
|
*/
|
||||||
function responseError(err: any) {
|
function responseError(err: any) {
|
||||||
console.error('请求错误', err);
|
console.log('error->', '请求错误', err);
|
||||||
|
|
||||||
if (!err) {
|
if (!err) {
|
||||||
return Promise.reject('未知错误');
|
return Promise.reject({ message: '未知错误' });
|
||||||
}
|
}
|
||||||
if (typeof err === 'string') {
|
if (typeof err === 'string') {
|
||||||
return Promise.reject(err);
|
return Promise.reject({ message: err });
|
||||||
}
|
}
|
||||||
if (err.response) {
|
if (err.response) {
|
||||||
// 有报错响应
|
// 有报错响应
|
||||||
const res = err.response;
|
const res = err.response;
|
||||||
/* token过期,判断请求token与本地是否相同,若不同需要重发 */
|
|
||||||
if (res.data.code in TOKEN_ERROR_CODE) {
|
if (res.data.code in TOKEN_ERROR_CODE) {
|
||||||
clearToken();
|
clearToken();
|
||||||
return Promise.reject('token过期,重新登录');
|
return Promise.reject({ message: 'token过期,重新登录' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Promise.reject('未知错误');
|
return Promise.reject(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 创建请求实例 */
|
/* 创建请求实例 */
|
||||||
|
|||||||
13
src/api/response/chat.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import type { ChatPopulate, ModelSchema } from '@/types/mongoSchema';
|
||||||
|
import type { ChatItemType } from '@/types/chat';
|
||||||
|
|
||||||
|
export type InitChatResponse = {
|
||||||
|
chatId: string;
|
||||||
|
modelId: string;
|
||||||
|
name: string;
|
||||||
|
avatar: string;
|
||||||
|
intro: string;
|
||||||
|
chatModel: ModelSchema.service.chatModel; // 对话模型名
|
||||||
|
modelName: ModelSchema.service.modelName; // 底层模型
|
||||||
|
history: ChatItemType[];
|
||||||
|
};
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
import { GET, POST, PUT } from './request';
|
import { GET, POST, PUT } from './request';
|
||||||
import { createHashPassword } from '@/utils/tools';
|
import { createHashPassword, Obj2Query } from '@/utils/tools';
|
||||||
import { ResLogin } from './response/user';
|
import { ResLogin } from './response/user';
|
||||||
import { EmailTypeEnum } from '@/constants/common';
|
import { EmailTypeEnum } from '@/constants/common';
|
||||||
import { UserType, UserUpdateParams } from '@/types/user';
|
import { UserType, UserUpdateParams } from '@/types/user';
|
||||||
|
import type { PagingData, RequestPaging } from '@/types';
|
||||||
|
import { BillSchema, PaySchema } from '@/types/mongoSchema';
|
||||||
|
import { adaptBill } from '@/utils/adapt';
|
||||||
|
|
||||||
export const sendCodeToEmail = ({ email, type }: { email: string; type: `${EmailTypeEnum}` }) =>
|
export const sendCodeToEmail = ({ email, type }: { email: string; type: `${EmailTypeEnum}` }) =>
|
||||||
GET('/user/sendEmail', { email, type });
|
GET('/user/sendEmail', { email, type });
|
||||||
@@ -46,3 +49,19 @@ export const postLogin = ({ email, password }: { email: string; password: string
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const putUserInfo = (data: UserUpdateParams) => PUT('/user/update', data);
|
export const putUserInfo = (data: UserUpdateParams) => PUT('/user/update', data);
|
||||||
|
|
||||||
|
export const getUserBills = (data: RequestPaging) =>
|
||||||
|
GET<PagingData<BillSchema>>(`/user/getBill?${Obj2Query(data)}`).then((res) => ({
|
||||||
|
...res,
|
||||||
|
data: res.data.map((bill) => adaptBill(bill))
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const getPayOrders = () => GET<PaySchema[]>(`/user/getPayOrders`);
|
||||||
|
|
||||||
|
export const getPayCode = (amount: number) =>
|
||||||
|
GET<{
|
||||||
|
codeUrl: string;
|
||||||
|
payId: string;
|
||||||
|
}>(`/user/getPayCode?amount=${amount}`);
|
||||||
|
|
||||||
|
export const checkPayResult = (payId: string) => GET<number>(`/user/checkPayResult?payId=${payId}`);
|
||||||
|
|||||||
1
src/components/Icon/icons/chatSend.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1679805359001" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1328" xmlns:xlink="http://www.w3.org/1999/xlink" width="48" height="48"><path d="M416.583186 1022.194004c-5.417989 0-10.835979-1.203998-16.253968-3.611993-15.049971-6.621987-24.681952-21.069959-24.681952-37.323927l0-299.795414c0-12.641975 5.417989-24.079953 15.651969-31.905938 9.631981-7.825985 22.273956-10.23398 34.915932-7.825985l417.787184 99.931805 84.279835-599.590829c1.203998-9.631981-8.427984-16.253968-16.855967-11.437978L147.489712 573.102881l139.061728 35.517931c19.865961 4.815991 34.313933 22.875955 32.507937 43.343915-2.407995 25.885949-26.487948 42.139918-50.567901 36.119929L30.70194 627.282775c-16.253968-4.213992-27.691946-17.457966-30.099941-33.711934-2.407995-16.253968 5.417989-32.507937 19.865961-40.93592L962.59612 6.621987c13.243974-7.825985 30.099941-7.223986 43.343915 1.203998 12.641975 8.427984 19.865961 24.079953 17.457966 39.129924l-105.349794 750.090535c-1.805996 11.437978-7.825985 21.671958-17.457966 28.293945-9.631981 6.621987-21.069959 8.427984-32.507937 6.019988l-411.165197-98.125808 0 154.111699 81.87184-76.453851c15.049971-13.845973 37.925926-16.855967 54.179894-4.213992 20.46796 15.651969 21.069959 45.149912 3.009994 62.005879L444.275132 1011.358025C436.449148 1018.582011 426.817166 1022.194004 416.583186 1022.194004L416.583186 1022.194004z" p-id="1329"></path><path d="M416.583186 722.398589c-9.631981 0-19.263962-3.611993-27.089947-10.23398-16.855967-15.049971-18.059965-40.93592-3.009994-57.791887l216.117578-242.003527c15.049971-16.855967 40.93592-18.059965 57.791887-3.009994 16.855967 15.049971 18.059965 40.93592 3.009994 57.791887l-216.117578 242.003527C438.857143 718.184597 427.419165 722.398589 416.583186 722.398589L416.583186 722.398589z" p-id="1330"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.9 KiB |
1
src/components/Icon/icons/copy.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1679805221456" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1173" xmlns:xlink="http://www.w3.org/1999/xlink" width="48" height="48"><path d="M267.3 834.6h-96.5c-27.4 0-49.7-22.3-49.7-49.7V115.2c0-27.4 22.3-49.7 49.7-49.7H727c27.4 0 49.7 22.3 49.7 49.7v96.5h-42.6v-96.5c0-3.9-3.2-7.1-7.1-7.1H170.8c-3.9 0-7.1 3.2-7.1 7.1v669.7c0 3.9 3.2 7.1 7.1 7.1h96.5v42.6z" p-id="1174"></path><path d="M851.9 959.5H295.7c-27.4 0-49.7-22.3-49.7-49.7V240.1c0-27.4 22.3-49.7 49.7-49.7h556.2c27.4 0 49.7 22.3 49.7 49.7v669.7c-0.1 27.4-22.3 49.7-49.7 49.7zM295.7 233c-3.9 0-7.1 3.2-7.1 7.1v669.7c0 3.9 3.2 7.1 7.1 7.1h556.2c3.9 0 7.1-3.2 7.1-7.1V240.1c0-3.9-3.2-7.1-7.1-7.1H295.7z" p-id="1175"></path></svg>
|
||||||
|
After Width: | Height: | Size: 878 B |
1
src/components/Icon/icons/home.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1679114254212" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2776" width="48" height="48" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M923.733333 394.666667c-85.333333-70.4-206.933333-174.933333-362.666666-309.333334C533.333333 61.866667 490.666667 61.866667 462.933333 85.333333c-155.733333 134.4-277.333333 238.933333-362.666666 309.333334-14.933333 14.933333-25.6 34.133333-25.6 53.333333 0 38.4 32 70.4 70.4 70.4H192v358.4c0 29.866667 23.466667 53.333333 53.333333 53.333333H405.333333c29.866667 0 53.333333-23.466667 53.333334-53.333333v-206.933333h106.666666v206.933333c0 29.866667 23.466667 53.333333 53.333334 53.333333h160c29.866667 0 53.333333-23.466667 53.333333-53.333333V518.4h46.933333c38.4 0 70.4-32 70.4-70.4 0-21.333333-10.666667-40.533333-25.6-53.333333z m-44.8 59.733333h-57.6c-29.866667 0-53.333333 23.466667-53.333333 53.333333v358.4h-138.666667V661.333333c0-29.866667-23.466667-53.333333-53.333333-53.333333h-128c-29.866667 0-53.333333 23.466667-53.333333 53.333333v206.933334H256V507.733333c0-29.866667-23.466667-53.333333-53.333333-53.333333H145.066667c-4.266667 0-6.4-2.133333-6.4-6.4 0-2.133333 2.133333-4.266667 2.133333-6.4 85.333333-70.4 206.933333-174.933333 362.666667-309.333333 4.266667-4.266667 10.666667-4.266667 14.933333 0 155.733333 134.4 277.333333 238.933333 362.666667 309.333333 2.133333 2.133333 2.133333 2.133333 2.133333 4.266667 2.133333 6.4-2.133333 8.533333-4.266667 8.533333z" p-id="2777"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.6 KiB |
1
src/components/Icon/icons/menu.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1679316084227" class="icon" viewBox="0 0 1305 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1173" xmlns:xlink="http://www.w3.org/1999/xlink" width="61.171875" height="48"><path d="M0.837818 75.218317c0 19.642164 8.098902 39.191237 21.969435 53.06177 13.963624 13.963624 33.512697 22.062525 53.247951 22.062525a76.055204 76.055204 0 0 0 53.06177-21.969434c13.963624-13.963624 22.062525-33.512697 22.062526-53.154861A76.055204 76.055204 0 0 0 129.303156 21.970365 76.055204 76.055204 0 0 0 76.055204 0.000931a76.055204 76.055204 0 0 0-53.247951 21.969434A76.706839 76.706839 0 0 0 0.837818 75.218317M0.837818 476.160498c0 19.642164 8.005811 39.377419 21.969435 53.247952 13.963624 13.963624 33.419606 21.969435 53.247951 21.969434a76.241385 76.241385 0 0 0 53.154861-21.969434 75.962113 75.962113 0 0 0 21.969435-53.247952 76.241385 76.241385 0 0 0-21.969435-53.154861 75.962113 75.962113 0 0 0-53.154861-21.969434 76.241385 76.241385 0 0 0-53.247951 21.969434 75.962113 75.962113 0 0 0-21.969435 53.154861M0.837818 877.19577c0 19.642164 8.005811 39.284328 21.969435 53.247951 13.963624 13.963624 33.419606 21.969435 53.247951 21.969435a76.241385 76.241385 0 0 0 53.154861-21.969435 75.962113 75.962113 0 0 0 21.969435-53.247951 76.241385 76.241385 0 0 0-21.969435-53.247952 75.962113 75.962113 0 0 0-53.154861-21.969434 76.241385 76.241385 0 0 0-53.247951 21.969434 76.520658 76.520658 0 0 0-21.969435 53.247952M1304.109361 75.218317c0 41.518508-32.395607 75.124295-72.331571 75.124295H373.945843c-40.029055 0-72.331571-33.512697-72.331571-75.124295C301.521181 33.513628 333.916788 0.000931 373.945843 0.000931h857.831947c40.029055 0 72.331571 33.605788 72.331571 75.217386M1231.77779 551.377884H373.945843c-40.029055 0-72.331571-33.605788-72.331571-75.217386 0-41.518508 32.302516-75.124295 72.331571-75.124295h857.831947c40.029055-0.186182 72.331571 33.512697 72.331571 75.124295 0 41.425417-32.395607 75.217386-72.331571 75.217386zM1304.109361 877.102679c0 41.611599-32.395607 75.310477-72.331571 75.310477H373.945843c-40.029055 0-72.331571-33.698878-72.331571-75.310477 0-41.425417 32.302516-75.124295 72.331571-75.124295h857.831947c40.029055-0.093091 72.331571 33.698878 72.331571 75.124295" p-id="1174"></path></svg>
|
||||||
|
After Width: | Height: | Size: 2.3 KiB |
1
src/components/Icon/icons/model.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1679070302676" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1173" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128"><path d="M910.13 243.22L545.97 32.97c-19.82-11.46-44.41-11.4-64.16 0.13L115.54 246.51c-19.5 11.36-31.68 32.43-31.76 54.99L82.1 725.44c-0.08 22.87 12.16 44.16 31.97 55.6l364.16 210.25c9.86 5.7 20.92 8.55 31.97 8.55 11.13 0 22.27-2.89 32.19-8.67l366.27-213.41c19.5-11.36 31.66-32.43 31.75-54.99l1.69-423.93c0.08-22.88-12.16-44.18-31.97-55.62zM513.68 88.9l335.28 193.58-332.93 192.2c-1.38 0.8-2.63 1.76-3.94 2.64-1.32-0.88-2.56-1.85-3.94-2.64L178.66 284.46 513.68 88.9zM146.69 725.68l1.24-384.39 327.91 189.32c1.59 0.92 2.74 2.31 3.54 3.89-0.09 1.49-0.29 2.95-0.28 4.45l0.7 175.55-0.8 202.69-332.31-191.51z m398.5 189.44l-0.8-200.61 0.7-175.54c0.01-1.5-0.2-2.97-0.28-4.46 0.8-1.59 1.95-2.98 3.53-3.9l329.03-189.96-1.23 381.29-330.95 193.18z" p-id="1174"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.1 KiB |
1
src/components/Icon/icons/pay.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1679410564438" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2824" xmlns:xlink="http://www.w3.org/1999/xlink" width="32" height="32"><path d="M693.095316 281.760857l-131.632817 223.935003 103.718481 0 0 49.478312-120.846571 0 0 68.193688 120.846571 0 0 50.115659-120.846571 0 0 99.276514-62.164435 0L482.169975 673.483519 356.88022 673.483519l0-50.115659 125.289755 0 0-68.193688L356.88022 555.174172l0-49.478312 106.893053 0-130.364204-223.935003 70.099647 0c60.895822 111.230417 97.898433 181.748475 111.012698 211.562689l1.268612 0c4.441967-12.262847 16.596562-37.002611 36.474732-74.219292l74.536749-137.343396L693.095316 281.760857 693.095316 281.760857zM693.095316 281.760857" p-id="2825"></path><path d="M784.470674 621.448522c-15.061578 0-27.247797 12.187435-27.247797 27.247797s12.187435 27.247797 27.247797 27.247797l71.98128 0c-61.204765 128.843816-192.338895 217.986027-344.464118 217.986027-210.6687 0-381.478892-170.782216-381.478892-381.475243 0-210.696675 170.810191-381.465512 381.478892-381.465512 192.121175 0 350.635679 142.189179 377.137878 326.968701l55.08064 0C917.333181 242.953241 734.255278 76.493794 511.987837 76.493794 271.197197 76.493794 76.012135 271.688586 76.012135 512.456117c0 240.762665 195.185062 435.972053 435.975702 435.972053 164.236031 0 307.128238-90.894915 381.475243-225.064956l0 61.57574c0 15.061578 12.187435 27.247797 27.276989 27.247797 15.004412 0 27.247797-12.187435 27.247797-27.247797L947.987865 648.697535c0-3.297419 0-27.247797-27.247797-27.247797L784.470674 621.449738 784.470674 621.448522zM784.470674 621.448522" p-id="2826"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.7 KiB |
1
src/components/Icon/icons/share.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1679070718083" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5975" id="mx_n_1679070718084" width="48" height="48" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M1023.82 694.91v146.26c0 102.38-80.44 182.82-182.82 182.82H182.83C80.45 1024 0 943.56 0 841.18V183.01C0 80.63 80.45 0.19 182.83 0.19h146.26c21.94 0 36.57 14.62 36.57 36.57 0 21.94-14.62 36.56-36.57 36.56H182.83c-58.5 0-109.7 51.19-109.7 109.7v658.17c0 58.5 51.19 109.7 109.7 109.7h658.17c58.5 0 109.7-51.19 109.7-109.7V694.91c0-21.94 14.62-36.56 36.56-36.56 21.93 0 36.56 14.63 36.56 36.56z" p-id="5976"></path><path d="M1012.6 292.61L684.73 5.86c-6.56-5.7-15.02-6.32-21.96-1.49-6.94 4.83-11.31 14.24-11.31 24.65v132.66h-80.9c-84.89 0-164.74 41.49-224.82 116.92-29.27 36.79-52.28 79.65-68.44 127.34C260.57 455.5 252.11 508.02 252.11 562.27c0 40.13 4.65 85.72 12.17 118.79 2.47 11.02 9.89 18.95 18.72 19.94h1.81c8.08 0 15.59-6.07 19.21-15.61 50.29-134.27 154.86-220.98 266.46-220.98h80.9v138.12c0 10.28 4.37 19.69 11.31 24.65 6.94 4.83 15.4 4.33 21.96-1.49l327.96-286.75c5.89-5.21 9.51-13.87 9.51-23.16-0.01-9.3-3.53-17.97-9.52-23.17z m-88.21 16.04L717.4 477.58v-81.92c0-11.07-7.41-20.08-16.52-20.08h-78.98c-49.84 0-95.79 2.5-146.79 32.25-30.31 17.68-130.92 89.06-150 121.09-0.51-7.94 20.14-85.47 23-92.41C390.11 334.54 504.6 237.8 621.9 237.8h78.98c9.1 0 16.52-9.02 16.52-20.08v-78l206.99 168.93z" p-id="5977"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.5 KiB |
@@ -1,23 +1,24 @@
|
|||||||
type TIconfont = {
|
import React from 'react';
|
||||||
name: string;
|
import type { IconProps } from '@chakra-ui/react';
|
||||||
color?: string;
|
import { Icon } from '@chakra-ui/react';
|
||||||
width?: number | string;
|
import dynamic from 'next/dynamic';
|
||||||
height?: number | string;
|
|
||||||
className?: string;
|
const map = {
|
||||||
|
model: require('./icons/model.svg').default,
|
||||||
|
share: require('./icons/share.svg').default,
|
||||||
|
home: require('./icons/home.svg').default,
|
||||||
|
menu: require('./icons/menu.svg').default,
|
||||||
|
pay: require('./icons/pay.svg').default,
|
||||||
|
copy: require('./icons/copy.svg').default,
|
||||||
|
chatSend: require('./icons/chatSend.svg').default
|
||||||
};
|
};
|
||||||
|
|
||||||
function Icon({ name, color = 'inherit', width = 16, height = 16, className = '' }: TIconfont) {
|
export type IconName = keyof typeof map;
|
||||||
const style = {
|
|
||||||
fill: color,
|
|
||||||
width,
|
|
||||||
height
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
const MyIcon = ({ name, w = 'auto', h = 'auto', ...props }: { name: IconName } & IconProps) => {
|
||||||
<svg className={`icon ${className}`} aria-hidden="true" style={style}>
|
return map[name] ? (
|
||||||
<use xlinkHref={`#${name}`}></use>
|
<Icon as={map[name]} w={w} h={h} boxSizing={'content-box'} verticalAlign={'top'} {...props} />
|
||||||
</svg>
|
) : null;
|
||||||
);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export default Icon;
|
export default MyIcon;
|
||||||
|
|||||||
23
src/components/Iconfont/index.tsx
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
type TIconfont = {
|
||||||
|
name: string;
|
||||||
|
color?: string;
|
||||||
|
width?: number | string;
|
||||||
|
height?: number | string;
|
||||||
|
className?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function Iconfont({ name, color = 'inherit', width = 16, height = 16, className = '' }: TIconfont) {
|
||||||
|
const style = {
|
||||||
|
fill: color,
|
||||||
|
width,
|
||||||
|
height
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<svg className={`icon ${className}`} aria-hidden="true" style={style}>
|
||||||
|
<use xlinkHref={`#${name}`}></use>
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Iconfont;
|
||||||
@@ -1,15 +1,13 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { useToast } from '@chakra-ui/react';
|
import { useToast } from '@chakra-ui/react';
|
||||||
import { getTokenLogin } from '@/api/user';
|
|
||||||
import { useUserStore } from '@/store/user';
|
import { useUserStore } from '@/store/user';
|
||||||
import { useGlobalStore } from '@/store/global';
|
import { useGlobalStore } from '@/store/global';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
const unAuthPage: { [key: string]: boolean } = {
|
const unAuthPage: { [key: string]: boolean } = {
|
||||||
'/': true,
|
'/': true,
|
||||||
'/login': true,
|
'/login': true
|
||||||
'/chat': true
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Auth = ({ children }: { children: JSX.Element }) => {
|
const Auth = ({ children }: { children: JSX.Element }) => {
|
||||||
@@ -19,7 +17,7 @@ const Auth = ({ children }: { children: JSX.Element }) => {
|
|||||||
position: 'top',
|
position: 'top',
|
||||||
status: 'warning'
|
status: 'warning'
|
||||||
});
|
});
|
||||||
const { userInfo, setUserInfo } = useUserStore();
|
const { userInfo, initUserInfo } = useUserStore();
|
||||||
const { setLoading } = useGlobalStore();
|
const { setLoading } = useGlobalStore();
|
||||||
|
|
||||||
useQuery(
|
useQuery(
|
||||||
@@ -29,17 +27,12 @@ const Auth = ({ children }: { children: JSX.Element }) => {
|
|||||||
return setLoading(false);
|
return setLoading(false);
|
||||||
} else {
|
} else {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
return getTokenLogin();
|
return initUserInfo();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess(user) {
|
|
||||||
if (user) {
|
|
||||||
setUserInfo(user);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onError(error) {
|
onError(error) {
|
||||||
console.log(error);
|
console.log('error->', error);
|
||||||
router.push('/login');
|
router.push('/login');
|
||||||
toast();
|
toast();
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import { Box } from '@chakra-ui/react';
|
import { Box, useColorMode, Flex } from '@chakra-ui/react';
|
||||||
import Navbar from './navbar';
|
import Navbar from './navbar';
|
||||||
import NavbarPhone from './navbarPhone';
|
import NavbarPhone from './navbarPhone';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
@@ -29,8 +29,8 @@ const navbarList = [
|
|||||||
// {
|
// {
|
||||||
// label: '数据',
|
// label: '数据',
|
||||||
// icon: 'icon-datafull',
|
// icon: 'icon-datafull',
|
||||||
// link: '/training/dataList',
|
// link: '/data/list',
|
||||||
// activeLink: ['/training/dataList']
|
// activeLink: ['/data/list', '/data/detail']
|
||||||
// },
|
// },
|
||||||
{
|
{
|
||||||
label: '账号',
|
label: '账号',
|
||||||
@@ -43,9 +43,16 @@ const navbarList = [
|
|||||||
const Layout = ({ children }: { children: JSX.Element }) => {
|
const Layout = ({ children }: { children: JSX.Element }) => {
|
||||||
const { isPc } = useScreen();
|
const { isPc } = useScreen();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { colorMode, setColorMode } = useColorMode();
|
||||||
const { Loading } = useLoading({ defaultLoading: true });
|
const { Loading } = useLoading({ defaultLoading: true });
|
||||||
const { loading } = useGlobalStore();
|
const { loading } = useGlobalStore();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (colorMode === 'dark' && router.pathname !== '/chat') {
|
||||||
|
setColorMode('light');
|
||||||
|
}
|
||||||
|
}, [colorMode, router.pathname, setColorMode]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{!unShowLayoutRoute[router.pathname] ? (
|
{!unShowLayoutRoute[router.pathname] ? (
|
||||||
@@ -55,29 +62,21 @@ const Layout = ({ children }: { children: JSX.Element }) => {
|
|||||||
<Box h={'100%'} position={'fixed'} left={0} top={0} w={'80px'}>
|
<Box h={'100%'} position={'fixed'} left={0} top={0} w={'80px'}>
|
||||||
<Navbar navbarList={navbarList} />
|
<Navbar navbarList={navbarList} />
|
||||||
</Box>
|
</Box>
|
||||||
<Box ml={'80px'} p={7}>
|
<Box h={'100%'} ml={'80px'}>
|
||||||
<Box maxW={'1100px'} m={'auto'}>
|
<Box h={'100%'} py={7} px={'5vw'} m={'auto'} overflowY={'auto'}>
|
||||||
<Auth>{children}</Auth>
|
<Auth>{children}</Auth>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<Box pt={'60px'}>
|
<Flex h={'100%'} flexDirection={'column'}>
|
||||||
<Box
|
<Box h={'60px'} borderBottom={'1px solid rgba(0,0,0,0.1)'}>
|
||||||
h={'60px'}
|
|
||||||
position={'fixed'}
|
|
||||||
top={0}
|
|
||||||
left={0}
|
|
||||||
right={0}
|
|
||||||
zIndex={100}
|
|
||||||
borderBottom={'1px solid rgba(0,0,0,0.1)'}
|
|
||||||
>
|
|
||||||
<NavbarPhone navbarList={navbarList} />
|
<NavbarPhone navbarList={navbarList} />
|
||||||
</Box>
|
</Box>
|
||||||
<Box py={3} px={4}>
|
<Box flex={'1 0 0'} h={0} py={3} px={4} overflowY={'auto'}>
|
||||||
<Auth>{children}</Auth>
|
<Auth>{children}</Auth>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Flex>
|
||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React from 'react';
|
|||||||
import { Box, Flex } from '@chakra-ui/react';
|
import { Box, Flex } from '@chakra-ui/react';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import Icon from '../Icon';
|
import Icon from '../Iconfont';
|
||||||
|
|
||||||
export enum NavbarTypeEnum {
|
export enum NavbarTypeEnum {
|
||||||
normal = 'normal',
|
normal = 'normal',
|
||||||
@@ -34,7 +34,7 @@ const Navbar = ({
|
|||||||
>
|
>
|
||||||
{/* logo */}
|
{/* logo */}
|
||||||
<Box pb={4}>
|
<Box pb={4}>
|
||||||
<Image src={'/logo.png'} width={'35'} height={'35'} alt=""></Image>
|
<Image src={'/icon/logo.png'} width={'35'} height={'35'} alt=""></Image>
|
||||||
</Box>
|
</Box>
|
||||||
{/* 导航列表 */}
|
{/* 导航列表 */}
|
||||||
<Box flex={1}>
|
<Box flex={1}>
|
||||||
@@ -45,11 +45,12 @@ const Navbar = ({
|
|||||||
flexDirection={'column'}
|
flexDirection={'column'}
|
||||||
alignItems={'center'}
|
alignItems={'center'}
|
||||||
justifyContent={'center'}
|
justifyContent={'center'}
|
||||||
onClick={() =>
|
onClick={() => {
|
||||||
|
if (item.link === router.pathname) return;
|
||||||
router.push(item.link, undefined, {
|
router.push(item.link, undefined, {
|
||||||
shallow: true
|
shallow: true
|
||||||
})
|
});
|
||||||
}
|
}}
|
||||||
cursor={'pointer'}
|
cursor={'pointer'}
|
||||||
fontSize={'sm'}
|
fontSize={'sm'}
|
||||||
w={'60px'}
|
w={'60px'}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import Icon from '../Icon';
|
import Icon from '../Iconfont';
|
||||||
import {
|
import {
|
||||||
Flex,
|
Flex,
|
||||||
Drawer,
|
Drawer,
|
||||||
@@ -48,7 +48,7 @@ const NavbarPhone = ({
|
|||||||
<DrawerContent maxWidth={'50vw'}>
|
<DrawerContent maxWidth={'50vw'}>
|
||||||
<DrawerBody p={4}>
|
<DrawerBody p={4}>
|
||||||
<Box py={4}>
|
<Box py={4}>
|
||||||
<Image src={'/logo.png'} margin={'auto'} w={'35'} h={'35'} alt=""></Image>
|
<Image src={'/icon/logo.png'} margin={'auto'} w={'35'} h={'35'} alt=""></Image>
|
||||||
</Box>
|
</Box>
|
||||||
{navbarList.map((item) => (
|
{navbarList.map((item) => (
|
||||||
<Flex
|
<Flex
|
||||||
@@ -57,6 +57,7 @@ const NavbarPhone = ({
|
|||||||
alignItems={'center'}
|
alignItems={'center'}
|
||||||
justifyContent={'center'}
|
justifyContent={'center'}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
if (item.link === router.pathname) return;
|
||||||
router.push(item.link);
|
router.push(item.link);
|
||||||
onClose();
|
onClose();
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -1,5 +1,43 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
export const codeLight: { [key: string]: React.CSSProperties } = {
|
export const codeLight: { [key: string]: React.CSSProperties } = {
|
||||||
|
'code[class*=language-]': {
|
||||||
|
color: '#d4d4d4',
|
||||||
|
textShadow: 'none',
|
||||||
|
direction: 'ltr',
|
||||||
|
textAlign: 'left',
|
||||||
|
whiteSpace: 'pre',
|
||||||
|
wordSpacing: 'normal',
|
||||||
|
wordBreak: 'normal',
|
||||||
|
lineHeight: '1.5',
|
||||||
|
MozTabSize: '4',
|
||||||
|
OTabSize: '4',
|
||||||
|
tabSize: '4',
|
||||||
|
WebkitHyphens: 'none',
|
||||||
|
MozHyphens: 'none',
|
||||||
|
msHyphens: 'none',
|
||||||
|
hyphens: 'none'
|
||||||
|
},
|
||||||
|
'pre[class*=language-]': {
|
||||||
|
color: '#d4d4d4',
|
||||||
|
textShadow: 'none',
|
||||||
|
direction: 'ltr',
|
||||||
|
textAlign: 'left',
|
||||||
|
whiteSpace: 'pre',
|
||||||
|
wordSpacing: 'normal',
|
||||||
|
wordBreak: 'normal',
|
||||||
|
lineHeight: '1.5',
|
||||||
|
MozTabSize: '4',
|
||||||
|
OTabSize: '4',
|
||||||
|
tabSize: '4',
|
||||||
|
WebkitHyphens: 'none',
|
||||||
|
MozHyphens: 'none',
|
||||||
|
msHyphens: 'none',
|
||||||
|
hyphens: 'none',
|
||||||
|
padding: '1em',
|
||||||
|
margin: '.5em 0',
|
||||||
|
overflow: 'auto',
|
||||||
|
background: '#1e1e1e'
|
||||||
|
},
|
||||||
'code[class*=language-] ::selection': {
|
'code[class*=language-] ::selection': {
|
||||||
textShadow: 'none',
|
textShadow: 'none',
|
||||||
background: '#264f78'
|
background: '#264f78'
|
||||||
|
|||||||
@@ -4,17 +4,17 @@
|
|||||||
width: 4px;
|
width: 4px;
|
||||||
height: 14px;
|
height: 14px;
|
||||||
transform: translate(4px, 2px) scaleY(1.3);
|
transform: translate(4px, 2px) scaleY(1.3);
|
||||||
background-color: rgba(0, 0, 0, 0.7);
|
background-color: var(--chakra-colors-chakra-body-text);
|
||||||
animation: blink 0.6s infinite;
|
animation: blink 0.6s infinite;
|
||||||
}
|
}
|
||||||
.animation {
|
.animation {
|
||||||
:last-child::after {
|
> :last-child::after {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
content: '';
|
content: '';
|
||||||
width: 4px;
|
width: 4px;
|
||||||
height: 14px;
|
height: 14px;
|
||||||
transform: translate(4px, 2px) scaleY(1.3);
|
transform: translate(4px, 2px) scaleY(1.3);
|
||||||
background-color: rgba(0, 0, 0, 0.7);
|
background-color: var(--chakra-colors-chakra-body-text);
|
||||||
animation: blink 0.6s infinite;
|
animation: blink 0.6s infinite;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@
|
|||||||
.markdown h6 {
|
.markdown h6 {
|
||||||
cursor: text;
|
cursor: text;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
margin: 20px 0 10px;
|
margin: 10px 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
@@ -65,7 +65,6 @@
|
|||||||
.markdown h4 .mini-icon-link,
|
.markdown h4 .mini-icon-link,
|
||||||
.markdown h5 .mini-icon-link,
|
.markdown h5 .mini-icon-link,
|
||||||
.markdown h6 .mini-icon-link {
|
.markdown h6 .mini-icon-link {
|
||||||
color: #000000;
|
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
.markdown h1:hover a.anchor,
|
.markdown h1:hover a.anchor,
|
||||||
@@ -103,12 +102,9 @@
|
|||||||
font-size: inherit;
|
font-size: inherit;
|
||||||
}
|
}
|
||||||
.markdown h1 {
|
.markdown h1 {
|
||||||
color: #000000;
|
|
||||||
font-size: 28px;
|
font-size: 28px;
|
||||||
}
|
}
|
||||||
.markdown h2 {
|
.markdown h2 {
|
||||||
border-bottom: 1px solid #cccccc;
|
|
||||||
color: #000000;
|
|
||||||
font-size: 24px;
|
font-size: 24px;
|
||||||
}
|
}
|
||||||
.markdown h3 {
|
.markdown h3 {
|
||||||
@@ -121,8 +117,7 @@
|
|||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
}
|
}
|
||||||
.markdown h6 {
|
.markdown h6 {
|
||||||
color: #777777;
|
font-size: 12px;
|
||||||
font-size: 14px;
|
|
||||||
}
|
}
|
||||||
.markdown p,
|
.markdown p,
|
||||||
.markdown blockquote,
|
.markdown blockquote,
|
||||||
@@ -131,15 +126,7 @@
|
|||||||
.markdown dl,
|
.markdown dl,
|
||||||
.markdown table,
|
.markdown table,
|
||||||
.markdown pre {
|
.markdown pre {
|
||||||
margin: 15px 0;
|
margin: 10px 0;
|
||||||
}
|
|
||||||
.markdown hr {
|
|
||||||
background: url('https://a248.e.akamai.net/assets.github.com/assets/primer/markdown/dirty-shade-350cca8f57223ebd53603021b2e670f4f319f1b7.png')
|
|
||||||
repeat-x scroll 0 0 transparent;
|
|
||||||
border: 0 none;
|
|
||||||
color: #cccccc;
|
|
||||||
height: 4px;
|
|
||||||
padding: 0;
|
|
||||||
}
|
}
|
||||||
.markdown > h2:first-child,
|
.markdown > h2:first-child,
|
||||||
.markdown > h1:first-child,
|
.markdown > h1:first-child,
|
||||||
@@ -173,7 +160,7 @@
|
|||||||
}
|
}
|
||||||
.markdown ul,
|
.markdown ul,
|
||||||
.markdown ol {
|
.markdown ol {
|
||||||
padding-left: 30px;
|
padding-left: 1em;
|
||||||
}
|
}
|
||||||
.markdown ul.no-list,
|
.markdown ul.no-list,
|
||||||
.markdown ol.no-list {
|
.markdown ol.no-list {
|
||||||
@@ -235,12 +222,10 @@
|
|||||||
}
|
}
|
||||||
.markdown table th,
|
.markdown table th,
|
||||||
.markdown table td {
|
.markdown table td {
|
||||||
border: 1px solid #cccccc;
|
|
||||||
padding: 6px 13px;
|
padding: 6px 13px;
|
||||||
}
|
}
|
||||||
.markdown table tr {
|
.markdown table tr {
|
||||||
background-color: #ffffff;
|
background-color: #ffffff;
|
||||||
border-top: 1px solid #cccccc;
|
|
||||||
}
|
}
|
||||||
.markdown table tr:nth-child(2n) {
|
.markdown table tr:nth-child(2n) {
|
||||||
background-color: #f0f0f0;
|
background-color: #f0f0f0;
|
||||||
@@ -324,12 +309,10 @@
|
|||||||
}
|
}
|
||||||
.markdown code,
|
.markdown code,
|
||||||
.markdown tt {
|
.markdown tt {
|
||||||
background-color: #f0f0f0;
|
|
||||||
border: 1px solid #eaeaea;
|
border: 1px solid #eaeaea;
|
||||||
border-radius: 3px 3px 3px 3px;
|
border-radius: 3px 3px 3px 3px;
|
||||||
margin: 0 2px;
|
margin: 0 2px;
|
||||||
padding: 0 5px;
|
padding: 0 5px;
|
||||||
white-space: nowrap;
|
|
||||||
}
|
}
|
||||||
.markdown pre > code {
|
.markdown pre > code {
|
||||||
background: none repeat scroll 0 0 transparent;
|
background: none repeat scroll 0 0 transparent;
|
||||||
@@ -340,10 +323,9 @@
|
|||||||
}
|
}
|
||||||
.markdown .highlight pre,
|
.markdown .highlight pre,
|
||||||
.markdown pre {
|
.markdown pre {
|
||||||
background-color: #f0f0f0;
|
|
||||||
border: 1px solid #cccccc;
|
border: 1px solid #cccccc;
|
||||||
border-radius: 3px 3px 3px 3px;
|
border-radius: 3px 3px 3px 3px;
|
||||||
font-size: 13px;
|
font-size: max(0.9em, 14px);
|
||||||
line-height: 19px;
|
line-height: 19px;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
padding: 6px 10px;
|
padding: 6px 10px;
|
||||||
@@ -354,10 +336,15 @@
|
|||||||
border: medium none;
|
border: medium none;
|
||||||
}
|
}
|
||||||
.markdown {
|
.markdown {
|
||||||
font-size: 14px;
|
|
||||||
line-height: 1.6;
|
|
||||||
letter-spacing: 0.5px;
|
|
||||||
text-align: justify;
|
text-align: justify;
|
||||||
|
overflow-y: hidden;
|
||||||
|
tab-size: 4;
|
||||||
|
word-spacing: normal;
|
||||||
|
word-break: break-all;
|
||||||
|
|
||||||
|
p {
|
||||||
|
white-space: pre-line;
|
||||||
|
}
|
||||||
|
|
||||||
pre {
|
pre {
|
||||||
display: block;
|
display: block;
|
||||||
@@ -368,12 +355,61 @@
|
|||||||
border-radius: 0;
|
border-radius: 0;
|
||||||
background-color: #222 !important;
|
background-color: #222 !important;
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
|
color: #fff;
|
||||||
}
|
}
|
||||||
|
|
||||||
pre code {
|
pre code {
|
||||||
background-color: #222;
|
background-color: #222 !important;
|
||||||
color: #fff;
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
font-family: 'Söhne,ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif,Helvetica Neue,Arial,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji';
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
text-decoration: underline;
|
||||||
|
color: var(--chakra-colors-blue-600);
|
||||||
|
}
|
||||||
|
|
||||||
|
table {
|
||||||
|
border-collapse: separate;
|
||||||
|
border-spacing: 0px;
|
||||||
|
color: var(--chakra-colors-gray-700);
|
||||||
|
|
||||||
|
thead tr:first-child th {
|
||||||
|
border-bottom-width: 1px;
|
||||||
|
border-left-width: 1px;
|
||||||
|
border-top-width: 1px;
|
||||||
|
border-color: #ccc;
|
||||||
|
background-color: rgba(236, 236, 241, 0.2);
|
||||||
|
overflow: hidden;
|
||||||
|
|
||||||
|
&:first-child {
|
||||||
|
border-top-left-radius: 0.375rem;
|
||||||
|
}
|
||||||
|
&:last-child {
|
||||||
|
border-right-width: 1px;
|
||||||
|
border-top-right-radius: 0.375rem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
td {
|
||||||
|
border-bottom-width: 1px;
|
||||||
|
border-left-width: 1px;
|
||||||
|
border-color: #ccc;
|
||||||
|
|
||||||
|
&:last-of-type {
|
||||||
|
border-right-width: 1px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tbody tr:last-child {
|
||||||
|
overflow: hidden;
|
||||||
|
td {
|
||||||
|
&:first-child {
|
||||||
|
border-bottom-left-radius: 0.375rem;
|
||||||
|
}
|
||||||
|
&:last-child {
|
||||||
|
border-bottom-right-radius: 0.375rem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,47 @@
|
|||||||
import React, { memo } from 'react';
|
import React, { memo, useMemo } from 'react';
|
||||||
import ReactMarkdown from 'react-markdown';
|
import ReactMarkdown from 'react-markdown';
|
||||||
import remarkGfm from 'remark-gfm';
|
|
||||||
import styles from './index.module.scss';
|
|
||||||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
|
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
|
||||||
import { codeLight } from './codeLight';
|
import { Box, Flex, useColorModeValue } from '@chakra-ui/react';
|
||||||
import { Box, Flex } from '@chakra-ui/react';
|
|
||||||
import { useCopyData } from '@/utils/tools';
|
import { useCopyData } from '@/utils/tools';
|
||||||
import Icon from '@/components/Icon';
|
import Icon from '@/components/Icon';
|
||||||
|
import remarkGfm from 'remark-gfm';
|
||||||
|
import remarkMath from 'remark-math';
|
||||||
|
import rehypeKatex from 'rehype-katex';
|
||||||
|
|
||||||
const Markdown = ({ source, isChatting }: { source: string; isChatting: boolean }) => {
|
import 'katex/dist/katex.min.css';
|
||||||
// const formatSource = useMemo(() => source.replace(/\n/g, '\n'), [source]);
|
import styles from './index.module.scss';
|
||||||
|
import { codeLight } from './codeLight';
|
||||||
|
|
||||||
|
const Markdown = ({ source, isChatting = false }: { source: string; isChatting?: boolean }) => {
|
||||||
|
const formatSource = useMemo(() => source, [source]);
|
||||||
const { copyData } = useCopyData();
|
const { copyData } = useCopyData();
|
||||||
// console.log(source);
|
|
||||||
return (
|
return (
|
||||||
<ReactMarkdown
|
<ReactMarkdown
|
||||||
className={`${styles.markdown} ${
|
className={`${styles.markdown} ${
|
||||||
isChatting ? (source === '' ? styles.waitingAnimation : styles.animation) : ''
|
isChatting ? (source === '' ? styles.waitingAnimation : styles.animation) : ''
|
||||||
}`}
|
}`}
|
||||||
rehypePlugins={[remarkGfm]}
|
remarkPlugins={[remarkMath]}
|
||||||
|
rehypePlugins={[remarkGfm, rehypeKatex]}
|
||||||
components={{
|
components={{
|
||||||
pre: 'div',
|
pre: 'div',
|
||||||
code({ node, inline, className, children, ...props }) {
|
code({ node, inline, className, children, ...props }) {
|
||||||
const match = /language-(\w+)/.exec(className || '');
|
const match = /language-(\w+)/.exec(className || '');
|
||||||
const code = String(children).replace(/\n$/, '');
|
const code = String(children);
|
||||||
return !inline ? (
|
|
||||||
<Box my={3} borderRadius={'md'} overflow={'hidden'}>
|
return !inline || match ? (
|
||||||
|
<Box my={3} borderRadius={'md'} overflow={'hidden'} backgroundColor={'#222'}>
|
||||||
<Flex
|
<Flex
|
||||||
py={2}
|
py={2}
|
||||||
px={5}
|
px={5}
|
||||||
backgroundColor={'#323641'}
|
backgroundColor={useColorModeValue('#323641', 'gray.600')}
|
||||||
color={'#fff'}
|
color={'#fff'}
|
||||||
fontSize={'sm'}
|
fontSize={'sm'}
|
||||||
userSelect={'none'}
|
userSelect={'none'}
|
||||||
>
|
>
|
||||||
<Box flex={1}>{match?.[1]}</Box>
|
<Box flex={1}>{match?.[1]}</Box>
|
||||||
<Flex cursor={'pointer'} onClick={() => copyData(code)} alignItems={'center'}>
|
<Flex cursor={'pointer'} onClick={() => copyData(code)} alignItems={'center'}>
|
||||||
<Icon name={'icon-fuzhi'} width={15} height={15} color={'#fff'}></Icon>
|
<Icon name={'copy'} width={15} height={15} fill={'#fff'}></Icon>
|
||||||
<Box ml={1}>复制代码</Box>
|
<Box ml={1}>复制代码</Box>
|
||||||
</Flex>
|
</Flex>
|
||||||
</Flex>
|
</Flex>
|
||||||
@@ -50,13 +56,14 @@ const Markdown = ({ source, isChatting }: { source: string; isChatting: boolean
|
|||||||
</Box>
|
</Box>
|
||||||
) : (
|
) : (
|
||||||
<code className={className} {...props}>
|
<code className={className} {...props}>
|
||||||
{children}
|
{code}
|
||||||
</code>
|
</code>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
linkTarget="_blank"
|
||||||
>
|
>
|
||||||
{source}
|
{formatSource}
|
||||||
</ReactMarkdown>
|
</ReactMarkdown>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
76
src/components/ScrollData/index.tsx
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
import React, { useRef, useEffect, useMemo } from 'react';
|
||||||
|
import type { BoxProps } from '@chakra-ui/react';
|
||||||
|
import { Box } from '@chakra-ui/react';
|
||||||
|
import { throttle } from 'lodash';
|
||||||
|
import { useLoading } from '@/hooks/useLoading';
|
||||||
|
|
||||||
|
interface Props extends BoxProps {
|
||||||
|
nextPage: () => void;
|
||||||
|
isLoadAll: boolean;
|
||||||
|
requesting: boolean;
|
||||||
|
children: React.ReactNode;
|
||||||
|
initRequesting?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ScrollData = ({
|
||||||
|
children,
|
||||||
|
nextPage,
|
||||||
|
isLoadAll,
|
||||||
|
requesting,
|
||||||
|
initRequesting,
|
||||||
|
...props
|
||||||
|
}: Props) => {
|
||||||
|
const { Loading } = useLoading({ defaultLoading: true });
|
||||||
|
const elementRef = useRef<HTMLDivElement>(null);
|
||||||
|
const loadText = useMemo(() => {
|
||||||
|
if (requesting) return '请求中……';
|
||||||
|
if (isLoadAll) return '已加载全部';
|
||||||
|
return '点击加载更多';
|
||||||
|
}, [isLoadAll, requesting]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!elementRef.current) return;
|
||||||
|
|
||||||
|
const scrolling = throttle((e: Event) => {
|
||||||
|
const element = e.target as HTMLDivElement;
|
||||||
|
if (!element) return;
|
||||||
|
// 当前滚动位置
|
||||||
|
const scrollTop = element.scrollTop;
|
||||||
|
// 可视高度
|
||||||
|
const clientHeight = element.clientHeight;
|
||||||
|
// 内容总高度
|
||||||
|
const scrollHeight = element.scrollHeight;
|
||||||
|
// 判断是否滚动到底部
|
||||||
|
if (scrollTop + clientHeight + 100 >= scrollHeight) {
|
||||||
|
nextPage();
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
elementRef.current.addEventListener('scroll', scrolling);
|
||||||
|
return () => {
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
elementRef.current?.removeEventListener('scroll', scrolling);
|
||||||
|
};
|
||||||
|
}, [elementRef, nextPage]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box {...props} ref={elementRef} overflowY={'auto'} position={'relative'}>
|
||||||
|
{children}
|
||||||
|
<Box
|
||||||
|
mt={2}
|
||||||
|
fontSize={'xs'}
|
||||||
|
color={'blackAlpha.500'}
|
||||||
|
textAlign={'center'}
|
||||||
|
cursor={loadText === '点击加载更多' ? 'pointer' : 'default'}
|
||||||
|
onClick={() => {
|
||||||
|
if (loadText !== '点击加载更多') return;
|
||||||
|
nextPage();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{loadText}
|
||||||
|
</Box>
|
||||||
|
{initRequesting && <Loading fixed={false} />}
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ScrollData;
|
||||||
82
src/components/Slider/index.tsx
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import React, { useMemo } from 'react';
|
||||||
|
import {
|
||||||
|
Slider,
|
||||||
|
SliderTrack,
|
||||||
|
SliderFilledTrack,
|
||||||
|
SliderThumb,
|
||||||
|
SliderMark,
|
||||||
|
Box
|
||||||
|
} from '@chakra-ui/react';
|
||||||
|
|
||||||
|
const MySlider = ({
|
||||||
|
markList,
|
||||||
|
setVal,
|
||||||
|
activeVal,
|
||||||
|
max = 100,
|
||||||
|
min = 0,
|
||||||
|
step = 1
|
||||||
|
}: {
|
||||||
|
markList: {
|
||||||
|
label: string | number;
|
||||||
|
value: number;
|
||||||
|
}[];
|
||||||
|
activeVal?: number;
|
||||||
|
setVal: (index: number) => void;
|
||||||
|
max?: number;
|
||||||
|
min?: number;
|
||||||
|
step?: number;
|
||||||
|
}) => {
|
||||||
|
const startEndPointStyle = {
|
||||||
|
content: '""',
|
||||||
|
borderRadius: '10px',
|
||||||
|
width: '10px',
|
||||||
|
height: '10px',
|
||||||
|
backgroundColor: '#ffffff',
|
||||||
|
border: '2px solid #D7DBE2',
|
||||||
|
position: 'absolute',
|
||||||
|
zIndex: 1,
|
||||||
|
top: 0,
|
||||||
|
transform: 'translateY(-3px)'
|
||||||
|
};
|
||||||
|
const value = useMemo(() => {
|
||||||
|
const index = markList.findIndex((item) => item.value === activeVal);
|
||||||
|
return index > -1 ? index : 0;
|
||||||
|
}, [activeVal, markList]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Slider max={max} min={min} step={step} size={'lg'} value={value} onChange={setVal}>
|
||||||
|
{markList.map((item, i) => (
|
||||||
|
<SliderMark
|
||||||
|
key={item.value}
|
||||||
|
value={i}
|
||||||
|
mt={3}
|
||||||
|
fontSize={'sm'}
|
||||||
|
transform={'translateX(-50%)'}
|
||||||
|
{...(activeVal === item.value ? { color: 'blue.500', fontWeight: 'bold' } : {})}
|
||||||
|
>
|
||||||
|
<Box px={3} cursor={'pointer'}>
|
||||||
|
{item.label}
|
||||||
|
</Box>
|
||||||
|
</SliderMark>
|
||||||
|
))}
|
||||||
|
<SliderTrack
|
||||||
|
bg={'#EAEDF3'}
|
||||||
|
overflow={'visible'}
|
||||||
|
h={'4px'}
|
||||||
|
_before={{
|
||||||
|
...startEndPointStyle,
|
||||||
|
left: '-5px'
|
||||||
|
}}
|
||||||
|
_after={{
|
||||||
|
...startEndPointStyle,
|
||||||
|
right: '-5px'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SliderFilledTrack />
|
||||||
|
</SliderTrack>
|
||||||
|
<SliderThumb border={'2.5px solid'} borderColor={'blue.500'}></SliderThumb>
|
||||||
|
</Slider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MySlider;
|
||||||
49
src/components/WxConcat/index.tsx
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import {
|
||||||
|
Box,
|
||||||
|
Button,
|
||||||
|
Modal,
|
||||||
|
ModalOverlay,
|
||||||
|
ModalContent,
|
||||||
|
ModalHeader,
|
||||||
|
ModalFooter,
|
||||||
|
ModalBody,
|
||||||
|
ModalCloseButton,
|
||||||
|
useColorModeValue
|
||||||
|
} from '@chakra-ui/react';
|
||||||
|
import Image from 'next/image';
|
||||||
|
|
||||||
|
const WxConcat = ({ onClose }: { onClose: () => void }) => {
|
||||||
|
return (
|
||||||
|
<Modal isOpen={true} onClose={onClose}>
|
||||||
|
<ModalOverlay />
|
||||||
|
<ModalContent color={useColorModeValue('blackAlpha.700', 'white')}>
|
||||||
|
<ModalHeader>wx交流群</ModalHeader>
|
||||||
|
<ModalCloseButton />
|
||||||
|
<ModalBody textAlign={'center'}>
|
||||||
|
<Image
|
||||||
|
style={{ margin: 'auto' }}
|
||||||
|
src={'/imgs/wxerweima300.jpg'}
|
||||||
|
width={200}
|
||||||
|
height={200}
|
||||||
|
alt=""
|
||||||
|
/>
|
||||||
|
<Box mt={2}>
|
||||||
|
微信号:
|
||||||
|
<Box as={'span'} userSelect={'all'}>
|
||||||
|
YNyiqi
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
</ModalBody>
|
||||||
|
|
||||||
|
<ModalFooter>
|
||||||
|
<Button variant={'outline'} onClick={onClose}>
|
||||||
|
关闭
|
||||||
|
</Button>
|
||||||
|
</ModalFooter>
|
||||||
|
</ModalContent>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default WxConcat;
|
||||||
@@ -3,41 +3,71 @@ export enum EmailTypeEnum {
|
|||||||
findPassword = 'findPassword'
|
findPassword = 'findPassword'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const introPage = `
|
export const PRICE_SCALE = 100000;
|
||||||
## 欢迎使用 Doc GPT
|
|
||||||
|
|
||||||
时间比较赶,介绍没来得及完善,先直接上怎么使用:
|
export const introPage = `
|
||||||
|
## 欢迎使用 Fast GPT
|
||||||
|
|
||||||
|
[Git 仓库](https://github.com/c121914yu/FastGPT)
|
||||||
|
|
||||||
|
### 交流群/问题反馈
|
||||||
|
wx号: YNyiqi
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
### 快速开始
|
||||||
1. 使用邮箱注册账号。
|
1. 使用邮箱注册账号。
|
||||||
2. 进入账号页面,添加关联账号,目前只有 openai 的账号可以添加,直接去 openai 官网,把 API Key 粘贴过来。
|
2. 进入账号页面,添加关联账号,目前只有 openai 的账号可以添加,直接去 openai 官网,把 API Key 粘贴过来。
|
||||||
3. 进入模型页,创建一个模型,建议直接用 ChatGPT。
|
3. 如果填写了自己的 openai 账号,使用时会直接用你的账号。如果没有填写,需要付费使用平台的账号。
|
||||||
4. 在模型列表点击【对话】,即可使用 API 进行聊天。
|
4. 进入模型页,创建一个模型,建议直接用 ChatGPT。
|
||||||
|
5. 在模型列表点击【对话】,即可使用 API 进行聊天。
|
||||||
|
|
||||||
### 模型配置
|
### 定制 prompt
|
||||||
|
|
||||||
1. **提示语**:会在每个对话框的第一句自动加入,用于限定该模型的对话内容。
|
1. 进入模型编辑页
|
||||||
|
2. 调整温度和提示词
|
||||||
|
3. 使用该模型对话。每次对话时,提示词和温度都会自动注入,方便管理个人的模型。建议把自己日常经常需要使用的 5~10 个方向预设好。
|
||||||
|
|
||||||
|
### 知识库
|
||||||
|
|
||||||
2. **单句最大长度**:每个聊天,单次输入内容的最大长度。
|
1. 创建模型时选择【知识库】
|
||||||
|
2. 进入模型编辑页
|
||||||
|
3. 导入数据,可以选择手动导入,或者选择文件导入。文件导入会自动调用 chatGPT 理解文件内容,并生成知识库。
|
||||||
|
4. 使用该模型对话。
|
||||||
|
|
||||||
|
注意:使用知识库模型对话时,tokens 消耗会加快。
|
||||||
|
|
||||||
3. **上下文最大长度**:每个聊天,最多的轮数除以2,建议设置为偶数。可以持续聊天,但是旧的聊天内容会被截断,AI 就不会知道被截取的内容。
|
### 价格表
|
||||||
例如:上下文最大长度为6。在第 4 轮对话时,第一轮对话的内容不会被计入。
|
如果使用了自己的 Api Key,不会计费。可以在账号页,看到详细账单。单纯使用 chatGPT 模型进行对话,只有一个计费项目。使用知识库时,包含**对话**和**索引**生成两个计费项。
|
||||||
|
| 计费项 | 价格: 元/ 1K tokens(包含上下文)|
|
||||||
4. **过期时间**:生成对话框后,这个对话框多久过期。
|
| --- | --- |
|
||||||
|
| chatgpt - 对话 | 0.03 |
|
||||||
5. **聊天最大加载次数**:单个对话框最多被加载几次,设置为-1代表不限制,正数代表只能加载 n 次,防止被盗刷。
|
| 知识库 - 对话 | 0.03 |
|
||||||
|
| 知识库 - 索引 | 0.01 |
|
||||||
### 对话框介绍
|
| 文件拆分 | 0.03 |
|
||||||
|
`;
|
||||||
1. 每个对话框以 windowId 作为标识。
|
|
||||||
2. 每次点击【对话】,都会生成新的对话框,无法回到旧的对话框。对话框内刷新,会恢复对话内容。
|
export const chatProblem = `
|
||||||
3. 直接分享对话框(网页)的链接给朋友,会共享同一个对话内容。但是!!!千万不要两个人同时用一个链接,会串味,还没解决这个问题。
|
## 常见问题
|
||||||
4. 如果想分享一个纯的对话框,可以把链接里 windowId 参数去掉。例如:
|
**内容长度**
|
||||||
|
单次最长 4000 tokens, 上下文最长 8000 tokens, 上下文超长时会被截断。
|
||||||
* 当前网页链接:http://docgpt.ahapocket.cn/chat?chatId=6402c9f64cb5d6283f764&windowId=6402c94cb5d6283f76fb49
|
|
||||||
* 分享链接应为:http://docgpt.ahapocket.cn/chat?chatId=6402c9f64cb5d6283f764
|
**删除和复制**
|
||||||
|
点击对话头像,可以选择复制或删除该条内容。
|
||||||
### 其他问题
|
|
||||||
还有其他问题,可以加我 wx,拉个交流群大家一起聊聊。
|
**代理出错**
|
||||||

|
服务器代理不稳定,可以过一会儿再尝试。
|
||||||
|
`;
|
||||||
|
|
||||||
|
export const versionIntro = `
|
||||||
|
## Fast GPT V2.3
|
||||||
|
* 数据集导出功能,可用于知识库分享。
|
||||||
|
* 优化文件拆分功能,可自定义提示词。
|
||||||
|
* 定制知识库:创建模型时可以选择【知识库】模型, 可以手动导入知识点或者直接导入一个文件自动学习。
|
||||||
|
`;
|
||||||
|
|
||||||
|
export const shareHint = `
|
||||||
|
你正准备分享对话,请确保分享链接不会滥用,因为它是使用的是你的 API key。
|
||||||
|
* 分享空白对话:为该模型创建一个空白的聊天分享出去。
|
||||||
|
* 分享当前对话:会把当前聊天的内容也分享出去,但是要注意不要多个人同时用一个聊天内容。
|
||||||
`;
|
`;
|
||||||
|
|||||||
6
src/constants/data.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import type { DataType } from '@/types/data';
|
||||||
|
|
||||||
|
export const DataTypeTextMap: Record<DataType, string> = {
|
||||||
|
QA: '问答拆分',
|
||||||
|
abstract: '摘要总结'
|
||||||
|
};
|
||||||
@@ -1,22 +1,62 @@
|
|||||||
export enum OpenAiModelEnum {
|
import type { ServiceName, ModelDataType, ModelSchema } from '@/types/mongoSchema';
|
||||||
|
import type { RedisModelDataItemType } from '@/types/redis';
|
||||||
|
|
||||||
|
export enum ChatModelNameEnum {
|
||||||
GPT35 = 'gpt-3.5-turbo',
|
GPT35 = 'gpt-3.5-turbo',
|
||||||
GPT3 = 'text-davinci-003'
|
VECTOR_GPT = 'VECTOR_GPT',
|
||||||
|
GPT3 = 'text-davinci-003',
|
||||||
|
VECTOR = 'text-embedding-ada-002'
|
||||||
}
|
}
|
||||||
export const OpenAiList = [
|
|
||||||
|
export const ChatModelNameMap = {
|
||||||
|
[ChatModelNameEnum.GPT35]: 'gpt-3.5-turbo',
|
||||||
|
[ChatModelNameEnum.VECTOR_GPT]: 'gpt-3.5-turbo',
|
||||||
|
[ChatModelNameEnum.GPT3]: 'text-davinci-003',
|
||||||
|
[ChatModelNameEnum.VECTOR]: 'text-embedding-ada-002'
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ModelConstantsData = {
|
||||||
|
serviceCompany: `${ServiceName}`;
|
||||||
|
name: string;
|
||||||
|
model: `${ChatModelNameEnum}`;
|
||||||
|
trainName: string; // 空字符串代表不能训练
|
||||||
|
maxToken: number;
|
||||||
|
contextMaxToken: number;
|
||||||
|
maxTemperature: number;
|
||||||
|
price: number; // 多少钱 / 1token,单位: 0.00001元
|
||||||
|
};
|
||||||
|
|
||||||
|
export const modelList: ModelConstantsData[] = [
|
||||||
{
|
{
|
||||||
|
serviceCompany: 'openai',
|
||||||
name: 'chatGPT',
|
name: 'chatGPT',
|
||||||
model: OpenAiModelEnum.GPT35,
|
model: ChatModelNameEnum.GPT35,
|
||||||
trainName: 'turbo',
|
trainName: '',
|
||||||
canTraining: false,
|
maxToken: 4000,
|
||||||
maxToken: 4060
|
contextMaxToken: 7500,
|
||||||
|
maxTemperature: 2,
|
||||||
|
price: 3
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'GPT3',
|
serviceCompany: 'openai',
|
||||||
model: OpenAiModelEnum.GPT3,
|
name: '知识库',
|
||||||
trainName: 'davinci',
|
model: ChatModelNameEnum.VECTOR_GPT,
|
||||||
canTraining: true,
|
trainName: 'vector',
|
||||||
maxToken: 4060
|
maxToken: 4000,
|
||||||
|
contextMaxToken: 7000,
|
||||||
|
maxTemperature: 1,
|
||||||
|
price: 3
|
||||||
}
|
}
|
||||||
|
// {
|
||||||
|
// serviceCompany: 'openai',
|
||||||
|
// name: 'GPT3',
|
||||||
|
// model: ChatModelNameEnum.GPT3,
|
||||||
|
// trainName: 'davinci',
|
||||||
|
// maxToken: 4000,
|
||||||
|
// contextMaxToken: 7500,
|
||||||
|
// maxTemperature: 2,
|
||||||
|
// price: 30
|
||||||
|
// }
|
||||||
];
|
];
|
||||||
|
|
||||||
export enum TrainingStatusEnum {
|
export enum TrainingStatusEnum {
|
||||||
@@ -51,3 +91,34 @@ export const formatModelStatus = {
|
|||||||
text: '已关闭'
|
text: '已关闭'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const ModelDataStatusMap: Record<RedisModelDataItemType['status'], string> = {
|
||||||
|
ready: '训练完成',
|
||||||
|
waiting: '训练中'
|
||||||
|
};
|
||||||
|
|
||||||
|
export const defaultModel: ModelSchema = {
|
||||||
|
_id: '',
|
||||||
|
userId: '',
|
||||||
|
name: '',
|
||||||
|
avatar: '',
|
||||||
|
status: ModelStatusEnum.pending,
|
||||||
|
updateTime: Date.now(),
|
||||||
|
trainingTimes: 0,
|
||||||
|
systemPrompt: '',
|
||||||
|
intro: '',
|
||||||
|
temperature: 5,
|
||||||
|
service: {
|
||||||
|
company: 'openai',
|
||||||
|
trainId: '',
|
||||||
|
chatModel: ChatModelNameEnum.GPT35,
|
||||||
|
modelName: ChatModelNameEnum.GPT35
|
||||||
|
},
|
||||||
|
security: {
|
||||||
|
domain: ['*'],
|
||||||
|
contextMaxLen: 1,
|
||||||
|
contentMaxLen: 1,
|
||||||
|
expiredTime: 9999,
|
||||||
|
maxLoadAmount: 1
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
6
src/constants/redis.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export const VecModelDataPrefix = 'model:data';
|
||||||
|
export const VecModelDataIdx = `idx:${VecModelDataPrefix}:hash`;
|
||||||
|
export enum ModelDataStatusEnum {
|
||||||
|
ready = 'ready',
|
||||||
|
waiting = 'waiting'
|
||||||
|
}
|
||||||
@@ -20,30 +20,38 @@ const Button = defineStyleConfig({
|
|||||||
baseStyle: {},
|
baseStyle: {},
|
||||||
sizes: {
|
sizes: {
|
||||||
sm: {
|
sm: {
|
||||||
fontSize: 'sm',
|
fontSize: 'xs',
|
||||||
px: 3,
|
px: 3,
|
||||||
py: 0,
|
py: 0,
|
||||||
fontWeight: 'normal',
|
fontWeight: 'normal',
|
||||||
height: '26px'
|
height: '26px',
|
||||||
|
lineHeight: '26px'
|
||||||
},
|
},
|
||||||
md: {
|
md: {
|
||||||
fontSize: 'md',
|
fontSize: 'sm',
|
||||||
px: 6,
|
px: 6,
|
||||||
py: 0,
|
py: 0,
|
||||||
height: '34px',
|
height: '34px',
|
||||||
|
lineHeight: '34px',
|
||||||
fontWeight: 'normal'
|
fontWeight: 'normal'
|
||||||
},
|
},
|
||||||
lg: {
|
lg: {
|
||||||
fontSize: 'lg',
|
fontSize: 'md',
|
||||||
px: 8,
|
px: 8,
|
||||||
py: 0,
|
py: 0,
|
||||||
height: '42px',
|
height: '42px',
|
||||||
|
lineHeight: '42px',
|
||||||
fontWeight: 'normal'
|
fontWeight: 'normal'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
variants: {
|
variants: {
|
||||||
outline: {
|
white: {
|
||||||
borderWidth: '1.5px'
|
color: '#fff',
|
||||||
|
backgroundColor: 'transparent',
|
||||||
|
border: '1px solid #ffffff',
|
||||||
|
_hover: {
|
||||||
|
backgroundColor: 'rgba(255,255,255,0.1)'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
defaultProps: {
|
defaultProps: {
|
||||||
@@ -58,17 +66,12 @@ export const theme = extendTheme({
|
|||||||
global: {
|
global: {
|
||||||
'html, body': {
|
'html, body': {
|
||||||
color: 'blackAlpha.800',
|
color: 'blackAlpha.800',
|
||||||
fontSize: '14px',
|
|
||||||
fontFamily:
|
|
||||||
'Söhne,ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif,Helvetica Neue,Arial,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji',
|
|
||||||
height: '100%',
|
height: '100%',
|
||||||
overflowY: 'auto'
|
maxHeight: '100vh',
|
||||||
|
overflowY: 'hidden'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fonts: {
|
|
||||||
body: 'system-ui, sans-serif'
|
|
||||||
},
|
|
||||||
fontSizes: {
|
fontSizes: {
|
||||||
xs: '0.8rem',
|
xs: '0.8rem',
|
||||||
sm: '0.9rem',
|
sm: '0.9rem',
|
||||||
@@ -84,6 +87,16 @@ export const theme = extendTheme({
|
|||||||
'8xl': '6rem',
|
'8xl': '6rem',
|
||||||
'9xl': '8rem'
|
'9xl': '8rem'
|
||||||
},
|
},
|
||||||
|
fonts: {
|
||||||
|
body: '-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol"'
|
||||||
|
},
|
||||||
|
breakpoints: {
|
||||||
|
sm: '900px',
|
||||||
|
md: '1200px',
|
||||||
|
lg: '1500px',
|
||||||
|
xl: '1800',
|
||||||
|
'2xl': '2100'
|
||||||
|
},
|
||||||
components: {
|
components: {
|
||||||
Modal: ModalTheme,
|
Modal: ModalTheme,
|
||||||
Button
|
Button
|
||||||
|
|||||||
@@ -1,5 +1,22 @@
|
|||||||
|
export enum BillTypeEnum {
|
||||||
|
chat = 'chat',
|
||||||
|
splitData = 'splitData',
|
||||||
|
QA = 'QA',
|
||||||
|
abstract = 'abstract',
|
||||||
|
vector = 'vector',
|
||||||
|
return = 'return'
|
||||||
|
}
|
||||||
export enum PageTypeEnum {
|
export enum PageTypeEnum {
|
||||||
login = 'login',
|
login = 'login',
|
||||||
register = 'register',
|
register = 'register',
|
||||||
forgetPassword = 'forgetPassword'
|
forgetPassword = 'forgetPassword'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const BillTypeMap: Record<`${BillTypeEnum}`, string> = {
|
||||||
|
[BillTypeEnum.chat]: '对话',
|
||||||
|
[BillTypeEnum.splitData]: 'QA拆分',
|
||||||
|
[BillTypeEnum.QA]: 'QA拆分',
|
||||||
|
[BillTypeEnum.abstract]: '摘要总结',
|
||||||
|
[BillTypeEnum.vector]: '索引生成',
|
||||||
|
[BillTypeEnum.return]: '退款'
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useRef } from 'react';
|
import { useCallback, useRef } from 'react';
|
||||||
import {
|
import {
|
||||||
AlertDialog,
|
AlertDialog,
|
||||||
AlertDialogBody,
|
AlertDialogBody,
|
||||||
@@ -17,45 +17,52 @@ export const useConfirm = ({ title = '提示', content }: { title?: string; cont
|
|||||||
const cancelCb = useRef<any>();
|
const cancelCb = useRef<any>();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
openConfirm: (confirm?: any, cancel?: any) => {
|
openConfirm: useCallback(
|
||||||
onOpen();
|
(confirm?: any, cancel?: any) => {
|
||||||
confirmCb.current = confirm;
|
confirmCb.current = confirm;
|
||||||
cancelCb.current = cancel;
|
cancelCb.current = cancel;
|
||||||
},
|
|
||||||
ConfirmChild: () => (
|
|
||||||
<AlertDialog isOpen={isOpen} leastDestructiveRef={cancelRef} onClose={onClose}>
|
|
||||||
<AlertDialogOverlay>
|
|
||||||
<AlertDialogContent>
|
|
||||||
<AlertDialogHeader fontSize="lg" fontWeight="bold">
|
|
||||||
{title}
|
|
||||||
</AlertDialogHeader>
|
|
||||||
|
|
||||||
<AlertDialogBody>{content}</AlertDialogBody>
|
return onOpen;
|
||||||
|
},
|
||||||
|
[onOpen]
|
||||||
|
),
|
||||||
|
ConfirmChild: useCallback(
|
||||||
|
() => (
|
||||||
|
<AlertDialog isOpen={isOpen} leastDestructiveRef={cancelRef} onClose={onClose}>
|
||||||
|
<AlertDialogOverlay>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader fontSize="lg" fontWeight="bold">
|
||||||
|
{title}
|
||||||
|
</AlertDialogHeader>
|
||||||
|
|
||||||
<AlertDialogFooter>
|
<AlertDialogBody>{content}</AlertDialogBody>
|
||||||
<Button
|
|
||||||
colorScheme={'gray'}
|
<AlertDialogFooter>
|
||||||
onClick={() => {
|
<Button
|
||||||
onClose();
|
colorScheme={'gray'}
|
||||||
typeof cancelCb.current === 'function' && cancelCb.current();
|
onClick={() => {
|
||||||
}}
|
onClose();
|
||||||
>
|
typeof cancelCb.current === 'function' && cancelCb.current();
|
||||||
取消
|
}}
|
||||||
</Button>
|
>
|
||||||
<Button
|
取消
|
||||||
colorScheme="blue"
|
</Button>
|
||||||
ml={3}
|
<Button
|
||||||
onClick={() => {
|
colorScheme="blue"
|
||||||
onClose();
|
ml={4}
|
||||||
typeof confirmCb.current === 'function' && confirmCb.current();
|
onClick={() => {
|
||||||
}}
|
onClose();
|
||||||
>
|
typeof confirmCb.current === 'function' && confirmCb.current();
|
||||||
确认
|
}}
|
||||||
</Button>
|
>
|
||||||
</AlertDialogFooter>
|
确认
|
||||||
</AlertDialogContent>
|
</Button>
|
||||||
</AlertDialogOverlay>
|
</AlertDialogFooter>
|
||||||
</AlertDialog>
|
</AlertDialogContent>
|
||||||
|
</AlertDialogOverlay>
|
||||||
|
</AlertDialog>
|
||||||
|
),
|
||||||
|
[content, isOpen, onClose, title]
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,36 +1,33 @@
|
|||||||
import { useState, memo } from 'react';
|
import { useState, useCallback } from 'react';
|
||||||
import { Spinner, Flex } from '@chakra-ui/react';
|
import { Spinner, Flex } from '@chakra-ui/react';
|
||||||
|
|
||||||
export const useLoading = (props?: { defaultLoading: boolean }) => {
|
export const useLoading = (props?: { defaultLoading: boolean }) => {
|
||||||
const [isLoading, setIsLoading] = useState(props?.defaultLoading || false);
|
const [isLoading, setIsLoading] = useState(props?.defaultLoading || false);
|
||||||
|
|
||||||
const Loading = ({
|
const Loading = useCallback(
|
||||||
loading,
|
({ loading, fixed = true }: { loading?: boolean; fixed?: boolean }): JSX.Element | null => {
|
||||||
fixed = true
|
return isLoading || loading ? (
|
||||||
}: {
|
<Flex
|
||||||
loading?: boolean;
|
position={fixed ? 'fixed' : 'absolute'}
|
||||||
fixed?: boolean;
|
zIndex={100}
|
||||||
}): JSX.Element | null => {
|
backgroundColor={'rgba(255,255,255,0.5)'}
|
||||||
return isLoading || loading ? (
|
top={0}
|
||||||
<Flex
|
left={0}
|
||||||
position={fixed ? 'fixed' : 'absolute'}
|
right={0}
|
||||||
zIndex={100}
|
bottom={0}
|
||||||
backgroundColor={'rgba(255,255,255,0.5)'}
|
alignItems={'center'}
|
||||||
top={0}
|
justifyContent={'center'}
|
||||||
left={0}
|
>
|
||||||
right={0}
|
<Spinner thickness="4px" speed="0.65s" emptyColor="gray.200" color="blue.500" size="xl" />
|
||||||
bottom={0}
|
</Flex>
|
||||||
alignItems={'center'}
|
) : null;
|
||||||
justifyContent={'center'}
|
},
|
||||||
>
|
[isLoading]
|
||||||
<Spinner thickness="4px" speed="0.65s" emptyColor="gray.200" color="blue.500" size="xl" />
|
);
|
||||||
</Flex>
|
|
||||||
) : null;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isLoading,
|
isLoading,
|
||||||
setIsLoading,
|
setIsLoading,
|
||||||
Loading: memo(Loading)
|
Loading
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
81
src/hooks/usePagination.tsx
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { useState, useCallback, useMemo } from 'react';
|
||||||
|
import type { PagingData } from '../types/index';
|
||||||
|
import { IconButton, Flex, Box } from '@chakra-ui/react';
|
||||||
|
import { ArrowBackIcon, ArrowForwardIcon } from '@chakra-ui/icons';
|
||||||
|
import { useQuery, useMutation } from '@tanstack/react-query';
|
||||||
|
import { useToast } from './useToast';
|
||||||
|
|
||||||
|
export const usePagination = <T = any,>({
|
||||||
|
api,
|
||||||
|
pageSize = 10,
|
||||||
|
params = {}
|
||||||
|
}: {
|
||||||
|
api: (data: any) => any;
|
||||||
|
pageSize?: number;
|
||||||
|
params?: Record<string, any>;
|
||||||
|
}) => {
|
||||||
|
const { toast } = useToast();
|
||||||
|
const [pageNum, setPageNum] = useState(1);
|
||||||
|
const [total, setTotal] = useState(0);
|
||||||
|
const [data, setData] = useState<T[]>([]);
|
||||||
|
const maxPage = useMemo(() => Math.ceil(total / pageSize), [pageSize, total]);
|
||||||
|
|
||||||
|
const { mutate, isLoading } = useMutation({
|
||||||
|
mutationFn: async (num: number = pageNum) => {
|
||||||
|
try {
|
||||||
|
const res: PagingData<T> = await api({
|
||||||
|
pageNum: num,
|
||||||
|
pageSize,
|
||||||
|
...params
|
||||||
|
});
|
||||||
|
setPageNum(num);
|
||||||
|
setTotal(res.total);
|
||||||
|
setData(res.data);
|
||||||
|
} catch (error: any) {
|
||||||
|
toast({
|
||||||
|
title: error?.message || '获取数据异常',
|
||||||
|
status: 'error'
|
||||||
|
});
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
useQuery(['init'], () => {
|
||||||
|
mutate(1);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
const Pagination = useCallback(() => {
|
||||||
|
return (
|
||||||
|
<Flex alignItems={'center'} justifyContent={'end'}>
|
||||||
|
<IconButton
|
||||||
|
isDisabled={pageNum === 1}
|
||||||
|
icon={<ArrowBackIcon />}
|
||||||
|
aria-label={'left'}
|
||||||
|
size={'sm'}
|
||||||
|
onClick={() => mutate(pageNum - 1)}
|
||||||
|
/>
|
||||||
|
<Box mx={2}>
|
||||||
|
{pageNum}/{maxPage}
|
||||||
|
</Box>
|
||||||
|
<IconButton
|
||||||
|
isDisabled={pageNum === maxPage}
|
||||||
|
icon={<ArrowForwardIcon />}
|
||||||
|
aria-label={'left'}
|
||||||
|
size={'sm'}
|
||||||
|
onClick={() => mutate(pageNum + 1)}
|
||||||
|
/>
|
||||||
|
</Flex>
|
||||||
|
);
|
||||||
|
}, [maxPage, mutate, pageNum]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
total,
|
||||||
|
data,
|
||||||
|
isLoading,
|
||||||
|
Pagination,
|
||||||
|
getData: mutate
|
||||||
|
};
|
||||||
|
};
|
||||||
81
src/hooks/usePaging.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { useState, useCallback } from 'react';
|
||||||
|
import type { PagingData } from '../types/index';
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
import { useToast } from './useToast';
|
||||||
|
|
||||||
|
export const usePaging = <T = any>({
|
||||||
|
api,
|
||||||
|
pageSize = 10,
|
||||||
|
params = {}
|
||||||
|
}: {
|
||||||
|
api: (data: any) => any;
|
||||||
|
pageSize?: number;
|
||||||
|
params?: Record<string, any>;
|
||||||
|
}) => {
|
||||||
|
const { toast } = useToast();
|
||||||
|
const [data, setData] = useState<T[]>([]);
|
||||||
|
const [pageNum, setPageNum] = useState(1);
|
||||||
|
const [total, setTotal] = useState(0);
|
||||||
|
const [isLoadAll, setIsLoadAll] = useState(false);
|
||||||
|
const [requesting, setRequesting] = useState(false);
|
||||||
|
const [initRequesting, setInitRequesting] = useState(false);
|
||||||
|
|
||||||
|
const getData = useCallback(
|
||||||
|
async (num: number, init = false) => {
|
||||||
|
if (requesting) return;
|
||||||
|
if (!init && isLoadAll) return;
|
||||||
|
if (init) {
|
||||||
|
setInitRequesting(true);
|
||||||
|
}
|
||||||
|
setRequesting(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res: PagingData<T> = await api({
|
||||||
|
pageNum: num,
|
||||||
|
pageSize,
|
||||||
|
...params
|
||||||
|
});
|
||||||
|
setData((state) => {
|
||||||
|
const data = init ? res.data : state.concat(res.data);
|
||||||
|
if (data.length >= res.total) {
|
||||||
|
setIsLoadAll(true);
|
||||||
|
}
|
||||||
|
setTotal(res.total);
|
||||||
|
setPageNum(num);
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
toast({
|
||||||
|
title: error?.message || '获取数据异常',
|
||||||
|
status: 'error'
|
||||||
|
});
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
setRequesting(false);
|
||||||
|
setInitRequesting(false);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
[api, isLoadAll, pageSize, params, requesting, toast]
|
||||||
|
);
|
||||||
|
|
||||||
|
const nextPage = useCallback(() => {
|
||||||
|
if (requesting || isLoadAll) return;
|
||||||
|
getData(pageNum + 1);
|
||||||
|
}, [getData, isLoadAll, pageNum, requesting]);
|
||||||
|
|
||||||
|
useQuery(['init'], () => getData(1, true));
|
||||||
|
|
||||||
|
return {
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
total,
|
||||||
|
data,
|
||||||
|
getData,
|
||||||
|
requesting,
|
||||||
|
isLoadAll,
|
||||||
|
nextPage,
|
||||||
|
initRequesting,
|
||||||
|
setData
|
||||||
|
};
|
||||||
|
};
|
||||||
33
src/hooks/useRequest.tsx
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { useToast } from '@/hooks/useToast';
|
||||||
|
import { useMutation } from '@tanstack/react-query';
|
||||||
|
import type { UseMutationOptions } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
interface Props extends UseMutationOptions<any, any, any, any> {
|
||||||
|
successToast?: string;
|
||||||
|
errorToast?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useRequest = ({ successToast, errorToast, onSuccess, onError, ...props }: Props) => {
|
||||||
|
const { toast } = useToast();
|
||||||
|
const mutation = useMutation<unknown, unknown, any, unknown>({
|
||||||
|
...props,
|
||||||
|
onSuccess(res, variables: void, context: unknown) {
|
||||||
|
onSuccess?.(res, variables, context);
|
||||||
|
successToast &&
|
||||||
|
toast({
|
||||||
|
title: successToast,
|
||||||
|
status: 'success'
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onError(err: any, variables: void, context: unknown) {
|
||||||
|
onError?.(err, variables, context);
|
||||||
|
errorToast &&
|
||||||
|
toast({
|
||||||
|
title: typeof err === 'string' ? err : err?.message || errorToast,
|
||||||
|
status: 'error'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return mutation;
|
||||||
|
};
|
||||||
34
src/hooks/useSelectFile.tsx
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import React, { useRef, useCallback } from 'react';
|
||||||
|
import { Box } from '@chakra-ui/react';
|
||||||
|
|
||||||
|
export const useSelectFile = (props?: { fileType?: string; multiple?: boolean }) => {
|
||||||
|
const { fileType = '*', multiple = false } = props || {};
|
||||||
|
const SelectFileDom = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
const File = useCallback(
|
||||||
|
({ onSelect }: { onSelect: (e: File[]) => void }) => (
|
||||||
|
<Box position={'absolute'} w={0} h={0} overflow={'hidden'}>
|
||||||
|
<input
|
||||||
|
ref={SelectFileDom}
|
||||||
|
type="file"
|
||||||
|
accept={fileType}
|
||||||
|
multiple={multiple}
|
||||||
|
onChange={(e) => {
|
||||||
|
if (!e.target.files || e.target.files?.length === 0) return;
|
||||||
|
onSelect(Array.from(e.target.files));
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
),
|
||||||
|
[fileType, multiple]
|
||||||
|
);
|
||||||
|
|
||||||
|
const onOpen = useCallback(() => {
|
||||||
|
SelectFileDom.current && SelectFileDom.current.click();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return {
|
||||||
|
File,
|
||||||
|
onOpen
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,14 +1,11 @@
|
|||||||
import { useState, useMemo, useCallback } from 'react';
|
import { useState, useMemo, useCallback } from 'react';
|
||||||
import { sendCodeToEmail } from '@/api/user';
|
import { sendCodeToEmail } from '@/api/user';
|
||||||
import { EmailTypeEnum } from '@/constants/common';
|
import { EmailTypeEnum } from '@/constants/common';
|
||||||
import { useToast } from '@chakra-ui/react';
|
|
||||||
let timer: any;
|
let timer: any;
|
||||||
|
import { useToast } from './useToast';
|
||||||
|
|
||||||
export const useSendCode = () => {
|
export const useSendCode = () => {
|
||||||
const toast = useToast({
|
const { toast } = useToast();
|
||||||
position: 'top',
|
|
||||||
duration: 2000
|
|
||||||
});
|
|
||||||
const [codeSending, setCodeSending] = useState(false);
|
const [codeSending, setCodeSending] = useState(false);
|
||||||
const [codeCountDown, setCodeCountDown] = useState(0);
|
const [codeCountDown, setCodeCountDown] = useState(0);
|
||||||
const sendCodeText = useMemo(() => {
|
const sendCodeText = useMemo(() => {
|
||||||
@@ -43,13 +40,11 @@ export const useSendCode = () => {
|
|||||||
status: 'success',
|
status: 'success',
|
||||||
position: 'top'
|
position: 'top'
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
typeof error === 'string' &&
|
toast({
|
||||||
toast({
|
title: error.message || '发送验证码异常',
|
||||||
title: error,
|
status: 'error'
|
||||||
status: 'error',
|
});
|
||||||
position: 'top'
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
setCodeSending(false);
|
setCodeSending(false);
|
||||||
},
|
},
|
||||||
|
|||||||
18
src/hooks/useTabs.tsx
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import React, { useState, useCallback, useRef } from 'react';
|
||||||
|
|
||||||
|
export const useTabs = ({
|
||||||
|
tabs = []
|
||||||
|
}: {
|
||||||
|
tabs: {
|
||||||
|
id: string;
|
||||||
|
label: string;
|
||||||
|
}[];
|
||||||
|
}) => {
|
||||||
|
const [activeTab, setActiveTab] = useState(tabs[0].id);
|
||||||
|
|
||||||
|
return {
|
||||||
|
tabs,
|
||||||
|
activeTab,
|
||||||
|
setActiveTab
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import type { AppProps, NextWebVitalsMetric } from 'next/app';
|
import { useEffect } from 'react';
|
||||||
|
import type { AppProps } from 'next/app';
|
||||||
import Script from 'next/script';
|
import Script from 'next/script';
|
||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
import { ChakraProvider } from '@chakra-ui/react';
|
import { ChakraProvider, ColorModeScript } from '@chakra-ui/react';
|
||||||
import Layout from '@/components/Layout';
|
import Layout from '@/components/Layout';
|
||||||
import { theme } from '@/constants/theme';
|
import { theme } from '@/constants/theme';
|
||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
@@ -9,38 +10,53 @@ import NProgress from 'nprogress'; //nprogress module
|
|||||||
import Router from 'next/router';
|
import Router from 'next/router';
|
||||||
import 'nprogress/nprogress.css';
|
import 'nprogress/nprogress.css';
|
||||||
import '../styles/reset.scss';
|
import '../styles/reset.scss';
|
||||||
|
import { useToast } from '@/hooks/useToast';
|
||||||
|
|
||||||
//Binding events.
|
//Binding events.
|
||||||
Router.events.on('routeChangeStart', () => NProgress.start());
|
Router.events.on('routeChangeStart', () => NProgress.start());
|
||||||
Router.events.on('routeChangeComplete', () => NProgress.done());
|
Router.events.on('routeChangeComplete', () => NProgress.done());
|
||||||
Router.events.on('routeChangeError', () => NProgress.done());
|
Router.events.on('routeChangeError', () => NProgress.done());
|
||||||
|
|
||||||
export default function App({ Component, pageProps }: AppProps) {
|
// Create a client
|
||||||
// Create a client
|
const queryClient = new QueryClient({
|
||||||
const queryClient = new QueryClient({
|
defaultOptions: {
|
||||||
defaultOptions: {
|
queries: {
|
||||||
queries: {
|
refetchOnWindowFocus: false,
|
||||||
refetchOnWindowFocus: false,
|
retry: false,
|
||||||
retry: false,
|
cacheTime: 0
|
||||||
cacheTime: 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default function App({ Component, pageProps }: AppProps) {
|
||||||
|
const { toast } = useToast();
|
||||||
|
// 校验是否支持 click 事件
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof document.createElement('div').click !== 'function') {
|
||||||
|
toast({
|
||||||
|
title: '你的浏览器版本过低',
|
||||||
|
status: 'warning'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [toast]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head>
|
<Head>
|
||||||
<title>Doc GPT</title>
|
<title>Fast GPT</title>
|
||||||
<meta name="description" content="Generated by Doc GPT" />
|
<meta name="description" content="Generated by Fast GPT" />
|
||||||
<meta
|
<meta
|
||||||
name="viewport"
|
name="viewport"
|
||||||
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;"
|
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;"
|
||||||
/>
|
/>
|
||||||
<link rel="icon" href="/favicon.ico" />
|
<link rel="icon" href="/favicon.ico" />
|
||||||
</Head>
|
</Head>
|
||||||
<Script src="/iconfont.js" strategy="afterInteractive"></Script>
|
<Script src="/js/iconfont.js" strategy="afterInteractive"></Script>
|
||||||
|
<Script src="/js/qrcode.min.js" strategy="afterInteractive"></Script>
|
||||||
|
<Script src="/js/pdf.js" strategy="afterInteractive"></Script>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<ChakraProvider theme={theme}>
|
<ChakraProvider theme={theme}>
|
||||||
|
<ColorModeScript initialColorMode={theme.config.initialColorMode} />
|
||||||
<Layout>
|
<Layout>
|
||||||
<Component {...pageProps} />
|
<Component {...pageProps} />
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|||||||
@@ -1,116 +1,171 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { connectToDatabase, Chat, ChatWindow } from '@/service/mongo';
|
import { createParser, ParsedEvent, ReconnectInterval } from 'eventsource-parser';
|
||||||
import type { ModelType } from '@/types/model';
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
import { getOpenAIApi, authChat } from '@/service/utils/chat';
|
import { getOpenAIApi, authChat } from '@/service/utils/chat';
|
||||||
import { openaiProxy } from '@/service/utils/tools';
|
import { httpsAgent, openaiChatFilter } from '@/service/utils/tools';
|
||||||
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
|
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
|
||||||
import { ChatItemType } from '@/types/chat';
|
import { ChatItemType } from '@/types/chat';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import type { ModelSchema } from '@/types/mongoSchema';
|
||||||
|
import { PassThrough } from 'stream';
|
||||||
|
import { modelList } from '@/constants/model';
|
||||||
|
import { pushChatBill } from '@/service/events/pushBill';
|
||||||
|
|
||||||
/* 发送提示词 */
|
/* 发送提示词 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
res.writeHead(200, {
|
let step = 0; // step=1时,表示开始了流响应
|
||||||
Connection: 'keep-alive',
|
const stream = new PassThrough();
|
||||||
'Content-Encoding': 'none',
|
stream.on('error', () => {
|
||||||
'Cache-Control': 'no-cache',
|
console.log('error: ', 'stream error');
|
||||||
'Content-Type': 'text/event-stream'
|
stream.destroy();
|
||||||
|
});
|
||||||
|
res.on('close', () => {
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
res.on('error', () => {
|
||||||
|
console.log('error: ', 'request error');
|
||||||
|
stream.destroy();
|
||||||
});
|
});
|
||||||
const { chatId, windowId } = req.query as { chatId: string; windowId: string };
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!windowId || !chatId) {
|
const { chatId, prompt } = req.body as {
|
||||||
|
prompt: ChatItemType;
|
||||||
|
chatId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
if (!chatId || !prompt) {
|
||||||
throw new Error('缺少参数');
|
throw new Error('缺少参数');
|
||||||
}
|
}
|
||||||
|
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
let startTime = Date.now();
|
||||||
|
|
||||||
const { chat, userApiKey } = await authChat(chatId);
|
const { chat, userApiKey, systemKey, userId } = await authChat(chatId, authorization);
|
||||||
|
|
||||||
const model: ModelType = chat.modelId;
|
const model: ModelSchema = chat.modelId;
|
||||||
|
const modelConstantsData = modelList.find((item) => item.model === model.service.modelName);
|
||||||
|
if (!modelConstantsData) {
|
||||||
|
throw new Error('模型加载异常');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取对话内容
|
||||||
|
const prompts = [...chat.content, prompt];
|
||||||
|
|
||||||
|
// 如果有系统提示词,自动插入
|
||||||
|
if (model.systemPrompt) {
|
||||||
|
prompts.unshift({
|
||||||
|
obj: 'SYSTEM',
|
||||||
|
value: model.systemPrompt
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 控制在 tokens 数量,防止超出
|
||||||
|
const filterPrompts = openaiChatFilter(prompts, modelConstantsData.contextMaxToken);
|
||||||
|
|
||||||
|
// 格式化文本内容成 chatgpt 格式
|
||||||
const map = {
|
const map = {
|
||||||
Human: ChatCompletionRequestMessageRoleEnum.User,
|
Human: ChatCompletionRequestMessageRoleEnum.User,
|
||||||
AI: ChatCompletionRequestMessageRoleEnum.Assistant,
|
AI: ChatCompletionRequestMessageRoleEnum.Assistant,
|
||||||
SYSTEM: ChatCompletionRequestMessageRoleEnum.System
|
SYSTEM: ChatCompletionRequestMessageRoleEnum.System
|
||||||
};
|
};
|
||||||
// 读取对话内容
|
|
||||||
const prompts: ChatItemType[] = (await ChatWindow.findById(windowId)).content;
|
|
||||||
|
|
||||||
// 长度过滤
|
|
||||||
const maxContext = model.security.contextMaxLen;
|
|
||||||
const filterPrompts =
|
|
||||||
prompts.length > maxContext + 2
|
|
||||||
? [prompts[0], ...prompts.slice(prompts.length - maxContext)]
|
|
||||||
: prompts.slice(0, prompts.length);
|
|
||||||
|
|
||||||
// 格式化文本内容
|
|
||||||
const formatPrompts: ChatCompletionRequestMessage[] = filterPrompts.map(
|
const formatPrompts: ChatCompletionRequestMessage[] = filterPrompts.map(
|
||||||
(item: ChatItemType) => ({
|
(item: ChatItemType) => ({
|
||||||
role: map[item.obj],
|
role: map[item.obj],
|
||||||
content: item.value
|
content: item.value
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
// 第一句话,强调代码类型
|
// console.log(formatPrompts);
|
||||||
formatPrompts.unshift({
|
// 计算温度
|
||||||
role: ChatCompletionRequestMessageRoleEnum.System,
|
const temperature = modelConstantsData.maxTemperature * (model.temperature / 10);
|
||||||
content:
|
|
||||||
'If the content is code or code blocks, please label the code type as accurately as possible.'
|
|
||||||
});
|
|
||||||
|
|
||||||
// 获取 chatAPI
|
// 获取 chatAPI
|
||||||
const chatAPI = getOpenAIApi(userApiKey);
|
const chatAPI = getOpenAIApi(userApiKey || systemKey);
|
||||||
|
// 发出请求
|
||||||
const chatResponse = await chatAPI.createChatCompletion(
|
const chatResponse = await chatAPI.createChatCompletion(
|
||||||
{
|
{
|
||||||
model: model.service.chatModel,
|
model: model.service.chatModel,
|
||||||
temperature: 1,
|
temperature: temperature,
|
||||||
// max_tokens: model.security.contentMaxLen,
|
// max_tokens: modelConstantsData.maxToken,
|
||||||
messages: formatPrompts,
|
messages: formatPrompts,
|
||||||
stream: true
|
frequency_penalty: 0.5, // 越大,重复内容越少
|
||||||
|
presence_penalty: -0.5, // 越大,越容易出现新内容
|
||||||
|
stream: true,
|
||||||
|
stop: ['.!?。']
|
||||||
},
|
},
|
||||||
openaiProxy
|
{
|
||||||
|
timeout: 40000,
|
||||||
|
responseType: 'stream',
|
||||||
|
httpsAgent
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// 截取字符串内容
|
console.log('api response time:', `${(Date.now() - startTime) / 1000}s`);
|
||||||
const reg = /{"content"(.*)"}/g;
|
|
||||||
// @ts-ignore
|
// 创建响应流
|
||||||
const match = chatResponse.data.match(reg);
|
res.setHeader('Content-Type', 'text/event-stream;charset-utf-8');
|
||||||
let AIResponse = '';
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
if (match) {
|
res.setHeader('X-Accel-Buffering', 'no');
|
||||||
match.forEach((item: string, i: number) => {
|
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||||
try {
|
step = 1;
|
||||||
const json = JSON.parse(item);
|
|
||||||
// 开头的换行忽略
|
let responseContent = '';
|
||||||
if (i === 0 && json.content?.startsWith('\n')) return;
|
stream.pipe(res);
|
||||||
AIResponse += json.content;
|
|
||||||
const content = json.content.replace(/\n/g, '<br/>'); // 无法直接传输\n
|
const onParse = async (event: ParsedEvent | ReconnectInterval) => {
|
||||||
content && res.write(`data: ${content}\n\n`);
|
if (event.type !== 'event') return;
|
||||||
} catch (err) {
|
const data = event.data;
|
||||||
err;
|
if (data === '[DONE]') return;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
const content: string = json?.choices?.[0].delta.content || '';
|
||||||
|
if (!content || (responseContent === '' && content === '\n')) return;
|
||||||
|
|
||||||
|
responseContent += content;
|
||||||
|
// console.log('content:', content)
|
||||||
|
!stream.destroyed && stream.push(content.replace(/\n/g, '<br/>'));
|
||||||
|
} catch (error) {
|
||||||
|
error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
try {
|
||||||
|
for await (const chunk of chatResponse.data as any) {
|
||||||
|
if (stream.destroyed) {
|
||||||
|
// 流被中断了,直接忽略后面的内容
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
const parser = createParser(onParse);
|
||||||
|
parser.feed(decoder.decode(chunk));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('pipe error', error);
|
||||||
|
}
|
||||||
|
// close stream
|
||||||
|
!stream.destroyed && stream.push(null);
|
||||||
|
stream.destroy();
|
||||||
|
|
||||||
|
const promptsContent = formatPrompts.map((item) => item.content).join('');
|
||||||
|
// 只有使用平台的 key 才计费
|
||||||
|
pushChatBill({
|
||||||
|
isPay: !userApiKey,
|
||||||
|
modelName: model.service.modelName,
|
||||||
|
userId,
|
||||||
|
chatId,
|
||||||
|
text: promptsContent + responseContent
|
||||||
|
});
|
||||||
|
} catch (err: any) {
|
||||||
|
if (step === 1) {
|
||||||
|
// 直接结束流
|
||||||
|
console.log('error,结束');
|
||||||
|
stream.destroy();
|
||||||
|
} else {
|
||||||
|
res.status(500);
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
res.write(`data: [DONE]\n\n`);
|
|
||||||
|
|
||||||
// 存入库
|
|
||||||
await ChatWindow.findByIdAndUpdate(windowId, {
|
|
||||||
$push: {
|
|
||||||
content: {
|
|
||||||
obj: 'AI',
|
|
||||||
value: AIResponse
|
|
||||||
}
|
|
||||||
},
|
|
||||||
updateTime: Date.now()
|
|
||||||
});
|
|
||||||
|
|
||||||
res.end();
|
|
||||||
} catch (err: any) {
|
|
||||||
console.log(err?.response?.data || err);
|
|
||||||
// 删除最一条数据库记录, 也就是预发送的那一条
|
|
||||||
await ChatWindow.findByIdAndUpdate(windowId, {
|
|
||||||
$pop: { content: 1 },
|
|
||||||
updateTime: Date.now()
|
|
||||||
});
|
|
||||||
|
|
||||||
res.end();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
src/pages/api/chat/delChatRecordByIndex.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, Chat } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { chatId, index } = req.query as { chatId: string; index: string };
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
if (!chatId || !index) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
// 凭证校验
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
const chatRecord = await Chat.findById(chatId);
|
||||||
|
|
||||||
|
if (!chatRecord) {
|
||||||
|
throw new Error('找不到对话');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重新计算 index,跳过已经被删除的内容
|
||||||
|
let unDeleteIndex = +index;
|
||||||
|
let deletedIndex = 0;
|
||||||
|
for (deletedIndex = 0; deletedIndex < chatRecord.content.length; deletedIndex++) {
|
||||||
|
if (!chatRecord.content[deletedIndex].deleted) {
|
||||||
|
unDeleteIndex--;
|
||||||
|
if (unDeleteIndex < 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除最一条数据库记录, 也就是预发送的那一条
|
||||||
|
await Chat.updateOne(
|
||||||
|
{
|
||||||
|
_id: chatId,
|
||||||
|
userId
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
[`content.${deletedIndex}.deleted`]: true,
|
||||||
|
updateTime: Date.now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
jsonRes(res);
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { jsonRes } from '@/service/response';
|
|
||||||
import { connectToDatabase, ChatWindow } from '@/service/mongo';
|
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
try {
|
|
||||||
const { windowId } = req.query as { windowId: string };
|
|
||||||
|
|
||||||
if (!windowId) {
|
|
||||||
throw new Error('缺少参数');
|
|
||||||
}
|
|
||||||
|
|
||||||
await connectToDatabase();
|
|
||||||
|
|
||||||
// 删除最一条数据库记录, 也就是预发送的那一条
|
|
||||||
await ChatWindow.findByIdAndUpdate(windowId, {
|
|
||||||
$pop: { content: 1 },
|
|
||||||
updateTime: Date.now()
|
|
||||||
});
|
|
||||||
|
|
||||||
jsonRes(res);
|
|
||||||
} catch (err) {
|
|
||||||
jsonRes(res, {
|
|
||||||
code: 500,
|
|
||||||
error: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,16 +2,19 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase, Model, Chat } from '@/service/mongo';
|
import { connectToDatabase, Model, Chat } from '@/service/mongo';
|
||||||
import { authToken } from '@/service/utils/tools';
|
import { authToken } from '@/service/utils/tools';
|
||||||
import { ModelType } from '@/types/model';
|
import type { ModelSchema } from '@/types/mongoSchema';
|
||||||
|
|
||||||
/* 获取我的模型 */
|
/* 获取我的模型 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
try {
|
try {
|
||||||
const { modelId } = req.query;
|
const { modelId, isShare = 'false' } = req.query as {
|
||||||
|
modelId: string;
|
||||||
|
isShare?: 'true' | 'false';
|
||||||
|
};
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
if (!authorization) {
|
if (!authorization) {
|
||||||
throw new Error('无权操作');
|
throw new Error('无权生成对话');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!modelId) {
|
if (!modelId) {
|
||||||
@@ -24,7 +27,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
// 获取模型配置
|
// 获取模型配置
|
||||||
const model: ModelType | null = await Model.findOne({
|
const model = await Model.findOne<ModelSchema>({
|
||||||
_id: modelId,
|
_id: modelId,
|
||||||
userId
|
userId
|
||||||
});
|
});
|
||||||
@@ -38,11 +41,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
userId,
|
userId,
|
||||||
modelId,
|
modelId,
|
||||||
expiredTime: Date.now() + model.security.expiredTime,
|
expiredTime: Date.now() + model.security.expiredTime,
|
||||||
loadAmount: model.security.maxLoadAmount
|
loadAmount: model.security.maxLoadAmount,
|
||||||
|
isShare: isShare === 'true',
|
||||||
|
content: []
|
||||||
});
|
});
|
||||||
|
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
data: response._id
|
data: response._id // 即聊天框的 ID
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
|
|||||||
@@ -1,83 +1,170 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { createParser, ParsedEvent, ReconnectInterval } from 'eventsource-parser';
|
||||||
import { connectToDatabase, Chat } from '@/service/mongo';
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
import type { ModelType } from '@/types/model';
|
import { getOpenAIApi, authChat } from '@/service/utils/chat';
|
||||||
import { getOpenAIApi } from '@/service/utils/chat';
|
import { httpsAgent } from '@/service/utils/tools';
|
||||||
import { ChatItemType } from '@/types/chat';
|
import { ChatItemType } from '@/types/chat';
|
||||||
import { openaiProxy } from '@/service/utils/tools';
|
import { jsonRes } from '@/service/response';
|
||||||
|
import type { ModelSchema } from '@/types/mongoSchema';
|
||||||
|
import { PassThrough } from 'stream';
|
||||||
|
import { modelList } from '@/constants/model';
|
||||||
|
import { pushChatBill } from '@/service/events/pushBill';
|
||||||
|
|
||||||
/* 发送提示词 */
|
/* 发送提示词 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
let step = 0; // step=1时,表示开始了流响应
|
||||||
const { prompt, chatId } = req.body as { prompt: ChatItemType[]; chatId: string };
|
const stream = new PassThrough();
|
||||||
|
stream.on('error', () => {
|
||||||
|
console.log('error: ', 'stream error');
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
res.on('close', () => {
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
res.on('error', () => {
|
||||||
|
console.log('error: ', 'request error');
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
if (!prompt || !chatId) {
|
try {
|
||||||
|
const { chatId, prompt } = req.body as {
|
||||||
|
prompt: ChatItemType;
|
||||||
|
chatId: string;
|
||||||
|
};
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
if (!chatId || !prompt) {
|
||||||
throw new Error('缺少参数');
|
throw new Error('缺少参数');
|
||||||
}
|
}
|
||||||
|
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
// 获取 chat 数据
|
const { chat, userApiKey, systemKey, userId } = await authChat(chatId, authorization);
|
||||||
const chat = await Chat.findById(chatId)
|
|
||||||
.populate({
|
|
||||||
path: 'modelId',
|
|
||||||
options: {
|
|
||||||
strictPopulate: false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.populate({
|
|
||||||
path: 'userId',
|
|
||||||
options: {
|
|
||||||
strictPopulate: false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!chat || !chat.modelId || !chat.userId) {
|
const model: ModelSchema = chat.modelId;
|
||||||
throw new Error('聊天已过期');
|
|
||||||
|
// 读取对话内容
|
||||||
|
const prompts = [...chat.content, prompt];
|
||||||
|
|
||||||
|
// 上下文长度过滤
|
||||||
|
const maxContext = model.security.contextMaxLen;
|
||||||
|
const filterPrompts =
|
||||||
|
prompts.length > maxContext ? prompts.slice(prompts.length - maxContext) : prompts;
|
||||||
|
|
||||||
|
// 格式化文本内容
|
||||||
|
const formatPrompts: string[] = filterPrompts.map((item: ChatItemType) => item.value);
|
||||||
|
// 如果有系统提示词,自动插入
|
||||||
|
if (model.systemPrompt) {
|
||||||
|
formatPrompts.unshift(`${model.systemPrompt}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const model: ModelType = chat.modelId;
|
const promptText = formatPrompts.join('</s>');
|
||||||
|
|
||||||
// 获取 user 的 apiKey
|
// 计算温度
|
||||||
const user = chat.userId;
|
const modelConstantsData = modelList.find((item) => item.model === model.service.modelName);
|
||||||
|
if (!modelConstantsData) {
|
||||||
const userApiKey = user.accounts?.find((item: any) => item.type === 'openai')?.value;
|
throw new Error('模型异常,请用 chatgpt 模型');
|
||||||
|
|
||||||
if (!userApiKey) {
|
|
||||||
throw new Error('缺少ApiKey, 无法请求');
|
|
||||||
}
|
}
|
||||||
|
const temperature = modelConstantsData.maxTemperature * (model.temperature / 10);
|
||||||
|
|
||||||
// 获取 chatAPI
|
// 获取 chatAPI
|
||||||
const chatAPI = getOpenAIApi(userApiKey);
|
const chatAPI = getOpenAIApi(userApiKey || systemKey);
|
||||||
|
let startTime = Date.now();
|
||||||
// prompt处理
|
// console.log({
|
||||||
const formatPrompt = prompt.map((item) => `${item.value}\n\n###\n\n`).join('');
|
// model: model.service.chatModel,
|
||||||
|
// temperature: temperature,
|
||||||
// 发送请求
|
// prompt: promptText,
|
||||||
const response = await chatAPI.createCompletion(
|
// stream: true,
|
||||||
|
// max_tokens:
|
||||||
|
// model.trainingTimes > 0 ? modelConstantsData.trainedMaxToken : modelConstantsData.maxToken,
|
||||||
|
// presence_penalty: -0.5, // 越大,越容易出现新内容
|
||||||
|
// frequency_penalty: 0.5, // 越大,重复内容越少
|
||||||
|
// stop: [`###`]
|
||||||
|
// });
|
||||||
|
// 发出请求
|
||||||
|
const chatResponse = await chatAPI.createCompletion(
|
||||||
{
|
{
|
||||||
model: model.service.modelName,
|
model: model.service.chatModel,
|
||||||
prompt: formatPrompt,
|
temperature: temperature,
|
||||||
temperature: 0.5,
|
prompt: promptText,
|
||||||
max_tokens: model.security.contentMaxLen,
|
stream: true,
|
||||||
top_p: 1,
|
max_tokens: modelConstantsData.maxToken,
|
||||||
frequency_penalty: 0,
|
presence_penalty: -0.5, // 越大,越容易出现新内容
|
||||||
presence_penalty: 0.6,
|
frequency_penalty: 0.5, // 越大,重复内容越少
|
||||||
stop: ['###']
|
stop: [`###`, '。!?.!.']
|
||||||
},
|
},
|
||||||
openaiProxy
|
{
|
||||||
|
timeout: 40000,
|
||||||
|
responseType: 'stream',
|
||||||
|
httpsAgent
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const responseMessage = response.data.choices[0]?.text;
|
console.log('api response time:', `${(Date.now() - startTime) / 1000}s`);
|
||||||
|
|
||||||
jsonRes(res, {
|
// 创建响应流
|
||||||
data: responseMessage
|
res.setHeader('Content-Type', 'text/event-stream;charset-utf-8');
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
|
res.setHeader('X-Accel-Buffering', 'no');
|
||||||
|
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||||
|
step = 1;
|
||||||
|
|
||||||
|
let responseContent = '';
|
||||||
|
stream.pipe(res);
|
||||||
|
|
||||||
|
const onParse = async (event: ParsedEvent | ReconnectInterval) => {
|
||||||
|
if (event.type !== 'event') return;
|
||||||
|
const data = event.data;
|
||||||
|
if (data === '[DONE]') return;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
const content: string = json?.choices?.[0].text || '';
|
||||||
|
// console.log('content:', content);
|
||||||
|
if (!content || (responseContent === '' && content === '\n')) return;
|
||||||
|
|
||||||
|
responseContent += content;
|
||||||
|
!stream.destroyed && stream.push(content.replace(/\n/g, '<br/>'));
|
||||||
|
} catch (error) {
|
||||||
|
error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
try {
|
||||||
|
for await (const chunk of chatResponse.data as any) {
|
||||||
|
if (stream.destroyed) {
|
||||||
|
// 流被中断了,直接忽略后面的内容
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const parser = createParser(onParse);
|
||||||
|
parser.feed(decoder.decode(chunk));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('pipe error', error);
|
||||||
|
}
|
||||||
|
// close stream
|
||||||
|
!stream.destroyed && stream.push(null);
|
||||||
|
stream.destroy();
|
||||||
|
|
||||||
|
// 只有使用平台的 key 才计费
|
||||||
|
pushChatBill({
|
||||||
|
isPay: !userApiKey,
|
||||||
|
modelName: model.service.modelName,
|
||||||
|
userId,
|
||||||
|
chatId,
|
||||||
|
text: promptText + responseContent
|
||||||
});
|
});
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
jsonRes(res, {
|
// console.log(err?.response);
|
||||||
code: 500,
|
if (step === 1) {
|
||||||
error: err
|
// 直接结束流
|
||||||
});
|
console.log('error,结束');
|
||||||
|
stream.destroy();
|
||||||
|
} else {
|
||||||
|
res.status(500);
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase, Chat, ChatWindow } from '@/service/mongo';
|
import { connectToDatabase, Chat } from '@/service/mongo';
|
||||||
import type { ModelType } from '@/types/model';
|
import type { ChatPopulate } from '@/types/mongoSchema';
|
||||||
|
import type { InitChatResponse } from '@/api/response/chat';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
|
||||||
/* 获取我的模型 */
|
/* 初始化我的聊天框,需要身份验证 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
try {
|
||||||
const { chatId, windowId } = req.query as { chatId: string; windowId?: string };
|
const { authorization } = req.headers;
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
const { chatId } = req.query as { chatId: string };
|
||||||
|
|
||||||
if (!chatId) {
|
if (!chatId) {
|
||||||
throw new Error('缺少参数');
|
throw new Error('缺少参数');
|
||||||
@@ -15,74 +20,37 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
// 获取 chat 数据
|
// 获取 chat 数据
|
||||||
const chat = await Chat.findById(chatId).populate({
|
const chat = await Chat.findOne<ChatPopulate>({
|
||||||
|
_id: chatId,
|
||||||
|
userId
|
||||||
|
}).populate({
|
||||||
path: 'modelId',
|
path: 'modelId',
|
||||||
options: {
|
options: {
|
||||||
strictPopulate: false
|
strictPopulate: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 安全校验
|
if (!chat) {
|
||||||
if (chat.loadAmount === 0 || chat.expiredTime < Date.now()) {
|
throw new Error('聊天框不存在');
|
||||||
throw new Error('聊天框已过期');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chat.loadAmount > 0) {
|
// filter 掉被 deleted 的内容
|
||||||
await Chat.updateOne(
|
chat.content = chat.content.filter((item) => item.deleted !== true);
|
||||||
{
|
|
||||||
_id: chat._id
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$inc: { loadAmount: -1 }
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const model: ModelType = chat.modelId;
|
const model = chat.modelId;
|
||||||
|
jsonRes<InitChatResponse>(res, {
|
||||||
/* 查找是否有记录 */
|
|
||||||
let history = null;
|
|
||||||
let responseId = windowId;
|
|
||||||
try {
|
|
||||||
history = await ChatWindow.findById(windowId);
|
|
||||||
} catch (error) {
|
|
||||||
error;
|
|
||||||
}
|
|
||||||
|
|
||||||
const defaultContent = model.systemPrompt
|
|
||||||
? [
|
|
||||||
{
|
|
||||||
obj: 'SYSTEM',
|
|
||||||
value: model.systemPrompt
|
|
||||||
}
|
|
||||||
]
|
|
||||||
: [];
|
|
||||||
|
|
||||||
if (!history) {
|
|
||||||
// 没有记录,创建一个
|
|
||||||
const response = await ChatWindow.create({
|
|
||||||
chatId,
|
|
||||||
updateTime: Date.now(),
|
|
||||||
content: defaultContent
|
|
||||||
});
|
|
||||||
responseId = response._id;
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonRes(res, {
|
|
||||||
data: {
|
data: {
|
||||||
windowId: responseId,
|
chatId: chat._id,
|
||||||
chatSite: {
|
modelId: model._id,
|
||||||
modelId: model._id,
|
name: model.name,
|
||||||
name: model.name,
|
avatar: model.avatar,
|
||||||
avatar: model.avatar,
|
intro: model.intro,
|
||||||
secret: model.security,
|
modelName: model.service.modelName,
|
||||||
chatModel: model.service.chatModel
|
chatModel: model.service.chatModel,
|
||||||
},
|
history: chat.content
|
||||||
history: history ? history.content : defaultContent
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
code: 500,
|
code: 500,
|
||||||
error: err
|
error: err
|
||||||
|
|||||||
277
src/pages/api/chat/lafGpt.ts
Normal file
@@ -0,0 +1,277 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { createParser, ParsedEvent, ReconnectInterval } from 'eventsource-parser';
|
||||||
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
|
import { getOpenAIApi, authChat } from '@/service/utils/chat';
|
||||||
|
import { httpsAgent, openaiChatFilter, systemPromptFilter } from '@/service/utils/tools';
|
||||||
|
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
|
||||||
|
import { ChatItemType } from '@/types/chat';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import type { ModelSchema } from '@/types/mongoSchema';
|
||||||
|
import { PassThrough } from 'stream';
|
||||||
|
import { modelList } from '@/constants/model';
|
||||||
|
import { pushChatBill } from '@/service/events/pushBill';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
import { VecModelDataPrefix } from '@/constants/redis';
|
||||||
|
import { vectorToBuffer } from '@/utils/tools';
|
||||||
|
import { openaiCreateEmbedding } from '@/service/utils/openai';
|
||||||
|
|
||||||
|
/* 发送提示词 */
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
let step = 0; // step=1时,表示开始了流响应
|
||||||
|
const stream = new PassThrough();
|
||||||
|
stream.on('error', () => {
|
||||||
|
console.log('error: ', 'stream error');
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
res.on('close', () => {
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
res.on('error', () => {
|
||||||
|
console.log('error: ', 'request error');
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { chatId, prompt } = req.body as {
|
||||||
|
prompt: ChatItemType;
|
||||||
|
chatId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
if (!chatId || !prompt) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
const redis = await connectRedis();
|
||||||
|
let startTime = Date.now();
|
||||||
|
|
||||||
|
const { chat, userApiKey, systemKey, userId } = await authChat(chatId, authorization);
|
||||||
|
|
||||||
|
const model: ModelSchema = chat.modelId;
|
||||||
|
const modelConstantsData = modelList.find((item) => item.model === model.service.modelName);
|
||||||
|
if (!modelConstantsData) {
|
||||||
|
throw new Error('模型加载异常');
|
||||||
|
}
|
||||||
|
// 获取 chatAPI
|
||||||
|
const chatAPI = getOpenAIApi(userApiKey || systemKey);
|
||||||
|
|
||||||
|
// 请求一次 chatgpt 拆解需求
|
||||||
|
const promptResponse = await chatAPI.createChatCompletion(
|
||||||
|
{
|
||||||
|
model: model.service.chatModel,
|
||||||
|
temperature: 0,
|
||||||
|
// max_tokens: modelConstantsData.maxToken,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'system',
|
||||||
|
content: `服务端逻辑生成器。根据用户输入的需求,拆解成代码实现的步骤,并按格式返回: 1.\n2.\n3.\n ......
|
||||||
|
|
||||||
|
下面是一些例子:
|
||||||
|
实现一个手机号注册账号的方法,包含两个函数
|
||||||
|
* 发送手机验证码函数:
|
||||||
|
1. 从 query 中获取 phone
|
||||||
|
2. 校验手机号格式是否正确,不正确返回{error: "手机号格式错误"}
|
||||||
|
3. 给 phone 发送一个短信验证码,验证码长度为6位字符串,内容为:你正在注册laf, 验证码为:code
|
||||||
|
4. 数据库添加数据,表为"codes",内容为 {phone, code}
|
||||||
|
* 注册函数
|
||||||
|
1. 从 body 中获取 phone 和 code
|
||||||
|
2. 校验手机号格式是否正确,不正确返回{error: "手机号格式错误"}
|
||||||
|
2. 获取数据库数据,表为"codes",查找是否有符合 phone, code 等于body参数的记录,没有的话返回 {error:"验证码不正确"}
|
||||||
|
4. 添加数据库数据,表为"users" ,内容为{phone, code, createTime}
|
||||||
|
5. 删除数据库数据,删除 code 记录
|
||||||
|
---------------
|
||||||
|
更新博客记录。传入blogId,blogText,tags,还需要记录更新的时间
|
||||||
|
1. 从 body 中获取 blogId,blogText 和 tags
|
||||||
|
2. 校验 blogId 是否为空,为空则返回 {error: "博客ID不能为空"}
|
||||||
|
3. 校验 blogText 是否为空,为空则返回 {error: "博客内容不能为空"}
|
||||||
|
4. 校验 tags 是否为数组,不是则返回 {error: "标签必须为数组"}
|
||||||
|
5. 获取当前时间,记录为 updateTime
|
||||||
|
6. 更新数据库数据,表为"blogs",更新符合 blogId 的记录的内容为{blogText, tags, updateTime}
|
||||||
|
7. 返回结果 {message: "更新博客记录成功"}`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: prompt.value
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeout: 40000,
|
||||||
|
httpsAgent
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const promptResolve = promptResponse.data.choices?.[0]?.message?.content || '';
|
||||||
|
if (!promptResolve) {
|
||||||
|
throw new Error('gpt 异常');
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt.value += `\n${promptResolve}`;
|
||||||
|
console.log('prompt resolve success, time:', `${(Date.now() - startTime) / 1000}s`);
|
||||||
|
|
||||||
|
// 获取提示词的向量
|
||||||
|
const { vector: promptVector } = await openaiCreateEmbedding({
|
||||||
|
isPay: !userApiKey,
|
||||||
|
apiKey: userApiKey || systemKey,
|
||||||
|
userId,
|
||||||
|
text: prompt.value
|
||||||
|
});
|
||||||
|
|
||||||
|
// 读取对话内容
|
||||||
|
const prompts = [...chat.content, prompt];
|
||||||
|
|
||||||
|
// 搜索系统提示词, 按相似度从 redis 中搜出相关的 q 和 text
|
||||||
|
const redisData: any[] = await redis.sendCommand([
|
||||||
|
'FT.SEARCH',
|
||||||
|
`idx:${VecModelDataPrefix}:hash`,
|
||||||
|
`@modelId:{${String(
|
||||||
|
chat.modelId._id
|
||||||
|
)}} @vector:[VECTOR_RANGE 0.25 $blob]=>{$YIELD_DISTANCE_AS: score}`,
|
||||||
|
// `@modelId:{${String(chat.modelId._id)}}=>[KNN 10 @vector $blob AS score]`,
|
||||||
|
'RETURN',
|
||||||
|
'1',
|
||||||
|
'text',
|
||||||
|
'SORTBY',
|
||||||
|
'score',
|
||||||
|
'PARAMS',
|
||||||
|
'2',
|
||||||
|
'blob',
|
||||||
|
vectorToBuffer(promptVector),
|
||||||
|
'LIMIT',
|
||||||
|
'0',
|
||||||
|
'20',
|
||||||
|
'DIALECT',
|
||||||
|
'2'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 格式化响应值,获取 qa
|
||||||
|
const formatRedisPrompt = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
|
||||||
|
.map((i) => {
|
||||||
|
if (!redisData[i]) return '';
|
||||||
|
const text = (redisData[i][1] as string) || '';
|
||||||
|
|
||||||
|
if (!text) return '';
|
||||||
|
|
||||||
|
return text;
|
||||||
|
})
|
||||||
|
.filter((item) => item);
|
||||||
|
|
||||||
|
if (formatRedisPrompt.length === 0) {
|
||||||
|
throw new Error('对不起,我没有找到你的问题');
|
||||||
|
}
|
||||||
|
|
||||||
|
// textArr 筛选,最多 3000 tokens
|
||||||
|
const systemPrompt = systemPromptFilter(formatRedisPrompt, 3400);
|
||||||
|
|
||||||
|
prompts.unshift({
|
||||||
|
obj: 'SYSTEM',
|
||||||
|
value: `${model.systemPrompt} 知识库内容是最新的,知识库内容为: "${systemPrompt}"`
|
||||||
|
});
|
||||||
|
|
||||||
|
// 控制在 tokens 数量,防止超出
|
||||||
|
const filterPrompts = openaiChatFilter(prompts, modelConstantsData.contextMaxToken);
|
||||||
|
|
||||||
|
// 格式化文本内容成 chatgpt 格式
|
||||||
|
const map = {
|
||||||
|
Human: ChatCompletionRequestMessageRoleEnum.User,
|
||||||
|
AI: ChatCompletionRequestMessageRoleEnum.Assistant,
|
||||||
|
SYSTEM: ChatCompletionRequestMessageRoleEnum.System
|
||||||
|
};
|
||||||
|
const formatPrompts: ChatCompletionRequestMessage[] = filterPrompts.map(
|
||||||
|
(item: ChatItemType) => ({
|
||||||
|
role: map[item.obj],
|
||||||
|
content: item.value
|
||||||
|
})
|
||||||
|
);
|
||||||
|
console.log(formatPrompts);
|
||||||
|
// 计算温度
|
||||||
|
const temperature = modelConstantsData.maxTemperature * (model.temperature / 10);
|
||||||
|
|
||||||
|
// 发出请求
|
||||||
|
const chatResponse = await chatAPI.createChatCompletion(
|
||||||
|
{
|
||||||
|
model: model.service.chatModel,
|
||||||
|
temperature: temperature,
|
||||||
|
// max_tokens: modelConstantsData.maxToken,
|
||||||
|
messages: formatPrompts,
|
||||||
|
frequency_penalty: 0.5, // 越大,重复内容越少
|
||||||
|
presence_penalty: -0.5, // 越大,越容易出现新内容
|
||||||
|
stream: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeout: 40000,
|
||||||
|
responseType: 'stream',
|
||||||
|
httpsAgent
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('api response time:', `${(Date.now() - startTime) / 1000}s`);
|
||||||
|
|
||||||
|
// 创建响应流
|
||||||
|
res.setHeader('Content-Type', 'text/event-stream;charset-utf-8');
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
|
res.setHeader('X-Accel-Buffering', 'no');
|
||||||
|
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||||
|
step = 1;
|
||||||
|
|
||||||
|
let responseContent = '';
|
||||||
|
stream.pipe(res);
|
||||||
|
|
||||||
|
const onParse = async (event: ParsedEvent | ReconnectInterval) => {
|
||||||
|
if (event.type !== 'event') return;
|
||||||
|
const data = event.data;
|
||||||
|
if (data === '[DONE]') return;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
const content: string = json?.choices?.[0].delta.content || '';
|
||||||
|
if (!content || (responseContent === '' && content === '\n')) return;
|
||||||
|
|
||||||
|
responseContent += content;
|
||||||
|
// console.log('content:', content)
|
||||||
|
!stream.destroyed && stream.push(content.replace(/\n/g, '<br/>'));
|
||||||
|
} catch (error) {
|
||||||
|
error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
try {
|
||||||
|
for await (const chunk of chatResponse.data as any) {
|
||||||
|
if (stream.destroyed) {
|
||||||
|
// 流被中断了,直接忽略后面的内容
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const parser = createParser(onParse);
|
||||||
|
parser.feed(decoder.decode(chunk));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('pipe error', error);
|
||||||
|
}
|
||||||
|
// close stream
|
||||||
|
!stream.destroyed && stream.push(null);
|
||||||
|
stream.destroy();
|
||||||
|
|
||||||
|
const promptsContent = formatPrompts.map((item) => item.content).join('');
|
||||||
|
// 只有使用平台的 key 才计费
|
||||||
|
pushChatBill({
|
||||||
|
isPay: !userApiKey,
|
||||||
|
modelName: model.service.modelName,
|
||||||
|
userId,
|
||||||
|
chatId,
|
||||||
|
text: promptsContent + responseContent
|
||||||
|
});
|
||||||
|
} catch (err: any) {
|
||||||
|
if (step === 1) {
|
||||||
|
// 直接结束流
|
||||||
|
console.log('error,结束');
|
||||||
|
stream.destroy();
|
||||||
|
} else {
|
||||||
|
res.status(500);
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { jsonRes } from '@/service/response';
|
|
||||||
import { ChatItemType } from '@/types/chat';
|
|
||||||
import { connectToDatabase, ChatWindow } from '@/service/mongo';
|
|
||||||
import type { ModelType } from '@/types/model';
|
|
||||||
import { authChat } from '@/service/utils/chat';
|
|
||||||
|
|
||||||
/* 聊天预请求,存储聊天内容 */
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
try {
|
|
||||||
const { windowId, prompt, chatId } = req.body as {
|
|
||||||
windowId: string;
|
|
||||||
prompt: ChatItemType;
|
|
||||||
chatId: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!windowId || !prompt || !chatId) {
|
|
||||||
throw new Error('缺少参数');
|
|
||||||
}
|
|
||||||
|
|
||||||
await connectToDatabase();
|
|
||||||
|
|
||||||
const { chat } = await authChat(chatId);
|
|
||||||
|
|
||||||
// 长度校验
|
|
||||||
const model: ModelType = chat.modelId;
|
|
||||||
if (prompt.value.length > model.security.contentMaxLen) {
|
|
||||||
throw new Error('输入内容超长');
|
|
||||||
}
|
|
||||||
|
|
||||||
await ChatWindow.findByIdAndUpdate(windowId, {
|
|
||||||
$push: { content: prompt },
|
|
||||||
updateTime: Date.now()
|
|
||||||
});
|
|
||||||
|
|
||||||
jsonRes(res);
|
|
||||||
} catch (err) {
|
|
||||||
jsonRes(res, {
|
|
||||||
code: 500,
|
|
||||||
error: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
40
src/pages/api/chat/saveChat.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { ChatItemType } from '@/types/chat';
|
||||||
|
import { connectToDatabase, Chat } from '@/service/mongo';
|
||||||
|
|
||||||
|
/* 聊天内容存存储 */
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { chatId, prompts } = req.body as {
|
||||||
|
chatId: string;
|
||||||
|
prompts: ChatItemType[];
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!chatId || !prompts) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
// 存入库
|
||||||
|
await Chat.findByIdAndUpdate(chatId, {
|
||||||
|
$push: {
|
||||||
|
content: {
|
||||||
|
$each: prompts.map((item) => ({
|
||||||
|
obj: item.obj,
|
||||||
|
value: item.value
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
updateTime: new Date()
|
||||||
|
});
|
||||||
|
|
||||||
|
jsonRes(res);
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
222
src/pages/api/chat/vectorGpt.ts
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { createParser, ParsedEvent, ReconnectInterval } from 'eventsource-parser';
|
||||||
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
|
import { getOpenAIApi, authChat } from '@/service/utils/chat';
|
||||||
|
import { httpsAgent, openaiChatFilter, systemPromptFilter } from '@/service/utils/tools';
|
||||||
|
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
|
||||||
|
import { ChatItemType } from '@/types/chat';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import type { ModelSchema } from '@/types/mongoSchema';
|
||||||
|
import { PassThrough } from 'stream';
|
||||||
|
import { modelList } from '@/constants/model';
|
||||||
|
import { pushChatBill } from '@/service/events/pushBill';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
import { VecModelDataPrefix } from '@/constants/redis';
|
||||||
|
import { vectorToBuffer } from '@/utils/tools';
|
||||||
|
import { openaiCreateEmbedding } from '@/service/utils/openai';
|
||||||
|
|
||||||
|
/* 发送提示词 */
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
let step = 0; // step=1时,表示开始了流响应
|
||||||
|
const stream = new PassThrough();
|
||||||
|
stream.on('error', () => {
|
||||||
|
console.log('error: ', 'stream error');
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
res.on('close', () => {
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
res.on('error', () => {
|
||||||
|
console.log('error: ', 'request error');
|
||||||
|
stream.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { chatId, prompt } = req.body as {
|
||||||
|
prompt: ChatItemType;
|
||||||
|
chatId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
if (!chatId || !prompt) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
const redis = await connectRedis();
|
||||||
|
let startTime = Date.now();
|
||||||
|
|
||||||
|
const { chat, userApiKey, systemKey, userId } = await authChat(chatId, authorization);
|
||||||
|
|
||||||
|
const model: ModelSchema = chat.modelId;
|
||||||
|
const modelConstantsData = modelList.find((item) => item.model === model.service.modelName);
|
||||||
|
if (!modelConstantsData) {
|
||||||
|
throw new Error('模型加载异常');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取对话内容
|
||||||
|
const prompts = [...chat.content, prompt];
|
||||||
|
|
||||||
|
// 获取提示词的向量
|
||||||
|
const { vector: promptVector, chatAPI } = await openaiCreateEmbedding({
|
||||||
|
isPay: !userApiKey,
|
||||||
|
apiKey: userApiKey || systemKey,
|
||||||
|
userId,
|
||||||
|
text: prompt.value
|
||||||
|
});
|
||||||
|
|
||||||
|
// 搜索系统提示词, 按相似度从 redis 中搜出相关的 q 和 text
|
||||||
|
const redisData: any[] = await redis.sendCommand([
|
||||||
|
'FT.SEARCH',
|
||||||
|
`idx:${VecModelDataPrefix}:hash`,
|
||||||
|
`@modelId:{${String(
|
||||||
|
chat.modelId._id
|
||||||
|
)}} @vector:[VECTOR_RANGE 0.22 $blob]=>{$YIELD_DISTANCE_AS: score}`,
|
||||||
|
// `@modelId:{${String(chat.modelId._id)}}=>[KNN 10 @vector $blob AS score]`,
|
||||||
|
'RETURN',
|
||||||
|
'1',
|
||||||
|
'text',
|
||||||
|
'SORTBY',
|
||||||
|
'score',
|
||||||
|
'PARAMS',
|
||||||
|
'2',
|
||||||
|
'blob',
|
||||||
|
vectorToBuffer(promptVector),
|
||||||
|
'LIMIT',
|
||||||
|
'0',
|
||||||
|
'20',
|
||||||
|
'DIALECT',
|
||||||
|
'2'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 格式化响应值,获取 qa
|
||||||
|
const formatRedisPrompt = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
|
||||||
|
.map((i) => {
|
||||||
|
if (!redisData[i]) return '';
|
||||||
|
const text = (redisData[i][1] as string) || '';
|
||||||
|
|
||||||
|
if (!text) return '';
|
||||||
|
|
||||||
|
return text;
|
||||||
|
})
|
||||||
|
.filter((item) => item);
|
||||||
|
|
||||||
|
if (formatRedisPrompt.length === 0) {
|
||||||
|
throw new Error('对不起,我没有找到你的问题');
|
||||||
|
}
|
||||||
|
|
||||||
|
// textArr 筛选,最多 3000 tokens
|
||||||
|
const systemPrompt = systemPromptFilter(formatRedisPrompt, 3400);
|
||||||
|
|
||||||
|
prompts.unshift({
|
||||||
|
obj: 'SYSTEM',
|
||||||
|
value: `${model.systemPrompt} 知识库内容是最新的,知识库内容为: "${systemPrompt}"`
|
||||||
|
});
|
||||||
|
|
||||||
|
// 控制在 tokens 数量,防止超出
|
||||||
|
const filterPrompts = openaiChatFilter(prompts, modelConstantsData.contextMaxToken);
|
||||||
|
|
||||||
|
// 格式化文本内容成 chatgpt 格式
|
||||||
|
const map = {
|
||||||
|
Human: ChatCompletionRequestMessageRoleEnum.User,
|
||||||
|
AI: ChatCompletionRequestMessageRoleEnum.Assistant,
|
||||||
|
SYSTEM: ChatCompletionRequestMessageRoleEnum.System
|
||||||
|
};
|
||||||
|
const formatPrompts: ChatCompletionRequestMessage[] = filterPrompts.map(
|
||||||
|
(item: ChatItemType) => ({
|
||||||
|
role: map[item.obj],
|
||||||
|
content: item.value
|
||||||
|
})
|
||||||
|
);
|
||||||
|
// console.log(formatPrompts);
|
||||||
|
// 计算温度
|
||||||
|
const temperature = modelConstantsData.maxTemperature * (model.temperature / 10);
|
||||||
|
|
||||||
|
// 发出请求
|
||||||
|
const chatResponse = await chatAPI.createChatCompletion(
|
||||||
|
{
|
||||||
|
model: model.service.chatModel,
|
||||||
|
temperature: temperature,
|
||||||
|
// max_tokens: modelConstantsData.maxToken,
|
||||||
|
messages: formatPrompts,
|
||||||
|
frequency_penalty: 0.5, // 越大,重复内容越少
|
||||||
|
presence_penalty: -0.5, // 越大,越容易出现新内容
|
||||||
|
stream: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeout: 40000,
|
||||||
|
responseType: 'stream',
|
||||||
|
httpsAgent
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('api response time:', `${(Date.now() - startTime) / 1000}s`);
|
||||||
|
|
||||||
|
// 创建响应流
|
||||||
|
res.setHeader('Content-Type', 'text/event-stream;charset-utf-8');
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
|
res.setHeader('X-Accel-Buffering', 'no');
|
||||||
|
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||||
|
step = 1;
|
||||||
|
|
||||||
|
let responseContent = '';
|
||||||
|
stream.pipe(res);
|
||||||
|
|
||||||
|
const onParse = async (event: ParsedEvent | ReconnectInterval) => {
|
||||||
|
if (event.type !== 'event') return;
|
||||||
|
const data = event.data;
|
||||||
|
if (data === '[DONE]') return;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
const content: string = json?.choices?.[0].delta.content || '';
|
||||||
|
if (!content || (responseContent === '' && content === '\n')) return;
|
||||||
|
|
||||||
|
responseContent += content;
|
||||||
|
// console.log('content:', content)
|
||||||
|
!stream.destroyed && stream.push(content.replace(/\n/g, '<br/>'));
|
||||||
|
} catch (error) {
|
||||||
|
error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
try {
|
||||||
|
for await (const chunk of chatResponse.data as any) {
|
||||||
|
if (stream.destroyed) {
|
||||||
|
// 流被中断了,直接忽略后面的内容
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const parser = createParser(onParse);
|
||||||
|
parser.feed(decoder.decode(chunk));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('pipe error', error);
|
||||||
|
}
|
||||||
|
// close stream
|
||||||
|
!stream.destroyed && stream.push(null);
|
||||||
|
stream.destroy();
|
||||||
|
|
||||||
|
const promptsContent = formatPrompts.map((item) => item.content).join('');
|
||||||
|
// 只有使用平台的 key 才计费
|
||||||
|
pushChatBill({
|
||||||
|
isPay: !userApiKey,
|
||||||
|
modelName: model.service.modelName,
|
||||||
|
userId,
|
||||||
|
chatId,
|
||||||
|
text: promptsContent + responseContent
|
||||||
|
});
|
||||||
|
// jsonRes(res);
|
||||||
|
} catch (err: any) {
|
||||||
|
if (step === 1) {
|
||||||
|
// 直接结束流
|
||||||
|
console.log('error,结束');
|
||||||
|
stream.destroy();
|
||||||
|
} else {
|
||||||
|
res.status(500);
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/pages/api/data/delData.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, Data, DataItem } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import type { DataListItem } from '@/types/data';
|
||||||
|
import type { PagingData } from '@/types';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('缺少登录凭证');
|
||||||
|
}
|
||||||
|
|
||||||
|
await authToken(authorization);
|
||||||
|
|
||||||
|
const { dataId } = req.query as { dataId: string };
|
||||||
|
if (!dataId) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
await Data.findByIdAndUpdate(dataId, {
|
||||||
|
isDeleted: true
|
||||||
|
});
|
||||||
|
|
||||||
|
// 改变 dataItem 状态为 0
|
||||||
|
await DataItem.updateMany(
|
||||||
|
{
|
||||||
|
dataId
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
jsonRes<PagingData<DataListItem>>(res);
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/pages/api/data/getDataItems.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, DataItem } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
let {
|
||||||
|
dataId,
|
||||||
|
pageNum = 1,
|
||||||
|
pageSize = 10
|
||||||
|
} = req.query as { dataId: string; pageNum: string; pageSize: string };
|
||||||
|
pageNum = +pageNum;
|
||||||
|
pageSize = +pageSize;
|
||||||
|
|
||||||
|
if (!dataId) {
|
||||||
|
throw new Error('参数错误');
|
||||||
|
}
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
await authToken(authorization);
|
||||||
|
|
||||||
|
const dataItems = await DataItem.find({
|
||||||
|
dataId
|
||||||
|
})
|
||||||
|
.sort({ _id: -1 }) // 按照创建时间倒序排列
|
||||||
|
.skip((pageNum - 1) * pageSize)
|
||||||
|
.limit(pageSize);
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: {
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
data: dataItems,
|
||||||
|
total: await DataItem.countDocuments({
|
||||||
|
dataId
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
71
src/pages/api/data/getDataList.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, Data, DataItem } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import type { DataListItem } from '@/types/data';
|
||||||
|
import mongoose from 'mongoose';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('缺少登录凭证');
|
||||||
|
}
|
||||||
|
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
const datalist = await Data.aggregate<DataListItem>([
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
userId: new mongoose.Types.ObjectId(userId),
|
||||||
|
isDeleted: false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$sort: { createTime: -1 } // 按照创建时间倒序排列
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: 'dataitems',
|
||||||
|
localField: '_id',
|
||||||
|
foreignField: 'dataId',
|
||||||
|
as: 'items'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$addFields: {
|
||||||
|
totalData: {
|
||||||
|
$size: '$items' // 统计dataItem的总数
|
||||||
|
},
|
||||||
|
trainingData: {
|
||||||
|
$size: {
|
||||||
|
$filter: {
|
||||||
|
input: '$items',
|
||||||
|
as: 'item',
|
||||||
|
cond: { $ne: ['$$item.status', 0] } // 统计 status 不为0的数量
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$project: {
|
||||||
|
items: 0 // 不返回 items 字段
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: datalist
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
35
src/pages/api/data/postData.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, Data } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import type { DataType } from '@/types/data';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
let { name, type } = req.body as { name: string; type: DataType };
|
||||||
|
if (!name || !type) {
|
||||||
|
throw new Error('参数错误');
|
||||||
|
}
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
// 生成 data 集合
|
||||||
|
const data = await Data.create({
|
||||||
|
userId,
|
||||||
|
name,
|
||||||
|
type
|
||||||
|
});
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: data._id
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
37
src/pages/api/data/putDataName.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, Data } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import type { DataListItem } from '@/types/data';
|
||||||
|
import type { PagingData } from '@/types';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('缺少登录凭证');
|
||||||
|
}
|
||||||
|
|
||||||
|
await authToken(authorization);
|
||||||
|
|
||||||
|
const { dataId, name } = req.query as { dataId: string; name: string };
|
||||||
|
if (!dataId || !name) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
await Data.findByIdAndUpdate(dataId, {
|
||||||
|
name
|
||||||
|
});
|
||||||
|
|
||||||
|
jsonRes<PagingData<DataListItem>>(res);
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
69
src/pages/api/data/splitData.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, DataItem, Data } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { generateQA } from '@/service/events/generateQA';
|
||||||
|
import { generateAbstract } from '@/service/events/generateAbstract';
|
||||||
|
import { encode } from 'gpt-token-utils';
|
||||||
|
|
||||||
|
/* 拆分数据成QA */
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { text, dataId } = req.body as { text: string; dataId: string };
|
||||||
|
if (!text || !dataId) {
|
||||||
|
throw new Error('参数错误');
|
||||||
|
}
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
const DataRecord = await Data.findById(dataId);
|
||||||
|
|
||||||
|
if (!DataRecord) {
|
||||||
|
throw new Error('找不到数据集');
|
||||||
|
}
|
||||||
|
const replaceText = text.replace(/[\\n]+/g, ' ');
|
||||||
|
|
||||||
|
// 文本拆分成 chunk
|
||||||
|
let chunks = replaceText.match(/[^!?.。]+[!?.。]/g) || [];
|
||||||
|
|
||||||
|
const dataItems: any[] = [];
|
||||||
|
let splitText = '';
|
||||||
|
|
||||||
|
chunks.forEach((chunk) => {
|
||||||
|
splitText += chunk;
|
||||||
|
const tokens = encode(splitText).length;
|
||||||
|
if (tokens >= 780) {
|
||||||
|
dataItems.push({
|
||||||
|
userId,
|
||||||
|
dataId,
|
||||||
|
type: DataRecord.type,
|
||||||
|
text: splitText,
|
||||||
|
status: 1
|
||||||
|
});
|
||||||
|
splitText = '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 批量插入数据
|
||||||
|
await DataItem.insertMany(dataItems);
|
||||||
|
|
||||||
|
try {
|
||||||
|
generateQA();
|
||||||
|
generateAbstract();
|
||||||
|
} catch (error) {
|
||||||
|
error;
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: { chunks, replaceText }
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,48 +3,42 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase } from '@/service/mongo';
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
import { authToken } from '@/service/utils/tools';
|
import { authToken } from '@/service/utils/tools';
|
||||||
import { ModelStatusEnum, OpenAiList } from '@/constants/model';
|
import { ModelStatusEnum, modelList, ChatModelNameEnum, ChatModelNameMap } from '@/constants/model';
|
||||||
import { Model } from '@/service/models/model';
|
import { Model } from '@/service/models/model';
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
try {
|
try {
|
||||||
const { name, serviceModelName, serviceModelCompany = 'openai' } = req.body;
|
const { name, serviceModelName } = req.body as {
|
||||||
|
name: string;
|
||||||
|
serviceModelName: `${ChatModelNameEnum}`;
|
||||||
|
};
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
if (!authorization) {
|
if (!authorization) {
|
||||||
throw new Error('无权操作');
|
throw new Error('无权操作');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!name || !serviceModelName || !serviceModelCompany) {
|
if (!name || !serviceModelName) {
|
||||||
throw new Error('缺少参数');
|
throw new Error('缺少参数');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 凭证校验
|
// 凭证校验
|
||||||
const userId = await authToken(authorization);
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
const modelItem = OpenAiList.find((item) => item.model === serviceModelName);
|
const modelItem = modelList.find((item) => item.model === serviceModelName);
|
||||||
|
|
||||||
if (!modelItem) {
|
if (!modelItem) {
|
||||||
throw new Error('模型错误');
|
throw new Error('模型不存在');
|
||||||
}
|
}
|
||||||
|
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
// 重名校验
|
|
||||||
const authRepeatName = await Model.findOne({
|
|
||||||
name,
|
|
||||||
userId
|
|
||||||
});
|
|
||||||
if (authRepeatName) {
|
|
||||||
throw new Error('模型名重复');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 上限校验
|
// 上限校验
|
||||||
const authCount = await Model.countDocuments({
|
const authCount = await Model.countDocuments({
|
||||||
userId
|
userId
|
||||||
});
|
});
|
||||||
if (authCount >= 5) {
|
if (authCount >= 20) {
|
||||||
throw new Error('上限5个模型');
|
throw new Error('上限 20 个模型');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建模型
|
// 创建模型
|
||||||
@@ -53,10 +47,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
userId,
|
userId,
|
||||||
status: ModelStatusEnum.running,
|
status: ModelStatusEnum.running,
|
||||||
service: {
|
service: {
|
||||||
company: serviceModelCompany,
|
company: modelItem.serviceCompany,
|
||||||
trainId: modelItem.trainName,
|
trainId: '',
|
||||||
chatModel: modelItem.model,
|
chatModel: ChatModelNameMap[modelItem.model], // 聊天时用的模型
|
||||||
modelName: modelItem.model
|
modelName: modelItem.model // 最底层的模型,不会变,用于计费等核心操作
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
41
src/pages/api/model/data/delModelDataById.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
|
try {
|
||||||
|
let { dataId } = req.query as {
|
||||||
|
dataId: string;
|
||||||
|
};
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dataId) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 凭证校验
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
const redis = await connectRedis();
|
||||||
|
|
||||||
|
// 校验是否为该用户的数据
|
||||||
|
const dataItemUserId = await redis.hGet(dataId, 'userId');
|
||||||
|
if (dataItemUserId !== userId) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
// 删除
|
||||||
|
await redis.del(dataId);
|
||||||
|
jsonRes(res);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
68
src/pages/api/model/data/exportModelData.ts
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
import { VecModelDataIdx } from '@/constants/redis';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
|
try {
|
||||||
|
let { modelId } = req.query as {
|
||||||
|
modelId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!modelId) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 凭证校验
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
const redis = await connectRedis();
|
||||||
|
|
||||||
|
// 从 redis 中获取数据
|
||||||
|
const searchRes = await redis.ft.search(
|
||||||
|
VecModelDataIdx,
|
||||||
|
`@modelId:{${modelId}} @userId:{${userId}}`,
|
||||||
|
{
|
||||||
|
RETURN: ['q', 'text', 'rawVector'],
|
||||||
|
LIMIT: {
|
||||||
|
from: 0,
|
||||||
|
size: 10000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const data = searchRes.documents
|
||||||
|
.filter((item) => {
|
||||||
|
if (!item?.value?.rawVector) return false;
|
||||||
|
try {
|
||||||
|
JSON.parse(item.value.rawVector as string);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map((item: any) => ({
|
||||||
|
prompt: item.value.q,
|
||||||
|
completion: item.value.text,
|
||||||
|
vector: JSON.parse(item.value.rawVector)
|
||||||
|
}));
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: JSON.stringify(data)
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/pages/api/model/data/fetchingUrlData.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { httpsAgent } from '@/service/utils/tools';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取网站的内容
|
||||||
|
*/
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { url } = req.body as { url: string };
|
||||||
|
if (!url) {
|
||||||
|
throw new Error('缺少 url');
|
||||||
|
}
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
await authToken(authorization);
|
||||||
|
|
||||||
|
const data = await axios
|
||||||
|
.get(url, {
|
||||||
|
httpsAgent
|
||||||
|
})
|
||||||
|
.then((res) => res.data as string);
|
||||||
|
|
||||||
|
jsonRes(res, { data });
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
74
src/pages/api/model/data/getModelData.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
import { VecModelDataIdx } from '@/constants/redis';
|
||||||
|
import { SearchOptions } from 'redis';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
|
try {
|
||||||
|
let {
|
||||||
|
modelId,
|
||||||
|
pageNum = 1,
|
||||||
|
pageSize = 10
|
||||||
|
} = req.query as {
|
||||||
|
modelId: string;
|
||||||
|
pageNum: string;
|
||||||
|
pageSize: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
pageNum = +pageNum;
|
||||||
|
pageSize = +pageSize;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!modelId) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 凭证校验
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
const redis = await connectRedis();
|
||||||
|
|
||||||
|
// 从 redis 中获取数据
|
||||||
|
const searchRes = await redis.ft.search(
|
||||||
|
VecModelDataIdx,
|
||||||
|
`@modelId:{${modelId}} @userId:{${userId}}`,
|
||||||
|
{
|
||||||
|
RETURN: ['q', 'text', 'status'],
|
||||||
|
LIMIT: {
|
||||||
|
from: (pageNum - 1) * pageSize,
|
||||||
|
size: pageSize
|
||||||
|
},
|
||||||
|
SORTBY: {
|
||||||
|
BY: 'modelId',
|
||||||
|
DIRECTION: 'DESC'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: {
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
data: searchRes.documents.map((item) => ({
|
||||||
|
id: item.id,
|
||||||
|
...item.value
|
||||||
|
})),
|
||||||
|
total: searchRes.total
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
35
src/pages/api/model/data/getSplitData.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, SplitData, Model } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
|
||||||
|
/* 拆分数据成QA */
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { modelId } = req.query as { modelId: string };
|
||||||
|
if (!modelId) {
|
||||||
|
throw new Error('参数错误');
|
||||||
|
}
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
// 找到长度大于0的数据
|
||||||
|
const data = await SplitData.find({
|
||||||
|
userId,
|
||||||
|
modelId,
|
||||||
|
textList: { $exists: true, $not: { $size: 0 } }
|
||||||
|
});
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: data.map((item) => item.textList).flat().length
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
72
src/pages/api/model/data/pushModelDataInput.ts
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, Model } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { ModelDataSchema } from '@/types/mongoSchema';
|
||||||
|
import { generateVector } from '@/service/events/generateVector';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
import { VecModelDataPrefix, ModelDataStatusEnum } from '@/constants/redis';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
|
try {
|
||||||
|
const { modelId, data } = req.body as {
|
||||||
|
modelId: string;
|
||||||
|
data: { text: ModelDataSchema['text']; q: ModelDataSchema['q'] }[];
|
||||||
|
};
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!modelId || !Array.isArray(data)) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 凭证校验
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
const redis = await connectRedis();
|
||||||
|
|
||||||
|
// 验证是否是该用户的 model
|
||||||
|
const model = await Model.findOne({
|
||||||
|
_id: modelId,
|
||||||
|
userId
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!model) {
|
||||||
|
throw new Error('无权操作该模型');
|
||||||
|
}
|
||||||
|
|
||||||
|
const insertRes = await Promise.allSettled(
|
||||||
|
data.map((item) => {
|
||||||
|
return redis.sendCommand([
|
||||||
|
'HMSET',
|
||||||
|
`${VecModelDataPrefix}:${item.q.id}`,
|
||||||
|
'userId',
|
||||||
|
userId,
|
||||||
|
'modelId',
|
||||||
|
modelId,
|
||||||
|
'q',
|
||||||
|
item.q.text,
|
||||||
|
'text',
|
||||||
|
item.text,
|
||||||
|
'status',
|
||||||
|
ModelDataStatusEnum.waiting
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
generateVector();
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: insertRes.filter((item) => item.status === 'rejected').length
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
80
src/pages/api/model/data/pushModelDataJson.ts
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, Model } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { generateVector } from '@/service/events/generateVector';
|
||||||
|
import { vectorToBuffer, formatVector } from '@/utils/tools';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
import { VecModelDataPrefix, ModelDataStatusEnum } from '@/constants/redis';
|
||||||
|
import { customAlphabet } from 'nanoid';
|
||||||
|
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 12);
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
|
try {
|
||||||
|
const { modelId, data } = req.body as {
|
||||||
|
modelId: string;
|
||||||
|
data: { prompt: string; completion: string; vector?: number[] }[];
|
||||||
|
};
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!modelId || !Array.isArray(data)) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 凭证校验
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
const redis = await connectRedis();
|
||||||
|
|
||||||
|
// 验证是否是该用户的 model
|
||||||
|
const model = await Model.findOne({
|
||||||
|
_id: modelId,
|
||||||
|
userId
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!model) {
|
||||||
|
throw new Error('无权操作该模型');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 插入 redis
|
||||||
|
const insertRedisRes = await Promise.allSettled(
|
||||||
|
data.map((item) => {
|
||||||
|
const vector = item.vector;
|
||||||
|
|
||||||
|
return redis.sendCommand([
|
||||||
|
'HMSET',
|
||||||
|
`${VecModelDataPrefix}:${nanoid()}`,
|
||||||
|
'userId',
|
||||||
|
userId,
|
||||||
|
'modelId',
|
||||||
|
String(modelId),
|
||||||
|
...(vector
|
||||||
|
? ['vector', vectorToBuffer(formatVector(vector)), 'rawVector', JSON.stringify(vector)]
|
||||||
|
: []),
|
||||||
|
'q',
|
||||||
|
item.prompt,
|
||||||
|
'text',
|
||||||
|
item.completion,
|
||||||
|
'status',
|
||||||
|
vector ? ModelDataStatusEnum.ready : ModelDataStatusEnum.waiting
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
generateVector();
|
||||||
|
|
||||||
|
jsonRes(res, {
|
||||||
|
data: insertRedisRes.filter((item) => item.status === 'rejected').length
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
52
src/pages/api/model/data/putModelData.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
import { ModelDataStatusEnum } from '@/constants/redis';
|
||||||
|
import { generateVector } from '@/service/events/generateVector';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
|
try {
|
||||||
|
const { dataId, text, q } = req.body as { dataId: string; text: string; q?: string };
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dataId) {
|
||||||
|
throw new Error('缺少参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 凭证校验
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
const redis = await connectRedis();
|
||||||
|
|
||||||
|
// 校验是否为该用户的数据
|
||||||
|
const dataItemUserId = await redis.hGet(dataId, 'userId');
|
||||||
|
if (dataItemUserId !== userId) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新
|
||||||
|
await redis.sendCommand([
|
||||||
|
'HMSET',
|
||||||
|
dataId,
|
||||||
|
...(q ? ['q', q, 'status', ModelDataStatusEnum.waiting] : []),
|
||||||
|
'text',
|
||||||
|
text
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (q) {
|
||||||
|
generateVector();
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonRes(res);
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
78
src/pages/api/model/data/splitData.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { connectToDatabase, SplitData, Model } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { generateQA } from '@/service/events/generateQA';
|
||||||
|
import { encode } from 'gpt-token-utils';
|
||||||
|
|
||||||
|
/* 拆分数据成QA */
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
const { text, modelId, prompt } = req.body as { text: string; modelId: string; prompt: string };
|
||||||
|
if (!text || !modelId || !prompt) {
|
||||||
|
throw new Error('参数错误');
|
||||||
|
}
|
||||||
|
await connectToDatabase();
|
||||||
|
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
|
// 验证是否是该用户的 model
|
||||||
|
const model = await Model.findOne({
|
||||||
|
_id: modelId,
|
||||||
|
userId
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!model) {
|
||||||
|
throw new Error('无权操作该模型');
|
||||||
|
}
|
||||||
|
|
||||||
|
const replaceText = text.replace(/(\\n|\n)+/g, ' ');
|
||||||
|
|
||||||
|
// 文本拆分成 chunk
|
||||||
|
const chunks = replaceText.match(/[^!?.。]+[!?.。]/g) || [];
|
||||||
|
|
||||||
|
const textList: string[] = [];
|
||||||
|
let splitText = '';
|
||||||
|
|
||||||
|
/* 取 3k ~ 4K tokens 内容 */
|
||||||
|
chunks.forEach((chunk) => {
|
||||||
|
const tokens = encode(splitText + chunk).length;
|
||||||
|
if (tokens >= 4000) {
|
||||||
|
// 超过 4000,不要这块内容
|
||||||
|
textList.push(splitText);
|
||||||
|
splitText = chunk;
|
||||||
|
} else if (tokens >= 3000) {
|
||||||
|
// 超过 3000,取内容
|
||||||
|
textList.push(splitText + chunk);
|
||||||
|
splitText = '';
|
||||||
|
} else {
|
||||||
|
//没超过 3000,继续添加
|
||||||
|
splitText += chunk;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (splitText) {
|
||||||
|
textList.push(splitText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 批量插入数据
|
||||||
|
await SplitData.create({
|
||||||
|
userId,
|
||||||
|
modelId,
|
||||||
|
rawText: text,
|
||||||
|
textList,
|
||||||
|
prompt
|
||||||
|
});
|
||||||
|
|
||||||
|
generateQA();
|
||||||
|
|
||||||
|
jsonRes(res);
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { Chat, Model, Training, connectToDatabase } from '@/service/mongo';
|
import { Chat, Model, Training, connectToDatabase } from '@/service/mongo';
|
||||||
import { authToken, getUserOpenaiKey } from '@/service/utils/tools';
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { getUserApiOpenai } from '@/service/utils/openai';
|
||||||
import { TrainingStatusEnum } from '@/constants/model';
|
import { TrainingStatusEnum } from '@/constants/model';
|
||||||
import { getOpenAIApi } from '@/service/utils/chat';
|
|
||||||
import { TrainingItemType } from '@/types/training';
|
import { TrainingItemType } from '@/types/training';
|
||||||
import { openaiProxy } from '@/service/utils/tools';
|
import { httpsAgent } from '@/service/utils/tools';
|
||||||
|
import { connectRedis } from '@/service/redis';
|
||||||
|
import { VecModelDataIdx } from '@/constants/redis';
|
||||||
|
|
||||||
/* 获取我的模型 */
|
/* 获取我的模型 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
@@ -24,14 +26,33 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
// 凭证校验
|
// 凭证校验
|
||||||
const userId = await authToken(authorization);
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
await connectToDatabase();
|
// 验证是否是该用户的 model
|
||||||
|
const model = await Model.findOne({
|
||||||
// 删除模型
|
|
||||||
await Model.deleteOne({
|
|
||||||
_id: modelId,
|
_id: modelId,
|
||||||
userId
|
userId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!model) {
|
||||||
|
throw new Error('无权操作该模型');
|
||||||
|
}
|
||||||
|
|
||||||
|
await connectToDatabase();
|
||||||
|
const redis = await connectRedis();
|
||||||
|
|
||||||
|
// 获取 redis 中模型关联的所有数据
|
||||||
|
const searchRes = await redis.ft.search(
|
||||||
|
VecModelDataIdx,
|
||||||
|
`@modelId:{${modelId}} @userId:{${userId}}`,
|
||||||
|
{
|
||||||
|
LIMIT: {
|
||||||
|
from: 0,
|
||||||
|
size: 10000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
// 删除 redis 内容
|
||||||
|
await Promise.all(searchRes.documents.map((item) => redis.del(item.id)));
|
||||||
|
|
||||||
// 删除对应的聊天
|
// 删除对应的聊天
|
||||||
await Chat.deleteMany({
|
await Chat.deleteMany({
|
||||||
modelId
|
modelId
|
||||||
@@ -45,14 +66,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
|
|
||||||
// 如果正在训练,需要删除openai上的相关信息
|
// 如果正在训练,需要删除openai上的相关信息
|
||||||
if (training) {
|
if (training) {
|
||||||
const openai = getOpenAIApi(await getUserOpenaiKey(userId));
|
const { openai } = await getUserApiOpenai(userId);
|
||||||
// 获取训练记录
|
// 获取训练记录
|
||||||
const tuneRecord = await openai.retrieveFineTune(training.tuneId, openaiProxy);
|
const tuneRecord = await openai.retrieveFineTune(training.tuneId, { httpsAgent });
|
||||||
|
|
||||||
// 删除训练文件
|
// 删除训练文件
|
||||||
openai.deleteFile(tuneRecord.data.training_files[0].id, openaiProxy);
|
openai.deleteFile(tuneRecord.data.training_files[0].id, { httpsAgent });
|
||||||
// 取消训练
|
// 取消训练
|
||||||
openai.cancelFineTune(training.tuneId, openaiProxy);
|
openai.cancelFineTune(training.tuneId, { httpsAgent });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除对应训练记录
|
// 删除对应训练记录
|
||||||
@@ -60,6 +81,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
modelId
|
modelId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 删除模型
|
||||||
|
await Model.deleteOne({
|
||||||
|
_id: modelId,
|
||||||
|
userId
|
||||||
|
});
|
||||||
|
|
||||||
jsonRes(res);
|
jsonRes(res);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { jsonRes } from '@/service/response';
|
|||||||
import { connectToDatabase } from '@/service/mongo';
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
import { authToken } from '@/service/utils/tools';
|
import { authToken } from '@/service/utils/tools';
|
||||||
import { Model } from '@/service/models/model';
|
import { Model } from '@/service/models/model';
|
||||||
import { ModelType } from '@/types/model';
|
import type { ModelSchema } from '@/types/mongoSchema';
|
||||||
|
|
||||||
/* 获取我的模型 */
|
/* 获取我的模型 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
@@ -26,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
// 根据 userId 获取模型信息
|
// 根据 userId 获取模型信息
|
||||||
const model: ModelType | null = await Model.findOne({
|
const model = await Model.findOne<ModelSchema>({
|
||||||
userId,
|
userId,
|
||||||
_id: modelId
|
_id: modelId
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
// 根据 userId 获取模型信息
|
// 根据 userId 获取模型信息
|
||||||
const models = await Model.find({
|
const models = await Model.find({
|
||||||
userId
|
userId
|
||||||
|
}).sort({
|
||||||
|
_id: -1
|
||||||
});
|
});
|
||||||
|
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
|
|||||||
@@ -1,15 +1,7 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase, Model, Training } from '@/service/mongo';
|
import { connectToDatabase, Training } from '@/service/mongo';
|
||||||
import { getOpenAIApi } from '@/service/utils/chat';
|
import { authToken } from '@/service/utils/tools';
|
||||||
import formidable from 'formidable';
|
|
||||||
import { authToken, getUserOpenaiKey } from '@/service/utils/tools';
|
|
||||||
import { join } from 'path';
|
|
||||||
import fs from 'fs';
|
|
||||||
import type { ModelType } from '@/types/model';
|
|
||||||
import type { OpenAIApi } from 'openai';
|
|
||||||
import { ModelStatusEnum, TrainingStatusEnum } from '@/constants/model';
|
|
||||||
import { openaiProxy } from '@/service/utils/tools';
|
|
||||||
|
|
||||||
// 关闭next默认的bodyParser处理方式
|
// 关闭next默认的bodyParser处理方式
|
||||||
export const config = {
|
export const config = {
|
||||||
@@ -18,7 +10,7 @@ export const config = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* 上传文件,开始微调 */
|
/* 获取模型训练记录 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
try {
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
@@ -30,7 +22,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
if (!modelId) {
|
if (!modelId) {
|
||||||
throw new Error('参数错误');
|
throw new Error('参数错误');
|
||||||
}
|
}
|
||||||
const userId = await authToken(authorization);
|
await authToken(authorization);
|
||||||
|
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase, Model, Training } from '@/service/mongo';
|
import { connectToDatabase, Model, Training } from '@/service/mongo';
|
||||||
import { getOpenAIApi } from '@/service/utils/chat';
|
import { authToken } from '@/service/utils/tools';
|
||||||
import { authToken, getUserOpenaiKey } from '@/service/utils/tools';
|
import { getUserApiOpenai } from '@/service/utils/openai';
|
||||||
import type { ModelType } from '@/types/model';
|
import type { ModelSchema } from '@/types/mongoSchema';
|
||||||
import { TrainingItemType } from '@/types/training';
|
import { TrainingItemType } from '@/types/training';
|
||||||
import { ModelStatusEnum, TrainingStatusEnum } from '@/constants/model';
|
import { ModelStatusEnum, TrainingStatusEnum } from '@/constants/model';
|
||||||
import { OpenAiTuneStatusEnum } from '@/service/constants/training';
|
import { OpenAiTuneStatusEnum } from '@/service/constants/training';
|
||||||
import { openaiProxy } from '@/service/utils/tools';
|
import { httpsAgent } from '@/service/utils/tools';
|
||||||
|
|
||||||
/* 更新训练状态 */
|
/* 更新训练状态 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -26,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
// 获取模型
|
// 获取模型
|
||||||
const model: ModelType | null = await Model.findById(modelId);
|
const model = await Model.findById<ModelSchema>(modelId);
|
||||||
|
|
||||||
if (!model || model.status !== 'training') {
|
if (!model || model.status !== 'training') {
|
||||||
throw new Error('模型不在训练中');
|
throw new Error('模型不在训练中');
|
||||||
@@ -43,16 +43,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 用户的 openai 实例
|
// 用户的 openai 实例
|
||||||
const openai = getOpenAIApi(await getUserOpenaiKey(userId));
|
const { openai } = await getUserApiOpenai(userId);
|
||||||
|
|
||||||
// 获取 openai 的训练情况
|
// 获取 openai 的训练情况
|
||||||
const { data } = await openai.retrieveFineTune(training.tuneId, openaiProxy);
|
const { data } = await openai.retrieveFineTune(training.tuneId, { httpsAgent });
|
||||||
|
// console.log(data);
|
||||||
if (data.status === OpenAiTuneStatusEnum.succeeded) {
|
if (data.status === OpenAiTuneStatusEnum.succeeded) {
|
||||||
// 删除训练文件
|
// 删除训练文件
|
||||||
openai.deleteFile(data.training_files[0].id, openaiProxy);
|
openai.deleteFile(data.training_files[0].id, { httpsAgent });
|
||||||
|
|
||||||
// 更新模型
|
// 更新模型状态和模型内容
|
||||||
await Model.findByIdAndUpdate(modelId, {
|
await Model.findByIdAndUpdate(modelId, {
|
||||||
status: ModelStatusEnum.running,
|
status: ModelStatusEnum.running,
|
||||||
updateTime: new Date(),
|
updateTime: new Date(),
|
||||||
@@ -72,9 +72,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 取消微调 */
|
||||||
if (data.status === OpenAiTuneStatusEnum.cancelled) {
|
if (data.status === OpenAiTuneStatusEnum.cancelled) {
|
||||||
// 删除训练文件
|
// 删除训练文件
|
||||||
openai.deleteFile(data.training_files[0].id, openaiProxy);
|
openai.deleteFile(data.training_files[0].id, { httpsAgent });
|
||||||
|
|
||||||
// 更新模型
|
// 更新模型
|
||||||
await Model.findByIdAndUpdate(modelId, {
|
await Model.findByIdAndUpdate(modelId, {
|
||||||
@@ -87,11 +88,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
});
|
});
|
||||||
|
|
||||||
return jsonRes(res, {
|
return jsonRes(res, {
|
||||||
data: '模型微调取消'
|
data: '模型微调已取消'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('模型还在训练中');
|
jsonRes(res, {
|
||||||
|
data: '模型还在训练中'
|
||||||
|
});
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
code: 500,
|
code: 500,
|
||||||
@@ -2,15 +2,15 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase, Model, Training } from '@/service/mongo';
|
import { connectToDatabase, Model, Training } from '@/service/mongo';
|
||||||
import { getOpenAIApi } from '@/service/utils/chat';
|
|
||||||
import formidable from 'formidable';
|
import formidable from 'formidable';
|
||||||
import { authToken, getUserOpenaiKey } from '@/service/utils/tools';
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { getUserApiOpenai } from '@/service/utils/openai';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import type { ModelType } from '@/types/model';
|
import type { ModelSchema } from '@/types/mongoSchema';
|
||||||
import type { OpenAIApi } from 'openai';
|
import type { OpenAIApi } from 'openai';
|
||||||
import { ModelStatusEnum, TrainingStatusEnum } from '@/constants/model';
|
import { ModelStatusEnum, TrainingStatusEnum } from '@/constants/model';
|
||||||
import { openaiProxy } from '@/service/utils/tools';
|
import { httpsAgent } from '@/service/utils/tools';
|
||||||
|
|
||||||
// 关闭next默认的bodyParser处理方式
|
// 关闭next默认的bodyParser处理方式
|
||||||
export const config = {
|
export const config = {
|
||||||
@@ -30,6 +30,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
throw new Error('无权操作');
|
throw new Error('无权操作');
|
||||||
}
|
}
|
||||||
const { modelId } = req.query;
|
const { modelId } = req.query;
|
||||||
|
|
||||||
if (!modelId) {
|
if (!modelId) {
|
||||||
throw new Error('参数错误');
|
throw new Error('参数错误');
|
||||||
}
|
}
|
||||||
@@ -38,7 +39,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
// 获取模型的状态
|
// 获取模型的状态
|
||||||
const model: ModelType | null = await Model.findById(modelId);
|
const model = await Model.findById<ModelSchema>(modelId);
|
||||||
|
|
||||||
if (!model || model.status !== 'running') {
|
if (!model || model.status !== 'running') {
|
||||||
throw new Error('模型正忙');
|
throw new Error('模型正忙');
|
||||||
@@ -48,7 +49,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
const trainingType = model.service.trainId; // 目前都默认是 openai text-davinci-03
|
const trainingType = model.service.trainId; // 目前都默认是 openai text-davinci-03
|
||||||
|
|
||||||
// 获取用户的 API Key 实例化后的对象
|
// 获取用户的 API Key 实例化后的对象
|
||||||
openai = getOpenAIApi(await getUserOpenaiKey(userId));
|
const user = await getUserApiOpenai(userId);
|
||||||
|
openai = user.openai;
|
||||||
|
|
||||||
// 接收文件并保存
|
// 接收文件并保存
|
||||||
const form = formidable({
|
const form = formidable({
|
||||||
@@ -67,13 +69,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
});
|
});
|
||||||
const file = files.file;
|
const file = files.file;
|
||||||
|
|
||||||
// 上传文件
|
// 上传文件到 openai
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const uploadRes = await openai.createFile(
|
const uploadRes = await openai.createFile(
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
fs.createReadStream(file.filepath),
|
fs.createReadStream(file.filepath),
|
||||||
'fine-tune',
|
'fine-tune',
|
||||||
openaiProxy
|
{ httpsAgent }
|
||||||
);
|
);
|
||||||
uploadFileId = uploadRes.data.id; // 记录上传文件的 ID
|
uploadFileId = uploadRes.data.id; // 记录上传文件的 ID
|
||||||
|
|
||||||
@@ -82,9 +84,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
{
|
{
|
||||||
training_file: uploadFileId,
|
training_file: uploadFileId,
|
||||||
model: trainingType,
|
model: trainingType,
|
||||||
suffix: model.name
|
suffix: model.name,
|
||||||
|
n_epochs: 4
|
||||||
},
|
},
|
||||||
openaiProxy
|
{ httpsAgent }
|
||||||
);
|
);
|
||||||
|
|
||||||
trainId = trainRes.data.id; // 记录训练 ID
|
trainId = trainRes.data.id; // 记录训练 ID
|
||||||
@@ -114,9 +117,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (openai) {
|
if (openai) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
uploadFileId && openai.deleteFile(uploadFileId, openaiProxy);
|
uploadFileId && openai.deleteFile(uploadFileId, { httpsAgent });
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
trainId && openai.cancelFineTune(trainId, openaiProxy);
|
trainId && openai.cancelFineTune(trainId, { httpsAgent });
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
@@ -8,7 +8,8 @@ import type { ModelUpdateParams } from '@/types/model';
|
|||||||
/* 获取我的模型 */
|
/* 获取我的模型 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
try {
|
try {
|
||||||
const { name, service, security, systemPrompt } = req.body as ModelUpdateParams;
|
const { name, service, security, systemPrompt, intro, temperature } =
|
||||||
|
req.body as ModelUpdateParams;
|
||||||
const { modelId } = req.query as { modelId: string };
|
const { modelId } = req.query as { modelId: string };
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
@@ -33,8 +34,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name,
|
name,
|
||||||
service,
|
|
||||||
systemPrompt,
|
systemPrompt,
|
||||||
|
intro,
|
||||||
|
temperature,
|
||||||
|
// service,
|
||||||
security
|
security
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||