实践简单提示词,并观察大型语言模型(LLM)如何响应不同的指令和设置。前提条件开始前,请确保你拥有 LLM 的访问权限,无论是通过在线平台界面还是以编程方式通过 API。如果使用 API,你将需要:来自 LLM 提供商的 API 密钥(例如,OpenAI、Anthropic、Cohere)。一个已安装所需库(例如,requests 或提供商的特定 SDK,如 openai)的 Python 环境。对于代码示例,我们假设存在一个辅助函数 call_llm(prompt, temperature=0.7, max_tokens=100),它处理 API 调用并返回 LLM 的文本回复。你需要根据你具体的 API 提供商和设置进行调整。请记住安全地管理你的 API 密钥,例如使用环境变量。实验 1:基本指令遵循我们从一个直接的任务开始:根据简单指令生成创意文本。任务: 让 LLM 为一个环保咖啡新品牌编写一句简短的宣传语。提示词:Write a short, catchy tagline for a new brand of sustainable, bird-friendly coffee beans called 'Wing & Bean'.示例代码:prompt_v1 = """ Write a short, catchy tagline for a new brand of sustainable, bird-friendly coffee beans called 'Wing & Bean'. """ response_v1 = call_llm(prompt=prompt_v1, temperature=0.7, max_tokens=50) print(f"回复 1:\n{response_v1}") # 现在,我们增加一个限制:强调口感。 prompt_v2 = """ Write a short, catchy tagline for a new brand of sustainable, bird-friendly coffee beans called 'Wing & Bean'. Focus on the rich taste. """ response_v2 = call_llm(prompt=prompt_v2, temperature=0.7, max_tokens=50) print(f"\n回复 2 (口感侧重):\n{response_v2}")预期观察:第一个回复可能更广泛地侧重于可持续性或对鸟类友好的方面。第二个回复,受“强调浓郁口感”这一额外指令的引导,应该会生成强调风味的宣传语。这显示了在指令中增加具体限制或细节如何影响输出。尝试一下: 进一步修改提示词。要求生成押韵的宣传语,或少于五个字的宣传语。观察 LLM 如何适应这些新指令。实验 2:摘要生成LLM 通常用于文本摘要。我们来测试一下这项能力。任务: 总结一段关于远程工作益处的文字。输入文本:Remote work offers significant advantages for both employees and employers. Employees gain flexibility in their schedules, eliminate commuting time and costs, and often report a better work-life balance. Employers can access a wider talent pool, potentially reduce office overhead expenses, and may see increased productivity from focused employees. However, challenges like maintaining company culture and ensuring effective communication need careful management.提示词:Summarize the following text about remote work in a single sentence: Remote work offers significant advantages for both employees and employers. Employees gain flexibility in their schedules, eliminate commuting time and costs, and often report a better work-life balance. Employers can access a wider talent pool, potentially reduce office overhead expenses, and may see increased productivity from focused employees. However, challenges like maintaining company culture and ensuring effective communication need careful management.示例代码:input_text = """ Remote work offers significant advantages for both employees and employers. Employees gain flexibility in their schedules, eliminate commuting time and costs, and often report a better work-life balance. Employers can access a wider talent pool, potentially reduce office overhead expenses, and may see increased productivity from focused employees. However, challenges like maintaining company culture and ensuring effective communication need careful management. """ prompt = f"Summarize the following text about remote work in a single sentence:\n\n{input_text}" response = call_llm(prompt=prompt, temperature=0.5, max_tokens=60) print(f"摘要:\n{response}")预期观察:LLM 应该提供简洁的摘要,概括原始文本的主要内容,理想情况下如所要求地用一句话完成。尝试一下: 改变提示词,要求生成三句话的摘要。要求其专门为 CEO 受众进行总结。留意摘要的长度以及可能侧重点如何根据指令变化。实验 3:温度参数的影响如前所述,temperature 参数控制输出的随机性或创造性。较低的温度使输出更集中、更确定,而较高的温度则带来更多样化、有时出乎意料的结果。任务: 为一个奇幻故事的情节生成构思。提示词:Brainstorm three plot ideas for a fantasy story involving a lost map and a hidden city.示例代码(比较温度参数):prompt = "Brainstorm three plot ideas for a fantasy story involving a lost map and a hidden city." # 低温度:更可预测、集中的输出 response_low_temp = call_llm(prompt=prompt, temperature=0.2, max_tokens=150) print(f"回复 (温度=0.2):\n{response_low_temp}\n") # 高温度:更有创意、更多样化的输出 response_high_temp = call_llm(prompt=prompt, temperature=0.9, max_tokens=150) print(f"回复 (温度=0.9):\n{response_high_temp}") # 注意:多次运行高温度提示词可能会 # 每次产生明显不同的结果,而低温度 # 提示词会生成更相似的输出。预期观察:低温度($0.2$)的回复可能会提供与地图和隐藏城市相关的标准奇幻套路,每次运行时可能都非常相似。高温度($0.9$)的回复应该会提供更多样、可能更不寻常的情节转折或角色构思。它可能以不那么显眼的方式连接思想。多次运行高温度提示词可能会产生明显不同的构思组合,显示出增加的随机性。{"layout": {"title": "温度对下一个词概率的影响", "xaxis": {"title": "可能的下一个词"}, "yaxis": {"title": "概率", "range": [0, 0.6]}, "bargap": 0.2}, "data": [{"type": "bar", "name": "低温度(例如 0.2)", "x": ["the", "a", "ancient", "hidden", "magical"], "y": [0.5, 0.15, 0.1, 0.05, 0.03], "marker": {"color": "#339af0"}}, {"type": "bar", "name": "高温度(例如 0.9)", "x": ["the", "a", "ancient", "hidden", "magical"], "y": [0.25, 0.2, 0.18, 0.15, 0.12], "marker": {"color": "#ff922b"}}]}较低的温度使概率分布更集中,使得最有可能的下一个词的概率大大增加。较高的温度使分布趋于平坦,增加了选择不常见词语的机会。尝试一下: 使用一个提示词,让 LLM 完成一个句子,例如“宇宙飞船降落在了一个由……组成的星球上。”将其在 temperature=0.1 和 temperature=1.0 下多次运行。比较补全内容的连贯性和创造性。实验 4:使用 max_tokens 管理输出长度max_tokens 参数设置生成回复的长度限制(在某些模型中包括提示词,但通常指生成部分)。它对控制成本、延迟和确保输出符合要求都很重要。任务: 让 LLM 解释光合作用。提示词:Explain the process of photosynthesis in simple terms.示例代码(比较 max_tokens):prompt = "Explain the process of photosynthesis in simple terms." # 低 max_tokens:截断输出 response_short = call_llm(prompt=prompt, temperature=0.5, max_tokens=25) print(f"回复 (max_tokens=25):\n{response_short}\n") # 高 max_tokens:更完整的输出 response_long = call_llm(prompt=prompt, temperature=0.5, max_tokens=150) print(f"回复 (max_tokens=150):\n{response_long}")预期观察:max_tokens=25 的回复可能会在解释中途被截断,甚至可能在句中被截断。它只提供了答案的开头部分。max_tokens=150 的回复应该会提供一个更完整但仍然简单的光合作用解释。这表明 max_tokens 如何作为生成长度的硬性限制。尝试一下: 尝试编写一个短篇故事的任务。将 max_tokens 设置为一个非常小的值(例如 10),然后逐渐增加。观察故事如何发展,直到感觉完整或达到 token 限制。考虑权衡:更大的 max_tokens 允许更完整的答案,但会增加 API 成本和响应时间。实验总结这些简单的实验显示了与 LLM 的基本互动:指令重要: 清晰、具体的指令对于引导 LLM 生成所需输出是必要的。措辞上的细微变化可能导致不同的结果。参数调整行为: 像 temperature 这样的参数对输出的风格(创意型 vs. 可预测型)有很大影响,而 max_tokens 控制长度。迭代必要: 获得完美回复通常需要尝试不同的提示词和参数设置。这些基本技能是更高级提示工程的起点。随着课程的推进,你将学习处理更复杂任务和提高 LLM 在你应用中回复可靠性的方法。继续尝试!尝试不同的提示词、任务和参数组合,以培养你对 LLM 行为的直觉。