AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


探索 Prompt:从基础概念到高级工程技术
发布日期:2024-10-18 14:27:04 浏览次数: 1532 来源:大模型之路



写点废话:最近在做一些业务需求时,需要频繁调整Prompt来优化业务效果,不同的propmt对大模型输出影响还是很大的,书写prompt既是一门技术也是一门艺术,真正研究prompt的同学应该对prompt是一种既爱又恨的状态,爱是因为通过调整prompt我们可以实时优化模型回答效果,恨是模型“太不听话了”,有时候还傻傻的,接下来几天写一些关于prompt相关的内容,希望对大家有所帮助。

Prompt是引导模型生成所需输出的关键输入,随着技术的发展,围绕 Prompt 产生了一系列复杂且有效的工程技术。今天我们梳理一下目前 Prompt 的相关知识,包括其基本概念、一些先进的prompt技术以及为何需要以及多种先进的 Prompt 工程技术等。

一、Prompt 的基本概念

定义
Prompt 是给予大型语言模型(LLM)的输入或查询,它指示模型应生成何种类型的响应或输出。例如,当我们输入 “List the first three planets in our solar system.” 作为 Prompt 时,模型会输出 “The first three planets in our solar system are Mercury, Venus, and Earth.”

作用

  1. 引导输出
    通过明确的指令,Prompt 帮助模型聚焦于特定的主题或任务,从而生成符合用户期望的回答。

  2. 挖掘模型能力
    它能够激发模型利用其预训练知识和算法来处理各种不同的问题,无论是简单的事实查询还是复杂的逻辑推理和故事创作。


二、为什么需要 Prompt

LLM 的局限性

  1. 不一致性
    LLMs 使用概率方法生成文本,每次选择下一个单词时并非总是选择最可能的那个,而是从可能的下一个单词分布中采样。这导致对于相同的查询可能会产生不同的响应。

  2. 上下文敏感性
    LLMs 对上下文高度敏感,对话历史或查询方式的微小变化都可能导致不同的回答。

  3. 幻觉现象

    由于其概率性质,LLM 有时会给出不正确或不符合事实的响应。


Prompt 的弥补作用
为了克服这些局限性,我们需要更好的 Prompt 来提高模型的输出性能。通过精心设计 Prompt,可以引导模型更准确地理解任务,减少不一致性和幻觉现象,并更好地适应上下文。

三、Prompt Engineering

(一)Zero - shot Prompting

  1. 定义
    Zero - shot prompting 是一种机器学习范式,指 LLM 模型在其训练阶段未遇到的场景下,能够依靠其预先存在的知识来理解、解释并有效地响应 Prompt 的能力。

  2. 示例如前面提到的 “List the first three planets in our solar system.” 的例子,模型无需额外示例就能给出正确答案。


