Pdf Input Local

Code

1from pathlib import Path
2
3from kern.agent import Agent
4from kern.media import File
5from kern.models.openai.responses import OpenAIResponses
6from kern.utils.media import download_file
7
8pdf_path = Path(__file__).parent.joinpath("ThaiRecipes.pdf")
9
10# Download the file using the download_file function
11download_file(
12 "https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf", str(pdf_path)
13)
14
15agent = Agent(
16 model=OpenAIResponses(id="gpt-5-mini"),
17 tools=[{"type": "file_search"}],
18 markdown=True,
19 add_history_to_context=True,
20)
21
22agent.print_response(
23 "Summarize the contents of the attached file.",
24 files=[File(filepath=pdf_path)],
25)
26agent.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 OPENAI_API_KEY=xxx

Install dependencies

1uv pip install -U openai kern-ai

Run Agent

1python cookbook/11_models/openai/responses/pdf_input_local.py