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
1fe5cd75
authored
Apr 11, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 知识库匹配模式
parent
488e2f47
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
12 deletions
+18
-12
src/pages/api/chat/vectorGpt.ts
+6
-3
src/pages/api/openapi/chat/vectorGpt.ts
+10
-7
src/pages/model/detail/components/ModelDataCard.tsx
+1
-1
src/service/utils/tools.ts
+1
-1
No files found.
src/pages/api/chat/vectorGpt.ts
View file @
1fe5cd75
...
...
@@ -13,6 +13,7 @@ import { connectRedis } from '@/service/redis';
import
{
VecModelDataPrefix
}
from
'@/constants/redis'
;
import
{
vectorToBuffer
}
from
'@/utils/tools'
;
import
{
openaiCreateEmbedding
,
gpt35StreamResponse
}
from
'@/service/utils/openai'
;
import
dayjs
from
'dayjs'
;
/* 发送提示词 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
...
...
@@ -70,7 +71,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
`idx:
${
VecModelDataPrefix
}
:hash`
,
`@modelId:{
${
String
(
chat
.
modelId
.
_id
)}
} @vector:[VECTOR_RANGE 0.2
4
$blob]=>{$YIELD_DISTANCE_AS: score}`
,
)}
} @vector:[VECTOR_RANGE 0.2
2
$blob]=>{$YIELD_DISTANCE_AS: score}`
,
'RETURN'
,
'1'
,
'text'
,
...
...
@@ -97,12 +98,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
if
(
formatRedisPrompt
.
length
>
0
)
{
//
textArr 筛选
,最多 2800 tokens
//
系统提示词过滤
,最多 2800 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2800
);
prompts
.
unshift
({
obj
:
'SYSTEM'
,
value
:
`
${
model
.
systemPrompt
}
知识库内容是最新的,知识库内容为: "
${
systemPrompt
}
"`
value
:
`
${
model
.
systemPrompt
}
用知识库内容回答,知识库内容为: "当前时间:
${
dayjs
().
format
(
'YYYY/MM/DD HH:mm:ss'
)}
${
systemPrompt
}
"`
});
}
else
{
return
res
.
send
(
'对不起,你的问题不在知识库中。'
);
...
...
src/pages/api/openapi/chat/vectorGpt.ts
View file @
1fe5cd75
...
...
@@ -16,6 +16,7 @@ import { connectRedis } from '@/service/redis';
import
{
VecModelDataPrefix
}
from
'@/constants/redis'
;
import
{
vectorToBuffer
}
from
'@/utils/tools'
;
import
{
openaiCreateEmbedding
,
gpt35StreamResponse
}
from
'@/service/utils/openai'
;
import
dayjs
from
'dayjs'
;
/* 发送提示词 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
...
...
@@ -87,7 +88,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
redisData
:
any
[]
=
await
redis
.
sendCommand
([
'FT.SEARCH'
,
`idx:
${
VecModelDataPrefix
}
:hash`
,
`@modelId:{
${
modelId
}
} @vector:[VECTOR_RANGE 0.2
4
$blob]=>{$YIELD_DISTANCE_AS: score}`
,
`@modelId:{
${
modelId
}
} @vector:[VECTOR_RANGE 0.2
2
$blob]=>{$YIELD_DISTANCE_AS: score}`
,
'RETURN'
,
'1'
,
'text'
,
...
...
@@ -114,22 +115,24 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}
if
(
formatRedisPrompt
.
length
===
0
)
{
throw
new
Error
(
'对不起,我没有找到你的问题'
);
}
// system 合并
if
(
prompts
[
0
].
obj
===
'SYSTEM'
)
{
formatRedisPrompt
.
unshift
(
prompts
.
shift
()?.
value
||
''
);
}
// 系统提示词筛选,最多 2800 tokens
if
(
formatRedisPrompt
.
length
>
0
)
{
// 系统提示词过滤,最多 2800 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2800
);
prompts
.
unshift
({
obj
:
'SYSTEM'
,
value
:
`
${
model
.
systemPrompt
}
知识库内容是最新的,知识库内容为: "
${
systemPrompt
}
"`
value
:
`
${
model
.
systemPrompt
}
用知识库内容回答,知识库内容为: "当前时间:
${
dayjs
().
format
(
'YYYY/MM/DD HH:mm:ss'
)}
${
systemPrompt
}
"`
});
}
else
{
return
res
.
send
(
'对不起,你的问题不在知识库中。'
);
}
// 控制在 tokens 数量,防止超出
const
filterPrompts
=
openaiChatFilter
(
prompts
,
modelConstantsData
.
contextMaxToken
);
...
...
src/pages/model/detail/components/ModelDataCard.tsx
View file @
1fe5cd75
...
...
@@ -152,7 +152,7 @@ const ModelDataCard = ({ model }: { model: ModelSchema }) => {
>
手动输入
</
MenuItem
>
<
MenuItem
onClick=
{
onOpenSelectFileModal
}
>
文本
内容
QA 拆分
</
MenuItem
>
<
MenuItem
onClick=
{
onOpenSelectFileModal
}
>
文本
/文件
QA 拆分
</
MenuItem
>
<
MenuItem
onClick=
{
onOpenSelectUrlModal
}
>
网站内容 QA 拆分
</
MenuItem
>
<
MenuItem
onClick=
{
onOpenSelectCsvModal
}
>
csv 问答对导入
</
MenuItem
>
</
MenuList
>
...
...
src/service/utils/tools.ts
View file @
1fe5cd75
...
...
@@ -91,7 +91,7 @@ export const openaiChatFilter = (prompts: ChatItemType[], maxTokens: number) =>
const
formatPrompts
=
prompts
.
map
((
item
)
=>
({
obj
:
item
.
obj
,
value
:
item
.
value
.
replace
(
/
[\u
3000
\u
3001
\u
ff01-
\u
ff5e
\u
3002
]
/g
,
' '
)
// 中文标点改空格
//
.replace(/[\u3000\u3001\uff01-\uff5e\u3002]/g, ' ') // 中文标点改空格
.
replace
(
/
\n
+/g
,
'\n'
)
// 连续空行
.
replace
(
/
[^\S\r\n]
+/g
,
' '
)
// 连续空白内容
.
trim
()
...
...
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