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}")
相关推荐
Generalzy9 小时前
从本地 Demo 到生产级检索:Milvus 学习笔记(1)
golang·prompt·软件工程
sugar__salt9 小时前
Prompt工程实战指南:规范设计、LLM接口封装与避坑技巧
人工智能·python·prompt
城管不管14 小时前
Agent——001
android·java·数据库·llm·prompt
贺国亚1 天前
RAG检索增强-向量库与Chunking
prompt·embedding
Jing_jing_X2 天前
从 Prompt 对话到 OpenClaw:Agent 是怎么一步步发展出来的?
ai·prompt·个人开发·ai应用开发
城事漫游Molly2 天前
AI与质性研究的融合(三):AI赋能质性数据分析——从编码到理论构建的新范式
大数据·人工智能·机器学习·prompt·ai for science·智能体·定性研究
麦哲思科技任甲林2 天前
白话skills之二:Prompt和Skills的区别是什么?
prompt·sop·skills
人工智能培训2 天前
数字孪生赋能建筑行业 解锁工程全周期智慧管理
大数据·人工智能·机器学习·prompt·agent
-停泊3 天前
Skill和Prompt有何不同
prompt·agent·skill
贺国亚3 天前
01-Prompt工程-Few-Shot-CoT与Tool-Use
prompt