您可以使用 Genkit 建構應用程式,讓使用者與 PDF 檔案進行即時通訊。詳細步驟如下:
本指南將說明如何執行上述各項工作。
依附元件
開始工作前,請先設定下列依附元件:
工作
設定依附元件後,您就可以建構專案本身。
1. 設定專案
建立目錄結構和檔案,用於儲存原始碼。
$ mkdir -p chat-with-a-pdf/src && \ cd chat-with-a-pdf/src && \ touch index.ts
初始化新的 TypeScript 專案。
$ npm init -y
安裝 pdf-parse 模組:
$ npm i pdf-parse
安裝下列 Genkit 依附元件,以便在專案使用 Genkit:
$ npm install genkit @genkit-ai/googleai
genkit
提供 Genkit 核心功能。@genkit-ai/googleai
可讓您使用 Google AI Gemini 模型。
5. 取得及設定模型 API 金鑰
-
如要使用本程式碼研究室使用的 Gemini API,您必須先設定 API 金鑰。如果您還沒有金鑰,請在 Google AI Studio 建立金鑰。Gemini API 提供大量免費方案配額,不需要信用卡即可開始使用。建立 API 金鑰後,請使用下列指令將
GOOGLE_GENAI_API_KEY`
環境變數設為該組金鑰:$ export GOOGLE_GENAI_API_KEY=<your API key>
注意:雖然本教學課程使用 AI Studio 的 Gemini API,但 Genkit 支援多種模型供應工具,包括:
- Vertex AI 的 Gemini。
- 透過 Vertex AI Model Garden 使用 Anthropic 的 Claude 3 模型和 Llama 3.1,以及社群外掛程式。
- 透過 Ollama 開放原始碼模型。
- 社群支援的供應商,例如 OpenAI 和 Cohere。
2. 匯入必要的依附元件
在您建立的 index.ts
檔案中,新增下列指令列,以匯入這個專案所需的依附元件:
import { gemini15Flash, googleAI } from '@genkit-ai/googleai';
import { genkit } from 'genkit';
import pdf from 'pdf-parse';
import fs from 'fs';
import { createInterface } from "node:readline/promises";
- 前兩行會匯入 Genkit 和 Google AI 外掛程式。
- 第二行和第三行則是用於 PDF 剖析器。
- 第五行則是用來實作 UI。
3. 設定 Genkit 和預設模型
加入以下程式碼行來設定 Genkit,並將 Gemini 1.5 Flash 設為預設模型。
const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash,
});
接著,您可以為程式碼和錯誤處理新增骨架。
(async () => {
try {
// Step 1: get command line arguments
// Step 2: load PDF file
// Step 3: construct prompt
// Step 4: start chat
Step 5: chat loop
} catch (error) {
console.error("Error parsing PDF or interacting with Genkit:", error);
}
})(); // <-- don't forget the trailing parentheses to call the function!
4. 載入及剖析 PDF
在步驟 1 下方,新增程式碼來讀取從指令列傳入的 PDF 檔案名稱。
const filename = process.argv[2]; if (!filename) { console.error("Please provide a filename as a command line argument."); process.exit(1); }
在步驟 2 下方,新增程式碼來載入 PDF 檔案的內容。
let dataBuffer = fs.readFileSync(filename); const { text } = await pdf(dataBuffer);
5. 設定提示
在步驟 3 下方,新增設定提示的程式碼:
const prefix = process.argv[3] || "Sample prompt: Answer the user's questions about the contents of this PDF file.";
const prompt = `
${prefix}
Context:
${text}
`
- 如果使用者未透過指令列傳入自訂提示,第一個
const
宣告會定義預設提示。 - 第二個
const
宣告會將提示前置字串和 PDF 檔案的完整文字,插入模型的提示中。
6. 實作 UI
在步驟 4 下方,新增下列程式碼來啟動聊天並實作 UI:
const chat = ai.chat({ system: prompt })
const readline = createInterface(process.stdin, process.stdout);
console.log("You're chatting with Gemini. Ctrl-C to quit.\n");
第一個 const
宣告會透過呼叫 chat
方法,傳遞提示 (包含 PDF 檔案的完整文字),啟動與模型的對話。程式碼的其餘部分會將文字輸入內容例項化,然後向使用者顯示訊息。
7. 實作即時通訊迴圈
在步驟 5 下方,新增程式碼來接收使用者輸入內容,並使用 chat.send
將該輸入內容傳送至模型。應用程式會重複執行這部分的程式碼,直到使用者按下 CTRL + C 為止。
while (true) {
const userInput = await readline.question("> ");
const {text} = await chat.send(userInput);
console.log(text);
}
8. 執行應用程式
在終端機中執行應用程式。在專案的根資料夾中開啟終端機,然後執行下列指令:
npx tsx src/index.ts path/to/some.pdf
接著,您就可以開始與 PDF 檔案對話。