modello retriever pgvector
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Puoi utilizzare PostgreSQL e pgvector
come implementazione del retriever. Utilizza il seguente esempio come punto di partenza e modificalo in modo che funzioni con lo schema del tuo database.
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);
}),
};
}
);
Ecco come utilizzare il retriever in un flusso:
// 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.
//...
}
);
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-07-25 UTC.
[null,null,["Ultimo aggiornamento 2025-07-25 UTC."],[],[],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 );"]]