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}")
相关推荐
旋律翼26 小时前
AI Prompt 工程化设计最佳实践(Harness Engineering)
人工智能·prompt
人道领域14 小时前
【0-1的agent进阶篇】Prompt 与上下文工程
java·开发语言·prompt·mcp
haluhalu.15 小时前
prompts.chat:03-core-prompting-principles
prompt·github
承受失1 天前
Cloud Agent 开发笔记(4):Skill 与 MCP 集成、项目后记
人工智能·prompt
haluhalu.2 天前
prompts.chat:02-anatomy-of-effective-prompt
jvm·prompt
城事漫游Molly2 天前
用AI提炼研究问题与假设的完整提示词——从模糊想法到精准RQ/Hypothesis,AI辅助精化全流程
人工智能·机器学习·prompt·ai for science·博士生必读·应用语言学·研究问题
codingPower2 天前
提示词(Prompt)介绍:核心概念、主流框架与权威认证
prompt
岁岁养乐多3 天前
Claude Code 的 Prompt 工程:从静态分离到缓存优化的深度解析
prompt·ai编程
xcLeigh3 天前
提示词基础:什么是Prompt以及它为何如此重要
人工智能·ai·大模型·prompt·ai写作·提示词