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
3a49efd4
authored
Aug 14, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pg table name
parent
c5fd5706
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
37 additions
and
22 deletions
+37
-22
client/src/constants/plugin.ts
+2
-0
client/src/pages/api/openapi/kb/delDataById.ts
+2
-1
client/src/pages/api/openapi/kb/pushData.ts
+2
-2
client/src/pages/api/openapi/kb/searchTest.ts
+2
-1
client/src/pages/api/openapi/kb/updateData.ts
+2
-1
client/src/pages/api/plugins/kb/data/exportModelData.ts
+11
-7
client/src/pages/api/plugins/kb/data/getDataById.ts
+2
-1
client/src/pages/api/plugins/kb/data/getDataList.ts
+3
-2
client/src/pages/api/plugins/kb/delete.ts
+2
-1
client/src/service/moduleDispatch/kb/search.ts
+2
-1
client/src/service/mongo.ts
+5
-4
client/src/service/pg.ts
+2
-1
No files found.
client/src/constants/plugin.ts
View file @
3a49efd4
...
@@ -6,3 +6,5 @@ export const TrainingTypeMap = {
...
@@ -6,3 +6,5 @@ export const TrainingTypeMap = {
[
TrainingModeEnum
.
qa
]:
'qa'
,
[
TrainingModeEnum
.
qa
]:
'qa'
,
[
TrainingModeEnum
.
index
]:
'index'
[
TrainingModeEnum
.
index
]:
'index'
};
};
export
const
PgTrainingTableName
=
'modeldata'
;
client/src/pages/api/openapi/kb/delDataById.ts
View file @
3a49efd4
...
@@ -3,6 +3,7 @@ import { jsonRes } from '@/service/response';
...
@@ -3,6 +3,7 @@ import { jsonRes } from '@/service/response';
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
export
default
withNextCors
(
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
export
default
withNextCors
(
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
try
{
...
@@ -17,7 +18,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
...
@@ -17,7 +18,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
// 凭证校验
// 凭证校验
const
{
userId
}
=
await
authUser
({
req
});
const
{
userId
}
=
await
authUser
({
req
});
await
PgClient
.
delete
(
'modelData'
,
{
await
PgClient
.
delete
(
PgTrainingTableName
,
{
where
:
[[
'user_id'
,
userId
],
'AND'
,
[
'id'
,
dataId
]]
where
:
[[
'user_id'
,
userId
],
'AND'
,
[
'id'
,
dataId
]]
});
});
...
...
client/src/pages/api/openapi/kb/pushData.ts
View file @
3a49efd4
...
@@ -4,7 +4,7 @@ import { connectToDatabase, TrainingData } from '@/service/mongo';
...
@@ -4,7 +4,7 @@ import { connectToDatabase, TrainingData } from '@/service/mongo';
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
authKb
}
from
'@/service/utils/auth'
;
import
{
authKb
}
from
'@/service/utils/auth'
;
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
{
TrainingModeEnum
}
from
'@/constants/plugin'
;
import
{
PgTrainingTableName
,
TrainingModeEnum
}
from
'@/constants/plugin'
;
import
{
startQueue
}
from
'@/service/utils/tools'
;
import
{
startQueue
}
from
'@/service/utils/tools'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
modelToolMap
}
from
'@/utils/plugin'
;
import
{
modelToolMap
}
from
'@/utils/plugin'
;
...
@@ -129,7 +129,7 @@ export async function pushDataToKb({
...
@@ -129,7 +129,7 @@ export async function pushDataToKb({
try
{
try
{
const
{
rows
}
=
await
PgClient
.
query
(
`
const
{
rows
}
=
await
PgClient
.
query
(
`
SELECT COUNT(*) > 0 AS exists
SELECT COUNT(*) > 0 AS exists
FROM
modelData
FROM
${
PgTrainingTableName
}
WHERE md5(q)=md5('
${
q
}
') AND md5(a)=md5('
${
a
}
') AND user_id='
${
userId
}
' AND kb_id='
${
kbId
}
'
WHERE md5(q)=md5('
${
q
}
') AND md5(a)=md5('
${
a
}
') AND user_id='
${
userId
}
' AND kb_id='
${
kbId
}
'
`
);
`
);
const
exists
=
rows
[
0
]?.
exists
||
false
;
const
exists
=
rows
[
0
]?.
exists
||
false
;
...
...
client/src/pages/api/openapi/kb/searchTest.ts
View file @
3a49efd4
...
@@ -5,6 +5,7 @@ import { PgClient } from '@/service/pg';
...
@@ -5,6 +5,7 @@ import { PgClient } from '@/service/pg';
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
{
getVector
}
from
'../plugin/vector'
;
import
{
getVector
}
from
'../plugin/vector'
;
import
type
{
KbTestItemType
}
from
'@/types/plugin'
;
import
type
{
KbTestItemType
}
from
'@/types/plugin'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
export
type
Props
=
{
export
type
Props
=
{
model
:
string
;
model
:
string
;
...
@@ -39,7 +40,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
...
@@ -39,7 +40,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
SET LOCAL ivfflat.probes =
${
global
.
systemEnv
.
pgIvfflatProbe
||
10
}
;
SET LOCAL ivfflat.probes =
${
global
.
systemEnv
.
pgIvfflatProbe
||
10
}
;
select id,q,a,source,(vector <#> '[
${
select id,q,a,source,(vector <#> '[
${
vectors
[
0
]
vectors
[
0
]
}
]') * -1 AS score from
modelData
where kb_id='
${
kbId
}
' AND user_id='
${
userId
}
' order by vector <#> '[
${
}
]') * -1 AS score from
${
PgTrainingTableName
}
where kb_id='
${
kbId
}
' AND user_id='
${
userId
}
' order by vector <#> '[
${
vectors
[
0
]
vectors
[
0
]
}
]' limit 12;
}
]' limit 12;
COMMIT;`
COMMIT;`
...
...
client/src/pages/api/openapi/kb/updateData.ts
View file @
3a49efd4
...
@@ -5,6 +5,7 @@ import { PgClient } from '@/service/pg';
...
@@ -5,6 +5,7 @@ import { PgClient } from '@/service/pg';
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
{
KB
,
connectToDatabase
}
from
'@/service/mongo'
;
import
{
KB
,
connectToDatabase
}
from
'@/service/mongo'
;
import
{
getVector
}
from
'../plugin/vector'
;
import
{
getVector
}
from
'../plugin/vector'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
export
type
Props
=
{
export
type
Props
=
{
dataId
:
string
;
dataId
:
string
;
...
@@ -46,7 +47,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
...
@@ -46,7 +47,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
})();
})();
// 更新 pg 内容.仅修改a,不需要更新向量。
// 更新 pg 内容.仅修改a,不需要更新向量。
await
PgClient
.
update
(
'modelData'
,
{
await
PgClient
.
update
(
PgTrainingTableName
,
{
where
:
[[
'id'
,
dataId
],
'AND'
,
[
'user_id'
,
userId
]],
where
:
[[
'id'
,
dataId
],
'AND'
,
[
'user_id'
,
userId
]],
values
:
[
values
:
[
{
key
:
'source'
,
value
:
'手动修改'
},
{
key
:
'source'
,
value
:
'手动修改'
},
...
...
client/src/pages/api/plugins/kb/data/exportModelData.ts
View file @
3a49efd4
...
@@ -3,6 +3,7 @@ import { jsonRes } from '@/service/response';
...
@@ -3,6 +3,7 @@ import { jsonRes } from '@/service/response';
import
{
connectToDatabase
,
User
}
from
'@/service/mongo'
;
import
{
connectToDatabase
,
User
}
from
'@/service/mongo'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
try
{
...
@@ -38,16 +39,19 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
...
@@ -38,16 +39,19 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
}
}
// 统计数据
// 统计数据
const
count
=
await
PgClient
.
count
(
'modelData'
,
{
const
count
=
await
PgClient
.
count
(
PgTrainingTableName
,
{
where
:
[[
'kb_id'
,
kbId
],
'AND'
,
[
'user_id'
,
userId
]]
where
:
[[
'kb_id'
,
kbId
],
'AND'
,
[
'user_id'
,
userId
]]
});
});
// 从 pg 中获取所有数据
// 从 pg 中获取所有数据
const
pgData
=
await
PgClient
.
select
<
{
q
:
string
;
a
:
string
;
source
:
string
}
>
(
'modelData'
,
{
const
pgData
=
await
PgClient
.
select
<
{
q
:
string
;
a
:
string
;
source
:
string
}
>
(
where
:
[[
'kb_id'
,
kbId
],
'AND'
,
[
'user_id'
,
userId
]],
PgTrainingTableName
,
fields
:
[
'q'
,
'a'
,
'source'
],
{
order
:
[{
field
:
'id'
,
mode
:
'DESC'
}],
where
:
[[
'kb_id'
,
kbId
],
'AND'
,
[
'user_id'
,
userId
]],
limit
:
count
fields
:
[
'q'
,
'a'
,
'source'
],
});
order
:
[{
field
:
'id'
,
mode
:
'DESC'
}],
limit
:
count
}
);
const
data
:
[
string
,
string
,
string
][]
=
pgData
.
rows
.
map
((
item
)
=>
[
const
data
:
[
string
,
string
,
string
][]
=
pgData
.
rows
.
map
((
item
)
=>
[
item
.
q
.
replace
(
/
\n
/g
,
'\\n'
),
item
.
q
.
replace
(
/
\n
/g
,
'\\n'
),
...
...
client/src/pages/api/plugins/kb/data/getDataById.ts
View file @
3a49efd4
...
@@ -4,6 +4,7 @@ import { connectToDatabase } from '@/service/mongo';
...
@@ -4,6 +4,7 @@ import { connectToDatabase } from '@/service/mongo';
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
type
{
KbDataItemType
}
from
'@/types/plugin'
;
import
type
{
KbDataItemType
}
from
'@/types/plugin'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
export
type
Response
=
{
export
type
Response
=
{
id
:
string
;
id
:
string
;
...
@@ -28,7 +29,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
...
@@ -28,7 +29,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
const
where
:
any
=
[[
'user_id'
,
userId
],
'AND'
,
[
'id'
,
dataId
]];
const
where
:
any
=
[[
'user_id'
,
userId
],
'AND'
,
[
'id'
,
dataId
]];
const
searchRes
=
await
PgClient
.
select
<
KbDataItemType
>
(
'modelData'
,
{
const
searchRes
=
await
PgClient
.
select
<
KbDataItemType
>
(
PgTrainingTableName
,
{
fields
:
[
'kb_id'
,
'id'
,
'q'
,
'a'
,
'source'
],
fields
:
[
'kb_id'
,
'id'
,
'q'
,
'a'
,
'source'
],
where
,
where
,
limit
:
1
limit
:
1
...
...
client/src/pages/api/plugins/kb/data/getDataList.ts
View file @
3a49efd4
...
@@ -4,6 +4,7 @@ import { connectToDatabase } from '@/service/mongo';
...
@@ -4,6 +4,7 @@ import { connectToDatabase } from '@/service/mongo';
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
type
{
KbDataItemType
}
from
'@/types/plugin'
;
import
type
{
KbDataItemType
}
from
'@/types/plugin'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
try
{
...
@@ -41,14 +42,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
...
@@ -41,14 +42,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
];
];
const
[
searchRes
,
total
]
=
await
Promise
.
all
([
const
[
searchRes
,
total
]
=
await
Promise
.
all
([
PgClient
.
select
<
KbDataItemType
>
(
'modelData'
,
{
PgClient
.
select
<
KbDataItemType
>
(
PgTrainingTableName
,
{
fields
:
[
'id'
,
'q'
,
'a'
,
'source'
],
fields
:
[
'id'
,
'q'
,
'a'
,
'source'
],
where
,
where
,
order
:
[{
field
:
'id'
,
mode
:
'DESC'
}],
order
:
[{
field
:
'id'
,
mode
:
'DESC'
}],
limit
:
pageSize
,
limit
:
pageSize
,
offset
:
pageSize
*
(
pageNum
-
1
)
offset
:
pageSize
*
(
pageNum
-
1
)
}),
}),
PgClient
.
count
(
'modelData'
,
{
PgClient
.
count
(
PgTrainingTableName
,
{
fields
:
[
'id'
],
fields
:
[
'id'
],
where
where
})
})
...
...
client/src/pages/api/plugins/kb/delete.ts
View file @
3a49efd4
...
@@ -4,6 +4,7 @@ import { connectToDatabase, KB, App, TrainingData } from '@/service/mongo';
...
@@ -4,6 +4,7 @@ import { connectToDatabase, KB, App, TrainingData } from '@/service/mongo';
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
PgClient
}
from
'@/service/pg'
;
import
{
Types
}
from
'mongoose'
;
import
{
Types
}
from
'mongoose'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
try
{
...
@@ -21,7 +22,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
...
@@ -21,7 +22,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await
connectToDatabase
();
await
connectToDatabase
();
// delete all pg data
// delete all pg data
await
PgClient
.
delete
(
'modelData'
,
{
await
PgClient
.
delete
(
PgTrainingTableName
,
{
where
:
[[
'user_id'
,
userId
],
'AND'
,
[
'kb_id'
,
id
]]
where
:
[[
'user_id'
,
userId
],
'AND'
,
[
'kb_id'
,
id
]]
});
});
...
...
client/src/service/moduleDispatch/kb/search.ts
View file @
3a49efd4
...
@@ -5,6 +5,7 @@ import { getVector } from '@/pages/api/openapi/plugin/vector';
...
@@ -5,6 +5,7 @@ import { getVector } from '@/pages/api/openapi/plugin/vector';
import
{
countModelPrice
}
from
'@/service/events/pushBill'
;
import
{
countModelPrice
}
from
'@/service/events/pushBill'
;
import
type
{
SelectedKbType
}
from
'@/types/plugin'
;
import
type
{
SelectedKbType
}
from
'@/types/plugin'
;
import
type
{
QuoteItemType
}
from
'@/types/chat'
;
import
type
{
QuoteItemType
}
from
'@/types/chat'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
type
KBSearchProps
=
{
type
KBSearchProps
=
{
kbList
:
SelectedKbType
;
kbList
:
SelectedKbType
;
...
@@ -48,7 +49,7 @@ export async function dispatchKBSearch(props: Record<string, any>): Promise<KBSe
...
@@ -48,7 +49,7 @@ export async function dispatchKBSearch(props: Record<string, any>): Promise<KBSe
const
res
:
any
=
await
PgClient
.
query
(
const
res
:
any
=
await
PgClient
.
query
(
`BEGIN;
`BEGIN;
SET LOCAL ivfflat.probes =
${
global
.
systemEnv
.
pgIvfflatProbe
||
10
}
;
SET LOCAL ivfflat.probes =
${
global
.
systemEnv
.
pgIvfflatProbe
||
10
}
;
select kb_id,id,q,a,source from
modelData
where kb_id IN (
${
kbList
select kb_id,id,q,a,source from
${
PgTrainingTableName
}
where kb_id IN (
${
kbList
.
map
((
item
)
=>
`'
${
item
.
kbId
}
'`
)
.
map
((
item
)
=>
`'
${
item
.
kbId
}
'`
)
.
join
(
','
)}
) AND vector <#> '[
${
vectors
[
0
]}
]' < -
${
similarity
}
order by vector <#> '[
${
.
join
(
','
)}
) AND vector <#> '[
${
vectors
[
0
]}
]' < -
${
similarity
}
order by vector <#> '[
${
vectors
[
0
]
vectors
[
0
]
...
...
client/src/service/mongo.ts
View file @
3a49efd4
...
@@ -6,6 +6,7 @@ import { User } from './models/user';
...
@@ -6,6 +6,7 @@ import { User } from './models/user';
import
{
PRICE_SCALE
}
from
'@/constants/common'
;
import
{
PRICE_SCALE
}
from
'@/constants/common'
;
import
{
connectPg
,
PgClient
}
from
'./pg'
;
import
{
connectPg
,
PgClient
}
from
'./pg'
;
import
{
createHashPassword
}
from
'@/utils/tools'
;
import
{
createHashPassword
}
from
'@/utils/tools'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
/**
/**
* connect MongoDB and init data
* connect MongoDB and init data
...
@@ -92,7 +93,7 @@ async function initPg() {
...
@@ -92,7 +93,7 @@ async function initPg() {
await
connectPg
();
await
connectPg
();
await
PgClient
.
query
(
`
await
PgClient
.
query
(
`
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS
modelData
(
CREATE TABLE IF NOT EXISTS
${
PgTrainingTableName
}
(
id BIGSERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
vector VECTOR(1536) NOT NULL,
vector VECTOR(1536) NOT NULL,
user_id VARCHAR(50) NOT NULL,
user_id VARCHAR(50) NOT NULL,
...
@@ -101,9 +102,9 @@ async function initPg() {
...
@@ -101,9 +102,9 @@ async function initPg() {
q TEXT NOT NULL,
q TEXT NOT NULL,
a TEXT NOT NULL
a TEXT NOT NULL
);
);
CREATE INDEX IF NOT EXISTS modelData_userId_index ON
modelData
USING HASH (user_id);
CREATE INDEX IF NOT EXISTS modelData_userId_index ON
${
PgTrainingTableName
}
USING HASH (user_id);
CREATE INDEX IF NOT EXISTS modelData_kbId_index ON
modelData
USING HASH (kb_id);
CREATE INDEX IF NOT EXISTS modelData_kbId_index ON
${
PgTrainingTableName
}
USING HASH (kb_id);
CREATE INDEX IF NOT EXISTS idx_model_data_md5_q_a_user_id_kb_id ON
modelData
(md5(q), md5(a), user_id, kb_id);
CREATE INDEX IF NOT EXISTS idx_model_data_md5_q_a_user_id_kb_id ON
${
PgTrainingTableName
}
(md5(q), md5(a), user_id, kb_id);
`
);
`
);
console
.
log
(
'init pg successful'
);
console
.
log
(
'init pg successful'
);
}
catch
(
error
)
{
}
catch
(
error
)
{
...
...
client/src/service/pg.ts
View file @
3a49efd4
import
{
Pool
}
from
'pg'
;
import
{
Pool
}
from
'pg'
;
import
type
{
QueryResultRow
}
from
'pg'
;
import
type
{
QueryResultRow
}
from
'pg'
;
import
{
PgTrainingTableName
}
from
'@/constants/plugin'
;
export
const
connectPg
=
async
()
=>
{
export
const
connectPg
=
async
()
=>
{
if
(
global
.
pgClient
)
{
if
(
global
.
pgClient
)
{
...
@@ -173,7 +174,7 @@ export const insertKbItem = ({
...
@@ -173,7 +174,7 @@ export const insertKbItem = ({
source?: string;
source?: string;
}[];
}[];
}) => {
}) => {
return PgClient.insert(
'modelData'
, {
return PgClient.insert(
PgTrainingTableName
, {
values: data.map((item) => [
values: data.map((item) => [
{ key: 'user_id', value: userId },
{ key: 'user_id', value: userId },
{ key: 'kb_id', value: kbId },
{ key: 'kb_id', value: kbId },
...
...
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