Skip to content

Commit

Permalink
feat: maxlink and search sql
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Jun 5, 2023
1 parent 48fbc74 commit 2f1506b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/pages/api/openapi/kb/appKbSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,17 @@ export async function appKbSearch({
});

// search kb
const { rows: searchRes } = await PgClient.select<QuoteItemType>('modelData', {
fields: ['id', 'q', 'a', 'source'],
where: [
`kb_id IN (${model.chat.relatedKbs.map((item) => `'${item}'`).join(',')})`,
'AND',
`vector <#> '[${promptVector[0]}]' < ${similarity}`
],
order: [{ field: 'vector', mode: `<#> '[${promptVector[0]}]'` }],
limit: 8
});
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 <#> '[${
promptVector[0]
}]' limit 8;
COMMIT;`
);
const searchRes: QuoteItemType[] = res?.[2]?.rows || [];

// filter same search result
const idSet = new Set<string>();
Expand Down
2 changes: 1 addition & 1 deletion src/service/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const connectPg = async () => {
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DB_NAME,
max: maxLink,
max: Math.floor(maxLink * 0.5),
idleTimeoutMillis: 60000,
connectionTimeoutMillis: 20000
});
Expand Down

0 comments on commit 2f1506b

Please sign in to comment.