Template pengambil pgvector
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Anda dapat menggunakan PostgreSQL dan pgvector
sebagai penerapan retriever. Gunakan
contoh berikut sebagai titik awal dan memodifikasinya agar berfungsi dengan database Anda
skema.
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);
}),
};
}
);
Berikut cara menggunakan retriever dalam 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.
//...
}
);
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-25 UTC.
[null,null,["Terakhir diperbarui pada 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 );"]]