Skip to content

Instantly share code, notes, and snippets.

@peterfriese
Last active November 26, 2024 20:56
Show Gist options
  • Save peterfriese/082a75116b937b3fd4cc6b64c107ade3 to your computer and use it in GitHub Desktop.
Save peterfriese/082a75116b937b3fd4cc6b64c107ade3 to your computer and use it in GitHub Desktop.
Talk to a PDF with Genkit
import {gemini15Flash, googleAI} from '@genkit-ai/googleai';
import {genkit, z} from 'genkit';
import {createInterface} from "node:readline/promises";
import pdf from "pdf-parse";
const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash,
});
(async () => {
try {
const filename = process.argv[2];
if (!filename) {
console.error("Please provide a filename as a command line argument.");
process.exit(1);
}
const prefix = process.argv[3] || "Answer the user's questions about the contents of this PDF file.";
const data = await pdf(filename);
const prompt = `
${prefix}
Context:
${data.text}
`
const chat = ai.chat({system: prompt})
console.log("You're chatting with Gemini. Ctrl-C to quit.\n");
const readline = createInterface(process.stdin, process.stdout);
while (true) {
const userInput = await readline.question("> ");
const {text} = await chat.send(userInput);
console.log(text);
}
} catch (error) {
console.error("Error parsing PDF or interacting with Genkit:", error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment