第3天:AI中的提示技术(第一部分)

发布: (2026年5月4日 GMT+8 11:15)
3 分钟阅读
原文: Dev.to

Source: Dev.to

Day 3:AI 中的提示技术(第 1 部分)封面图片

AI 提示技术:Zero-shot、One-shot、Few-shot

在使用 ChatGPT 和其他 AI 工具之后,我曾以为提示只是简单的文本输入,AI 模型会神奇地处理它们。正如我在 Day 1 帖子中提到的,AI 模型是 下一个词的预测器,而不是思考者。它们基于训练数据进行预测(不过现代模型现在会使用实时搜索和工具调用来获得更好的结果)。

基础

获取 AI 模型响应的最简 Python 代码片段:

response = client.responses.create(
    model="gpt-5-nano",
    input=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": "How do I reverse a list in Python?"},
    ]
)

最重要 的一点是:role 键有三种可能的取值——systemuserassistant。系统提示决定模型的行为。

1. Zero-shot 提示

没有示例——仅在系统提示中给出指令:

SYSTEM_PROMPT = """
You are a helpful assistant that only answers Python programming questions.
If the user asks about anything else, politely decline.
"""

2. One-shot 提示

提供一个模型应如何响应的示例:

input = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": "What is the capital of France?"},
    {"role": "assistant", "content": "I'm sorry, I can only help with Python programming questions."},
    {"role": "user", "content": "How do I reverse a list in Python?"},
]

3. Few-shot 提示

提供多个示例,使模型的响应更精确:

input = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": "How to code a binary tree in Python?"},
    {"role": "assistant", "content": "Sure, here's an implementation..."},
    {"role": "user", "content": "What is the weather today?"},
    {"role": "assistant", "content": "I'm sorry, I can only help with Python programming questions."},
    {"role": "user", "content": "What is the capital of France?"},
    {"role": "assistant", "content": "I'm sorry, I can only help with Python programming questions."},
    {"role": "user", "content": "Why is 75% attendance required for the exam?"},
]

因此,作为一个想进入 AI 工程领域的初学者,你需要将思维方式从盲目与 AI 聊天转变为 为自己的 AI 产品设计系统提示

链接

0 浏览
Back to Blog

相关文章

阅读更多 »