0Pricing
AI Prompt Engineering · 课时

消除提示中的歧义

学习消除指令多种解读方式的技巧

消除提示中的歧义 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。

歧义对人工智能的影响

有歧义的提示词会迫使模型在多种有效理解之间做出选择。它会选择一种——通常是最常见的那一种——然后自信地执行,却不会说明自己做出了选择。

结果是:您得到了一份格式完全正确、却回答了错误问题的答案。在发送之前识别并消除歧义,比事后重写输出更快。

经典案例:“让它更好”

“让它更好”也许是世上最有歧义的提示词。要怎样更好?

  • 更短?更长?
  • 更正式?更随意?
  • 更多示例?更少示例?
  • 不同的语气?不同的结构?
  • 修正语法?更换词汇?

模型会选择一个维度并对其进行修改。如果那不是您想要的维度,您就会陷入不断重写的循环。

import anthropic

client = anthropic.Anthropic(api_key='sk-ant-your-key-here')

original_text = (
    'Our software helps companies manage their data. '
    'It has many features. Customers like it a lot.'
)

# Ambiguous improvement request
vague = f'Make this better:\n\n{original_text}'

# Unambiguous improvement request
specific = (
    f'Rewrite this product description to be exactly 50% shorter, '
    f'more confident in tone, and replace vague phrases like "many features" '
    f'and "a lot" with specific claims. Do not add new features I have not mentioned.\n\n'
    f'{original_text}'
)

for label, prompt in [('VAGUE', vague), ('SPECIFIC', specific)]:
    response = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=200,
        messages=[{'role': 'user', 'content': prompt}]
    )
    print(f'--- {label} ---')
    print(response.content[0].text)
    print()

多种理解:实际示例

任何包含指代不明的代词、相对形容词或缺失主语的提示词,都可能存在歧义。例如:

  • “提升性能”——提升什么的性能?速度、准确性,还是用户参与度?
  • “写一写影响”——是积极的、消极的、经济方面的,还是社会方面的影响?
  • “修复这个”——修复逻辑、风格、格式,还是语法?
  • “让它更专业”——使用正式词汇?采用结构化段落?删除表情符号?
import openai

client = openai.OpenAI(api_key='sk-your-key-here')

# Disambiguating 'fix this code'
buggy_code = 'def divide(a, b): return a / b'

ambiguous = f'Fix this:\n{buggy_code}'

unambiguous = (
    f'Fix only the division-by-zero bug in this function. '
    f'Add a guard that raises a ValueError with message "b cannot be zero" when b=0. '
    f'Do not change anything else — keep the function signature and return type identical.\n\n'
    f'{buggy_code}'
)

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{'role': 'user', 'content': unambiguous}]
)
print(response.choices[0].message.content)

消除歧义技巧:说明您的理解

如果您知道自己的提示词可能有多种理解方式,请明确说明您想要哪一种理解。

模板:“当我说[有歧义的术语]时,我指的是[具体定义]。”

示例:

  • “当我说编辑时,我指的是只修正语法和拼写,不要改变内容。”
  • “当我说简洁时,我指的是最多 3 个句子。”
  • “当我说专业时,我指的是不使用第一人称代词,也不使用缩写形式。”
import anthropic

client = anthropic.Anthropic(api_key='sk-ant-your-key-here')

response = client.messages.create(
    model='claude-opus-4-5',
    max_tokens=256,
    messages=[{
        'role': 'user',
        'content': (
            'Edit the following paragraph. '
            'When I say "edit", I mean: fix grammar and punctuation only. '
            'Do NOT change vocabulary, sentence structure, or content. '
            'When done, list each change you made in a numbered list below the edited text.\n\n'
            'The team have went to the meeting early, but the manager '
            'werent there so they waited for alot of time before leaving.'
        )
    }]
)
print(response.content[0].text)

消除歧义技巧:定义范围

在要求改进或扩展内容时,范围歧义很常见。请明确界定哪些内容属于范围之内,哪些属于范围之外。

范围之内:模型可以修改的内容
范围之外:必须保持不变的内容

对于代码编辑、文档修订和数据集处理等任务,这一点尤其重要,因为意外的改动可能造成严重问题。

import openai

