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
4dc541e0
authored
May 09, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: search prompt
parent
f52f514f
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
76 additions
and
53 deletions
+76
-53
src/components/Layout/index.tsx
+1
-1
src/components/Layout/navbar.tsx
+1
-3
src/pages/api/chat/chat.ts
+9
-5
src/pages/api/openapi/chat/chat.ts
+3
-4
src/pages/api/openapi/chat/lafGpt.ts
+2
-2
src/pages/chat/index.tsx
+1
-1
src/pages/model/components/detail/components/ModelDataCard.tsx
+2
-2
src/pages/model/components/detail/components/SelectFileModal.tsx
+1
-7
src/pages/model/share/index.tsx
+2
-2
src/pages/number/components/BillTable.tsx
+3
-3
src/pages/promotion/index.tsx
+2
-2
src/service/plugins/searchKb.ts
+48
-20
src/service/utils/chat/claude.ts
+1
-1
No files found.
src/components/Layout/index.tsx
View file @
4dc541e0
...
@@ -35,7 +35,7 @@ const Layout = ({ children, isPcDevice }: { children: JSX.Element; isPcDevice: b
...
@@ -35,7 +35,7 @@ const Layout = ({ children, isPcDevice }: { children: JSX.Element; isPcDevice: b
return
(
return
(
<>
<>
<
Box
h=
{
'100%'
}
overflow
=
{
'overlay
'
}
bg=
{
'gray.100'
}
>
<
Box
h=
{
'100%'
}
overflow
Y=
{
'auto
'
}
bg=
{
'gray.100'
}
>
{
isPc
?
(
{
isPc
?
(
pcUnShowLayoutRoute
[
router
.
pathname
]
?
(
pcUnShowLayoutRoute
[
router
.
pathname
]
?
(
<
Auth
>
{
children
}
</
Auth
>
<
Auth
>
{
children
}
</
Auth
>
...
...
src/components/Layout/navbar.tsx
View file @
4dc541e0
...
@@ -102,9 +102,7 @@ const Navbar = () => {
...
@@ -102,9 +102,7 @@ const Navbar = () => {
justifyContent=
{
'center'
}
justifyContent=
{
'center'
}
onClick=
{
()
=>
{
onClick=
{
()
=>
{
if
(
item
.
link
===
router
.
asPath
)
return
;
if
(
item
.
link
===
router
.
asPath
)
return
;
router
.
push
(
item
.
link
,
undefined
,
{
router
.
push
(
item
.
link
);
shallow
:
true
});
}
}
}
}
cursor=
{
'pointer'
}
cursor=
{
'pointer'
}
w=
{
'60px'
}
w=
{
'60px'
}
...
...
src/pages/api/chat/chat.ts
View file @
4dc541e0
...
@@ -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
s
}
=
await
searchKb
({
userOpenAiKey
,
userOpenAiKey
,
prompts
,
prompts
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
...
@@ -65,10 +65,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -65,10 +65,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// search result is empty
// search result is empty
if
(
code
===
201
)
{
if
(
code
===
201
)
{
return
res
.
send
(
searchPrompt
?.
value
);
return
res
.
send
(
searchPrompt
s
[
0
]
?.
value
);
}
}
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
prompts
.
splice
(
prompts
.
length
-
1
,
0
,
...
searchPrompts
);
}
else
{
}
else
{
// 没有用知识库搜索,仅用系统提示词
// 没有用知识库搜索,仅用系统提示词
model
.
chat
.
systemPrompt
&&
model
.
chat
.
systemPrompt
&&
...
@@ -103,8 +103,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -103,8 +103,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
stream
,
stream
,
chatResponse
:
streamResponse
,
chatResponse
:
streamResponse
,
prompts
,
prompts
,
systemPrompt
:
systemPrompt
:
showModelDetail
showModelDetail
&&
prompts
[
0
].
obj
===
ChatRoleEnum
.
System
?
prompts
[
0
].
value
:
''
?
prompts
.
filter
((
item
)
=>
item
.
obj
===
ChatRoleEnum
.
System
)
.
map
((
item
)
=>
item
.
value
)
.
join
(
'\n'
)
:
''
});
});
// 只有使用平台的 key 才计费
// 只有使用平台的 key 才计费
...
...
src/pages/api/openapi/chat/chat.ts
View file @
4dc541e0
...
@@ -73,7 +73,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -73,7 +73,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if
(
model
.
chat
.
useKb
)
{
if
(
model
.
chat
.
useKb
)
{
const
similarity
=
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
;
const
similarity
=
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
;
const
{
code
,
searchPrompt
}
=
await
searchKb
({
const
{
code
,
searchPrompt
s
}
=
await
searchKb
({
prompts
,
prompts
,
similarity
,
similarity
,
model
,
model
,
...
@@ -82,10 +82,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -82,10 +82,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// search result is empty
// search result is empty
if
(
code
===
201
)
{
if
(
code
===
201
)
{
return
res
.
send
(
searchPrompt
?.
value
);
return
res
.
send
(
searchPrompt
s
[
0
]
?.
value
);
}
}
prompts
.
splice
(
prompts
.
length
-
1
,
0
,
...
searchPrompts
);
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
}
else
{
}
else
{
// 没有用知识库搜索,仅用系统提示词
// 没有用知识库搜索,仅用系统提示词
if
(
model
.
chat
.
systemPrompt
)
{
if
(
model
.
chat
.
systemPrompt
)
{
...
...
src/pages/api/openapi/chat/lafGpt.ts
View file @
4dc541e0
...
@@ -122,14 +122,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -122,14 +122,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
prompts
=
[
prompt
];
const
prompts
=
[
prompt
];
// 获取向量匹配到的提示词
// 获取向量匹配到的提示词
const
{
searchPrompt
}
=
await
searchKb
({
const
{
searchPrompt
s
}
=
await
searchKb
({
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
prompts
,
prompts
,
model
,
model
,
userId
userId
});
});
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
prompts
.
splice
(
prompts
.
length
-
1
,
0
,
...
searchPrompts
);
// 计算温度
// 计算温度
const
temperature
=
(
modelConstantsData
.
maxTemperature
*
(
model
.
chat
.
temperature
/
10
)).
toFixed
(
const
temperature
=
(
modelConstantsData
.
maxTemperature
*
(
model
.
chat
.
temperature
/
10
)).
toFixed
(
...
...
src/pages/chat/index.tsx
View file @
4dc541e0
...
@@ -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/ModelDataCard.tsx
View file @
4dc541e0
...
@@ -279,9 +279,9 @@ const ModelDataCard = ({ modelId, isOwner }: { modelId: string; isOwner: boolean
...
@@ -279,9 +279,9 @@ const ModelDataCard = ({ modelId, isOwner }: { modelId: string; isOwner: boolean
</
Tbody
>
</
Tbody
>
</
Table
>
</
Table
>
</
TableContainer
>
</
TableContainer
>
<
Box
mt=
{
2
}
textAlign=
{
'
end'
}
>
<
Flex
mt=
{
2
}
justifyContent=
{
'flex-
end'
}
>
<
Pagination
/>
<
Pagination
/>
</
Bo
x
>
</
Fle
x
>
</
Box
>
</
Box
>
<
Loading
loading=
{
isLoading
}
fixed=
{
false
}
/>
<
Loading
loading=
{
isLoading
}
fixed=
{
false
}
/>
...
...
src/pages/model/components/detail/components/SelectFileModal.tsx
View file @
4dc541e0
...
@@ -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/pages/model/share/index.tsx
View file @
4dc541e0
...
@@ -101,9 +101,9 @@ const modelList = () => {
...
@@ -101,9 +101,9 @@ const modelList = () => {
<
Grid
templateColumns=
{
[
'1fr'
,
'1fr 1fr'
,
'1fr 1fr 1fr'
]
}
gridGap=
{
4
}
mt=
{
4
}
>
<
Grid
templateColumns=
{
[
'1fr'
,
'1fr 1fr'
,
'1fr 1fr 1fr'
]
}
gridGap=
{
4
}
mt=
{
4
}
>
<
ShareModelList
models=
{
models
}
onclickCollection=
{
onclickCollection
}
/>
<
ShareModelList
models=
{
models
}
onclickCollection=
{
onclickCollection
}
/>
</
Grid
>
</
Grid
>
<
Box
mt=
{
4
}
>
<
Flex
mt=
{
4
}
justifyContent=
{
'flex-end'
}
>
<
Pagination
/>
<
Pagination
/>
</
Bo
x
>
</
Fle
x
>
</
Card
>
</
Card
>
<
Loading
loading=
{
isLoading
}
/>
<
Loading
loading=
{
isLoading
}
/>
...
...
src/pages/number/components/BillTable.tsx
View file @
4dc541e0
import
React
from
'react'
;
import
React
from
'react'
;
import
{
Card
,
Box
,
Table
,
Thead
,
Tbody
,
Tr
,
Th
,
Td
,
TableContainer
}
from
'@chakra-ui/react'
;
import
{
Card
,
Box
,
Table
,
Thead
,
Tbody
,
Tr
,
Th
,
Td
,
TableContainer
,
Flex
}
from
'@chakra-ui/react'
;
import
{
BillTypeMap
}
from
'@/constants/user'
;
import
{
BillTypeMap
}
from
'@/constants/user'
;
import
{
getUserBills
}
from
'@/api/user'
;
import
{
getUserBills
}
from
'@/api/user'
;
import
type
{
UserBillType
}
from
'@/types/user'
;
import
type
{
UserBillType
}
from
'@/types/user'
;
...
@@ -48,9 +48,9 @@ const BillTable = () => {
...
@@ -48,9 +48,9 @@ const BillTable = () => {
<
Loading
loading=
{
isLoading
}
fixed=
{
false
}
/>
<
Loading
loading=
{
isLoading
}
fixed=
{
false
}
/>
</
TableContainer
>
</
TableContainer
>
<
Box
mt=
{
4
}
mr=
{
4
}
textAlign=
{
'
end'
}
>
<
Flex
mt=
{
4
}
px=
{
4
}
justifyContent=
{
'flex-
end'
}
>
<
Pagination
/>
<
Pagination
/>
</
Bo
x
>
</
Fle
x
>
</
Card
>
</
Card
>
);
);
};
};
...
...
src/pages/promotion/index.tsx
View file @
4dc541e0
...
@@ -139,9 +139,9 @@ const OpenApi = () => {
...
@@ -139,9 +139,9 @@ const OpenApi = () => {
<
Loading
loading=
{
isLoading
}
fixed=
{
false
}
/>
<
Loading
loading=
{
isLoading
}
fixed=
{
false
}
/>
</
TableContainer
>
</
TableContainer
>
<
Box
mt=
{
4
}
mr=
{
4
}
textAlign=
{
'
end'
}
>
<
Flex
mt=
{
4
}
px=
{
4
}
justifyContent=
{
'flex-
end'
}
>
<
Pagination
/>
<
Pagination
/>
</
Bo
x
>
</
Fle
x
>
</
Card
>
</
Card
>
<
Modal
isOpen=
{
isOpenWithdraw
}
onClose=
{
onCloseWithdraw
}
>
<
Modal
isOpen=
{
isOpenWithdraw
}
onClose=
{
onCloseWithdraw
}
>
<
ModalOverlay
/>
<
ModalOverlay
/>
...
...
src/service/plugins/searchKb.ts
View file @
4dc541e0
...
@@ -23,10 +23,10 @@ export const searchKb = async ({
...
@@ -23,10 +23,10 @@ export const searchKb = async ({
similarity
?:
number
;
similarity
?:
number
;
}):
Promise
<
{
}):
Promise
<
{
code
:
200
|
201
;
code
:
200
|
201
;
searchPrompt
?
:
{
searchPrompt
s
:
{
obj
:
`
${
ChatRoleEnum
}
`
;
obj
:
ChatRoleEnum
;
value
:
string
;
value
:
string
;
};
}
[]
;
}
>
=>
{
}
>
=>
{
async
function
search
(
textArr
:
string
[]
=
[])
{
async
function
search
(
textArr
:
string
[]
=
[])
{
// 获取提示词的向量
// 获取提示词的向量
...
@@ -85,17 +85,38 @@ export const searchKb = async ({
...
@@ -85,17 +85,38 @@ export const searchKb = async ({
};
};
const
filterRate
=
filterRateMap
[
systemPrompts
.
length
]
||
filterRateMap
[
0
];
const
filterRate
=
filterRateMap
[
systemPrompts
.
length
]
||
filterRateMap
[
0
];
// count fixed system prompt
// 计算固定提示词的 token 数量
const
fixedSystemPrompt
=
`
const
fixedPrompts
=
[
${
model
.
chat
.
systemPrompt
}
...(
model
.
chat
.
systemPrompt
${
?
[
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
?
'不回答知识库外的内容.'
:
''
{
}
obj
:
ChatRoleEnum
.
System
,
知识库内容为:`
;
value
:
model
.
chat
.
systemPrompt
}
]
:
[]),
...(
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
?
[
{
obj
:
ChatRoleEnum
.
System
,
value
:
`知识库是关于"
${
model
.
name
}
"的内容,根据知识库内容回答问题.`
}
]
:
[
{
obj
:
ChatRoleEnum
.
System
,
value
:
`我们来玩问答游戏,规则为:
1.你忘记你已有的知识
2.你只能回答关于"
${
model
.
name
}
"的问题
3.你只能从知识库中选择内容进行回答
4.如果问题不在知识库中,你会回答"我不知道。"
务必遵守规则`
}
])
];
const
fixedSystemTokens
=
modelToolMap
[
model
.
chat
.
chatModel
].
countTokens
({
const
fixedSystemTokens
=
modelToolMap
[
model
.
chat
.
chatModel
].
countTokens
({
messages
:
[{
obj
:
'System'
,
value
:
fixedSystemPrompt
}]
messages
:
fixedPrompts
});
});
const
maxTokens
=
modelConstantsData
.
systemMaxToken
-
fixedSystemTokens
;
const
maxTokens
=
modelConstantsData
.
systemMaxToken
-
fixedSystemTokens
;
const
filterSystemPrompt
=
filterRate
const
filterSystemPrompt
=
filterRate
...
@@ -111,31 +132,38 @@ ${
...
@@ -111,31 +132,38 @@ ${
if
(
!
filterSystemPrompt
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
if
(
!
filterSystemPrompt
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
return
{
return
{
code
:
201
,
code
:
201
,
searchPrompt
:
{
searchPrompts
:
[
obj
:
ChatRoleEnum
.
AI
,
{
obj
:
ChatRoleEnum
.
System
,
value
:
'对不起,你的问题不在知识库中。'
value
:
'对不起,你的问题不在知识库中。'
}
}
]
};
};
}
}
/* 高相似度+无上下文,不添加额外知识,仅用系统提示词 */
/* 高相似度+无上下文,不添加额外知识,仅用系统提示词 */
if
(
!
filterSystemPrompt
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
)
{
if
(
!
filterSystemPrompt
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
)
{
return
{
return
{
code
:
200
,
code
:
200
,
searchPrompt
:
model
.
chat
.
systemPrompt
searchPrompts
:
model
.
chat
.
systemPrompt
?
{
?
[
{
obj
:
ChatRoleEnum
.
System
,
obj
:
ChatRoleEnum
.
System
,
value
:
model
.
chat
.
systemPrompt
value
:
model
.
chat
.
systemPrompt
}
}
:
undefined
]
:
[]
};
};
}
}
/* 有匹配 */
/* 有匹配 */
return
{
return
{
code
:
200
,
code
:
200
,
searchPrompt
:
{
searchPrompts
:
[
{
obj
:
ChatRoleEnum
.
System
,
obj
:
ChatRoleEnum
.
System
,
value
:
`
${
fixedSystemPrompt
}
'
${
filterSystemPrompt
}
'`
value
:
`知识库:'
${
filterSystemPrompt
}
'`
}
},
...
fixedPrompts
]
};
};
};
};
src/service/utils/chat/claude.ts
View file @
4dc541e0
...
@@ -27,7 +27,7 @@ export const lafClaudChat = async ({
...
@@ -27,7 +27,7 @@ export const lafClaudChat = async ({
.
join
(
'\n'
);
.
join
(
'\n'
);
const
systemPromptText
=
systemPrompt
?
`\n知识库内容:'
${
systemPrompt
}
'\n`
:
''
;
const
systemPromptText
=
systemPrompt
?
`\n知识库内容:'
${
systemPrompt
}
'\n`
:
''
;
const
prompt
=
`
${
systemPromptText
}
我的问题
:'
${
messages
[
messages
.
length
-
1
].
value
}
'`
;
const
prompt
=
`
${
systemPromptText
}
\n我的问题是
:'
${
messages
[
messages
.
length
-
1
].
value
}
'`
;
const
lafResponse
=
await
axios
.
post
(
const
lafResponse
=
await
axios
.
post
(
'https://hnvacz.laf.run/claude-gpt'
,
'https://hnvacz.laf.run/claude-gpt'
,
...
...
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