prompt engineering(提示工程)的六大核心原则

prompt engineering的六大核心原则包括:

  1. 写清晰的指令:指令必须明确、具体,避免模糊和歧义,以确保模型能够准确理解任务要求。
python 复制代码
def clear_instruction_example():
    system_msg = "你是一个数学助手。请严格按照步骤计算,并只返回最终数字结果。"
    user_msg = "请计算长为 5.2 米、宽为 3.8 米的矩形面积,结果保留两位小数。"
    response = call_llm(system_msg, user_msg)
    print("清晰指令示例输出:", response)
  1. 提供参考文本:通过提供相关的背景信息或示例文本,帮助模型更好地理解任务上下文,生成更符合预期的输出。
python 复制代码
def with_reference_text_example():
    system_msg = """你是一个情感分析专家。请参考以下示例,对用户评论进行情感分类,只输出'正向'或'负向'。
示例1:
评论:"这个产品太棒了,我很喜欢!" -> 正向
示例2:
评论:"质量太差了,非常失望。" -> 负向
"""
    user_msg = "评论:'客服态度很好,问题解决了。'"
    response = call_llm(system_msg, user_msg)
    print("提供参考文本示例输出:", response)
  1. 拆解复杂任务:将复杂任务拆解为多个简单的子任务,逐步引导模型完成,提高输出的准确性和可靠性。
python 复制代码
def decompose_complex_task_example():
    system_msg = """你是一个数据专家。请按以下步骤思考:
步骤1:从用户问题中提取关键数字信息。
步骤2:根据提取的数字,计算"月均支出"和"储蓄率"。
步骤3:以JSON格式输出结果,如:{"月均支出": xxx, "储蓄率": "xx%"}
"""
    user_msg = "我第一季度支出3万,第二季度支出3.5万,第三季度支出4万,第四季度支出4.5万,年收入是25万。请帮我分析。"
    response = call_llm(system_msg, user_msg, stream=True)  # 使用流式输出展示思考过程[reference:6]
  1. 给模型思考时间:对于需要推理或复杂计算的任务,给予模型足够的思考时间,避免急于求成导致输出质量下降。
python 复制代码
def give_model_time_to_think_example():
    system_msg = """你是一个逻辑推理专家。请遵循以下原则:
1. 请一步一步地推理(Step-by-step reasoning)。
2. 在给出最终答案前,先详细列出你的思考过程。
3. 基于你的推理,给出最终结论。"""
    user_msg = "一家公司有63名员工,其中7/9是女性。男性员工中,有1/7是管理层。请问男性管理层有多少人?"
    response = call_llm(system_msg, user_msg, stream=True)  # 流式输出可展示思考过程
  1. 使用外部工具:结合外部工具或数据源,扩展模型的能力边界,提升输出的实用性和准确性。
python 复制代码
def mock_web_search(query: str) -> List[Dict[str, str]]:
    print(f"[系统调用工具]: 正在搜索 '{query}'...")
    return [{"title": "2026年AI发展趋势", "snippet": "2026年AI正朝着多模态...", "url": "https://example.com/1"}]

def use_external_tools_example():
    def call_llm_with_tools(system_msg: str, user_msg: str):
        messages = [{"role": "system", "content": system_msg}, {"role": "user", "content": user_msg}]
        completion = client.chat.completions.create(model=MODEL_NAME, messages=messages, temperature=0.5)
        response = completion.choices[0].message.content
        # 模拟工具调用逻辑 (在实际应用中,这里应解析模型输出并调用真实工具)
        if "搜索一下" in response:
            print(mock_web_search("2026年AI趋势"))
        return response

    system_msg = "你是一个研究助手。当用户询问需要最新信息的问题时,你应该表示'我将搜索一下',然后提供分析。"
    user_msg = "请告诉我2026年AI的主要发展趋势。"
    response = call_llm_with_tools(system_msg, user_msg)
    print("使用外部工具示例输出:", response)
  1. 系统地测试:通过系统地测试和优化提示词,不断迭代改进,确保模型输出的稳定性和高质量。这些原则是设计高质量提示词的基础,也是提升模型输出效果的关键。
python 复制代码
def systematic_testing_example():
    test_prompts = [
        "请用一句话总结机器学习的核心思想。",
        "将'Hello, world!'翻译成中文。"
    ]
    temperatures = [0.1, 0.7]

    for prompt in test_prompts:
        print(f"\n测试Prompt: {prompt}")
        for temp in temperatures:
            try:
                completion = client.chat.completions.create(
                    model=MODEL_NAME,
                    messages=[{"role": "user", "content": prompt}],
                    temperature=temp
                )
                response = completion.choices[0].message.content
                print(f"  Temperature {temp}: {response[:50]}...")
            except Exception as e:
                print(f"  调用失败: {e}")
相关推荐
deephub4 小时前
【无标题】
人工智能·prompt·大语言模型·claude
大数据魔法师6 小时前
AI Agent(五)- Prompt提示词
人工智能·prompt
小程故事多_8019 小时前
破除迷思,Harness Engineering从来都不是时代过渡品
人工智能·架构·prompt·aigc
Trouvaille ~1 天前
零基础入门 LangChain 与 LangGraph(五):核心组件上篇——消息、提示词模板、少样本与输出解析
人工智能·算法·langchain·prompt·输入输出·ai应用·langgraph
猫头虎1 天前
一个插件,国内直接用Claude Opus 4.7
人工智能·langchain·开源·prompt·aigc·ai编程·agi
m0_614619061 天前
超级逼真 AI 证件照:绝学“焚决”
ai·prompt
承渊政道1 天前
Prompt工程:连接大语言模型能力与真实应用的关键桥梁
人工智能·深度学习·语言模型·自然语言处理·chatgpt·prompt·transformer
liulilittle2 天前
Prompt for OpenCode + CodeX-5.3:多个重型任务交付给AI自动化完成
运维·自动化·prompt
小程故事多_802 天前
从推理到智能体,大模型强化学习中信用分配机制的演进与突破
人工智能·prompt·aigc·ai编程