generative-ai-for-beginners 进阶提示工程:七大提示技巧、温度参数与 Self-Refine 代码改进实战
generative-ai-for-beginners 进阶提示工程七大提示技巧、温度参数与 Self-Refine 代码改进实战【免费下载链接】generative-ai-for-beginners21 Lessons, Get Started Building with Generative AI项目地址: https://gitcode.com/GitHub_Trending/ge/generative-ai-for-beginners本文基于 generative-ai-for-beginners 课程第 05 课进阶提示整理成文围绕提示工程Prompt Engineering的核心方法论展开从解构一条简单提示词的上下文与输出限制到零样本、少样本、思维链、生成式知识、由简入繁、自我精炼Self-Refine与产婆术Maieutic提示等七种可应用于任意 LLM 的技巧并进一步讲解如何利用温度temperature参数在“多样化输出”与“确定性输出”之间做权衡。读完本文你能够独立构造并优化结构化提示词用温度参数控制输出的可预测性并掌握用 Self-Refine 循环让 AI 助手迭代改进真实代码的完整流程。前置回顾提示工程在做什么沿用课程第 04 课提示工程基础见 04-prompt-engineering-fundamentals/README.md的结论提示_工程_是一个引导模型给出更相关回答的过程方式是提供更有用的指令或上下文。写提示词包含两个步骤构造提示提供相关上下文与优化逐步改进提示词。本课源文档为 translations/et/05-advanced-prompts/README.md即第 05 课的翻译版本的目标是从“不断试各种提示词”进阶到“理解为什么某条提示词比另一条更好”并学会构造可迁移到任意 LLM 的提示。本课覆盖两大主题运用不同技巧扩展你的提示工程知识配置提示词以让输出产生变化或不再变化。学完本节应能达到的学习目标运用能改善提示结果的提示工程技术执行“多样化”或“确定性”两种风格的提示。提示工程不只是“写一句话”提示工程是创造“能产生期望结果”的提示的过程。它远不止写一段文本指令——提示工程并不是一门工程学科而是一组为达成期望结果可以套用的技巧集合。一条简单提示词的解剖以一条基础提示为例Generate 10 questions on geography.生成 10 个地理问题。这条看似简单的提示里其实同时用到了两类技巧上下文Context你指定了主题必须是“地理”输出限制Limiting the output你要求问题数量不超过 10 个。简单提示的局限即便用了上述技巧结果也不稳定原因是主题过大地理涵盖国家、首都、河流等你不知道模型会聚焦到哪一块格式缺失如果你想让问题以特定格式如编号、JSON、表格输出这条提示完全没有约束。可以看出构造一条提示需要考虑的维度远不止“说要什么”。而生成式 AI 的能力也远不止回答简单问题下面进入核心技术。七种提示技巧首先需要理解提示prompting是 LLM 的一种涌现emergent属性——它不是模型内置的功能开关而是我们在使用模型过程中发现并总结出来的能力。基于此课程归纳了七种基础技巧技巧一句话定义零样本提示Zero-shot最基础形式单条提示模型仅凭训练数据作答少样本提示Few-shot提供 1 个及以上示例让模型“照着做”思维链Chain-of-thought教模型把问题拆成步骤、逐步推理生成式知识Generated knowledge在提示中注入额外的事实或企业数据由简入繁Least-to-most把大问题拆成子问题序列并按顺序执行自我精炼Self-refine让模型批判自己的输出再要求它改进产婆术提示Maieutic要求模型逐一解释答案的各个部分剔除不一致项是一种自我精炼变体零样本提示Zero-shot prompting最简单的风格只有一条提示模型完全依赖自身训练数据回答。初学 LLM 时你几乎都在用这种形式。示例提示“What is Algebra?”什么是代数回答“Algebra 是研究数学符号及其运算规则的一门数学分支。”没有任何示例、没有任何上下文注入胜在简单直接缺点是模型无法得知你偏好的格式、领域深度或风格。少样本提示Few-shot prompting在请求中附带若干任务相关的示例帮助模型理解“输出应该长什么样”。示例提示“Write a poem in the style of Shakespeare. Here are a few examples of Shakespearean sonnets: Sonnet 18: Shall I compare thee to a summers day? Thou art more lovely and more temperate... Sonnet 116: Let me not to the marriage of true minds Admit impediments. Love is not love Which alters when it alteration finds... Sonnet 132: Thine eyes I love, and they, as pitying me, Knowing thy heart torment me with disdain,... Now, write a sonnet about the beauty of the moon.”用莎士比亚风格写一首关于月亮之美的十四行诗回答“Upon the sky, the moon doth softly gleam, In silvry light that casts its gentle grace,...”示例给 LLM 提供了期望输出的上下文、格式或文风。它帮助模型理解具体任务从而生成更准确、更贴合的回答。注意示例数量不必多——1 到几个精心挑选的示例往往比一堆杂乱示例更有效。思维链Chain-of-thought思维链是本课最有代表性的技巧通过多个步骤“带”着模型走让它理解_如何_做一件事而不只是_做什么_。先看一个反例——不用思维链提示“Alice has 5 apples, throws 3 apples, gives 2 to Bob and Bob gives one back, how many apples does Alice have?”回答5模型答 5是错的。正确计算是 (5 - 3 - 2 1 1)正确答案为 1 个苹果。那么如何教会 LLM 正确计算应用思维链意味着三步给 LLM 一个相似的示例展示计算过程演示如何一步步正确计算再给出原始提示。组合后的提示如下提示“Lisa has 7 apples, throws 1 apple, gives 4 apples to Bart and Bart gives one back: 7 - 1 6 6 - 4 2 2 1 3 Alice has 5 apples, throws 3 apples, gives 2 to Bob and Bob gives one back, how many apples does Alice have?”回答1可以看到我们写出了一条显著更长的提示——包含另一个示例、完整计算过程、然后才是原始问题——最终得到了正确答案 1。这就是思维链的威力用一个“解题示范”把模型的推理轨道对齐。生成式知识Generated knowledge构造提示时你常常想使用自己企业的数据提示的一部分来自企业内容另一部分才是你真正想问的问题。以保险业务为例提示可以做成一个模板{{company}}: {{company_name}} {{products}}: {{products_list}} Please suggest an insurance given the following budget and requirements: Budget: {{budget}} Requirements: {{requirements}}提示通过模板构造其中{{variable}}形式的变量会被企业 API 返回的真实值替换。变量替换后的提示如下Insurance company: ACME Insurance Insurance products (cost per month): - Car, cheap, 500 USD - Car, expensive, 1100 USD - Home, cheap, 600 USD - Home, expensive, 1200 USD - Life, cheap, 100 USD Please suggest an insurance given the following budget and requirements: Budget: $1000 Requirements: Car, Home, and Life insurance把这条提示交给 LLM会得到类似回答Given the budget and requirements, we suggest the following insurance package from ACME Insurance: - Car, cheap, 500 USD - Home, cheap, 600 USD - Life, cheap, 100 USD Total cost: $1,200 USD问题出现了它连 Life 保险也推荐了而预算只有 1000 USD总报价 1200 USD 已经超支。这说明需要优化提示把“允许选什么”说得更清楚。经过若干次_试错_得到改进后的提示Insurance company: ACME Insurance Insurance products (cost per month): - type: Car, cheap, cost: 500 USD - type: Car, expensive, cost: 1100 USD - type: Home, cheap, cost: 600 USD - type: Home, expensive, cost: 1200 USD - type: Life, cheap, cost: 100 USD Please suggest an insurance given the following budget and requirements: Budget: $1000 restrict choice to types: Car, Home关键改进点为每个产品增加了type和cost字段标签并在预算行后使用restrict限制这个关键词。此时回答变为Given the budget and requirements, we suggest the Car, Cheap insurance product which costs 500 USD per month.这个例子想说明的核心是即便已经用上了“生成式知识”这样的基础技巧绝大多数情况下仍需迭代优化提示才能得到期望结果——提示词是“调试对象”不是一次写对的产品。由简入繁Least-to-most由简入繁提示的思路是把一个大问题拆解为子问题引导 LLM 逐步“攻克”它。适合数据科学这类多阶段任务。例如提示How to perform data science in 5 steps?如何用 5 个步骤完成数据科学工作AI 助手回答收集数据Collect data清洗数据Clean data分析数据Analyze data可视化数据Plot data展示数据Present data与思维链的区别在于思维链通常靠“示范一个解题过程”来引导由简入繁则是明确要求模型先分解、再按序执行子步骤让模型自己规划拆解方案。自我精炼Self-refine批判结果对生成式 AI 和 LLM你不能盲目信任输出必须验证。毕竟 LLM 给出的只是“下一个最可能的词/句”而不一定是正确答案。一个好办法就是让 LLM 自我批判这正是 Self-refine 技巧。工作流程用初始提示要求 LLM 解决问题LLM 作答你批判这个回答并要求 AI 改进LLM 再次作答这次会考虑批判意见并提出自己的解决方案。这个循环可以重复任意次。文档给出的完整示例提示“Create a Python Web API with routes products and customers”创建一个带 products 和 customers 路由的 Python Web APIAI 回答import flask app Flask(__name__) app.route(/products) def products(): return Products app.route(/customers) def customers(): return Customers提示suggest 3 improvements of the above code对上述代码提出 3 条改进建议AI 回答Sure, here are three suggestions to improve the code: Import the correct module: The first line should import Flask instead of flask. The correct import statement should be from flask import Flask. Add a main function: Add a if __name__ __main__: block to the end of the file to run the application when the file is executed. Use f-strings: Use f-strings to format the return statements in the route functions. For example, return Products can be changed to return fThis is the {request.path} page.改进后的代码from flask import Flask, request app Flask(__name__) app.route(/products) def products(): return fThis is the {request.path} page app.route(/customers) def customers(): return fThis is the {request.path} page if __name__ __main__: app.run()可以看到正是由于对第一版回答做了批判第二版代码修正了导入错误、补齐了入口块并改进了返回语句。产婆术提示Maieutic prompting产婆术提示与 Self-refine 相似但更强调让 LLM 解释自己目标是通过追问减少输出的自相矛盾从而确保答案正确。工作流让 LLM 回答问题针对答案的每个部分要求 LLM 更深入地解释若发现不一致丢弃不一致的部分重复第 2、3 步直到所有部分都过一遍且你对答案满意。示例提示How can I create a crisis plan to mitigate a pandemic in 5 steps?如何用 5 步制定缓解疫情的危机计划 LLM 回答1. Identify the risks 2. Identify the stakeholders 3. Identify the resources 4. Identify the actions 5. Identify the communication channels它给出了 5 步但如何确认这是对的继续追问第一步提示Explain the first step in more detail, what are the risks in detail with a pandemic?The risks are the following: - Loss of life - Loss of business - Loss of reputation - Loss of customers - Loss of employees为进一步确认追问第一个风险的细节看回答是否与上文一致提示In a Pandemic, which is the biggest risk and why?The biggest risk is loss of life because its the most important thing to protect.What are the two biggest risks in a pandemic?The two biggest risks are loss of life and loss of business.此时 LLM 的回答保持了一致“生命”与“商业”均为最高优先级风险可以较有把握地进入下一步。但务必记住不应盲目信任 LLM始终要验证输出。让输出产生变化LLM 的非确定性LLM 本质上是非确定性的同一条提示每次运行都可能得到不同结果。例如提示“Generate code for a Python Web API”第一次运行生成# Import necessary modules import flask from flask import request, jsonify # Create a Flask app app flask.Flask(__name__) # Create a route for the API app.route(/api, methods[GET]) def api(): # Get the data from the request data request.args # Process the data result process_data(data) # Return the result as JSON return jsonify(result) # Function to process the data def process_data(data): # Do something with the data result data 1 # Return the result return result # Run the app if __name__ __main__: app.run()对同一条提示再次运行得到一个明显不同的版本#import necessary packages import flask from flask import request, jsonify #create the Flask application app flask.Flask(__name__) app.config[DEBUG] True #create a list of books books [ {id: 0, title: A Fire Upon the Deep, author: Vernor Vinge, first_sentence: The coldsleep itself was dreamless., year_published: 1992}, {id: 1, title: The Ones Who Walk Away From Omelas, author: Ursula K. Le Guin, first_sentence: With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea., published: 1973}, {id: 2, title: Dhalgren, author: Samuel R. Delany, first_sentence: to wound the autumnal city., published: 1975} ] #create an endpoint for the API app.route(/, methods[GET]) def home(): return h1Books API/h1 pA prototype API for retrieving books./p #create an endpoint to return all books app.route(/api/v1/resources/books/all, methods[GET]) def api_all(): return jsonify(books) #create an endpoint to return a single book app.route(/api/v1/resources/books, methods[GET]) def api_id(): #check if an ID was provided as part of the URL #if ID is provided, assign it to a variable #if no ID is provided, display an error if id in request.args: id int(request.args[id]) else: return Error: No id field provided. Please specify an id. #create an empty list for our results results [] #loop through the data and match results that fit the requested ID #IDs are unique, but other fields might return many results for book in books: if book[id] id: results.append(book) #use the jsonify function from Flask to convert our list of #Python dictionaries to the JSON format return jsonify(results) app.run()多样化的输出算问题吗取决于你的目的如果你需要特定的、可预期的回答那就是问题如果你能接受多样化输出比如“生成任意 3 个地理问题”则无所谓。用温度temperature控制输出变化如果希望输出更可控、更确定deterministic可以调节温度参数。温度是 0 到 1 之间的值0 最确定1 最多样默认值 0.7。把温度设为 0.1对同一提示“Generate code for a Python Web API”运行两次第一次#import necessary libraries import flask from flask import request, jsonify #create an instance of the Flask class app flask.Flask(__name__) #create an endpoint for the API app.route(/api/v1/endpoint, methods[GET]) def api_endpoint(): #get the request data data request.get_json() #process the data result process_data(data) #return the result return jsonify(result) #function to process the data def process_data(data): #process the data result {result: success} #return the result return result #run the Flask app if __name__ __main__: app.run()第二次#import necessary libraries import flask from flask import request, jsonify #create a Flask app app flask.Flask(__name__) #create a route for the API app.route(/api, methods[GET]) def api_call(): #get the data from the request data request.get_json() #process the data result process_data(data) #return the result return jsonify(result) #define the process_data function def process_data(data): #do something with the data result data 1 #return the result return result #run the app if __name__ __main__: app.run()两个输出之间只有极小差异——结构、路由、处理函数几乎一致只有函数命名之类的细节不同。反过来把温度设为 0.9同样运行两次第一次# Import necessary libraries import flask from flask import request, jsonify # Create a Flask app app flask.Flask(__name__) # Create a route for the API app.route(/api, methods[GET]) def api_call(): # Get the data from the request data request.args # Process the data result process_data(data) # Return the result return jsonify(result) # Function to process the data def process_data(data): # Do something with the data result data 1 # Return the result return result # Run the app if __name__ __main__: app.run()第二次import flask from flask import request, jsonify # create the Flask app app flask.Flask(__name__) app.config[DEBUG] True # create some test data books [ {id: 0, title: A Fire Upon The Deep, author: Vernor Vinge, first_sentence: The coldsleep itself was dreamless., year_published: 1992}, {id: 1, title: The Ones Who Walk Away From Omelas, author: Ursula K. Le Guin, first_sentence: With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea., published: 1973}, {id: 2, title: Dhalgren, author: Samuel R. Delany, first_sentence: to wound the autumnal city., published: 1975} ] # create an endpoint app.route(/, methods[GET]) def home(): return h1Welcome to our book API!/h1 app.route(/api/v1/resources/books原课中该输出即在此处截断恰好说明高温度下模型“跑题”到了一个图书 API 的实现。可以看到0.9 温度下的两次结果差异极大一次仍是简单的单端点 API另一次直接演化成了带测试数据的 Books API。课程原表可以归纳为温度输出特征0.1两次输出高度一致仅命名级差异适合需要稳定复现的任务默认 0.7平衡确定性与多样性0.9两次输出结构完全不同适合头脑风暴、创意生成注意除了温度还有 top-k、top-p、重复惩罚repetition penalty、长度惩罚length penalty、多样性惩罚diversity penalty等参数可以调节输出但课程明确将其置于本课范围之外此处只作了解即可。提示的最佳实践技巧之外课程还总结了几条在提示 LLM 时值得遵循的最佳实践——随着使用增多你会逐步形成自己的风格明确上下文Specify context上下文很重要你能指定的领域、主题越精确越好限制输出想要特定条数或特定长度就明确写出来同时说明“要什么”和“要成什么样”例如 “Create a Python Web API with routes products and customers, divide it into 3 files”使用模板Use templates经常需要用企业数据充实提示模板中用变量占位、运行时替换为真实数据与上文“生成式知识”的{{variable}}模板一脉相承拼写正确Spell correctlyLLM 也许能容错但拼写正确会拿到更好的回答。实战任务用 Self-Refine 改进一个 Flask API课程给出的练习代码与仓库中 05-advanced-prompts/python/aoai-assignment.py 完全一致from flask import Flask, request app Flask(__name__) app.route(/) def hello(): name request.args.get(name, World) return fHello, {name}! if __name__ __main__: app.run()任务要求使用 AI 助手如 GitHub Copilot 或 ChatGPT对这段代码应用 “self-refine” 技巧进行改进。提示把提示写成“要求改进代码”的形式并限定改进条数如“给出 3 条改进”是个好主意也可以指定改进方向例如架构、性能、安全等方面。参考答案Self-Refine 循环的真实产物仓库中的 05-advanced-prompts/python/aoai-solution.py 就是一次 Self-Refine 循环的落盘结果。文件开头保留了 AI 给出的三条改进建议以注释形式增加输入校验防止恶意输入被服务器处理建议使用 flask-wtf 校验并清洗用户输入使用环境变量存放数据库凭据、API 密钥等敏感信息避免硬编码实现错误处理通过app.errorhandler()装饰器向用户返回有意义的错误信息。随后的代码正是按建议改造后的版本并且进一步落实了安全加固import os from flask import Flask, render_template_string from flask_wtf import FlaskForm from wtforms import StringField, SubmitField from wtforms.validators import DataRequired, Length, Email from markupsafe import escape app Flask(__name__) # SECURITY: Load secret key from environment variable instead of hardcoding app.config[SECRET_KEY] os.environ.get(FLASK_SECRET_KEY, os.urandom(32)) class HelloForm(FlaskForm): name StringField(Name, validators[DataRequired(), Length(min3)]) email StringField(Email, validators[DataRequired(), Email()]) submit SubmitField(Submit)路由处理中使用escape()防 XSS并返回 400 错误处理器app.route(/, methods[GET, POST]) def hello(): form HelloForm() if form.validate_on_submit(): # SECURITY: Use escape() to prevent XSS attacks safe_name escape(form.name.data) safe_email escape(form.email.data) return fHello, {safe_name} ({safe_email})! # SECURITY: Use Flasks render_template_string for proper escaping return render_template_string(FORM_TEMPLATE, formform) app.errorhandler(400) def bad_request(error): return Bad request, 400 if __name__ __main__: app.run()这个文件很好地印证了本课两个观点一是 Self-Refine“批判—改进”循环能产出可落地的安全加固输入校验、XSS 转义、环境变量管理密钥、错误处理二是“限定改进方向/条数”的最佳实践让改进结果聚焦、可控。仓库还提供了 JavaScript 版本的同一练习05-advanced-prompts/javascript/assignment.js 是初始的 Express 代码05-advanced-prompts/javascript/solution.js 则完整记录了两次提示及其产物——第一次提示“find 3 improvements to the code and apply”找出 3 处改进并应用结果包括 ES module 化、移除未使用参数、改进日志文案第二次提示“improve the code and make it more secure”提升安全性结果引入环境变量、express-validator 输入校验、HTTPS 证书加载与路径校验。两版答案互为印证Self-Refine 的效果与你的提示措辞、限定条件直接相关。知识检查为什么我要使用思维链chain-of-thought提示下面给出 1 个正确答案和 2 个错误答案为了教 LLM 如何解决问题。B为了教 LLM 在代码中查找错误。C为了指示 LLM 想出不止一种解决方案。答案1。因为思维链的核心是“向 LLM 展示如何解决问题”——给它一系列步骤以及相似问题及其解法示范如上文苹果问题中“7 - 1 6 / 6 - 4 2 / 2 1 3”的完整演算。挑战你在练习中刚用过 Self-Refine 技巧。现在挑一个你自己写的程序先想清楚希望对它做哪些改进然后用 Self-Refine 技巧让 AI 助手落实这些变更——你认为结果变好了还是变坏了把“改进前后”的差异记录下来这正是评估提示技巧价值的最佳方式。延伸阅读本课源文档爱沙尼亚语翻译translations/et/05-advanced-prompts/README.md英文原版第 05 课05-advanced-prompts/README.md第 04 课 提示工程基础04-prompt-engineering-fundamentals/README.md练习代码与答案05-advanced-prompts/python/aoai-assignment.py、05-advanced-prompts/python/aoai-solution.py、05-advanced-prompts/javascript/solution.js下一课应用本课的提示工程技术构建文本生成应用见 06-text-generation-apps/README.md【免费下载链接】generative-ai-for-beginners21 Lessons, Get Started Building with Generative AI项目地址: https://gitcode.com/GitHub_Trending/ge/generative-ai-for-beginners创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

相关新闻

最新新闻

日新闻

周新闻

月新闻