client = openai.OpenAI(api_key='sk-your-key-here')

code_block = '''
def calculate_tax(income, rate):
    return income * rate

def calculate_net(income, tax):
    return income - tax
'''

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{
        'role': 'user',
        'content': (
            'Add Python type hints to the following code.\n'
            'IN SCOPE: adding type hints to parameters and return values only.\n'
            'OUT OF SCOPE: changing function names, logic, docstrings, or formatting.\n'
            'Do not add any comments or docstrings.\n\n'
            + code_block
        )
    }]
)
print(response.choices[0].message.content)

消除歧义技巧:指定输出形式

当您想要特定格式却没有说明时,就会产生输出歧义。“给我这些数据”——是段落?列表?表格?还是 JSON 对象?

请始终明确写出您期望的确切输出形式。只要明确说明,模型就会精准地匹配该形式。

import anthropic

client = anthropic.Anthropic(api_key='sk-ant-your-key-here')

candidates_text = (
    'Alice: 5 years Python, worked at Stripe, has a CS degree.\n'
    'Bob: 3 years JavaScript, worked at a startup, self-taught.\n'
    'Carol: 8 years Java, worked at Google, has an MS in CS.'
)

response = client.messages.create(
    model='claude-opus-4-5',
    max_tokens=300,
    messages=[{
        'role': 'user',
        'content': (
            'Extract the candidate data below into a JSON array. '
            'Each object must have exactly these keys: name, years_experience, primary_language, '
            'previous_employer, education_level. '
            'education_level values: DEGREE, MASTERS, SELF_TAUGHT.\n\n'
            + candidates_text
        )
    }]
)
print(response.content[0].text)

消除歧义技巧:请模型澄清

编写复杂且明知存在歧义的提示词时,您可以指示模型在尝试执行任务之前先提出澄清性问题。

在构建人工智能助手时,这一方法尤其有用——您希望模型收集信息,而不是自行假设,就像优秀的人类顾问一样。

import openai

client = openai.OpenAI(api_key='sk-your-key-here')

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[
        {
            'role': 'system',
            'content': (
                'You are a professional copywriter. '
                'Before starting any writing task, ask exactly 3 clarifying questions '
                'that would most improve the output quality. '
                'Only proceed to write after the user answers those questions.'
            )
        },
        {
            'role': 'user',
            'content': 'Write a landing page headline for my business.'
        }
    ]
)
print(response.choices[0].message.content)

需要明确界定的相对词语

相对词语没有固定含义——不同的人会有不同理解:

  • “简洁”——1 个句子?3 个句子?1 个段落?
  • “正式”——不使用缩写形式?采用学术引用格式?使用法律语言?
  • “简单”——五年级阅读水平?不使用技术术语?只使用短句?
  • “全面”——涵盖所有主要情况?包括边缘情况?提供示例?

请将每个相对词语替换为具体且可衡量的表达。

import anthropic

client = anthropic.Anthropic(api_key='sk-ant-your-key-here')

# Relative terms mapped to concrete equivalents
mapping = [
    ('Give me a brief summary',   'Summarize in exactly 2 sentences'),
    ('Write something formal',    'Write using no contractions, no first person, and Flesch-Kincaid grade 12+'),
    ('Make it simple',            'Use only words a 10-year-old would know; max 15 words per sentence'),
    ('Be comprehensive',          'Cover at least 5 distinct subtopics with one example each'),
]

for vague, concrete in mapping:
    print(f'Vague:    "{vague}"')
    print(f'Concrete: "{concrete}"')
    print()

主语歧义:谁执行操作?

当指令适用于谁或什么不明确时,就会产生主语歧义。

“重写引言”——重写什么的引言?您粘贴的文档?我们 5 轮对话前讨论的文档?还是一篇新的文档?

“翻译这个”——翻译哪一部分?整个回复?只是摘要部分?还是代码注释?

请始终写明您指的是哪个具体对象或部分,尤其是在多轮对话中。

import openai

client = openai.OpenAI(api_key='sk-your-key-here')

