代码
cookbook/models/openai/chat/generate_images.py
Copy
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.dalle import DalleTools
image_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
tools=[DalleTools()],
description="You are an AI agent that can generate images using DALL-E.",
instructions="When the user asks you to create an image, use the `create_image` tool to create the image.",
markdown=True,
show_tool_calls=True,
)
image_agent.print_response("生成一张白色暹罗猫的图片")
images = image_agent.get_images()
if images and isinstance(images, list):
for image_response in images:
image_url = image_response.url
print(image_url)
用法
1
创建虚拟环境
打开
Terminal
并创建一个 python 虚拟环境。Copy
python3 -m venv .venv
source .venv/bin/activate
2
设置你的 API 密钥
Copy
export OPENAI_API_KEY=xxx
3
安装库
Copy
pip install -U openai agno
4
运行 Agent
Copy
python cookbook/models/openai/chat/generate_images.py