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
a837552b
authored
May 09, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: search prompt
parent
f52f514f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
27 deletions
+25
-27
src/pages/api/chat/chat.ts
+4
-1
src/pages/chat/index.tsx
+1
-1
src/pages/model/components/detail/components/SelectFileModal.tsx
+1
-7
src/service/plugins/searchKb.ts
+19
-18
No files found.
src/pages/api/chat/chat.ts
View file @
a837552b
...
@@ -55,7 +55,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -55,7 +55,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 使用了知识库搜索
// 使用了知识库搜索
if
(
model
.
chat
.
useKb
)
{
if
(
model
.
chat
.
useKb
)
{
const
{
code
,
searchPrompt
}
=
await
searchKb
({
const
{
code
,
searchPrompt
,
aiPrompt
}
=
await
searchKb
({
userOpenAiKey
,
userOpenAiKey
,
prompts
,
prompts
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
...
@@ -68,6 +68,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -68,6 +68,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return
res
.
send
(
searchPrompt
?.
value
);
return
res
.
send
(
searchPrompt
?.
value
);
}
}
if
(
aiPrompt
)
{
prompts
.
splice
(
prompts
.
length
-
1
,
0
,
aiPrompt
);
}
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
}
else
{
}
else
{
// 没有用知识库搜索,仅用系统提示词
// 没有用知识库搜索,仅用系统提示词
...
...
src/pages/chat/index.tsx
View file @
a837552b
...
@@ -508,7 +508,7 @@ const Chat = ({
...
@@ -508,7 +508,7 @@ const Chat = ({
isLeavePage.current = true;
isLeavePage.current = true;
controller.current?.abort();
controller.current?.abort();
};
};
}, []);
}, [
modelId, chatId
]);
return (
return (
<Flex
<Flex
...
...
src/pages/model/components/detail/components/SelectFileModal.tsx
View file @
a837552b
...
@@ -61,13 +61,7 @@ const SelectFileModal = ({
...
@@ -61,13 +61,7 @@ const SelectFileModal = ({
const
{
openConfirm
,
ConfirmChild
}
=
useConfirm
({
const
{
openConfirm
,
ConfirmChild
}
=
useConfirm
({
content
:
`确认导入该文件,需要一定时间进行拆解,该任务无法终止!如果余额不足,未完成的任务会被直接清除。一共
${
content
:
`确认导入该文件,需要一定时间进行拆解,该任务无法终止!如果余额不足,未完成的任务会被直接清除。一共
${
splitRes
.
chunks
.
length
splitRes
.
chunks
.
length
}
组。
${
}
组。
${
splitRes
.
tokens
?
`大约
${
splitRes
.
tokens
}
个tokens。`
:
''
}
`
splitRes
.
tokens
?
`大约
${
splitRes
.
tokens
}
个tokens, 约
${
formatPrice
(
splitRes
.
tokens
*
modeMap
[
mode
].
price
)}
元`
:
''
}
`
});
});
const
onSelectFile
=
useCallback
(
const
onSelectFile
=
useCallback
(
...
...
src/service/plugins/searchKb.ts
View file @
a837552b
...
@@ -24,7 +24,11 @@ export const searchKb = async ({
...
@@ -24,7 +24,11 @@ export const searchKb = async ({
}):
Promise
<
{
}):
Promise
<
{
code
:
200
|
201
;
code
:
200
|
201
;
searchPrompt
?:
{
searchPrompt
?:
{
obj
:
`
${
ChatRoleEnum
}
`
;
obj
:
`
${
ChatRoleEnum
.
System
}
`
;
value
:
string
;
};
aiPrompt
?:
{
obj
:
`
${
ChatRoleEnum
.
AI
}
`
;
value
:
string
;
value
:
string
;
};
};
}
>
=>
{
}
>
=>
{
...
@@ -85,24 +89,11 @@ export const searchKb = async ({
...
@@ -85,24 +89,11 @@ export const searchKb = async ({
};
};
const
filterRate
=
filterRateMap
[
systemPrompts
.
length
]
||
filterRateMap
[
0
];
const
filterRate
=
filterRateMap
[
systemPrompts
.
length
]
||
filterRateMap
[
0
];
// count fixed system prompt
const
fixedSystemPrompt
=
`
${
model
.
chat
.
systemPrompt
}
${
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
?
'不回答知识库外的内容.'
:
''
}
知识库内容为:`
;
const
fixedSystemTokens
=
modelToolMap
[
model
.
chat
.
chatModel
].
countTokens
({
messages
:
[{
obj
:
'System'
,
value
:
fixedSystemPrompt
}]
});
const
maxTokens
=
modelConstantsData
.
systemMaxToken
-
fixedSystemTokens
;
const
filterSystemPrompt
=
filterRate
const
filterSystemPrompt
=
filterRate
.
map
((
rate
,
i
)
=>
.
map
((
rate
,
i
)
=>
modelToolMap
[
model
.
chat
.
chatModel
].
sliceText
({
modelToolMap
[
model
.
chat
.
chatModel
].
sliceText
({
text
:
systemPrompts
[
i
],
text
:
systemPrompts
[
i
],
length
:
Math
.
floor
(
m
axTokens
*
rate
)
length
:
Math
.
floor
(
m
odelConstantsData
.
systemMaxToken
*
rate
)
})
})
)
)
.
join
(
'\n'
);
.
join
(
'\n'
);
...
@@ -112,7 +103,7 @@ ${
...
@@ -112,7 +103,7 @@ ${
return
{
return
{
code
:
201
,
code
:
201
,
searchPrompt
:
{
searchPrompt
:
{
obj
:
ChatRoleEnum
.
AI
,
obj
:
ChatRoleEnum
.
System
,
value
:
'对不起,你的问题不在知识库中。'
value
:
'对不起,你的问题不在知识库中。'
}
}
};
};
...
@@ -135,7 +126,17 @@ ${
...
@@ -135,7 +126,17 @@ ${
code
:
200
,
code
:
200
,
searchPrompt
:
{
searchPrompt
:
{
obj
:
ChatRoleEnum
.
System
,
obj
:
ChatRoleEnum
.
System
,
value
:
`
${
fixedSystemPrompt
}
'
${
filterSystemPrompt
}
'`
value
:
`知识库:'
${
filterSystemPrompt
}
'`
}
},
aiPrompt
:
ModelVectorSearchModeEnum
.
hightSimilarity
?
{
obj
:
'AI'
,
value
:
`我来玩一个问答游戏,规则为:
1.我完全忘记我已有的知识
2.我只能回答关于"
${
model
.
chat
.
systemPrompt
||
model
.
name
}
"的问题
3.我只能从知识库中选择内容进行回答
4.如果问题不在知识库中,我会回答"我不知道。"`
}
:
undefined
};
};
};
};
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