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
39869bc4
authored
Apr 30, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: search kb model
parent
f109f1cf
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
84 additions
and
150 deletions
+84
-150
src/pages/api/chat/chat.ts
+7
-40
src/pages/api/openapi/chat/chat.ts
+6
-42
src/pages/api/openapi/chat/lafGpt.ts
+3
-15
src/pages/api/openapi/chat/vectorGpt.ts
+6
-45
src/service/tools/searchKb.ts
+62
-8
No files found.
src/pages/api/chat/chat.ts
View file @
39869bc4
...
@@ -57,62 +57,29 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -57,62 +57,29 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 使用了知识库搜索
// 使用了知识库搜索
if
(
model
.
chat
.
useKb
)
{
if
(
model
.
chat
.
useKb
)
{
const
{
systemPrompts
}
=
await
searchKb_openai
({
const
{
code
,
searchPrompt
}
=
await
searchKb_openai
({
apiKey
:
userApiKey
||
systemKey
,
apiKey
:
userApiKey
||
systemKey
,
isPay
:
!
userApiKey
,
isPay
:
!
userApiKey
,
text
:
prompt
.
value
,
text
:
prompt
.
value
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
,
model
Id
,
model
,
userId
userId
});
});
// filter system prompt
// search result is empty
if
(
if
(
code
===
201
)
{
systemPrompts
.
length
===
0
&&
return
res
.
send
(
searchPrompt
?.
value
);
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
return
res
.
send
(
'对不起,你的问题不在知识库中。'
);
}
}
/* 高相似度+无上下文,不添加额外知识,仅用系统提示词 */
if
(
systemPrompts
.
length
===
0
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
)
{
prompts
.
unshift
({
obj
:
'SYSTEM'
,
value
:
model
.
chat
.
systemPrompt
});
}
else
{
// 有匹配情况下,system 添加知识库内容。
// 系统提示词过滤,最多 2500 tokens
const
filterSystemPrompt
=
systemPromptFilter
({
model
:
model
.
chat
.
chatModel
,
prompts
:
systemPrompts
,
maxTokens
:
2500
});
prompts
.
unshift
({
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
obj
:
'SYSTEM'
,
value
:
`
${
model
.
chat
.
systemPrompt
}
${
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
?
`不回答知识库外的内容.`
:
''
}
知识库内容为:
${
filterSystemPrompt
}
'
`
});
}
}
else
{
}
else
{
// 没有用知识库搜索,仅用系统提示词
// 没有用知识库搜索,仅用系统提示词
if
(
model
.
chat
.
systemPrompt
)
{
model
.
chat
.
systemPrompt
&&
prompts
.
unshift
({
prompts
.
unshift
({
obj
:
'SYSTEM'
,
obj
:
'SYSTEM'
,
value
:
model
.
chat
.
systemPrompt
value
:
model
.
chat
.
systemPrompt
});
});
}
}
}
// 控制总 tokens 数量,防止超出
// 控制总 tokens 数量,防止超出
const
filterPrompts
=
openaiChatFilter
({
const
filterPrompts
=
openaiChatFilter
({
...
...
src/pages/api/openapi/chat/chat.ts
View file @
39869bc4
...
@@ -67,57 +67,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -67,57 +67,21 @@ 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
{
systemPrompts
}
=
await
searchKb_openai
({
const
{
code
,
searchPrompt
}
=
await
searchKb_openai
({
apiKey
,
apiKey
,
isPay
:
true
,
isPay
:
true
,
text
:
prompts
[
prompts
.
length
-
1
].
value
,
text
:
prompts
[
prompts
.
length
-
1
].
value
,
similarity
,
similarity
,
model
Id
,
model
,
userId
userId
});
});
// filter system prompt
// search result is empty
if
(
if
(
code
===
201
)
{
systemPrompts
.
length
===
0
&&
return
res
.
send
(
searchPrompt
?.
value
);
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
return
jsonRes
(
res
,
{
code
:
500
,
message
:
'对不起,你的问题不在知识库中。'
,
data
:
'对不起,你的问题不在知识库中。'
});
}
}
/* 高相似度+无上下文,不添加额外知识,仅用系统提示词 */
if
(
systemPrompts
.
length
===
0
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
)
{
prompts
.
unshift
({
obj
:
'SYSTEM'
,
value
:
model
.
chat
.
systemPrompt
});
}
else
{
// 有匹配情况下,system 添加知识库内容。
// 系统提示词过滤,最多 2500 tokens
const
filterSystemPrompt
=
systemPromptFilter
({
model
:
model
.
chat
.
chatModel
,
prompts
:
systemPrompts
,
maxTokens
:
2500
});
prompts
.
unshift
({
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
obj
:
'SYSTEM'
,
value
:
`
${
model
.
chat
.
systemPrompt
}
${
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
?
`不回答知识库外的内容.`
:
''
}
知识库内容为:
${
filterSystemPrompt
}
'
`
});
}
}
else
{
}
else
{
// 没有用知识库搜索,仅用系统提示词
// 没有用知识库搜索,仅用系统提示词
if
(
model
.
chat
.
systemPrompt
)
{
if
(
model
.
chat
.
systemPrompt
)
{
...
...
src/pages/api/openapi/chat/lafGpt.ts
View file @
39869bc4
...
@@ -131,26 +131,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -131,26 +131,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
prompts
=
[
prompt
];
const
prompts
=
[
prompt
];
// 获取向量匹配到的提示词
// 获取向量匹配到的提示词
const
{
s
ystemPrompts
}
=
await
searchKb_openai
({
const
{
s
earchPrompt
}
=
await
searchKb_openai
({
isPay
:
true
,
isPay
:
true
,
apiKey
,
apiKey
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
,
text
:
prompt
.
value
,
text
:
prompt
.
value
,
model
Id
,
model
,
userId
userId
});
});
// system 筛选,最多 2500 tokens
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
const
filterSystemPrompt
=
systemPromptFilter
({
model
:
model
.
chat
.
chatModel
,
prompts
:
systemPrompts
,
maxTokens
:
2500
});
prompts
.
unshift
({
obj
:
'SYSTEM'
,
value
:
`
${
model
.
chat
.
systemPrompt
}
知识库是最新的,下面是知识库内容:
${
filterSystemPrompt
}
`
});
// 控制上下文 tokens 数量,防止超出
// 控制上下文 tokens 数量,防止超出
const
filterPrompts
=
openaiChatFilter
({
const
filterPrompts
=
openaiChatFilter
({
...
@@ -181,8 +171,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -181,8 +171,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}
);
);
console
.
log
(
'code response. time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
let
responseContent
=
''
;
let
responseContent
=
''
;
if
(
isStream
)
{
if
(
isStream
)
{
...
...
src/pages/api/openapi/chat/vectorGpt.ts
View file @
39869bc4
...
@@ -68,60 +68,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -68,60 +68,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}
// 获取向量匹配到的提示词
// 获取向量匹配到的提示词
const
{
systemPrompts
}
=
await
searchKb_openai
({
const
{
code
,
searchPrompt
}
=
await
searchKb_openai
({
isPay
:
true
,
isPay
:
true
,
apiKey
,
apiKey
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
,
text
:
prompts
[
prompts
.
length
-
1
].
value
,
text
:
prompts
[
prompts
.
length
-
1
].
value
,
model
Id
,
model
,
userId
userId
});
});
// system 合并
// search result is empty
if
(
prompts
[
0
].
obj
===
'SYSTEM'
)
{
if
(
code
===
201
)
{
systemPrompts
.
unshift
(
prompts
.
shift
()?.
value
||
''
);
return
res
.
send
(
searchPrompt
?.
value
);
}
/* 高相似度+退出,无法匹配时直接退出 */
if
(
systemPrompts
.
length
===
0
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
return
jsonRes
(
res
,
{
code
:
500
,
message
:
'对不起,你的问题不在知识库中。'
,
data
:
'对不起,你的问题不在知识库中。'
});
}
}
/* 高相似度+无上下文,不添加额外知识 */
if
(
systemPrompts
.
length
===
0
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
)
{
prompts
.
unshift
({
obj
:
'SYSTEM'
,
value
:
model
.
chat
.
systemPrompt
});
}
else
{
// 有匹配或者低匹配度模式情况下,添加知识库内容。
// 系统提示词过滤,最多 2500 tokens
const
systemPrompt
=
systemPromptFilter
({
model
:
model
.
chat
.
chatModel
,
prompts
:
systemPrompts
,
maxTokens
:
2500
});
prompts
.
unshift
({
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
obj
:
'SYSTEM'
,
value
:
`
${
model
.
chat
.
systemPrompt
}
${
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
?
`不回答知识库外的内容.`
:
''
}
知识库内容为:
${
systemPrompt
}
'
`
});
}
// 控制在 tokens 数量,防止超出
// 控制在 tokens 数量,防止超出
const
filterPrompts
=
openaiChatFilter
({
const
filterPrompts
=
openaiChatFilter
({
...
...
src/service/tools/searchKb.ts
View file @
39869bc4
import
{
openaiCreateEmbedding
}
from
'../utils/openai'
;
import
{
openaiCreateEmbedding
}
from
'../utils/openai'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
ModelDataStatusEnum
}
from
'@/constants/model'
;
import
{
ModelDataStatusEnum
,
ModelVectorSearchModeEnum
}
from
'@/constants/model'
;
import
{
ModelSchema
}
from
'@/types/mongoSchema'
;
import
{
systemPromptFilter
}
from
'../utils/tools'
;
/**
/**
* use openai embedding search kb
* use openai embedding search kb
...
@@ -10,16 +12,22 @@ export const searchKb_openai = async ({
...
@@ -10,16 +12,22 @@ export const searchKb_openai = async ({
isPay
,
isPay
,
text
,
text
,
similarity
,
similarity
,
model
Id
,
model
,
userId
userId
}:
{
}:
{
apiKey
:
string
;
apiKey
:
string
;
isPay
:
boolean
;
isPay
:
boolean
;
text
:
string
;
text
:
string
;
model
Id
:
string
;
model
:
ModelSchema
;
userId
:
string
;
userId
:
string
;
similarity
:
number
;
similarity
:
number
;
})
=>
{
}):
Promise
<
{
code
:
200
|
201
;
searchPrompt
?:
{
obj
:
'Human'
|
'AI'
|
'SYSTEM'
;
value
:
string
;
};
}
>
=>
{
// 获取提示词的向量
// 获取提示词的向量
const
{
vector
:
promptVector
}
=
await
openaiCreateEmbedding
({
const
{
vector
:
promptVector
}
=
await
openaiCreateEmbedding
({
isPay
,
isPay
,
...
@@ -28,12 +36,12 @@ export const searchKb_openai = async ({
...
@@ -28,12 +36,12 @@ export const searchKb_openai = async ({
text
text
});
});
const
vectorSearch
=
await
PgClient
.
select
<
{
id
:
string
;
q
:
string
;
a
:
string
}
>
(
'modelData'
,
{
const
vectorSearch
=
await
PgClient
.
select
<
{
q
:
string
;
a
:
string
}
>
(
'modelData'
,
{
fields
:
[
'
id'
,
'
q'
,
'a'
],
fields
:
[
'q'
,
'a'
],
where
:
[
where
:
[
[
'status'
,
ModelDataStatusEnum
.
ready
],
[
'status'
,
ModelDataStatusEnum
.
ready
],
'AND'
,
'AND'
,
[
'model_id'
,
model
I
d
],
[
'model_id'
,
model
.
_i
d
],
'AND'
,
'AND'
,
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
],
],
...
@@ -43,5 +51,51 @@ export const searchKb_openai = async ({
...
@@ -43,5 +51,51 @@ export const searchKb_openai = async ({
const
systemPrompts
:
string
[]
=
vectorSearch
.
rows
.
map
((
item
)
=>
`
${
item
.
q
}
\n
${
item
.
a
}
`
);
const
systemPrompts
:
string
[]
=
vectorSearch
.
rows
.
map
((
item
)
=>
`
${
item
.
q
}
\n
${
item
.
a
}
`
);
return
{
systemPrompts
};
// filter system prompt
if
(
systemPrompts
.
length
===
0
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
return
{
code
:
201
,
searchPrompt
:
{
obj
:
'AI'
,
value
:
'对不起,你的问题不在知识库中。'
}
};
}
/* 高相似度+无上下文,不添加额外知识,仅用系统提示词 */
if
(
systemPrompts
.
length
===
0
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
)
{
return
{
code
:
200
,
searchPrompt
:
model
.
chat
.
systemPrompt
?
{
obj
:
'SYSTEM'
,
value
:
model
.
chat
.
systemPrompt
}
:
undefined
};
}
// 有匹配情况下,system 添加知识库内容。
// 系统提示词过滤,最多 2500 tokens
const
filterSystemPrompt
=
systemPromptFilter
({
model
:
model
.
chat
.
chatModel
,
prompts
:
systemPrompts
,
maxTokens
:
2500
});
return
{
code
:
200
,
searchPrompt
:
{
obj
:
'SYSTEM'
,
value
:
`
${
model
.
chat
.
systemPrompt
}
${
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
?
`不回答知识库外的内容.`
:
''
}
知识库内容为:
${
filterSystemPrompt
}
'
`
}
};
};
};
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