导语|近年来,AI生成内容(AIGC)领域的飞速发展催生了诸多创新技术与应用,尤其是AI绘画。为降低构建Prompt的复杂性,我们基于ChatGPT实现了一个Stable Diffusion提示词生成器,支持多语言提示词和智能优化与扩展提示词,助力初学者轻松探索AI绘画世界。

本文作者:perryprli,腾讯WXG工程师

一、背景

近年来,AI生成内容(AIGC)领域发展迅猛,涌现出众多创新技术和应用。其中,AI绘画作为一个重要的分支,自2022年起取得了显著的进展,吸引了广泛关注。

从2022年2月初备受瞩目的Disco Diffusion,到4月份OpenAI发布的强大文本到图像生成模型DALL·E 2,紧接着5月份Google推出了Imagen模型。同年8月份,初创公司StabilityAI发布并开源了卓越的AI绘画模型Stable Diffusion

今年,令人惊叹且广受好评的Midjourney也成为了热门话题。这些技术和应用的快速发展充分展现了AI绘画领域的巨大潜力。

目前,AI绘画模型中最受欢迎的无疑是Stable Diffusion和Midjourney。在AI绘画领域,Prompt(提示词)起着至关重要的作用。通常情况下,Prompt质量越高,生成的图片效果越令人满意。

然而,无论是Stable Diffusion还是Midjourney,都面临着构建Prompt的复杂性较高,对于初学者来说具有一定的门槛。此外,这些模型都是基于英文语料数据集进行训练的,因此并不原生支持中文提示词。

为了解决这一问题,我们借助ChatGPT实现了一个Stable Diffusion的提示词生成器(Prompt Generator),并搭建了一个将文本生成图片的服务,从而降低了AI绘画的门槛。具体特点包括:

  1. 支持包括中文在内的多种语言提示词
  2. 支持智能优化与扩展提示词功能
  3. 只需输入少量关键词,即可生成效果不错的图片

二、实现原理

1. Stable Diffusion

1.1 简介

Stable Diffusion是一个2022年发布的深度学习文本到图像的潜在扩散模型(LDM / Latent Diffusion Model),由CompVis、Stability AI和LAION的研究人员和工程师创建。它使用来自LAION-5B开源数据库子集的512x512图像进行训练,通过引入隐向量空间来解决Diffusion速度瓶颈,除了可用于文生图任务,还可以用于图生图、特定角色刻画,甚至是超分或者上色任务。本文不对其原理进行赘述,感兴趣的同学可以阅读原论文

1.2 如何部署

Stable Diffusion WebUI的有多种部署方式,这里介绍最简单的方式:Docker部署。我们在一个8GB GPU上就可以运行和体验该服务。只需要下面介绍具体的步骤:

  1. 拉取Github代码
git clone https://github.com/AbdBarho/stable-diffusion-webui-docker.git
 2. 运行

# 进入项目目录cd stable-diffusion-webui-docker# 下载相关模型docker compose --profile download up --build# 等待下载完成后,编译运行镜像docker compose --profile auto up --build
 3. 打开WebUI

默认运行端口为7860,获取服务器IP后,在浏览器打开链接: http://{ip}:7860/. 页面如下所示:

  1. 下载其他模型

默认下载的sd模型是v1.5版本的,包括v1-5-pruned-emaonly.ckpt和sd-v1-5-inpainting.ckpt。如果需要体验其他模型,可以先访问这个网站: https://civitai.com/ 。上面可以找到各种基于sd的开源模型和效果图,以及模型对应的点赞数、下载量和评价。可以根据业务需求或者点赞数/下载量来筛选模型,然后下载到部署的服务器上,sd模型的存放路径为:

stable-diffusion-webui-docker/data/StableDiffusion/

我们AI绘画服务默认部署的是DreamShaper模型。

2. Prompt Generator

Stable Diffusion的提示词包括两种:正面和负面提示词。简单来说,正面提示词是你希望模型生成的图片内容,负面提示词是不希望模型生成的内容。例如,sd 不太擅长生成手,我们可以在负面提示词中加入“bad hands”、“missing fingers”、“extra fingers”等词。关于如何写出高质量的提示词,可以参考这篇文章:《如何写好Stable Diffusion提示词?》

本文中提到的提示词生成器,指的是正面提示词的自动生成。负面提示词相对来说比较简单和固定,一般没必要进行生成。我们使用的默认负面提示词如下:

facelowres,bad anatomy,bad hands,text,error,missing fingers,extra digit,fewer digits,cropped,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,username,blurry,bad feet,ugly,duplicate,trannsexual,hermaphrodite,out of frame,extra fingers,mutated hands,poorly drawn hands,poorly drawn face,mutation,deformed,blurry,bad anatomy,bad proportions,extra limbs,cloned face,disfigured,more than 2 nipples,out of frame,ugly,extra limbs,bad anatomy,gross,worst quality,low quality,normal quality,signature,watermark,username,blurry,proportions,malformed limbs,missing arms,missing legs,extra arms,extra legs,mutated hands,fused fingers,too many fingers,long neck
如何基于ChatGPT实现AI绘画的提示词生成器?核心是Prompt Engineering。

提到Prompt工程,最近吴恩达教授与 OpenAI 合作推出的 《ChatGPT Prompt Engineering for Developers》教程面向入门 LLM 的开发者,深入浅出地介绍了对于开发者,如何构造 Prompt 并基于 OpenAI 提供的 API 实现包括总结、推断、转换等多种常用功能,是入门 LLM 开发的经典教程。本文会结合该教程,介绍AI绘画提示词生成器的最佳实践。

2.1 背景知识

由于ChatGPT的训练数据只截止到2021年,有一些新的知识它是不知道的。因此我们对于一些特定的任务,我们需要提供一些背景知识给ChatGPT。例如,一句话介绍什么是Stable Diffusion。

Stable Diffusion is an AI art generation model similar to DALLE-2.
2.2  任务要求

吴恩达教授在教程的Guidelines中提到,Prompt工程有两个原则:

  1. Write clear and specific instructions
  2. Give the model time to “think”

我们需要保证任务要求清晰和明确。对于复杂的任务,还需要将任务进行拆解,把一个综合任务拆解成若干个小的任务。例如,我们可以将该任务拆分成两个子任务。如下所示:

Please perform the following actions:1. Translate the IDEA into English if it is not in English.2. Write a detailed prompt exactly about the English IDEA follow the rules delimited by triple backticks.Rules: ```1. Follow the structure of the example prompts. This means Write a description of the scene, followed by modifiers divided by commas to alter the mood, style, lighting, and more, excluding the artist name, separated by commas.2. Use at most 50 words.```

大家可能会疑惑,这个任务不是挺简单的么,为什么要拆解成两个子任务?不这样做会有什么影响吗?答案是:拆解任务会增加ChatGPT输出结果的可靠性和鲁棒性。

因为我们在实验的过程中发现,对于中文提示词,如果不拆解任务,就算在Prompt中要求ChatGPT输出英文内容,有时候它还是会输出中文的结果。所以,如果要求ChatGPT先将用户输入的非英文提示词翻译成英文,再对英文提示词进行优化,输出的结果才会保证是英文的。

此外,ChatGPT有时候会生成一大段文本,而内容太长反而会影响AI绘画的效果。因此,我们需要在Prompt中限制其返回结果的字数,对应上面的第二条规则:

Use at most 50 words.

2.3 合规性检查

作为一个AI绘画服务,需要过滤/拒绝不合规的绘画提示词,避免AI生成不健康的内容。因此我们需要对提示词进行合规性检查,包括检查提示词是否包含色情、暴力、政治敏感、误导性、仇恨言论或歧视性语言等。

ChatGPT本身就有内容合规性判别的能力,我们可以借助它的能力来实现合规性检查,对于不满足要求的提示词直接返回某个错误代码(1004)。我们只需要在prompt中加入以下内容:

If the IDEA is inappropriate or misleading or containing hate speech or discriminatory language, then simply write \"1004\"

2.4 少量样本提示("Few-shot" prompting)

为了让ChatGPT生成高质量的绘画提示词,我们可以提供少量样本/例子让它“学习”,即教程中所说的"Few-shot" prompting。该方法本质上是大语言模型(LLM)Fine-tune的一种方式。样本不在多而在精,样本太多会增加ChatGPT的调用成本(按照tokens数量收费)。

在样本数量有限的情况下,我们要尽量选择多样化的样本,使其覆盖尽可能多的场景。例如,我们可以提供以下几个样本(User表示输入文本,Assistant表示预期输出的内容):

样本1

User: 一个弓箭手向森林怪兽射箭

Assistant:

portait of a homer simpson archer shooting arrow at forest monster, front game card, drark, marvel comics, dark, intricate, highly detailed, smooth, artstation, digital illustration

绘图效果:

样本2

User: 画一个海盗

Assistant:

pirate, concept art, deep focus, fantasy, intricate, highly detailed, digital painting, artstation, matte, sharp focus, illustration

绘图效果:

样本3

User: painting of rachel lane / alison brie, hybrid

Assistant:

a fantasy style portrait painting of rachel lane / alison brie hybrid in the style of francois boucher oil painting unreal 5 daz. rpg portrait, extremely detailed artgerm

绘图效果:

样本4

User: 希腊女神雅典娜

Assistant:

athena, greek goddess, claudia black, art by artgerm and greg rutkowski and magali villeneuve, bronze greek armor, owl crown, d & d, fantasy, intricate, portrait, highly detailed, headshot, digital painting, trending on artstation, concept art, sharp focus, illustration

绘图效果:

样本5

User: steve urkle as the hulk

Assistant:

ultra realistic illustration of steve urkle as the hulk, intricate, elegant, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration

绘图效果:

样本6

User: 落霞与孤鹜齐飞,秋水共长天一色

Assistant:

sunset with lone wild duck flying together, autumn water and sky merging into one color, traditional chinese landscape painting, ink wash, liu kuo-sung, guo xi, fan kuan, qi baishi, serene atmosphere, misty mountains, tranquil lake, vibrant colors, harmony with nature, poetic, calligraphy, trending on artstation, concept art, sharp focus, illustration

绘图效果:

通过提供少量样本让ChatGPT“学习”,可以提高模型输出的可靠性,提升Prompt的生成效果。

2.5 小结

首先,我们提供任务相关的背景知识,让ChatGPT对任务有初步的了解。然后,对任务要求进行清晰和明确的描述,并且把相对复杂的任务拆分成两个子任务。

此外,基于ChatGPT实现了提示词内容合规性检查,对不满足条件的请求进行拒绝。

最后,为了提升效果,我们选取了几个不同风格的高质量样本让它“学习”,从而实现举一反三,提高输出结果的可靠性。

Prompt工程不是一蹴而就的,需要根据需求不断迭代调试和实验,从而不断优化我们的Prompt,提升最终效果。这也是教程中提到的 Iterative Prompt Develelopment。

三、效果展示

这里展示几个例子,可以看出Prompt优化前后的效果对比:

1. 中国古代,赛博朋克,机械仙鹤

优化前:cyberpunk mechanical crane in ancient China

优化后:

cyberpunk mechanical crane in ancient China, concept art, art by Johnson Ting, highly detailed, intricate, sharp focus, vivid colors, neon lights, futuristic, digital painting, trending on ArtStation, Unreal Engine 5, 4K UHD image, fantasy, sci-fi, mechanical, robotic, steampunk, industrial, cinematic, atmospheric, epic, breathtaking.

2. 梵高风格,塞尔达旷野之息

优化前:The Legend of Zelda: Breath of the Wild in the style of Vincent van Gogh

优化后:

The Legend of Zelda: Breath of the Wild in the style of Vincent van Gogh, digital painting, art by Lois van Baarle, highly detailed, intricate, vibrant colors, bold brushstrokes, post-impressionist, art nouveau, inspired by Starry Night, landscape, trending on ArtStation, concept art, sharp focus, illustration.

3. 春眠不觉晓,处处闻啼鸟

优化前:Sleeping in Spring, Unaware of the Dawn, Birds Singing Everywhere

优化后:

Sleeping in Spring, Unaware of the Dawn, Birds Singing Everywhere, traditional Chinese landscape painting, ink wash, Li Bai's poem, serene atmosphere, blooming flowers, lush trees, tranquil pond, poetic, calligraphy, trending on artstation, concept art, sharp focus, illustration.

4. 天空之城

优化前:castle in the sky

优化后:

castle in the sky, ghibli studio, hayao miyazaki, anime, floating island, steampunk, intricate, highly detailed, digital painting, artstation, matte, sharp focus, illustration.

四、总结与展望

本文介绍了基于ChatGPT的AI绘图提示词生成器的实现原理,总结了大语言模型Prompt工程的一些实践经验。

我们知道,以ChatGPT为代表的大语言模型在很多领域都可以发挥巨大作用。但很多时候由于没有做好Prompt工程,任务需求描述的不够准确,或者没有提供有效的样本,导致模型的效果不够理想。作为LLM的使用者,我们需要利用Prompt来充分发挥出模型的潜力,从而提高业务的落地效果。

未来随着LLM和AIGC相关技术的发展,Prompt工程可能会越来越简单,甚至在不久的将来可能都不需要Prompt工程。因为到时候大语言模型已经足够聪明,我们只需要简单描述几句话它就能完成复杂的任务。但至少对于目前来说,Prompt工程还是有必要的,希望能跟大家多多交流和探讨。

五、参考文献

  1. Stable Diffusion
  2. High-Resolution Image Synthesis with Latent Diffusion Models
  3. Stable Diffusion WebUI Docker
  4. ChatGPT Prompt Engineering for Developers

— — 技术直播预告 — —

来直播间,听《技术人说》科普对谈!

本期直播,我们邀请了腾讯的最新款调酒机器人来到直播间!!

本周三晚,直播间见👇

本周四晚,还有精彩的技术干货直播!

微信工程师分享:《详解C++类型描述符》

本周四晚,直播间见👇

往期回顾:

基于GPT设计数据产品的一些思考

产品经理解析:如何教会大模型使用工具?

AI热潮下,一名普通美术师的应对

AGI近未来:见证、记录与思考

点个关注,我们下期再见👋

文章来源于腾讯云开发者社区,点击查看原文