Skip to content

Commit 38c3d6d

Browse files
authored
Replace bobFacts with menuSuggestion in rag docs (#113)
* Replace bobFacts with menuSuggestion in rag docs * Add more context to sentence about example app * Format rag.md
1 parent cd9c272 commit 38c3d6d

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

docs/rag.md

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,8 @@ Embedding model support is provided through the following plugins:
121121

122122
## Defining a RAG Flow
123123

124-
The following examples show how you could ingest a collection of PDF documents
125-
into a vector database and retrieve them for use in a flow.
126-
127-
It uses the local file-based vector similarity retriever
128-
that Genkit provides out-of-the box for simple testing and prototyping (_do not
129-
use in production_)
124+
The following examples show how you could ingest a collection of restaurant menu PDF documents
125+
into a vector database and retrieve them for use in a flow that determines what food items are available.
130126

131127
### Install dependencies for processing PDFs
132128

@@ -153,7 +149,7 @@ configureGenkit({
153149
// the local vector store requires an embedder to translate from text to vector
154150
devLocalVectorstore([
155151
{
156-
indexName: 'bob-facts',
152+
indexName: 'menuQA',
157153
embedder: textEmbeddingGecko,
158154
},
159155
]),
@@ -175,7 +171,7 @@ use in production_)
175171
```ts
176172
import { devLocalIndexerRef } from '@genkit-ai/dev-local-vectorstore';
177173

178-
export const pdfIndexer = devLocalIndexerRef('bob-facts');
174+
export const menuPdfIndexer = devLocalIndexerRef('menuQA');
179175
```
180176

181177
#### Create chunking config
@@ -199,23 +195,22 @@ More chunking options for this library can be found in the [llm-chunk documentat
199195
#### Define your indexer flow
200196

201197
```ts
198+
import { index } from '@genkit-ai/ai';
199+
import { Document } from '@genkit-ai/ai/retriever';
200+
import { defineFlow, run } from '@genkit-ai/flow';
201+
import { readFile } from 'fs/promises';
202202
import { chunk } from 'llm-chunk';
203203
import path from 'path';
204204
import pdf from 'pdf-parse';
205-
import { readFile } from 'fs/promises';
206-
import z from 'zod';
207-
208-
import { Document, index } from '@genkit-ai/ai/retriever';
209-
import { defineFlow, run } from '@genkit-ai/flow';
210-
import { devLocalVectorstore } from '@genkit-ai/dev-local-vectorstore';
205+
import * as z from 'zod';
211206

212-
export const indexPdf = defineFlow(
207+
export const indexMenu = defineFlow(
213208
{
214-
name: 'indexPdf',
215-
input: z.string().describe('PDF file path'),
216-
output: z.void(),
209+
name: 'indexMenu',
210+
inputSchema: z.string().describe('PDF file path'),
211+
outputSchema: z.void(),
217212
},
218-
async (filePath) => {
213+
async (filePath: string) => {
219214
filePath = path.resolve(filePath);
220215

221216
// Read the pdf.
@@ -235,7 +230,7 @@ export const indexPdf = defineFlow(
235230

236231
// Add documents to the index.
237232
await index({
238-
indexer: pdfIndexer,
233+
indexer: menuPdfIndexer,
239234
documents,
240235
});
241236
}
@@ -252,10 +247,10 @@ async function extractTextFromPdf(filePath: string) {
252247
#### Run the indexer flow
253248

254249
```posix-terminal
255-
genkit flow:run indexPdf "'../pdfs'"
250+
genkit flow:run indexMenu "'../pdfs'"
256251
```
257252

258-
After running the `indexPdf` flow, the vector database will be seeded with documents and ready to be used in Genkit flows with retrieval steps.
253+
After running the `indexMenu` flow, the vector database will be seeded with documents and ready to be used in Genkit flows with retrieval steps.
259254

260255
### Define a flow with retrieval
261256

@@ -264,32 +259,39 @@ the indexer example, this example uses Genkit's file-based vector retriever,
264259
which you should not use in production.
265260

266261
```ts
267-
import { defineFlow } from '@genkit-ai/flow';
268-
import { generate } from '@genkit-ai/ai/generate';
262+
import { generate } from '@genkit-ai/ai';
269263
import { retrieve } from '@genkit-ai/ai/retriever';
270-
import {
271-
devLocalRetrieverRef,
272-
devLocalVectorstore,
273-
} from '@genkit-ai/dev-local-vectorstore';
274-
import { geminiPro, textEmbeddingGecko, vertexAI } from '@genkit-ai/vertexai';
264+
import { devLocalRetrieverRef } from '@genkit-ai/dev-local-vectorstore';
265+
import { defineFlow } from '@genkit-ai/flow';
266+
import { geminiPro } from '@genkit-ai/vertexai';
275267
import * as z from 'zod';
276268

277269
// Define the retriever reference
278-
export const bobFactRetriever = devLocalRetrieverRef('bob-facts');
270+
export const menuRetriever = devLocalRetrieverRef('menuQA');
279271

280-
export const ragFlow = defineFlow(
281-
{ name: 'ragFlow', input: z.string(), output: z.string() },
282-
async (input) => {
272+
export const menuQAFlow = defineFlow(
273+
{ name: 'menuQA', inputSchema: z.string(), outputSchema: z.string() },
274+
async (input: string) => {
275+
// retrieve relevant documents
283276
const docs = await retrieve({
284-
retriever: bobFactRetriever,
277+
retriever: menuRetriever,
285278
query: input,
286279
options: { k: 3 },
287280
});
288281

289282
// generate a response
290283
const llmResponse = await generate({
291284
model: geminiPro,
292-
prompt: `Answer this question: ${input}`,
285+
prompt: `
286+
You are acting as a helpful AI assistant that can answer
287+
questions about the food available on the menu at Genkit Grub Pub.
288+
289+
Use only the context provided to answer the question.
290+
If you don't know, do not make up an answer.
291+
Do not add or change items on the menu.
292+
293+
Question: ${input}
294+
`,
293295
context: docs,
294296
});
295297

@@ -345,19 +347,21 @@ import {
345347
} from '@genkit-ai/ai/retriever';
346348
import * as z from 'zod';
347349

348-
const MyAdvancedOptionsSchema = CommonRetrieverOptionsSchema.extend({
350+
export const menuRetriever = devLocalRetrieverRef('menuQA');
351+
352+
const advancedMenuRetrieverOptionsSchema = CommonRetrieverOptionsSchema.extend({
349353
preRerankK: z.number().max(1000),
350354
});
351355

352-
const advancedRetriever = defineRetriever(
356+
const advancedMenuRetriever = defineRetriever(
353357
{
354-
name: `custom/myAdvancedRetriever`,
355-
configSchema: MyAdvancedOptionsSchema,
358+
name: `custom/advancedMenuRetriever`,
359+
configSchema: advancedMenuRetrieverOptionsSchema,
356360
},
357361
async (input, options) => {
358362
const extendedPrompt = await extendPrompt(input);
359363
const docs = await retrieve({
360-
retriever: bobFactsRetriever,
364+
retriever: menuRetriever,
361365
query: extendedPrompt,
362366
options: { k: options.preRerankK || 10 },
363367
});
@@ -375,7 +379,7 @@ And then you can just swap out your retriever:
375379
```javascript
376380
const docs = await retrieve({
377381
retriever: advancedRetriever,
378-
query: 'Who is Bob?',
382+
query: input,
379383
options: { preRerankK: 7, k: 3 },
380384
});
381385
```

0 commit comments

Comments
 (0)