P
Prompt Master
Prompt Engineering 教程与提示词实战

从任务定义、示例和工作流到评测与安全边界

Prompt Engineering 教程与提示词实战Prompt Library

TikZ diagram

generate TikZ code prompt example

本章 Prompt 决定
01要解决的 Prompt 问题

generate TikZ code prompt example

02完成后带走

一份保留任务边界的可复用 Prompt、示例集、评测记录或安全规则。

03做到什么算完成

至少跑一个代表性样本,检查结果,并记录仍需人工复核的部分。

TL;DR(中文)

  • 这是一个 code generation + “可视化结果验证”的任务:模型生成 TikZ(LaTeX)代码,你在本地编译看图是否符合预期。
  • 关键技巧是 iterative prompting:先要一个粗版本,再用具体 feedback 修正(比例、位置、细节)。
  • 落地建议:把画面拆成 primitives(circle/line/rectangle)、约束坐标系与比例,并要求输出可编译的最小示例。

Background

This prompt tests an LLM's code generation capabilities by prompting it to draw an object in TikZ. The model is expected to generate LaTeX code that can then be compiled by the user.

How to Apply(中文)

把这个任务当成 “spec → code → compile → feedback → refine” 的循环:

  1. 定义对象的关键部件(body/head/legs/horn…)与大致几何关系
  2. 让模型输出可编译的 TikZ 代码(最好包含 \\begin{tikzpicture}
  3. 编译后给出具体反馈(坐标、比例、缺少元素)

How to Iterate(中文)

  1. 限制坐标范围(例如 x/y 在 [-5,5]),避免图形跑飞
  2. 要求分层输出:先画轮廓,再加细节
  3. 提供失败反馈:贴出编译报错或截图描述(但不要放进 code block 里)
  4. 让模型输出 “parameters” 方便调整(例如 scale、line width)

Self-check rubric(中文)

  • 代码是否可编译(LaTeX/TikZ syntax correct)?
  • 是否符合 prompt 的关键要求(unicorn 的辨识度)?
  • 是否易于迭代(结构清晰、参数可调)?

Practice(中文)

练习:把对象换成你工作里常见的图:

  • flowchart
  • system architecture diagram
  • mind map

要求:每个 node 有 label,布局不重叠,并输出可编译代码。

Prompt

Draw a unicorn in TikZ

Code / API

OpenAI (Python)

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {
            "role": "user",
            "content": "Draw a unicorn in TikZ",
        }
    ],
    temperature=1,
    max_tokens=1000,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0,
)

Fireworks (Python)

import fireworks.client

fireworks.client.api_key = "<FIREWORKS_API_KEY>"

completion = fireworks.client.ChatCompletion.create(
    model="accounts/fireworks/models/mixtral-8x7b-instruct",
    messages=[
        {
            "role": "user",
            "content": "Draw a unicorn in TikZ",
        }
    ],
    stop=["<|im_start|>", "<|im_end|>", "<|endoftext|>"],
    stream=True,
    n=1,
    top_p=1,
    top_k=40,
    presence_penalty=0,
    frequency_penalty=0,
    prompt_truncate_len=1024,
    context_length_exceeded_behavior="truncate",
    temperature=0.9,
    max_tokens=4000,
)

Reference

📚 相关资源