(二)Few - shot Prompting

  1. 定义
    Few - shot prompting 涉及为模型提供少量(通常在 1 到 10 个之间)的示例来指导其生成响应或执行特定任务。

  2. 模型学习方式

  • 模式识别:模型从提供的少数示例中识别模式,并将这种理解应用于新的、类似的任务或 Prompt。

  • 上下文理解:通过分析示例的上下文和结构,模型可以推断出生成适当响应所需的规则和指南。

  • 步骤

    • 定义任务:明确指定要模型做什么。

    • 选择示例:选择一些高质量、具有代表性的示例来展示期望的结果。

    • 制作 Prompt:将这些示例整合到一个 Prompt 中,清晰地向模型展示如何执行任务。

    • 生成和评估:让模型根据 Prompt 生成输出,并评估其性能。

    • 必要时改进:如果生成的输出不符合期望,则调整示例或 Prompt。


    简单示例:


    (三)Chain - of - Thought Prompting

    1. 定义
      Chain - of - Thought (CoT) prompting 是一种通过鼓励模型生成导致最终答案或输出的中间推理步骤来提高模型性能的技术。

    2. 适用场景
      对于需要逻辑推理、问题解决和多步思考的任务特别有用,它有助于将复杂问题分解为可管理的部分,提高模型输出的透明度和可解释性。


    简单示例:

    (四)Self - Consistency Prompting

    1. 定义
      Self - Consistency prompting 是一种通过生成对同一 Prompt 的多个响应,然后选择最一致或最常见的答案来提高 AI 模型响应的可靠性和准确性的技术。

    2. 工作流程

    • 多次尝试:模型对同一 Prompt 生成多个可能的完成。

    • 一致性检查:分析这些完成,选择最频繁或最常见的答案。

    • 最终输出:将最一致的响应作为最终输出,减少错误或不一致的可能性。


    简单示例:

    Prompt : What is the sum of the first 10 positive integers?
    Steps: Generate multiple answers to the prompt:Answer 1: "The sum is 55."Answer 2: "The sum is 55."Answer 3: "The sum is 54."Answer 4: "The sum is 55."Answer 5: "The sum is 55."
    Select the most consistent answer:The most frequent answer is "55," so the final output is "The sum is 55."
    Final Output: The sum of the first 10 positive integers is 55.

    (五)Prompt Chaining

    1. 定义
      Prompt chaining 是一种将多个 Prompt 按顺序链接在一起的技术,每个 Prompt 都基于前一个 Prompt 的输出构建。

    2. 工作流程

    • 定义总体任务:明确指定要完成的复杂任务。

    • 分解任务:将任务分解为更小、可管理的步骤。

    • 制作顺序 Prompt:为每个步骤创建 Prompt,确保它们相互构建。

    • 生成中间输出:针对每个 Prompt 运行 AI 模型并生成响应。

    • 改进和集成:在每个步骤改进输出并将它们集成到最终结果中。

    • 评估最终输出:评估最终输出的准确性、连贯性和相关性。


      示例:写一篇paper:

    Prompt 1: “Generate an outline for a research paper on the impact of climate change on agriculture.”
    Model Output 1: Outline including introduction, literature review, methodology, results, discussion, and conclusion.
    Prompt 2: “Expand the introduction section based on the outline.”
    Model Output 2: Detailed introduction with background information and research questions.
    Prompt 3: “Provide a summary of recent literature on the impact of climate change on agriculture.”
    Model Output 3: Summary of key studies and findings.
    Prompt 4: “Describe the methodology to be used in the research.”
    Model Output 4: Detailed description of research methods and data collection techniques.
    Prompt 5: “Draft the results section based on hypothetical data.”
    Model Output 5: Results with tables, graphs, and analysis.
    Prompt 6: “Write the discussion section interpreting the results.”
    Model Output 6: Discussion with implications, limitations, and future research directions.
    Prompt 7: “Conclude the paper with a summary of findings and recommendations.”
    Model Output 7: Conclusion summarizing the research and providing recommendations.

    (六)Tree of Thought (ToT) Prompting

    1. 定义
      Tree of Thought prompting 是一种借助 LLMs 解决复杂问题的方法,通过将问题分解为更小的步骤,类似于决策树。模型在每个步骤生成多个可能的想法(分支),并在做出最终决策之前评估这些想法。

    2. 关键特征

    • 分支解决方案:模型探索不同的解决方案分支,而不是产生单一解决方案。

    • 路径推理:每个分支代表不同的 “思想” 或方法,LLM 评估多个路径并在选择最逻辑或有用的路径之前进行比较。

    • 递归性:该技术可以在不同级别探索各种分支,意味着模型可以在得出最终结论之前探索多个推理步骤。

    示例:旅游选最好的路线

    Prompt using Tree of Thought:
    Prompt: “You are planning to travel from City A to City B. You have two routes to consider: Route 1 (shorter but has possible traffic) and Route 2 (longer but avoids traffic). Break the problem down, explore both routes, and choose the best one based on overall travel time.”
    Thought Process:
    Branch 1: Consider Route 1 (Shorter, but has possible traffic)
    Thought: Route 1 is 30 km, but it goes through the city center where traffic can be unpredictable.
    Scenario 1: If there is no traffic, Route 1 will take about 40 minutes.
    Scenario 2: If there is heavy traffic, Route 1 could take up to 70 minutes.
    Thought: Route 1 is risky because of the traffic variability.
    Branch 2: Consider Route 2 (Longer, but avoids traffic)
    Thought: Route 2 is 40 km but avoids the city center.
    Scenario: Even with longer distance, Route 2 has more consistent travel times (around 50 minutes).
    Thought: Route 2 offers more predictability.
    Conclusion:
    After evaluating both routes, Route 2 is the safer choice because it provides a more consistent travel time, even though it’s slightly longer.

    (七)Automatic Prompt Engineering (APE)

    1. 定义
      Automatic Prompt Engineering (APE) 是一种旨在自动化为大型语言模型(LLMs)生成和改进 Prompt 的创新技术。

    2. 工作流程

    • 初始 Prompt 生成:通过预定义模板、随机化或基于先前示例创建一组多样化的 Prompt。

    • 评估:根据每个 Prompt 评估模型生成的响应,评估标准可以包括响应准确性、相关性、连贯性或用户定义的标准。

    • 优化循环:根据评估结果修改和改进 Prompt,如调整措辞、改变结构或添加 / 删除上下文。

    • 最终选择:经过多次迭代后,选择性能最佳的 Prompt 来指导 AI 模型完成手头的任务。

    示例:

    Manual Prompt: “Write a recipe for chocolate cake.”
    However, you might want a more detailed and engaging recipe. Automatic Prompt Engineering (APE) can improve this by testing multiple variations and optimizing them based on the quality of generated recipes.
    APE Process:
    Initial Prompts Generated:
    1. “Write a recipe for chocolate cake, including ingredients, steps, and cooking time.”
    2. “Give a step-by-step recipe for a moist chocolate cake with a rich flavor.”
    3.List the ingredients and instructions for baking a chocolate cake for beginners.”
    Evaluation: The system checks which recipe is clearer, more accurate, and easier to follow.
    Optimized Prompt (Final): “Provide a detailed recipe for a rich and moist chocolate cake, including ingredients, preparation steps, and baking time for beginners.”

    (八)Program - Aided Language Models (PALM)

    1. 定义
      Program - Aided Language Models (PALM) 是一种混合方法,允许语言模型通过使用外部程序或脚本来解决复杂、基于规则或计算任务,并在生成最终响应时使用这些程序的输出。

    2. 优势

    • 处理结构化数据:帮助 LLMs 克服处理结构化或复杂数据的困难。

    • 减少幻觉:降低 LLM 在关键准确性领域(如数学或数据处理)产生幻觉答案的风险。

    • 更好地处理多步问题:对于需要分解为小步骤的复杂任务处理得更好。

    • 提高准确性:更好地处理需要精确、结构化响应的任务。

    示例:

    Prompt with PALM:
    “You are given a list of numbers: [45, 12, 23, 89, 1]. Use a sorting algorithm via an external program to order the list in ascending order.”
    Here’s how the process would work:
    LLM recognizes the task:
    The LLM identifies that sorting is required but instead of attempting to sort itself, it delegates this task to an external program.
    Delegating to a Sorting Algorithm:
    The external program (Python, for instance) uses a built-in sorting function:
    sorted_list = sorted([45, 12, 23, 89, 1])
    Program Output: The program returns the sorted list: [1, 12, 23, 45, 89].
    Final Response: The LLM outputs: “The sorted list is [1, 12, 23, 45, 89].”
    Without PALM, the LLM might attempt to sort the list but could get it wrong, especially with larger or more complex sets of numbers.

    Prompt 作为与大型语言模型交互的关键元素,其重要性不可忽视。从基础的定义和作用,到为克服 LLM 局限性而发展出的多种 Prompt 工程技术,我们看到了这一领域的不断发展和创新。这些技术从不同角度和层面提升了模型的性能,使其能够更准确、更有效地生成符合用户需求的输出。



    53AI,企业落地应用大模型首选服务商

    产品:大模型应用平台+智能体定制开发+落地咨询服务

    承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

    联系我们

    售前咨询
    186 6662 7370
    预约演示
    185 8882 0121

    微信扫码

    与创始人交个朋友

    回到顶部

     
    扫码咨询