Generate Images from Text Prompts
Generate images and image variations from text via OpenAI's Image endpoint in a LlamaIndex agent.
Why it matters
Leverage AI to create compelling visual content from textual descriptions. This tool integrates with OpenAI's image generation capabilities, allowing for the creation of original images and variations based on existing ones.
Outcomes
What it gets done
Generate images based on detailed text prompts.
Create variations of existing images using their URLs.
Integrate image generation into automated workflows.
Display generated images within notebook environments.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-tool-tools-text-to-image | bash Overview
Text to Image Tool
A LlamaIndex tool that generates and varies images via OpenAI's Image endpoint, with notebook display support. Use for text-to-image generation and iterative refinement across a conversation, given an OpenAI API key.
What it does
The Text to Image Tool lets a LlamaIndex agent use OpenAI's Image endpoint to generate new images from text and create variations of existing images. TextToImageToolSpec can be initialized either by setting openai.api_key globally or by passing api_key directly to the tool spec.
Three functions are exposed. generate_images creates images from a text prompt, letting the caller specify the number of images and the resolution. show_images displays generated images using matplotlib, useful specifically inside Jupyter notebooks where inline image display matters. generate_image_variation takes a URL to an existing image and produces a variation of it. The source's own worked example shows a multi-turn flow with a Context object to preserve chat history: first asking for two images of a beach at sunset, then a follow-up asking to improve the quality of the second image - showing the tool supports iterative refinement across a conversation, not just one-shot generation.
When to use - and when NOT to
Use it when an agent or notebook workflow needs to generate images from text descriptions, or create variations of an existing image via URL. Use a Context object across agent.run calls specifically when you need follow-up requests (like refining a previously generated image) to reference earlier results in the same conversation. Use show_images only in a Jupyter-notebook-style environment where matplotlib display works. Do not use it without an OpenAI API key, set globally or passed directly to the tool spec.
Capabilities
generate_images creates images from a text prompt with configurable count and resolution. show_images displays images via matplotlib in notebooks. generate_image_variation produces a variation of an image given its URL.
How to install
from llama_index.tools.text_to_image import TextToImageToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
openai.api_key = "sk-your-key"
tool_spec = TextToImageToolSpec()
An OpenAI API key is required, either set globally as shown above or passed directly as TextToImageToolSpec(api_key="sk-your-key").
Who it's for
Developers building LlamaIndex agents or notebook workflows that need to generate and iteratively refine images from text descriptions using OpenAI's Image endpoint.
Source README
Text to Image Tool
This tool allows Agents to use the OpenAI Image endpoint to generate and create variations of images.
Usage
This tool has more extensive example usage documented in a Jupyter notebook here
Another example showcases retrieval augmentation over a knowledge corpus with text-to-image. Notebook.
from llama_index.tools.text_to_image import TextToImageToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
openai.api_key = "sk-your-key"
tool_spec = TextToImageToolSpec()
### OR
tool_spec = TextToImageToolSpec(api_key="sk-your-key")
agent = FunctionAgent(
tools=tool_spec.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
### Context to store chat history
from llama_index.core.workflow import Context
ctx = Context(agent)
print(
await agent.run(
"show 2 images of a beautiful beach with a palm tree at sunset",
ctx=ctx,
)
)
print(await agent.run("make the second image higher quality", ctx=ctx))
generate_images: Generate images from a prompt, specifying the number of images and resolutionshow_images: Show the images using matplot, useful for Jupyter notebooksgenerate_image_variation: Generate a variation of an image given a URL.
This loader is designed to be used as a way to load data as a Tool in a Agent.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.