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
c31d247f
authored
Apr 12, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 知识库openapi
parent
e903eb5b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
6 deletions
+23
-6
Makefile
+1
-1
src/pages/api/openapi/chat/vectorGpt.ts
+22
-5
No files found.
Makefile
View file @
c31d247f
...
@@ -34,7 +34,7 @@ run: ## Run a dev service from host.
...
@@ -34,7 +34,7 @@ run: ## Run a dev service from host.
.PHONY
:
docker-build
.PHONY
:
docker-build
docker-build
:
##
Build docker image with the desktop-frontend.
docker-build
:
##
Build docker image with the desktop-frontend.
docker build
-t
c121914yu/fast-gpt:latest .
docker build
-t
c121914yu/fast-gpt:latest
.
--network
host
--build-arg
HTTP_PROXY
=
http://127.0.0.1:7890
--build-arg
HTTPS_PROXY
=
http://127.0.0.1:7890
##@ Deployment
##@ Deployment
...
...
src/pages/api/openapi/chat/vectorGpt.ts
View file @
c31d247f
...
@@ -10,7 +10,7 @@ import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } fr
...
@@ -10,7 +10,7 @@ import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } fr
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
PassThrough
}
from
'stream'
;
import
{
PassThrough
}
from
'stream'
;
import
{
modelList
}
from
'@/constants/model'
;
import
{
modelList
,
ModelVectorSearchModeMap
,
ModelVectorSearchModeEnum
}
from
'@/constants/model'
;
import
{
pushChatBill
}
from
'@/service/events/pushBill'
;
import
{
pushChatBill
}
from
'@/service/events/pushBill'
;
import
{
connectRedis
}
from
'@/service/redis'
;
import
{
connectRedis
}
from
'@/service/redis'
;
import
{
VecModelDataPrefix
}
from
'@/constants/redis'
;
import
{
VecModelDataPrefix
}
from
'@/constants/redis'
;
...
@@ -85,10 +85,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -85,10 +85,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
});
// 搜索系统提示词, 按相似度从 redis 中搜出相关的 q 和 text
// 搜索系统提示词, 按相似度从 redis 中搜出相关的 q 和 text
const
similarity
=
ModelVectorSearchModeMap
[
model
.
search
.
mode
]?.
similarity
||
0.22
;
// 搜索系统提示词, 按相似度从 redis 中搜出相关的 q 和 text
const
redisData
:
any
[]
=
await
redis
.
sendCommand
([
const
redisData
:
any
[]
=
await
redis
.
sendCommand
([
'FT.SEARCH'
,
'FT.SEARCH'
,
`idx:
${
VecModelDataPrefix
}
:hash`
,
`idx:
${
VecModelDataPrefix
}
:hash`
,
`@modelId:{
${
modelId
}
} @vector:[VECTOR_RANGE
0.22
$blob]=>{$YIELD_DISTANCE_AS: score}`
,
`@modelId:{
${
modelId
}
} @vector:[VECTOR_RANGE
${
similarity
}
$blob]=>{$YIELD_DISTANCE_AS: score}`
,
'RETURN'
,
'RETURN'
,
'1'
,
'1'
,
'text'
,
'text'
,
...
@@ -120,7 +122,24 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -120,7 +122,24 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
formatRedisPrompt
.
unshift
(
prompts
.
shift
()?.
value
||
''
);
formatRedisPrompt
.
unshift
(
prompts
.
shift
()?.
value
||
''
);
}
}
if
(
formatRedisPrompt
.
length
>
0
)
{
/* 高相似度+退出,无法匹配时直接退出 */
if
(
formatRedisPrompt
.
length
===
0
&&
model
.
search
.
mode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
return
res
.
send
(
'对不起,你的问题不在知识库中。'
);
}
/* 高相似度+无上下文,不添加额外知识 */
if
(
formatRedisPrompt
.
length
===
0
&&
model
.
search
.
mode
===
ModelVectorSearchModeEnum
.
noContext
)
{
prompts
.
unshift
({
obj
:
'SYSTEM'
,
value
:
model
.
systemPrompt
});
}
else
{
// 有匹配或者低匹配度模式情况下,添加知识库内容。
// 系统提示词过滤,最多 2800 tokens
// 系统提示词过滤,最多 2800 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2800
);
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2800
);
...
@@ -130,8 +149,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -130,8 +149,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
'YYYY/MM/DD HH:mm:ss'
'YYYY/MM/DD HH:mm:ss'
)}
${
systemPrompt
}
"`
)}
${
systemPrompt
}
"`
});
});
}
else
{
return
res
.
send
(
'对不起,你的问题不在知识库中。'
);
}
}
// 控制在 tokens 数量,防止超出
// 控制在 tokens 数量,防止超出
...
...
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