diff --git a/docs/deploy/fastgpt/pg/init.sql b/docs/deploy/fastgpt/pg/init.sql index 1ea5e9912..84329f7b6 100644 --- a/docs/deploy/fastgpt/pg/init.sql +++ b/docs/deploy/fastgpt/pg/init.sql @@ -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 diff --git a/src/constants/model.ts b/src/constants/model.ts index 969609fb5..466c266d7 100644 --- a/src/constants/model.ts +++ b/src/constants/model.ts @@ -113,15 +113,15 @@ export const ModelVectorSearchModeMap: Record< > = { [appVectorSearchModeEnum.hightSimilarity]: { text: '高相似度, 无匹配时拒绝回复', - similarity: 0.18 + similarity: 0.8 }, [appVectorSearchModeEnum.noContext]: { text: '高相似度,无匹配时直接回复', - similarity: 0.18 + similarity: 0.8 }, [appVectorSearchModeEnum.lowSimilarity]: { text: '低相似度匹配', - similarity: 0.7 + similarity: 0.3 } }; diff --git a/src/pages/api/openapi/kb/appKbSearch.ts b/src/pages/api/openapi/kb/appKbSearch.ts index 4b4e0bca9..80e9e67a7 100644 --- a/src/pages/api/openapi/kb/appKbSearch.ts +++ b/src/pages/api/openapi/kb/appKbSearch.ts @@ -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(); diff --git a/src/pages/api/plugins/kb/data/getDataList.ts b/src/pages/api/plugins/kb/data/getDataList.ts index caa408cb3..f2a4c0061 100644 --- a/src/pages/api/plugins/kb/data/getDataList.ts +++ b/src/pages/api/plugins/kb/data/getDataList.ts @@ -39,22 +39,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< : []) ]; - const searchRes = await PgClient.select('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('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) { diff --git a/src/service/pg.ts b/src/service/pg.ts index abb43a570..af4c62af8 100644 --- a/src/service/pg.ts +++ b/src/service/pg.ts @@ -113,7 +113,7 @@ class Pg { return pg.query(sql); } async count(table: string, props: GetProps) { - const sql = `SELECT COUNT(*) + const sql = `SELECT COUNT(${props?.fields?.[0] || '*'}) FROM ${table} ${this.getWhereStr(props.where)} `; diff --git a/src/utils/plugin/google.ts b/src/utils/plugin/google.ts index aab791ce1..ba7fa4666 100644 --- a/src/utils/plugin/google.ts +++ b/src/utils/plugin/google.ts @@ -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] || '非法环境'); };