OUTLINE STEPS 提示
提示词工程:生成大纲和分步说明
Section titled “提示词工程:生成大纲和分步说明”像支持 ChatGPT 的那些现代大型语言模型(LLMs)擅长组织信息。一种强大的提示词工程(prompt engineering)技术是请求 LLM 为各种任务或流程生成结构化大纲或分步步骤。这使我们能够利用 LLM 的知识和组织能力来获得清晰简洁的说明或计划。
理解如何提示生成大纲和步骤
Section titled “理解如何提示生成大纲和步骤”与其使用特定的’指令’,不如通过自然语言指导 LLM 生成结构化大纲或顺序步骤。通过在提示词中清晰表达你对大纲或分步指南的需求,你可以引导 LLM 利用其组织能力和语言理解来提供清晰、可操作的内容。
基本方法如下:
User: Can you outline the steps to bake a chocolate cake?
LLM: Absolutely! Here is an outline of the steps to bake a chocolate cake:1. Gather Ingredients & Preheat Oven: Collect all necessary ingredients (flour, sugar, cocoa powder, eggs, etc.) and preheat your oven to the specified temperature (e.g., 350°F or 175°C).2. Prepare Dry Ingredients: In a large bowl, whisk together flour, sugar, cocoa powder, baking soda, baking powder, and salt.3. Prepare Wet Ingredients: In a separate bowl, combine eggs, milk, oil (or melted butter), and vanilla extract.4. Combine Wet and Dry Ingredients: Gradually add the wet ingredients to the dry ingredients, mixing until just combined. Be careful not to overmix.5. Add Hot Water/Coffee (Optional): For a richer chocolate flavor and moister cake, stir in hot water or hot brewed coffee into the batter.6. Prepare Baking Pan(s): Grease and flour your cake pan(s), or line with parchment paper.7. Pour Batter and Bake: Pour the batter evenly into the prepared pan(s). Bake in the preheated oven for the time specified in your recipe (typically 25-35 minutes, depending on pan size).8. Check for Doneness: Insert a toothpick into the center of the cake. If it comes out clean or with a few moist crumbs, the cake is done.9. Cool the Cake: Let the cake(s) cool in the pan(s) for about 10-15 minutes before inverting them onto a wire rack to cool completely.10. Frost and Decorate: Once completely cooled, frost and decorate your cake as desired.在此示例中,用户直接要求一个大纲。LLM 根据提示词回应了一个结构化的分步流程。
请求生成大纲和步骤的最佳实践
Section titled “请求生成大纲和步骤的最佳实践”为了有效地促使 LLM 生成大纲或步骤,请考虑以下最佳实践:
- 清晰定义任务或流程:提供你想要大纲的任务或流程的具体而简洁的描述。例如,不要说“告诉我关于烹饪”,而是说“列出制作一份简单蒜香橄榄油意面的步骤大纲”。
- 指定所需的详细程度:说明你需要一个高层次的概览还是非常详细、细致的步骤。例如:“提供一个详细的分步指南,说明如何组装平板包装书架,包括常见的陷阱。”
- 请求使用动作动词:鼓励在每个步骤的开头使用清晰的动作动词,以确保说明具有可操作性且易于理解(例如,“准备”、“混合”、“倒入”)。
- 分解复杂任务:如果任务很复杂,你可以先请 LLM 将其分解为主要阶段,然后再详细列出每个阶段的步骤。“列出推出新移动应用的主要阶段大纲,然后列出‘开发’阶段的关键步骤。”
- 要求包含重要注意事项:提示 LLM 在每个步骤中包含必要的细节、先决条件、工具或特定要求。“在列出如何更换汽车轮胎的大纲时,包含安全注意事项和必要工具。”
- 要求特定格式:你可以要求输出以编号列表、项目符号列表或适当时甚至表格的形式呈现。“请将步骤以编号列表的形式提供。”
应用示例:使用 OpenAI API 进行 Python 实现
Section titled “应用示例:使用 OpenAI API 进行 Python 实现”让我们探索一个使用 Python 和 OpenAI API 请求生成大纲的实际示例。此脚本将向 LLM 发送提示词并打印其结构化回复。
from openai import OpenAIimport os
# 这是一个使用环境变量存储 API 密钥的好习惯。# 确保设置了 OPENAI_API_KEY 环境变量,或者在测试时替换为你的实际密钥。# client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))client = OpenAI(api_key='YOUR_OPENAI_API_KEY') # 替换为你的密钥
def generate_step_by_step_guide(task_description): try: response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": "你是一位旨在提供清晰分步说明的有用助手。" }, { "role": "user", "content": f"Can you outline the steps to {task_description}? Please provide a numbered list." } ], max_tokens=300, # 根据需要为较长的大纲调整 temperature=0.7, n=1, stop=None ) return response.choices[0].message.content.strip() except Exception as e: return f"发生错误:{e}"
# 示例用法:task = "assemble a piece of flat-pack furniture, like a bookshelf"
guide = generate_step_by_step_guide(task)print(f"{task} 的分步指南:\n")print(guide)在此示例中,generate_step_by_step_guide 函数将用户的请求发送给 LLM。提示词被构建为明确要求为给定任务列出步骤大纲,在此例中是“组装一件平板包装家具,如书架”。
运行此脚本时,你将收到来自 LLM 的回复。用户提示词实际上是“你能列出组装一件平板包装家具(如书架)的步骤大纲吗?” LLM 的输出将类似于以下内容(实际输出可能有所不同):
Step-by-step guide to assemble a piece of flat-pack furniture, like a bookshelf:
Here are the general steps to assemble a piece of flat-pack furniture like a bookshelf:
1. **Prepare Your Workspace:** Clear a spacious area, preferably on a carpeted floor or a protective sheet to avoid scratching the furniture or your floor. Ensure good lighting.2. **Unpack and Inventory Parts:** Carefully open all boxes. Lay out all the pieces and compare them against the parts list in the instruction manual. Check for any damaged or missing items. Contact customer service if needed.3. **Gather Your Tools:** Most flat-pack furniture comes with basic tools like an Allen wrench. However, you might also need a Phillips head screwdriver, a flat head screwdriver, a rubber mallet, and sometimes a power drill (use with caution on a low torque setting).4. **Read the Instructions Thoroughly:** Before starting, read the entire instruction manual from beginning to end. Understand the orientation of each part and the sequence of assembly.5. **Identify Hardware:** Sort and identify all screws, dowels, cam locks, and other hardware. Some manuals label them with letters or numbers.6. **Assemble Step-by-Step:** Follow the manual's instructions precisely. Typically, you'll start by attaching smaller components or hardware to larger panels. * Insert dowels and cam locks as indicated. * Connect side panels to shelves or a base. * Ensure pieces are aligned correctly before tightening screws fully.7. **Attach the Back Panel:** The back panel (often a thin piece of hardboard) usually adds significant stability. Align it carefully and secure it with small nails or screws as instructed.8. **Check for Squareness:** Before fully tightening all connections, ensure the unit is square. You can check this by measuring diagonals; they should be equal.9. **Final Tightening:** Once assembled and squared, go back and ensure all screws and connectors are securely tightened, but avoid over-tightening, which can damage the material.10. **Install Shelves (if adjustable):** If your bookshelf has adjustable shelves, insert the shelf pins at your desired heights and place the shelves on them.11. **Safety First (Anchoring):** If the bookshelf is tall or will hold heavy items, it's crucial to anchor it to the wall using the provided anti-tip hardware to prevent it from falling over, especially if you have children or pets.12. **Clean Up:** Dispose of packaging materials responsibly and clean your newly assembled bookshelf.本章演示了如何通过提示词促使 LLMs 生成结构化大纲和分步流程。通过清晰定义任务、指定所需的细节和格式,并使用面向行动的语言,你可以有效地利用 LLM 的能力为各种流程创建有用、有组织的说明。这项技术对于规划、教学和解决问题至关重要。