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
d85b4c09
authored
Mar 17, 2023
by
Archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 优化系统提示消息
parent
1e770088
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
26 additions
and
28 deletions
+26
-28
src/pages/api/chat/chatGpt.ts
+11
-10
src/pages/api/chat/init.ts
+2
-11
src/pages/chat/index.tsx
+3
-3
src/pages/model/components/ModelEditForm.tsx
+3
-1
src/service/errorCode.ts
+2
-1
src/service/response.ts
+5
-2
No files found.
src/pages/api/chat/chatGpt.ts
View file @
d85b4c09
...
@@ -35,9 +35,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -35,9 +35,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 上下文长度过滤
// 上下文长度过滤
const
maxContext
=
model
.
security
.
contextMaxLen
;
const
maxContext
=
model
.
security
.
contextMaxLen
;
const
filterPrompts
=
const
filterPrompts
=
prompts
.
length
>
maxContext
+
2
prompts
.
length
>
maxContext
?
prompts
.
slice
(
prompts
.
length
-
maxContext
)
:
prompts
;
?
[
prompts
[
0
],
...
prompts
.
slice
(
prompts
.
length
-
maxContext
)]
:
prompts
.
slice
(
0
,
prompts
.
length
);
// 格式化文本内容
// 格式化文本内容
const
map
=
{
const
map
=
{
...
@@ -51,11 +49,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -51,11 +49,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
content
:
item
.
value
content
:
item
.
value
})
})
);
);
// 第一句话,强调代码类型
// 如果有系统提示词,自动插入
formatPrompts
.
unshift
({
if
(
model
.
systemPrompt
)
{
role
:
ChatCompletionRequestMessageRoleEnum
.
System
,
formatPrompts
.
unshift
({
content
:
'如果你想返回代码,请务必声明代码的类型!并且在代码块前加一个换行符。'
role
:
'system'
,
});
content
:
model
.
systemPrompt
});
}
// 获取 chatAPI
// 获取 chatAPI
const
chatAPI
=
getOpenAIApi
(
userApiKey
);
const
chatAPI
=
getOpenAIApi
(
userApiKey
);
...
@@ -77,8 +77,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -77,8 +77,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
);
);
console
.
log
(
console
.
log
(
'response success'
,
'response success'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
,
`time:
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
,
formatPrompts
.
reduce
((
sum
,
item
)
=>
sum
+
item
.
content
.
length
,
0
)
`promptLen:
${
formatPrompts
.
length
}
`
,
`contentLen:
${
formatPrompts
.
reduce
((
sum
,
item
)
=>
sum
+
item
.
content
.
length
,
0
)}
`
);
);
// 创建响应流
// 创建响应流
...
...
src/pages/api/chat/init.ts
View file @
d85b4c09
...
@@ -49,21 +49,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -49,21 +49,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
error
;
error
;
}
}
const
defaultContent
=
model
.
systemPrompt
?
[
{
obj
:
'SYSTEM'
,
value
:
model
.
systemPrompt
}
]
:
[];
if
(
!
history
)
{
if
(
!
history
)
{
// 没有记录,创建一个
// 没有记录,创建一个
const
response
=
await
ChatWindow
.
create
({
const
response
=
await
ChatWindow
.
create
({
chatId
,
chatId
,
updateTime
:
Date
.
now
(),
updateTime
:
Date
.
now
(),
content
:
defaultContent
content
:
[]
});
});
responseId
=
response
.
_id
;
responseId
=
response
.
_id
;
}
}
...
@@ -78,7 +69,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -78,7 +69,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
secret
:
model
.
security
,
secret
:
model
.
security
,
chatModel
:
model
.
service
.
chatModel
chatModel
:
model
.
service
.
chatModel
},
},
history
:
history
?
history
.
content
:
defaultContent
history
:
history
?
history
.
content
:
[]
}
}
});
});
}
catch
(
err
)
{
}
catch
(
err
)
{
...
...
src/pages/chat/index.tsx
View file @
d85b4c09
...
@@ -249,9 +249,9 @@ const Chat = ({
...
@@ -249,9 +249,9 @@ const Chat = ({
/* 对长度进行限制 */
/* 对长度进行限制 */
const
maxContext
=
chatSiteData
.
secret
.
contextMaxLen
;
const
maxContext
=
chatSiteData
.
secret
.
contextMaxLen
;
const
requestPrompt
=
const
requestPrompt
=
newChatList
.
length
>
maxContext
+
2
newChatList
.
length
>
maxContext
+
1
?
[
newChatList
[
0
],
...
newChatList
.
slice
(
newChatList
.
length
-
maxContext
-
1
,
-
1
)]
?
newChatList
.
slice
(
newChatList
.
length
-
maxContext
-
1
,
-
1
)
:
newChatList
.
slice
(
0
,
newChatList
.
length
-
1
);
:
newChatList
.
slice
(
0
,
-
1
);
if
(
typeof
fnMap
[
chatSiteData
.
chatModel
]
===
'function'
)
{
if
(
typeof
fnMap
[
chatSiteData
.
chatModel
]
===
'function'
)
{
await
fnMap
[
chatSiteData
.
chatModel
](
requestPrompt
);
await
fnMap
[
chatSiteData
.
chatModel
](
requestPrompt
);
...
...
src/pages/model/components/ModelEditForm.tsx
View file @
d85b4c09
...
@@ -99,7 +99,9 @@ const ModelEditForm = ({ model }: { model?: ModelType }) => {
...
@@ -99,7 +99,9 @@ const ModelEditForm = ({ model }: { model?: ModelType }) => {
rows=
{
4
}
rows=
{
4
}
maxLength=
{
500
}
maxLength=
{
500
}
{
...
register
('
systemPrompt
')}
{
...
register
('
systemPrompt
')}
placeholder=
{
'系统的提示词,会在进入聊天时放置在第一句,用于限定模型的聊天范围'
}
placeholder=
{
'模型默认的 prompt 词,可以通过调整该内容,生成一个限定范围的模型,更方便的去使用。'
}
/>
/>
</
FormControl
>
</
FormControl
>
</
Card
>
</
Card
>
...
...
src/service/errorCode.ts
View file @
d85b4c09
...
@@ -2,7 +2,8 @@ export const openaiError: Record<string, string> = {
...
@@ -2,7 +2,8 @@ export const openaiError: Record<string, string> = {
context_length_exceeded
:
'内容超长了,请重置对话'
,
context_length_exceeded
:
'内容超长了,请重置对话'
,
Unauthorized
:
'API-KEY 不合法'
,
Unauthorized
:
'API-KEY 不合法'
,
rate_limit_reached
:
'同时访问用户过多,请稍后再试'
,
rate_limit_reached
:
'同时访问用户过多,请稍后再试'
,
'Bad Request'
:
'上下文太多了,请重开对话~'
'Bad Request'
:
'上下文太多了,请重开对话~'
,
'Too Many Requests'
:
'请求次数太多了,请慢点~'
};
};
export
const
proxyError
:
Record
<
string
,
boolean
>
=
{
export
const
proxyError
:
Record
<
string
,
boolean
>
=
{
ECONNABORTED
:
true
,
ECONNABORTED
:
true
,
...
...
src/service/response.ts
View file @
d85b4c09
...
@@ -28,8 +28,11 @@ export const jsonRes = (
...
@@ -28,8 +28,11 @@ export const jsonRes = (
}
else
if
(
openaiError
[
error
?.
response
?.
statusText
])
{
}
else
if
(
openaiError
[
error
?.
response
?.
statusText
])
{
msg
=
openaiError
[
error
.
response
.
statusText
];
msg
=
openaiError
[
error
.
response
.
statusText
];
}
}
// console.log(error?.response)
// console.log(error?.response);
console
.
log
(
'error->'
,
error
.
code
,
error
?.
response
?.
statusText
,
msg
);
console
.
log
(
'error->'
);
console
.
log
(
'code:'
,
error
.
code
);
console
.
log
(
'statusText:'
,
error
?.
response
?.
statusText
);
console
.
log
(
'msg:'
,
msg
);
}
}
res
.
json
({
res
.
json
({
...
...
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