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
707be336
authored
Jun 06, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pg index
parent
942aeeac
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
23 additions
and
20 deletions
+23
-20
docs/deploy/fastgpt/pg/init.sql
+1
-1
src/constants/model.ts
+3
-3
src/pages/api/openapi/kb/appKbSearch.ts
+3
-3
src/pages/api/plugins/kb/data/getDataList.ts
+13
-10
src/service/pg.ts
+1
-1
src/utils/plugin/google.ts
+2
-2
No files found.
docs/deploy/fastgpt/pg/init.sql
View file @
707be336
...
...
@@ -16,6 +16,6 @@ CREATE TABLE IF NOT EXISTS modelData (
-- CREATE INDEX IF NOT EXISTS modelData_userId_index ON modelData USING HASH (user_id);
-- CREATE INDEX IF NOT EXISTS modelData_kbId_index ON modelData 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 vector_index ON modelData USING ivfflat (vector vector_cosine_ops) WITH (lists = 1000
);
-- CREATE INDEX
modeldata_id_desc_idx ON modeldata (id DESC
);
-- vector 索引,可以参考 [pg vector](https://github.com/pgvector/pgvector) 去配置,根据数据量去配置
EOSQL
src/constants/model.ts
View file @
707be336
...
...
@@ -113,15 +113,15 @@ export const ModelVectorSearchModeMap: Record<
>
=
{
[
appVectorSearchModeEnum
.
hightSimilarity
]:
{
text
:
'高相似度, 无匹配时拒绝回复'
,
similarity
:
0.
1
8
similarity
:
0.8
},
[
appVectorSearchModeEnum
.
noContext
]:
{
text
:
'高相似度,无匹配时直接回复'
,
similarity
:
0.
1
8
similarity
:
0.8
},
[
appVectorSearchModeEnum
.
lowSimilarity
]:
{
text
:
'低相似度匹配'
,
similarity
:
0.
7
similarity
:
0.
3
}
};
...
...
src/pages/api/openapi/kb/appKbSearch.ts
View file @
707be336
...
...
@@ -93,15 +93,15 @@ export async function appKbSearch({
// search kb
const
res
:
any
=
await
PgClient
.
query
(
`BEGIN;
SET LOCAL ivfflat.probes =
${
process
.
env
.
PG_IVFFLAT_PROBE
||
100
}
;
select id,q,a,source from modelData where kb_id IN (
${
model
.
chat
.
relatedKbs
.
map
((
item
)
=>
`'
${
item
}
'`
)
.
join
(
','
)}
) AND vector <#> '[
${
promptVector
[
0
]}
]' <
${
similarity
}
order by vector <#> '[
${
.
join
(
','
)}
) AND vector <#> '[
${
promptVector
[
0
]}
]' <
-
${
similarity
}
order by vector <#> '[
${
promptVector
[
0
]
}
]' limit 8;
COMMIT;`
);
const
searchRes
:
QuoteItemType
[]
=
res
?.[
2
]?.
rows
||
[];
const
searchRes
:
QuoteItemType
[]
=
res
?.[
1
]?.
rows
||
[];
// filter same search result
const
idSet
=
new
Set
<
string
>
();
...
...
src/pages/api/plugins/kb/data/getDataList.ts
View file @
707be336
...
...
@@ -39,22 +39,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
:
[])
];
const
searchRes
=
await
PgClient
.
select
<
KbDataItemType
>
(
'modelData'
,
{
fields
:
[
'id'
,
'q'
,
'a'
,
'source'
],
where
,
order
:
[{
field
:
'id'
,
mode
:
'DESC'
}],
limit
:
pageSize
,
offset
:
pageSize
*
(
pageNum
-
1
)
});
const
[
searchRes
,
total
]
=
await
Promise
.
all
([
PgClient
.
select
<
KbDataItemType
>
(
'modelData'
,
{
fields
:
[
'id'
,
'q'
,
'a'
,
'source'
],
where
,
limit
:
pageSize
,
offset
:
pageSize
*
(
pageNum
-
1
)
}),
PgClient
.
count
(
'modelData'
,
{
fields
:
[
'id'
],
where
})
]);
jsonRes
(
res
,
{
data
:
{
pageNum
,
pageSize
,
data
:
searchRes
.
rows
,
total
:
await
PgClient
.
count
(
'modelData'
,
{
where
})
total
}
});
}
catch
(
err
)
{
...
...
src/service/pg.ts
View file @
707be336
...
...
@@ -113,7 +113,7 @@ class Pg {
return pg.query<T>(sql);
}
async count(table: string, props: GetProps) {
const sql = `
SELECT
COUNT
(
*
)
const sql = `
SELECT
COUNT
(
$
{
props
?.
fields
?.[
0
]
||
'*'
}
)
FROM
$
{
table
}
$
{
this
.
getWhereStr
(
props
.
where
)}
`;
...
...
src/utils/plugin/google.ts
View file @
707be336
...
...
@@ -27,8 +27,8 @@ export const authGoogleToken = async (data: {
`https://www.recaptcha.net/recaptcha/api/siteverify?
${
Obj2Query
(
data
)}
`
);
if
(
res
.
data
.
success
&&
res
.
data
.
score
&&
res
.
data
.
score
>=
0.7
)
{
if
(
res
.
data
.
success
)
{
return
Promise
.
resolve
(
''
);
}
return
Promise
.
reject
(
res
.
data
[
'error-codes'
]
[
0
]
||
'非法环境'
);
return
Promise
.
reject
(
res
?.
data
?.[
'error-codes'
]?.
[
0
]
||
'非法环境'
);
};
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