logo
P
Prompt Master

Prompt 大师

掌握和 AI 对话的艺术

Code snippets

generate code from a comment/instruction

TL;DR(中文)

  • 这是一个最小的 code generation 测试:给模型一个自然语言 instruction(放在 comment 里),让它输出可运行的代码。
  • 关键风险:模型会遗漏输入/输出、忽略边界条件、或生成与目标语言/运行环境不匹配的代码。
  • 落地建议:把 instruction 写成 checklist(language/runtime/IO/examples/error handling),并用 test cases 做 evaluation

Background

This prompt tests an LLM's code generation capabilities by asking it to generate a code snippet given details about the program through a comment using /* <instruction> */.

How to Apply(中文)

你可以把 “comment instruction” 当成一个稳定的输入协议:

  • 统一用 /* ... */(或你团队约定的格式)描述需求
  • 明确语言与运行环境(browser / Node.js / Python)
  • 明确输入输出(CLI / function / API handler)
  • 给 1-3 个 examples(input → output)

这样模型更容易生成可执行的代码,而不是只输出伪代码。

How to Iterate(中文)

  1. 加约束:语言版本、依赖限制、禁止使用某些 API
  2. 加测试:要求输出同时给出 3-5 个 test cases
  3. self-check:让模型先列 “assumptions”,再生成代码
  4. 多轮迭代:先让模型输出 plan/接口,再让它补实现细节

Self-check rubric(中文)

  • 是否满足需求(功能正确)?
  • 是否能运行(语法、依赖、环境匹配)?
  • 是否覆盖边界情况与错误处理?
  • 是否遵守约束(不使用禁止的库/API)?

Practice(中文)

练习:把 instruction 换成你工作里真实的小需求,并至少补齐:

  • language/runtime
  • function signature or CLI interface
  • 2-3 examples

然后用 test cases 回归对比不同模型/不同 prompt 的质量。

Prompt

/_
Ask the user for their name and say "Hello"
_/

Code / API

OpenAI (Python)

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {
            "role": "user",
            "content": '/*\nAsk the user for their name and say "Hello"\n*/',
        }
    ],
    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": '/*\nAsk the user for their name and say "Hello"\n*/',
        }
    ],
    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,
)