Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
c3ccbcb7
authored
Mar 28, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 输入超长提示
parent
7a6d0ea6
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
12 deletions
+45
-12
src/constants/model.ts
+3
-0
src/pages/api/chat/chatGpt.ts
+5
-5
src/pages/api/chat/delChatRecordByIndex.ts
+20
-2
src/pages/api/chat/gpt3.ts
+1
-1
src/pages/chat/index.tsx
+16
-4
No files found.
src/constants/model.ts
View file @
c3ccbcb7
...
@@ -12,6 +12,7 @@ export type ModelConstantsData = {
...
@@ -12,6 +12,7 @@ export type ModelConstantsData = {
model
:
`
${
ChatModelNameEnum
}
`
;
model
:
`
${
ChatModelNameEnum
}
`
;
trainName
:
string
;
// 空字符串代表不能训练
trainName
:
string
;
// 空字符串代表不能训练
maxToken
:
number
;
maxToken
:
number
;
contextMaxToken
:
number
;
maxTemperature
:
number
;
maxTemperature
:
number
;
trainedMaxToken
:
number
;
// 训练后最大多少tokens
trainedMaxToken
:
number
;
// 训练后最大多少tokens
price
:
number
;
// 多少钱 / 1token,单位: 0.00001元
price
:
number
;
// 多少钱 / 1token,单位: 0.00001元
...
@@ -24,6 +25,7 @@ export const modelList: ModelConstantsData[] = [
...
@@ -24,6 +25,7 @@ export const modelList: ModelConstantsData[] = [
model
:
ChatModelNameEnum
.
GPT35
,
model
:
ChatModelNameEnum
.
GPT35
,
trainName
:
''
,
trainName
:
''
,
maxToken
:
4000
,
maxToken
:
4000
,
contextMaxToken
:
7500
,
trainedMaxToken
:
2000
,
trainedMaxToken
:
2000
,
maxTemperature
:
2
,
maxTemperature
:
2
,
price
:
3
price
:
3
...
@@ -34,6 +36,7 @@ export const modelList: ModelConstantsData[] = [
...
@@ -34,6 +36,7 @@ export const modelList: ModelConstantsData[] = [
// model: ChatModelNameEnum.GPT3,
// model: ChatModelNameEnum.GPT3,
// trainName: 'davinci',
// trainName: 'davinci',
// maxToken: 4000,
// maxToken: 4000,
// contextMaxToken: 7500,
// trainedMaxToken: 2000,
// trainedMaxToken: 2000,
// maxTemperature: 2,
// maxTemperature: 2,
// price: 30
// price: 30
...
...
src/pages/api/chat/chatGpt.ts
View file @
c3ccbcb7
...
@@ -44,6 +44,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -44,6 +44,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
,
authorization
);
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
,
authorization
);
const
model
:
ModelSchema
=
chat
.
modelId
;
const
model
:
ModelSchema
=
chat
.
modelId
;
const
modelConstantsData
=
modelList
.
find
((
item
)
=>
item
.
model
===
model
.
service
.
modelName
);
if
(
!
modelConstantsData
)
{
throw
new
Error
(
'模型异常,请用 chatgpt 模型'
);
}
// 读取对话内容
// 读取对话内容
const
prompts
=
[...
chat
.
content
,
prompt
];
const
prompts
=
[...
chat
.
content
,
prompt
];
...
@@ -57,7 +61,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -57,7 +61,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}
// 控制在 tokens 数量,防止超出
// 控制在 tokens 数量,防止超出
const
filterPrompts
=
openaiChatFilter
(
prompts
,
7500
);
const
filterPrompts
=
openaiChatFilter
(
prompts
,
modelConstantsData
.
contextMaxToken
);
// 格式化文本内容成 chatgpt 格式
// 格式化文本内容成 chatgpt 格式
const
map
=
{
const
map
=
{
...
@@ -73,10 +77,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -73,10 +77,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
);
);
// console.log(formatPrompts);
// console.log(formatPrompts);
// 计算温度
// 计算温度
const
modelConstantsData
=
modelList
.
find
((
item
)
=>
item
.
model
===
model
.
service
.
modelName
);
if
(
!
modelConstantsData
)
{
throw
new
Error
(
'模型异常'
);
}
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
// 获取 chatAPI
// 获取 chatAPI
...
...
src/pages/api/chat/delChatRecordByIndex.ts
View file @
c3ccbcb7
...
@@ -9,13 +9,31 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -9,13 +9,31 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if
(
!
chatId
||
!
index
)
{
if
(
!
chatId
||
!
index
)
{
throw
new
Error
(
'缺少参数'
);
throw
new
Error
(
'缺少参数'
);
}
}
console
.
log
(
index
);
await
connectToDatabase
();
await
connectToDatabase
();
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
.
findByIdAndUpdate
(
chatId
,
{
await
Chat
.
findByIdAndUpdate
(
chatId
,
{
$set
:
{
$set
:
{
[
`content.
${
i
ndex
}
.deleted`
]:
true
,
[
`content.
${
deletedI
ndex
}
.deleted`
]:
true
,
updateTime
:
Date
.
now
()
updateTime
:
Date
.
now
()
}
}
});
});
...
...
src/pages/api/chat/gpt3.ts
View file @
c3ccbcb7
...
@@ -62,7 +62,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -62,7 +62,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 计算温度
// 计算温度
const
modelConstantsData
=
modelList
.
find
((
item
)
=>
item
.
model
===
model
.
service
.
modelName
);
const
modelConstantsData
=
modelList
.
find
((
item
)
=>
item
.
model
===
model
.
service
.
modelName
);
if
(
!
modelConstantsData
)
{
if
(
!
modelConstantsData
)
{
throw
new
Error
(
'模型异常'
);
throw
new
Error
(
'模型异常
,请用 chatgpt 模型
'
);
}
}
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
...
...
src/pages/chat/index.tsx
View file @
c3ccbcb7
...
@@ -37,6 +37,7 @@ import SlideBar from './components/SlideBar';
...
@@ -37,6 +37,7 @@ import SlideBar from './components/SlideBar';
import
Empty
from
'./components/Empty'
;
import
Empty
from
'./components/Empty'
;
import
Icon
from
'@/components/Icon'
;
import
Icon
from
'@/components/Icon'
;
import
{
encode
}
from
'gpt-token-utils'
;
import
{
encode
}
from
'gpt-token-utils'
;
import
{
modelList
}
from
'@/constants/model'
;
const
Markdown
=
dynamic
(()
=>
import
(
'@/components/Markdown'
));
const
Markdown
=
dynamic
(()
=>
import
(
'@/components/Markdown'
));
...
@@ -200,6 +201,18 @@ const Chat = ({ chatId }: { chatId: string }) => {
...
@@ -200,6 +201,18 @@ const Chat = ({ chatId }: { chatId: string }) => {
return
;
return
;
}
}
// 长度校验
const
tokens
=
encode
(
val
).
length
;
const
model
=
modelList
.
find
((
item
)
=>
item
.
model
===
chatData
.
modelName
);
if
(
model
&&
tokens
>=
model
.
maxToken
)
{
toast
({
title
:
'单次输入超出 4000 tokens'
,
status
:
'warning'
});
return
;
}
const
newChatList
:
ChatSiteItemType
[]
=
[
const
newChatList
:
ChatSiteItemType
[]
=
[
...
chatData
.
history
,
...
chatData
.
history
,
{
{
...
@@ -252,15 +265,14 @@ const Chat = ({ chatId }: { chatId: string }) => {
...
@@ -252,15 +265,14 @@ const Chat = ({ chatId }: { chatId: string }) => {
}
}
},
[
},
[
inputVal
,
inputVal
,
chatData
?.
modelId
,
chatData
,
chatData
.
history
,
isChatting
,
isChatting
,
resetInputVal
,
resetInputVal
,
scrollToBottom
,
scrollToBottom
,
toast
,
gptChatPrompt
,
gptChatPrompt
,
pushChatHistory
,
pushChatHistory
,
chatId
,
chatId
toast
]);
]);
// 删除一句话
// 删除一句话
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment