# 提示

提示词描述了模型应生成的内容。好的提示词应当详尽、具体且结构清晰，能够生成更优质的图像和视频。

本指南将向您展示如何编写高效的提示词，并介绍一些能够增强其效果的技巧。

## 编写优质提示

每个有效的提示都需要三个核心要素。

1. 主题 - 您想要生成的内容。请从这里开始编写提示词。
2. 风格 - 媒介或美学。它应该呈现什么样的视觉效果？
3. 上下文 - 关于动作、场景和氛围的详细信息。

将这些元素作为结构化的叙述来使用，而非关键词列表。现代模型对语言的理解优于关键词匹配。从简单开始，然后逐步添加细节。

上下文对于创建更好的提示词尤为重要。尝试添加光照、艺术细节和氛围。

  
    
    一只可爱的猫在宁静的夏日午后，惬意地躺在池塘里的一片叶子上，采用 lofi 艺术风格，插画。
  
  
    
    一只可爱的猫咪慵懒地躺在夏日午后波光粼粼的池塘中一片漂浮的叶子上。清澈的倒影在水面上泛起涟漪，阳光洒下柔和而细腻的高光。这幅插画细节丰富、精致考究，线条优雅，色彩和谐，营造出一种令人放松、宁静且充满奇思妙想的低保真氛围，带有动漫风格，视觉上令人倍感舒适。
  

请具体描述并添加上下文。使用摄影术语，例如镜头类型、焦距、拍摄角度和景深。

> [!TIP]
> 尝试使用 [提示词增强器](https://huggingface.co/models?sort=downloads&search=prompt+enhancer) 来帮助你优化提示词结构。

## 引导与负向

大多数 Diffusers 流水线仍然使用无分类器引导（classifier-free guidance）以及可选的 `negative_prompt` 来引导采样过程。

在 Stable Diffusion 系列流水线中，请传入 `guidance_scale` 和 `negative_prompt`。`guidance_scale` 的值越高，生成结果就越严格地遵循提示词。数值过高可能导致画面不自然或色彩过饱和。

```py
import torch
from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, device_map="cuda"  # or "mps", "xpu", "cpu"
)
image = pipeline(
    prompt="a cozy reading nook with afternoon light",
    negative_prompt="blurry, low quality, distorted",
    guidance_scale=7.5,
).images[0]
```

在 Qwen-Image 和 Flux 等较新的检查点上，经典 CFG 通常指 `true_cfg_scale` 与 `negative_prompt` 的组合。当启用真实 CFG 时（即 `true_cfg_scale > 1` 且提供了 `negative_prompt`），负向提示仍然生效。其 `guidance_scale` 参数在 transformer 支持时代表蒸馏或嵌入的引导，这与经典 CFG 并不相同。请查阅所加载模型的 pipeline API 以确认具体行为。

Modular Diffusers 可以用 guider 替换 guidance 算法。请参阅 [Guiders](./guiders)。

## 提示词加权

提示词加权（Prompt weighting）会使某些词的作用增强，而另一些词的作用减弱。它会对注意力分数进行缩放，从而让你控制每个概念的影响程度。

Diffusers 通过 `prompt_embeds` 和 `pooled_prompt_embeds` 参数来处理此问题，这些参数接收缩放后的文本嵌入向量。请使用 [sd_embed](https://github.com/xhinker/sd_embed) 库来生成这些嵌入。它还支持更长的提示词。对于 Stable Diffusion 系列，若希望使用更简洁的语法进行加权，您也可以使用 [Compel](https://github.com/damian0815/compel)。

> [!NOTE]
> sd_embed 库仅支持 Stable Diffusion、Stable Diffusion XL、Stable Diffusion 3、Stable Cascade 和 Flux。对于 Flux 类模型，提示词加权并不总能带来帮助，因为这类模型本身已经能够紧密地遵循提示词。

```shell
uv pip install git+https://github.com/xhinker/sd_embed.git@main
```

使用数值乘数或括号来格式化加权文本。括号越多，权重越强。

| 格式 | multiplier |
|---|---|
| `(cat)` | 增加 1.1 倍 |
| `((cat))` | 增加 1.21 倍 |
| `(cat:1.5)` | 增加 1.5 倍 |
| `(cat:0.5)` | 将权重设置为 0.5× |

创建一个加权提示词，并将其传递给 [get_weighted_text_embeddings_sdxl](https://github.com/xhinker/sd_embed/blob/4a47f71150a22942fa606fb741a1c971d95ba56f/src/sd_embed/embedding_funcs.py#L405) 以生成嵌入。

> [!TIP]
> 您也可以将负面提示词传递给 `negative_prompt_embeds` 和 `negative_pooled_prompt_embeds`。

```py
import torch
from diffusers import DiffusionPipeline
from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl

pipeline = DiffusionPipeline.from_pretrained(
    "Lykon/dreamshaper-xl-1-0", dtype=torch.bfloat16, device_map="cuda"  # or "mps", "xpu", "cpu"
)

prompt = """
A (cute cat:1.4) lounges on a (floating leaf:1.2) in a (sparkling pool:1.1) during a peaceful summer afternoon.
Gentle ripples reflect pastel skies, while (sunlight:1.1) casts soft highlights. The illustration is smooth and polished
with elegant, sketchy lines and subtle gradients, evoking a ((whimsical, nostalgic, dreamy lofi atmosphere:2.0)), 
(anime-inspired:1.6), calming, comforting, and visually serene.
"""

prompt_embeds, _, pooled_prompt_embeds, *_ = get_weighted_text_embeddings_sdxl(pipeline, prompt=prompt)
```

将嵌入传递到 `prompt_embeds` 和 `pooled_prompt_embeds` 以生成您的图像。

```py
image = pipeline(prompt_embeds=prompt_embeds, pooled_prompt_embeds=pooled_prompt_embeds).images[0]
```

  

