代码

cookbook/models/openai/responses/image_agent_bytes.py
from pathlib import Path

from agno.agent import Agent
from agno.media import Image
from agno.models.openai import OpenAIResponses
from agno.tools.googlesearch import GoogleSearchTools
from agno.utils.media import download_image

agent = Agent(
    model=OpenAIResponses(id="gpt-4o"),
    tools=[GoogleSearchTools()],
    markdown=True,
)

image_path = Path(__file__).parent.joinpath("sample.jpg")

download_image(
    url="https://upload.wikimedia.org/wikipedia/commons/0/04/GoldenGateBridge-001.jpg",
    output_path=str(image_path),
)

# 将图片文件内容读取为字节
image_bytes = image_path.read_bytes()

agent.print_response(
    "告诉我关于这张图片的信息,并提供与之相关的最新新闻。",
    images=[
        Image(content=image_bytes),
    ],
    stream=True,
)

用法

1

创建虚拟环境

打开 Terminal 并创建一个 python 虚拟环境。

python3 -m venv .venv
source .venv/bin/activate
2

设置你的 API 密钥

export OPENAI_API_KEY=xxx
3

安装库

pip install -U openai agno googlesearch-python
4

运行代理

python cookbook/models/openai/responses/image_agent_bytes.py