# Clear subject reference prevents wrong-section edits
contract_text = (
    '## Section 1: Payment Terms\nPayment is due within 30 days.\n\n'
    '## Section 2: Termination\nEither party may terminate with 30 days notice.'
)

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{
        'role': 'user',
        'content': (
            'In the contract below, rewrite ONLY Section 2 (Termination). '
            'Change the notice period from 30 days to 90 days. '
            'Do not change Section 1 or any other text.\n\n'
            + contract_text
        )
    }]
)
print(response.choices[0].message.content)

时间歧义:什么时候?

提示词中的时间表达可能存在歧义:“最近的”“当前的”“最新的”“现在”。

模型的训练数据有截止日期——除非您告诉它,否则它不知道“现在”指的是什么。对于有时效性的任务,请始终提供明确日期。

  • “最近的研究”→“发表于 2024 年或 2025 年的研究”
  • “当前的最佳实践”→“截至 2025 年 1 月的最佳实践”
  • “最新版本”→“3.12 版(2024 年 10 月发布)”
import anthropic
from datetime import date

client = anthropic.Anthropic(api_key='sk-ant-your-key-here')

today = date.today().isoformat()

response = client.messages.create(
    model='claude-opus-4-5',
    max_tokens=256,
    system=f'Today is {today}. Use this as the reference for any time-related words.',
    messages=[{
        'role': 'user',
        'content': (
            'Describe the best practices for Python async programming '
            'as of January 2025. If your knowledge does not cover this timeframe, '
            'say so explicitly and share what you know up to your cutoff.'
        )
    }]
)
print(response.content[0].text)

建立歧义雷达

发送任何提示词之前,请检查其中是否存在以下歧义信号:

  • 没有明确指代对象的代词:它、这、那、他们
  • 相对形容词:更好、更短、正式、简单、全面、最近
  • 含糊的动词:修复、改进、更新、制作、处理一下
  • 缺少范围:没有说明哪些内容属于范围之内或范围之外
  • 缺少格式:没有指定输出格式
  • 缺少上下文:没有说明受众是谁,也没有说明输出的用途
def scan_prompt_for_ambiguity(prompt):
    '''Simple heuristic scanner for common ambiguity patterns.'''
    warnings = []
    vague_verbs = ['fix', 'improve', 'make it', 'update', 'change it', 'redo']
    relative_adj = ['better', 'shorter', 'longer', 'formal', 'simple', 'recent', 'comprehensive']
    missing_format = ['json', 'table', 'list', 'bullet', 'paragraph', 'word', 'sentence']

    lower = prompt.lower()
    for v in vague_verbs:
        if v in lower:
            warnings.append(f'Vague verb detected: "{v}" — specify what change exactly')
    for a in relative_adj:
        if a in lower:
            warnings.append(f'Relative adjective: "{a}" — anchor with a measurable definition')
    if not any(f in lower for f in missing_format):
        warnings.append('No output format specified — add format, length, or structure')
    return warnings

test = 'Make the report better and more formal.'
print('Prompt:', test)
for w in scan_prompt_for_ambiguity(test):
    print(' WARNING:', w)

知识检查

一位开发者发送了以下提示词:“简化代码。”人工智能缩短了代码,但开发者想要的是使用更易读的变量名称,而不是让代码变短。核心问题是什么?

消除歧义——回顾

歧义会迫使模型猜测,而它的猜测是最普通的理解方式,并不是您的理解。消除歧义的关键技巧:

  • 说明您的理解:“当我说 X 时,我指的是 Y”
  • 定义范围:列出哪些内容属于范围之内和范围之外
  • 写明输出形式:JSON / 表格 / 段落 / 句子数量
  • 明确相对词语:“简洁”→“2 个句子”,“正式”→“不使用缩写形式”
  • 写明主语:“第 2 节”,而不是“这部分”
  • 请求澄清:指示模型在继续之前先提出问题

常见问题解答

「消除提示中的歧义」课时是免费的吗?

是的 — 「消除提示中的歧义」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。

「消除提示中的歧义」这节课中我会学到什么?

学习消除指令多种解读方式的技巧 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AI Prompt Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「消除提示中的歧义」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AI Prompt Engineering 课中编写并运行代码吗?

能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为什么具体性很重要
  2. 消除提示中的歧义
  3. 添加具体细节
  4. 比较模糊提示与具体提示
← 返回 AI Prompt Engineering