|
| 1 | +/** |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { index } from '@genkit-ai/ai'; |
| 18 | +import { Document } from '@genkit-ai/ai/retriever'; |
| 19 | +import { devLocalIndexerRef } from '@genkit-ai/dev-local-vectorstore'; |
| 20 | +import { defineFlow, run } from '@genkit-ai/flow'; |
| 21 | +import { readFile } from 'fs/promises'; |
| 22 | +import { chunk } from 'llm-chunk'; |
| 23 | +import path from 'path'; |
| 24 | +import pdf from 'pdf-parse'; |
| 25 | +import * as z from 'zod'; |
| 26 | + |
| 27 | +// Create a reference to the configured local indexer. |
| 28 | +export const menuPdfIndexer = devLocalIndexerRef('menuQA'); |
| 29 | + |
| 30 | +// Create chunking config for indexing a pdf of a menu |
| 31 | +// See full options in https://www.npmjs.com/package/llm-chunk |
| 32 | +const chunkingConfig = { |
| 33 | + minLength: 1000, |
| 34 | + maxLength: 2000, |
| 35 | + splitter: 'sentence', |
| 36 | + overlap: 100, |
| 37 | + delimiters: '', |
| 38 | +} as any; |
| 39 | + |
| 40 | +// Define a flow to index documents into the "vector store" |
| 41 | +// genkit flow:run indexMenu '"./docs/.pdf"' |
| 42 | +export const indexMenu = defineFlow( |
| 43 | + { |
| 44 | + name: 'indexMenu', |
| 45 | + inputSchema: z.string().describe('PDF file path'), |
| 46 | + outputSchema: z.void(), |
| 47 | + }, |
| 48 | + async (filePath: string) => { |
| 49 | + filePath = path.resolve(filePath); |
| 50 | + |
| 51 | + // Read the pdf. |
| 52 | + const pdfTxt = await run('extract-text', () => |
| 53 | + extractTextFromPdf(filePath) |
| 54 | + ); |
| 55 | + |
| 56 | + // Divide the pdf text into segments. |
| 57 | + const chunks = await run('chunk-it', async () => |
| 58 | + chunk(pdfTxt, chunkingConfig) |
| 59 | + ); |
| 60 | + |
| 61 | + // Convert chunks of text into documents to store in the index. |
| 62 | + const documents = chunks.map((text) => { |
| 63 | + return Document.fromText(text, { filePath }); |
| 64 | + }); |
| 65 | + |
| 66 | + // Add documents to the index. |
| 67 | + await index({ |
| 68 | + indexer: menuPdfIndexer, |
| 69 | + documents, |
| 70 | + }); |
| 71 | + } |
| 72 | +); |
| 73 | + |
| 74 | +async function extractTextFromPdf(filePath: string) { |
| 75 | + const pdfFile = path.resolve(filePath); |
| 76 | + const dataBuffer = await readFile(pdfFile); |
| 77 | + const data = await pdf(dataBuffer); |
| 78 | + return data.text; |
| 79 | +} |
0 commit comments