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}")
相关推荐
技灵AI12 小时前
AI 视频转场提示词怎么写?让前后画面衔接更自然
人工智能·prompt·aigc·音视频
Caroline51617 小时前
AI Logo 设计实战:如何用 GPT Image 2.5 生成透明背景 Logo?附 Prompt 示例
人工智能·gpt·prompt·图像生成
AI Workforce17 小时前
数据质量:4 层去重 + 黑名单机制——宁可漏杀,不可错杀
大数据·人工智能·ai·架构·prompt
醒醒该学习了!1 天前
Prompt 设计之文生图(Text-to-Image)提示词设计方法
prompt
拆房老料1 天前
【第四部分:Agentic 智能体设计模式】16. 一个 Prompt 搞不定复杂任务?试试 Prompt Chaining
java·前端·人工智能·设计模式·prompt
志尊宝1 天前
AI原生前端开发(013):端侧文本生成实战:智能提示、自动补全
人工智能·prompt·ai-native
forest_091 天前
Agent 系列17|多 Agent 实战
人工智能·机器学习·架构·prompt·agi
志尊宝2 天前
AI原生前端开发(012):Prompt API详解:如何优雅调用本地AI模型
人工智能·prompt·ai-native
紧跟先前的步伐2 天前
Go AI Agent 实战 04|Prompt 与消息角色:System、User、Assistant
人工智能·golang·prompt
sell-cloud2 天前
Prompt Engineering要凉了?Fable 5.5能“猜“意图,但银行正在封杀AI Agent——企业AI的真正护城河是什么
人工智能·prompt