mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-24 22:03:54 +00:00
feat: 合并
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
# Doc GPT
|
||||
# Fast GPT
|
||||
|
||||
Fast GPT 允许你是用自己的 openai API KEY 来快速的调用 openai 接口,包括 GPT3 及其微调方法,以及最新的 gpt3.5 接口。
|
||||
|
||||
## 初始化
|
||||
复制 .env.template 成 .env.local ,填写核心参数
|
||||
@@ -74,7 +76,7 @@ docker run -d --name mongo \
|
||||
|
||||
# 介绍页
|
||||
|
||||
## 欢迎使用 Doc GPT
|
||||
## 欢迎使用 Fast GPT
|
||||
|
||||
时间比较赶,介绍没来得及完善,先直接上怎么使用:
|
||||
|
||||
|
@@ -23,6 +23,7 @@
|
||||
"axios": "^1.3.3",
|
||||
"crypto": "^1.0.1",
|
||||
"dayjs": "^1.11.7",
|
||||
"eventsource-parser": "^0.1.0",
|
||||
"formidable": "^2.1.1",
|
||||
"framer-motion": "^9.0.6",
|
||||
"hyperdown": "^2.4.29",
|
||||
|
6312
pnpm-lock.yaml
generated
6312
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -356,7 +356,7 @@
|
||||
line-height: 1.6;
|
||||
letter-spacing: 0.5px;
|
||||
text-align: justify;
|
||||
|
||||
word-break: break-all;
|
||||
pre {
|
||||
display: block;
|
||||
width: 100%;
|
||||
@@ -369,7 +369,7 @@
|
||||
}
|
||||
|
||||
pre code {
|
||||
background-color: #222;
|
||||
background-color: #222 !important;
|
||||
color: #fff;
|
||||
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';
|
||||
|
@@ -26,8 +26,8 @@ const Markdown = ({ source, isChatting }: { source: string; isChatting: boolean
|
||||
code({ node, inline, className, children, ...props }) {
|
||||
const match = /language-(\w+)/.exec(className || '');
|
||||
const code = String(children).replace(/\n$/, '');
|
||||
return !inline ? (
|
||||
<Box my={3} borderRadius={'md'} overflow={'hidden'}>
|
||||
return !inline || match ? (
|
||||
<Box my={3} borderRadius={'md'} overflow={'hidden'} backgroundColor={'#222'}>
|
||||
<Flex
|
||||
py={2}
|
||||
px={5}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { Readable } from 'stream';
|
||||
import { createParser, ParsedEvent, ReconnectInterval } from 'eventsource-parser';
|
||||
import { connectToDatabase, ChatWindow } from '@/service/mongo';
|
||||
import type { ModelType } from '@/types/model';
|
||||
import { getOpenAIApi, authChat } from '@/service/utils/chat';
|
||||
@@ -9,21 +9,13 @@ import { ChatItemType } from '@/types/chat';
|
||||
|
||||
/* 发送提示词 */
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
|
||||
const responseData: string[] = [];
|
||||
const stream = new Readable({
|
||||
read(size) {
|
||||
const data = responseData.shift() || null;
|
||||
this.push(data);
|
||||
}
|
||||
});
|
||||
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');
|
||||
|
||||
res.on('close', () => {
|
||||
res.end();
|
||||
stream.destroy();
|
||||
});
|
||||
|
||||
const { chatId, windowId } = req.query as { chatId: string; windowId: string };
|
||||
@@ -58,16 +50,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const formatPrompts: ChatCompletionRequestMessage[] = filterPrompts.map(
|
||||
(item: ChatItemType) => ({
|
||||
role: map[item.obj],
|
||||
content: item.value.replace(/(\n| )/g, '')
|
||||
content: item.value.replace(/\n/g, ' ')
|
||||
})
|
||||
);
|
||||
// 第一句话,强调代码类型
|
||||
formatPrompts.unshift({
|
||||
role: ChatCompletionRequestMessageRoleEnum.System,
|
||||
content:
|
||||
'If the content is code or code blocks, please mark the code type as accurately as possible!'
|
||||
content: '如果你想返回代码,请务必声明代码的类型!'
|
||||
});
|
||||
|
||||
// 获取 chatAPI
|
||||
const chatAPI = getOpenAIApi(userApiKey);
|
||||
const chatResponse = await chatAPI.createChatCompletion(
|
||||
@@ -78,48 +68,57 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
messages: formatPrompts,
|
||||
stream: true
|
||||
},
|
||||
openaiProxy
|
||||
{
|
||||
responseType: 'stream',
|
||||
httpsAgent: openaiProxy?.httpsAgent
|
||||
}
|
||||
);
|
||||
|
||||
// 截取字符串内容
|
||||
const reg = /{"content"(.*)"}/g;
|
||||
// @ts-ignore
|
||||
const match = chatResponse.data.match(reg);
|
||||
if (!match) return;
|
||||
|
||||
let AIResponse = '';
|
||||
|
||||
// 循环给 stream push 内容
|
||||
match.forEach((item: string, i: number) => {
|
||||
try {
|
||||
const json = JSON.parse(item);
|
||||
// 开头的换行忽略
|
||||
if (i === 0 && json.content?.startsWith('\n')) return;
|
||||
AIResponse += json.content;
|
||||
const content = json.content.replace(/\n/g, '<br/>'); // 无法直接传输\n
|
||||
if (content) {
|
||||
responseData.push(`event: responseData\ndata: ${content}\n\n`);
|
||||
// res.write(`event: responseData\n`)
|
||||
// res.write(`data: ${content}\n\n`)
|
||||
// 解析数据
|
||||
const decoder = new TextDecoder();
|
||||
new ReadableStream({
|
||||
async start(controller) {
|
||||
// callback
|
||||
async function onParse(event: ParsedEvent | ReconnectInterval) {
|
||||
if (event.type === 'event') {
|
||||
const data = event.data;
|
||||
if (data === '[DONE]') {
|
||||
controller.close();
|
||||
res.write('event: done\ndata: \n\n');
|
||||
res.end();
|
||||
// 存入库
|
||||
await ChatWindow.findByIdAndUpdate(windowId, {
|
||||
$push: {
|
||||
content: {
|
||||
obj: 'AI',
|
||||
value: AIResponse
|
||||
}
|
||||
},
|
||||
updateTime: Date.now()
|
||||
});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const json = JSON.parse(data);
|
||||
const content: string = json.choices[0].delta.content || '';
|
||||
res.write(`event: responseData\ndata: ${content.replace(/\n/g, '<br/>')}\n\n`);
|
||||
AIResponse += content;
|
||||
} catch (e) {
|
||||
// maybe parse error
|
||||
controller.error(e);
|
||||
res.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const parser = createParser(onParse);
|
||||
for await (const chunk of chatResponse.data as any) {
|
||||
parser.feed(decoder.decode(chunk));
|
||||
}
|
||||
} catch (err) {
|
||||
err;
|
||||
}
|
||||
});
|
||||
|
||||
responseData.push(`event: done\ndata: \n\n`);
|
||||
// 存入库
|
||||
(async () => {
|
||||
await ChatWindow.findByIdAndUpdate(windowId, {
|
||||
$push: {
|
||||
content: {
|
||||
obj: 'AI',
|
||||
value: AIResponse
|
||||
}
|
||||
},
|
||||
updateTime: Date.now()
|
||||
});
|
||||
})();
|
||||
} catch (err: any) {
|
||||
let errorText = err;
|
||||
if (err.code === 'ECONNRESET') {
|
||||
@@ -143,17 +142,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
}
|
||||
}
|
||||
console.error(errorText);
|
||||
responseData.push(`event: serviceError\ndata: ${errorText}\n\n`);
|
||||
|
||||
res.write(`event: serviceError\ndata: ${errorText}\n\n`);
|
||||
res.end();
|
||||
// 删除最一条数据库记录, 也就是预发送的那一条
|
||||
(async () => {
|
||||
await ChatWindow.findByIdAndUpdate(windowId, {
|
||||
$pop: { content: 1 },
|
||||
updateTime: Date.now()
|
||||
});
|
||||
})();
|
||||
await ChatWindow.findByIdAndUpdate(windowId, {
|
||||
$pop: { content: 1 },
|
||||
updateTime: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
// 开启 stream 传输
|
||||
stream.pipe(res);
|
||||
}
|
||||
|
@@ -148,6 +148,7 @@ const Chat = () => {
|
||||
);
|
||||
});
|
||||
event.addEventListener('done', () => {
|
||||
console.log('done');
|
||||
clearTimeout(timer);
|
||||
event.close();
|
||||
setChatList((state) =>
|
||||
@@ -324,7 +325,7 @@ const Chat = () => {
|
||||
height={30}
|
||||
/>
|
||||
</Box>
|
||||
<Box flex={'1 0 0'} w={0} overflowX={'auto'}>
|
||||
<Box flex={'1 0 0'} w={0} overflowX={'hidden'}>
|
||||
{item.obj === 'AI' ? (
|
||||
<Markdown
|
||||
source={item.value}
|
||||
|
@@ -77,7 +77,7 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
|
||||
return (
|
||||
<>
|
||||
<Box fontWeight={'bold'} fontSize={'2xl'} textAlign={'center'}>
|
||||
找回 DocGPT 账号
|
||||
找回 FastGPT 账号
|
||||
</Box>
|
||||
<form onSubmit={handleSubmit(onclickFindPassword)}>
|
||||
<FormControl mt={8} isInvalid={!!errors.email}>
|
||||
|
@@ -58,7 +58,7 @@ const LoginForm = ({ setPageType, loginSuccess }: Props) => {
|
||||
return (
|
||||
<>
|
||||
<Box fontWeight={'bold'} fontSize={'2xl'} textAlign={'center'}>
|
||||
登录 DocGPT
|
||||
登录 FastGPT
|
||||
</Box>
|
||||
<form onSubmit={handleSubmit(onclickLogin)}>
|
||||
<FormControl mt={8} isInvalid={!!errors.email}>
|
||||
|
@@ -78,7 +78,7 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
|
||||
return (
|
||||
<>
|
||||
<Box fontWeight={'bold'} fontSize={'2xl'} textAlign={'center'}>
|
||||
注册 DocGPT 账号
|
||||
注册 FastGPT 账号
|
||||
</Box>
|
||||
<form onSubmit={handleSubmit(onclickRegister)}>
|
||||
<FormControl mt={8} isInvalid={!!errors.email}>
|
||||
|
@@ -20,10 +20,13 @@ export const jsonRes = (
|
||||
|
||||
let msg = message;
|
||||
if ((code < 200 || code >= 400) && !message) {
|
||||
msg =
|
||||
typeof error === 'string'
|
||||
? error
|
||||
: openaiError[error?.response?.data?.message] || error?.message || '请求错误';
|
||||
msg = error?.message || '请求错误';
|
||||
if (typeof error === 'string') {
|
||||
msg = error;
|
||||
} else if (error?.response?.data?.message in openaiError) {
|
||||
msg = openaiError[error?.response?.data?.message];
|
||||
}
|
||||
|
||||
console.error(error);
|
||||
console.error(msg);
|
||||
}
|
||||
|
@@ -15,19 +15,19 @@ let mailTransport = nodemailer.createTransport({
|
||||
|
||||
const emailMap: { [key: string]: any } = {
|
||||
[EmailTypeEnum.register]: {
|
||||
subject: '注册 DocGPT 账号',
|
||||
html: (code: string) => `<div>您正在注册 DocGPT 账号,验证码为:${code}</div>`
|
||||
subject: '注册 FastGPT 账号',
|
||||
html: (code: string) => `<div>您正在注册 FastGPT 账号,验证码为:${code}</div>`
|
||||
},
|
||||
[EmailTypeEnum.findPassword]: {
|
||||
subject: '修改 DocGPT 密码',
|
||||
html: (code: string) => `<div>您正在修改 DocGPT 账号密码,验证码为:${code}</div>`
|
||||
subject: '修改 FastGPT 密码',
|
||||
html: (code: string) => `<div>您正在修改 FastGPT 账号密码,验证码为:${code}</div>`
|
||||
}
|
||||
};
|
||||
|
||||
export const sendCode = (email: string, code: string, type: `${EmailTypeEnum}`) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = {
|
||||
from: `"DocGPT" ${myEmail}`,
|
||||
from: `"FastGPT" ${myEmail}`,
|
||||
to: email,
|
||||
subject: emailMap[type]?.subject,
|
||||
html: emailMap[type]?.html(code)
|
||||
@@ -46,7 +46,7 @@ export const sendCode = (email: string, code: string, type: `${EmailTypeEnum}`)
|
||||
export const sendTrainSucceed = (email: string, modelName: string) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = {
|
||||
from: `"DocGPT" ${myEmail}`,
|
||||
from: `"FastGPT" ${myEmail}`,
|
||||
to: email,
|
||||
subject: '模型训练完成通知',
|
||||
html: `你的模型 ${modelName} 已于 ${dayjs().format('YYYY-MM-DD HH:mm')} 训练完成!`
|
||||
|
Reference in New Issue
Block a user