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
a98c56f9
authored
Apr 19, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: pg
parent
1e5714da
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
18 additions
and
68 deletions
+18
-68
src/pages/api/chat/vectorGpt.ts
+3
-9
src/pages/api/openapi/chat/lafGpt.ts
+2
-2
src/pages/api/openapi/chat/vectorGpt.ts
+3
-9
src/service/events/pushBill.ts
+9
-3
src/service/mongo.ts
+1
-1
src/service/redis.ts
+0
-44
No files found.
src/pages/api/chat/vectorGpt.ts
View file @
a98c56f9
...
...
@@ -66,14 +66,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
similarity
=
ModelVectorSearchModeMap
[
model
.
search
.
mode
]?.
similarity
||
0.22
;
const
vectorSearch
=
await
PgClient
.
select
<
{
id
:
string
;
q
:
string
;
a
:
string
}
>
(
'modelData'
,
{
fields
:
[
'id'
,
'q'
,
'a'
],
where
:
[[
'model_id'
,
model
.
_id
],
'AND'
,
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
],
order
:
[{
field
:
'vector'
,
mode
:
`<=> '[
${
promptVector
}
]'`
}],
where
:
[
[
'model_id'
,
model
.
_id
],
'AND'
,
[
'user_id'
,
userId
],
'AND'
,
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
],
limit
:
30
});
...
...
@@ -97,8 +91,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
}
else
{
// 有匹配情况下,添加知识库内容。
// 系统提示词过滤,最多 2
5
00 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2
5
00
);
// 系统提示词过滤,最多 2
0
00 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2
0
00
);
prompts
.
unshift
({
obj
:
'SYSTEM'
,
...
...
src/pages/api/openapi/chat/lafGpt.ts
View file @
a98c56f9
...
...
@@ -163,8 +163,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
formatRedisPrompt
:
string
[]
=
vectorSearch
.
rows
.
map
((
item
)
=>
`
${
item
.
q
}
\n
${
item
.
a
}
`
);
// textArr 筛选,最多
30
00 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
30
00
);
// textArr 筛选,最多
25
00 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
25
00
);
prompts
.
unshift
({
obj
:
'SYSTEM'
,
...
...
src/pages/api/openapi/chat/vectorGpt.ts
View file @
a98c56f9
...
...
@@ -80,14 +80,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
similarity
=
ModelVectorSearchModeMap
[
model
.
search
.
mode
]?.
similarity
||
0.22
;
const
vectorSearch
=
await
PgClient
.
select
<
{
id
:
string
;
q
:
string
;
a
:
string
}
>
(
'modelData'
,
{
fields
:
[
'id'
,
'q'
,
'a'
],
where
:
[[
'model_id'
,
model
.
_id
],
'AND'
,
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
],
order
:
[{
field
:
'vector'
,
mode
:
`<=> '[
${
promptVector
}
]'`
}],
where
:
[
[
'model_id'
,
model
.
_id
],
'AND'
,
[
'user_id'
,
userId
],
'AND'
,
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
],
limit
:
30
});
...
...
@@ -116,8 +110,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
}
else
{
// 有匹配或者低匹配度模式情况下,添加知识库内容。
// 系统提示词过滤,最多 2
5
00 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2
5
00
);
// 系统提示词过滤,最多 2
0
00 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2
0
00
);
prompts
.
unshift
({
obj
:
'SYSTEM'
,
...
...
src/service/events/pushBill.ts
View file @
a98c56f9
...
...
@@ -23,7 +23,9 @@ export const pushChatBill = async ({
// 计算 token 数量
const
tokens
=
Math
.
floor
(
encode
(
text
).
length
*
0.75
);
console
.
log
(
`chat generate success. text len:
${
text
.
length
}
. token len:
${
tokens
}
`
);
console
.
log
(
`chat generate success. text len:
${
text
.
length
}
. token len:
${
tokens
}
. pay:
${
isPay
}
`
);
if
(
isPay
)
{
await
connectToDatabase
();
...
...
@@ -79,7 +81,9 @@ export const pushSplitDataBill = async ({
let
billId
;
try
{
console
.
log
(
`splitData generate success. text len:
${
text
.
length
}
. token len:
${
tokenLen
}
`
);
console
.
log
(
`splitData generate success. text len:
${
text
.
length
}
. token len:
${
tokenLen
}
. pay:
${
isPay
}
`
);
if
(
isPay
)
{
try
{
...
...
@@ -130,7 +134,9 @@ export const pushGenerateVectorBill = async ({
let
billId
;
try
{
console
.
log
(
`vector generate success. text len:
${
text
.
length
}
. token len:
${
tokenLen
}
`
);
console
.
log
(
`vector generate success. text len:
${
text
.
length
}
. token len:
${
tokenLen
}
. pay:
${
isPay
}
`
);
if
(
isPay
)
{
try
{
...
...
src/service/mongo.ts
View file @
a98c56f9
...
...
@@ -16,7 +16,7 @@ export async function connectToDatabase(): Promise<void> {
mongoose
.
set
(
'strictQuery'
,
true
);
global
.
mongodb
=
await
mongoose
.
connect
(
process
.
env
.
MONGODB_URI
as
string
,
{
bufferCommands
:
true
,
dbName
:
process
.
env
.
NODE_ENV
===
'development'
?
'doc_gpt_test'
:
'doc_gpt'
,
dbName
:
process
.
env
.
MONGODB_NAME
,
maxPoolSize
:
5
,
minPoolSize
:
1
,
maxConnecting
:
5
...
...
src/service/redis.ts
deleted
100644 → 0
View file @
1e5714da
import
{
createClient
}
from
'redis'
;
import
{
customAlphabet
}
from
'nanoid'
;
const
nanoid
=
customAlphabet
(
'abcdefghijklmnopqrstuvwxyz1234567890'
,
10
);
export
const
connectRedis
=
async
()
=>
{
// 断开了,重连
if
(
global
.
redisClient
&&
!
global
.
redisClient
.
isOpen
)
{
await
global
.
redisClient
.
disconnect
();
}
else
if
(
global
.
redisClient
)
{
// 没断开,不再连接
return
global
.
redisClient
;
}
try
{
global
.
redisClient
=
createClient
({
url
:
process
.
env
.
REDIS_URL
});
global
.
redisClient
.
on
(
'error'
,
(
err
)
=>
{
console
.
log
(
'Redis Client Error'
,
err
);
global
.
redisClient
=
null
;
});
global
.
redisClient
.
on
(
'end'
,
()
=>
{
global
.
redisClient
=
null
;
});
global
.
redisClient
.
on
(
'ready'
,
()
=>
{
console
.
log
(
'redis connected'
);
});
await
global
.
redisClient
.
connect
();
await
global
.
redisClient
.
SELECT
(
0
);
return
global
.
redisClient
;
}
catch
(
error
)
{
console
.
log
(
error
,
'=='
);
global
.
redisClient
=
null
;
return
Promise
.
reject
(
'redis 连接失败'
);
}
};
export
const
getKey
=
(
prefix
=
''
)
=>
{
return
`
${
prefix
}
:
${
nanoid
()}
`
;
};
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