pgvector 检索器模板
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 PostgreSQL 和 pgvector
作为检索器实现。使用
从以下示例入手,对其进行修改以与您的数据库配合使用
架构。
import { genkit, z, Document } from 'genkit';
import { googleAI, textEmbedding004 } from '@genkit-ai/googleai';
import { toSql } from 'pgvector';
import postgres from 'postgres';
const ai = genkit({
plugins: [googleAI()],
});
const sql = postgres({ ssl: false, database: 'recaps' });
const QueryOptions = z.object({
show: z.string(),
k: z.number().optional(),
});
const sqlRetriever = ai.defineRetriever(
{
name: 'pgvector-myTable',
configSchema: QueryOptions,
},
async (input, options) => {
const embedding = (await ai.embed({
embedder: textEmbedding004,
content: input,
}))[0].embedding;
const results = await sql`
SELECT episode_id, season_number, chunk as content
FROM embeddings
WHERE show_id = ${options.show}
ORDER BY embedding <#> ${toSql(embedding)} LIMIT ${options.k ?? 3}
`;
return {
documents: results.map((row) => {
const { content, ...metadata } = row;
return Document.fromText(content, metadata);
}),
};
}
);
下面介绍了如何在 flow 中使用检索器:
// Simple flow to use the sqlRetriever
export const askQuestionsOnGoT = ai.defineFlow(
{
name: 'askQuestionsOnGoT',
inputSchema: z.string(),
outputSchema: z.string(),
},
async (inputQuestion) => {
const docs = await ai.retrieve({
retriever: sqlRetriever,
query: inputQuestion,
options: {
show: 'Game of Thrones',
},
});
console.log(docs);
// Continue with using retrieved docs
// in RAG prompts.
//...
}
);
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[],[],null,["# pgvector retriever template\n\n\u003cbr /\u003e\n\nYou can use PostgreSQL and `pgvector` as your retriever implementation. Use the\nfollowing example as a starting point and modify it to work with your database\nschema. \n\n import { genkit, z, Document } from 'genkit';\n import { googleAI, textEmbedding004 } from '@genkit-ai/googleai';\n import { toSql } from 'pgvector';\n import postgres from 'postgres';\n\n const ai = genkit({\n plugins: [googleAI()],\n });\n\n const sql = postgres({ ssl: false, database: 'recaps' });\n\n const QueryOptions = z.object({\n show: z.string(),\n k: z.number().optional(),\n });\n\n const sqlRetriever = ai.defineRetriever(\n {\n name: 'pgvector-myTable',\n configSchema: QueryOptions,\n },\n async (input, options) =\u003e {\n const embedding = (await ai.embed({\n embedder: textEmbedding004,\n content: input,\n }))[0].embedding;\n const results = await sql`\n SELECT episode_id, season_number, chunk as content\n FROM embeddings\n WHERE show_id = ${options.show}\n ORDER BY embedding \u003c#\u003e ${toSql(embedding)} LIMIT ${options.k ?? 3}\n `;\n return {\n documents: results.map((row) =\u003e {\n const { content, ...metadata } = row;\n return Document.fromText(content, metadata);\n }),\n };\n }\n );\n\nAnd here's how to use the retriever in a flow: \n\n // Simple flow to use the sqlRetriever\n export const askQuestionsOnGoT = ai.defineFlow(\n {\n name: 'askQuestionsOnGoT',\n inputSchema: z.string(),\n outputSchema: z.string(),\n },\n async (inputQuestion) =\u003e {\n const docs = await ai.retrieve({\n retriever: sqlRetriever,\n query: inputQuestion,\n options: {\n show: 'Game of Thrones',\n },\n });\n console.log(docs);\n\n // Continue with using retrieved docs\n // in RAG prompts.\n //...\n }\n );"]]