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}")
相关推荐
水木流年追梦14 分钟前
大模型入门-应用篇2-RAG (检索增强生成):从原理到 Python 实战
开发语言·python·算法·prompt
小雨青年22 分钟前
GitHub Copilot 自定义扩展实战:Instructions、Prompt Files、Agents 和 Hooks 怎么用
prompt·github·copilot
Aision_5 小时前
从工具调用到 MCP、Skill完整学习记录
java·python·gpt·学习·langchain·prompt·agi
长亭外的少年18 小时前
从 Prompt 到工程体系:如何真正把 AI 用进软件开发
人工智能·prompt
狐狐生风20 小时前
LangGraph 工具调用集成
python·langchain·prompt·agent·langgraph
情绪总是阴雨天~2 天前
提示词工程实战:金融行业 Prompt 设计与大模型应用
人工智能·金融·prompt
适应规律2 天前
python版代码风格撰写prompt
prompt
知识汲取者2 天前
从 Prompt 到 Context 再到 Harness ,AI Coding 正在经历第三次范式跃迁
人工智能·prompt
书到用时方恨少!2 天前
提示词工程终章:ReAct——让大模型“边想边做”的智能体革命
prompt·agent·react·智能体·提示词工程
X.AI6662 天前
主流7个大模型测评,谁更懂代码和空间感知?
3d·prompt