DALLEImageGenerator
使用 OpenAI 的 DALL-E 模型生成图像。
| pipeline 中的最常见位置 | 在 PromptBuilder 之后,灵活 |
| 必需的初始化变量 | "api_key": OpenAI API 密钥。可以使用OPENAI_API_KEY 环境变量设置。 |
| 强制运行变量 | “prompt”: 包含模型提示的字符串 |
| 输出变量 | “images”: 一系列生成的图像 ”revised_prompt”: 如果 OpenAI 对提示进行了任何修改,则包含用于生成图像的提示的字符串 |
| API 参考 | Generators (生成器) |
| GitHub 链接 | https://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/openai_dalle.py |
概述
该DALLEImageGenerator 组件使用 OpenAI 的 DALL-E 模型生成图像。
默认情况下,该组件使用dall-e-3 模型、标准图片质量和 1024x1024 分辨率。您可以通过以下方式更改这些参数:model(在组件初始化期间),quality,和size(在组件初始化或运行时)参数。
DALLEImageGenerator 需要 OpenAI 密钥才能工作。它使用一个OPENAI_API_KEY 环境变量,默认为此。否则,您可以在初始化时使用api_key:
image_generator = DALLEImageGenerator(api_key=Secret.from_token("<your-api-key>"))
查看我们的 API 参考 以获取详细的组件参数说明,或 OpenAI 文档 以获取 OpenAI API 参数的详细信息。
用法
单独使用
from haystack.components.generators import DALLEImageGenerator
image_generator = DALLEImageGenerator()
response = image_generator.run("Show me a picture of a black cat.")
print(response)
在 pipeline 中
在下面的管道中,我们首先设置一个PromptBuilder,它将使用详细的模板描述各种艺术元素来构建图像描述。然后,该管道将此结构化提示传递给一个DALLEImageGenerator,以根据此详细描述生成图像。
from haystack import Pipeline
from haystack.components.generators import DALLEImageGenerator
from haystack.components.builders import PromptBuilder
prompt_builder = PromptBuilder(
template="""Create a {style} image with the following details:
Main subject: {prompt}
Artistic style: {art_style}
Lighting: {lighting}
Color palette: {colors}
Composition: {composition}
Additional details: {details}"""
)
image_generator = DALLEImageGenerator()
pipeline = Pipeline()
pipeline.add_component("prompt_builder", prompt_builder)
pipeline.add_component("image_generator", image_generator)
pipeline.connect("prompt_builder.prompt", "image_generator.prompt")
results = pipeline.run(
{
"prompt": "a mystical treehouse library",
"style": "photorealistic",
"art_style": "fantasy concept art with intricate details",
"lighting": "dusk with warm lantern light glowing from within",
"colors": "rich earth tones, deep greens, and golden accents",
"composition": "wide angle view showing the entire structure nestled in an ancient oak tree",
"details": "spiral staircases wrapping around branches, stained glass windows, floating books, and magical fireflies providing ambient illumination"
}
)
generated_images = results["image_generator"]["images"]
revised_prompt = results["image_generator"]["revised_prompt"]
print(f"Generated image URL: {generated_images[0]}")
print(f"Revised prompt: {revised_prompt}")
更新于 11 个月前
