Agent with Image Input

AWS Bedrock supports image input with models like amazon.nova-pro-v1:0. You can use this to analyze images and get information about them.

Code

1from pathlib import Path
2from kern.agent import Agent
3from kern.media import Image
4from kern.models.aws import AwsBedrock
5from kern.tools.hackernews import HackerNewsTools
6
7agent = Agent(
8 model=AwsBedrock(id="amazon.nova-pro-v1:0"),
9 tools=[HackerNewsTools()],
10 markdown=True,
11)
12
13image_path = Path(__file__).parent.joinpath("sample.jpg")
14
15# Read the image file content as bytes
16with open(image_path, "rb") as img_file:
17 image_bytes = img_file.read()
18
19agent.print_response(
20 "Tell me about this image and give me the latest news about it.",
21 images=[
22 Image(content=image_bytes, format="jpeg"),
23 ],
24)

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 AWS Credentials

1export AWS_ACCESS_KEY_ID=***
2export AWS_SECRET_ACCESS_KEY=***
3export AWS_REGION=***

Install dependencies

1uv pip install -U boto3 kern-ai

Add an Image

Place an image file named sample.jpg in the same directory as your script.

Run Agent

1python image_agent.py