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
1409916b
authored
Apr 06, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 知识库范围
parent
fc7edcb5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
34 deletions
+16
-34
src/pages/api/chat/vectorGpt.ts
+9
-15
src/pages/api/openapi/lafGpt.ts
+7
-19
No files found.
src/pages/api/chat/vectorGpt.ts
View file @
1409916b
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
createParser
,
ParsedEvent
,
ReconnectInterval
}
from
'eventsource-parser'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
getOpenAIApi
,
authChat
}
from
'@/service/utils/chat'
;
import
{
authChat
}
from
'@/service/utils/chat'
;
import
{
httpsAgent
,
openaiChatFilter
,
systemPromptFilter
}
from
'@/service/utils/tools'
;
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
...
...
@@ -13,8 +12,7 @@ import { pushChatBill } from '@/service/events/pushBill';
import
{
connectRedis
}
from
'@/service/redis'
;
import
{
VecModelDataPrefix
}
from
'@/constants/redis'
;
import
{
vectorToBuffer
}
from
'@/utils/tools'
;
import
{
openaiCreateEmbedding
}
from
'@/service/utils/openai'
;
import
{
gpt35StreamResponse
}
from
'@/service/utils/openai'
;
import
{
openaiCreateEmbedding
,
gpt35StreamResponse
}
from
'@/service/utils/openai'
;
/* 发送提示词 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
...
...
@@ -73,7 +71,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
`@modelId:{
${
String
(
chat
.
modelId
.
_id
)}
} @vector:[VECTOR_RANGE 0.24 $blob]=>{$YIELD_DISTANCE_AS: score}`
,
// `@modelId:{${String(chat.modelId._id)}}=>[KNN 10 @vector $blob AS score]`,
'RETURN'
,
'1'
,
'text'
,
...
...
@@ -90,17 +87,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
'2'
]);
const
formatRedisPrompt
:
string
[]
=
[];
// 格式化响应值,获取 qa
const
formatRedisPrompt
=
[
2
,
4
,
6
,
8
,
10
,
12
,
14
,
16
,
18
,
20
]
.
map
((
i
)
=>
{
if
(
!
redisData
[
i
])
return
''
;
const
text
=
(
redisData
[
i
][
1
]
as
string
)
||
''
;
if
(
!
text
)
return
''
;
return
text
;
})
.
filter
((
item
)
=>
item
);
for
(
let
i
=
2
;
i
<
42
;
i
+=
2
)
{
const
text
=
redisData
[
i
]?.[
1
];
if
(
text
)
{
formatRedisPrompt
.
push
(
text
);
}
}
if
(
formatRedisPrompt
.
length
===
0
)
{
throw
new
Error
(
'对不起,我没有找到你的问题'
);
...
...
src/pages/api/openapi/lafGpt.ts
View file @
1409916b
...
...
@@ -132,9 +132,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
redisData
:
any
[]
=
await
redis
.
sendCommand
([
'FT.SEARCH'
,
`idx:
${
VecModelDataPrefix
}
:hash`
,
`@modelId:{
${
String
(
model
.
_id
)}
} @vector:[VECTOR_RANGE 0.25 $blob]=>{$YIELD_DISTANCE_AS: score}`
,
`@modelId:{
${
String
(
model
.
_id
)}
}=>[KNN 20 @vector $blob AS score]`
,
'RETURN'
,
'1'
,
'text'
,
...
...
@@ -144,27 +142,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
'2'
,
'blob'
,
vectorToBuffer
(
promptVector
),
'LIMIT'
,
'0'
,
'20'
,
'DIALECT'
,
'2'
]);
// 格式化响应值,获取 qa
const
formatRedisPrompt
=
[
2
,
4
,
6
,
8
,
10
,
12
,
14
,
16
,
18
,
20
]
.
map
((
i
)
=>
{
if
(
!
redisData
[
i
])
return
''
;
const
text
=
(
redisData
[
i
][
1
]
as
string
)
||
''
;
if
(
!
text
)
return
''
;
return
text
;
})
.
filter
((
item
)
=>
item
);
if
(
formatRedisPrompt
.
length
===
0
)
{
throw
new
Error
(
'对不起,我没有找到你的问题'
);
const
formatRedisPrompt
:
string
[]
=
[];
for
(
let
i
=
2
;
i
<
42
;
i
+=
2
)
{
const
text
=
redisData
[
i
]?.[
1
];
if
(
text
)
{
formatRedisPrompt
.
push
(
text
);
}
}
// textArr 筛选,最多 3200 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