logo
P
Prompt Master

Prompt 大师

掌握和 AI 对话的艺术

TikZ diagram

generate TikZ code prompt example

#TL;DR(中文)

  • 这是一个 code generation + “可视化结果验证”的任务:模型生成
    code
    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 代码(最好包含
    code
    \\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

text
Draw a unicorn in TikZ

#Code / API

#OpenAI (Python)

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)

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

1v1免费职业咨询