Agent with PDF Input (Local file)

Code

1from pathlib import Path
2from kern.agent import Agent
3from kern.media import File
4from kern.models.google import Gemini
5from kern.utils.media import download_file
6
7pdf_path = Path(__file__).parent.joinpath("ThaiRecipes.pdf")
8
9# Download the file using the download_file function
10download_file(
11 "https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf", str(pdf_path)
12)
13
14agent = Agent(
15 model=Gemini(id="gemini-2.0-flash-exp"),
16 markdown=True,
17 add_history_to_context=True,
18)
19
20agent.print_response(
21 "Summarize the contents of the attached file.",
22 files=[File(filepath=pdf_path)],
23)
24agent.print_response("Suggest me a recipe from the attached file.")

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export GOOGLE_API_KEY=xxx

Install dependencies

1uv pip install -U google-genai kern-ai

Run Agent

1python cookbook/11_models/google/gemini/pdf_input_local.py