Dall-E 图像生成器 (Dall-E Image Generator)
这个笔记本展示了如何使用 OpenAI LLM 合成的提示生成图像。这些图像是使用 Dall-E 生成的,它使用与 LLM 相同的 OpenAI API 密钥。
# 如果您想在笔记本中显示图像,则需要安装以下软件包
pip install opencv-python scikit-image
from langchain.llms import OpenAI
import os
os.environ["OPENAI_API_KEY"] = "<your-key-here>"
作为链运行 (Run as a chain)
from langchain.utilities.dalle_image_generator import DallEAPIWrapper
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
input_variables=["image_desc"],
template="根据以下描述生成一个图像的详细提示:{image_desc}",
)
chain = LLMChain(llm=llm, prompt=prompt)
image_url = DallEAPIWrapper().run(chain.run("在一个闹鬼的博物馆里的万圣节之夜"))
https://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-mg1OWiziXxQN1aR2XRsLNndg.png?st=2023-01-31T07%3A34%3A15Z&se=2023-01-31T09%3A34%3A15Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A44Z&ske=2023-01-31T22%3A19%3A44Z&sks=b&skv=2021-08-06&sig=XDPee5aEng%2BcbXq2mqhh39uHGZTBmJgGAerSd0g%2BMEs%3D
# 您可以点击上面的链接显示图像
# 或者您可以尝试以下选项在此笔记本中内联显示图像
try:
import google.colab
IN_COLAB = True
except:
IN_COLAB = False
if IN_COLAB:
from google.colab.patches import cv2_imshow # 用于图像显示
from skimage import io
image = io.imread(image_url)
cv2_imshow(image)
else:
import cv2
from skimage import io
image = io.imread(image_url)
cv2.imshow('image', image)
cv2.waitKey(0) # 等待键盘输入
cv2.destroyAllWindows()
作为带有代理的工具运行 (Run as a tool with an agent)
from langchain.agents import load_tools
from langchain.agents import initialize_agent
tools = load_tools(['dalle-image-generator'])
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
output = agent.run("创建一个闹鬼的博物馆里的万圣节之夜的图像")
> 进入新的 AgentExecutor 链...
将此描述转化为图像的最佳方法是什么?
操作:Dall-E 图像生成器
操作输入:A spooky Halloween night at a haunted museumhttps://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A36Z&ske=2023-01-31T22%3A19%3A36Z&sks=b&skv=2021-08-06&sig=XsomxxBfu2CP78SzR9lrWUlbask4wBNnaMsHamy4VvU%3D
观察:https://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A36Z&ske=2023-01-31T22%3A19%3A36Z&sks=b&skv=2021-08-06&sig=XsomxxBfu2CP78SzR9lrWUlbask4wBNnaMsHamy4VvU%3D
思考:有了生成的图像,我现在可以给出最终答案。
最终答案:您可以在此处查看一个闹鬼的博物馆里的万圣节之夜的图像:https://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22
> 完成链。