CREATE A LIST 提示
Prompt Engineering:使用 LLM 生成列表
Section titled “Prompt Engineering:使用 LLM 生成列表”像 ChatGPT 这样的大语言模型(LLM)非常擅长生成各种类型的列表,从推荐和想法到结构化数据。通过精心设计 prompt,您可以指导 LLM 生成组织良好、相关且实用的列表,以满足您的特定需求。
指示 LLM 创建列表
Section titled “指示 LLM 创建列表”要让 LLM 生成列表,您只需在 prompt 中直接提出要求即可。关键是要明确列表的类型、项目标准、所需的项目数量以及任何特定的格式。
基本方法是在您的 prompt 中直接请求列表:
User: Can you create a list of 5 must-read science fiction books published in the 21st century?
LLM: Certainly! Here are 5 must-read science fiction books published in the 21st century:
1. **"Project Hail Mary" by Andy Weir (2021)** - A thrilling space adventure with a witty protagonist and fascinating scientific problem-solving.2. **"The Three-Body Problem" by Cixin Liu (2008, English translation 2014)** - A mind-bending epic dealing with humanity's first contact with an alien civilization.3. **"Klara and the Sun" by Kazuo Ishiguro (2021)** - A poignant story exploring themes of love, humanity, and artificial intelligence through the eyes of an 'Artificial Friend'.4. **"Exhalation: Stories" by Ted Chiang (2019)** - A collection of profound and thought-provoking short stories exploring philosophical and scientific concepts.5. **"Ancillary Justice" by Ann Leckie (2013)** - A unique space opera featuring an AI protagonist, exploring themes of identity, empire, and consciousness.在此示例中,用户的 prompt 指定了主题(必读科幻小说)、一个约束(21 世纪出版)和所需的数量(5 项)。LLM 生成的列表符合这些标准。
列表生成 Prompt 的最佳实践
Section titled “列表生成 Prompt 的最佳实践”为了在使用 LLM 生成列表时获得最佳结果,请考虑以下最佳实践:
- 清晰具体:清晰说明列表的主题(例如,‘健康早餐创意’、‘远程工作者的生产力工具’、‘Python 的优点和缺点’)。
- 指定数量:指明您希望列表中有多少项(例如,‘前 3 名’、‘大约 10 个’、‘不超过 7 个’)。如果您不指定,LLM 将自行决定长度。
- 定义标准或约束:提供列表项目应满足的任何特定标准(例如,‘适合初学者’、‘50 美元以下’、‘开源替代方案’)。
- 请求格式:您可以要求列表采用特定格式,例如项目符号列表(许多 LLM 的默认格式)、编号列表,如果需要,甚至可以是简单的逗号分隔列表。对于更复杂的数据,您可以要求表格状结构(使用 Markdown)甚至 JSON。
- 添加上下文:如果相关背景信息有助于 LLM 生成更有针对性和更实用的列表,请提供。例如,‘我计划五月去意大利旅行,请列出 5 件必备物品。’
- 迭代和完善:如果第一个列表不太符合要求,请完善您的 prompt。添加更多细节,澄清约束条件,或要求提供不同的版本。
应用示例:用于生成列表的 Python 实现
Section titled “应用示例:用于生成列表的 Python 实现”这个脚本定义了一个函数 generate_list_from_llm(),该函数接受列表的主题、标准和项目数量,并提示 ChatGPT 生成列表。
from openai import OpenAIimport os
# client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))# For this example, use a placeholder. Replace with your actual key.client = OpenAI(api_key='YOUR_API_KEY')
def generate_list_from_llm(list_topic, criteria, number_of_items): prompt_content = f"Please generate a list of {number_of_items} {list_topic} that meet the following criteria: {criteria}. Present the list with a brief explanation for each item."
try: response = client.chat.completions.create( model="gpt-3.5-turbo", # 或者使用更新的模型 messages=[ {"role": "system", "content": "You are an AI assistant skilled at creating well-structured and informative lists."}, {"role": "user", "content": prompt_content} ], max_tokens=400, # 根据预期的列表长度进行调整 temperature=0.6, n=1 ) return response.choices[0].message.content except Exception as e: return f"An error occurred: {e}"
# Example usage:topic = "travel destinations in Southeast Asia"criteria_for_list = "suitable for budget travelers interested in culture and nature, avoiding major tourist traps"num_items = 3
generated_list = generate_list_from_llm(topic, criteria_for_list, num_items)print("Generated List of Travel Destinations:")print(generated_list)脚本运行时,它会将请求发送给 LLM,LLM 将根据提供的主题、标准和项目数量生成列表。
Generated List of Travel Destinations:
Here are 3 travel destinations in Southeast Asia suitable for budget travelers interested in culture and nature, avoiding major tourist traps:
1. **Luang Prabang, Laos:** This charming town is a UNESCO World Heritage site, known for its stunning temples, serene atmosphere, and beautiful natural surroundings like the Kuang Si Falls. It offers affordable guesthouses, delicious street food, and opportunities for cultural immersion through alms giving ceremonies and local markets. It's less crowded than many other Southeast Asian hotspots.
2. **Northern Sumatra, Indonesia (e.g., Bukit Lawang, Lake Toba):** For nature lovers, this region offers incredible biodiversity, including opportunities to see orangutans in their natural habitat near Bukit Lawang. Lake Toba, a massive volcanic crater lake, provides stunning scenery and a unique Batak culture. Costs are generally low, especially outside of peak seasons.
3. **Central Vietnam (e.g., Phong Nha-Ke Bang National Park, Hue):** While parts of Vietnam are very touristy, the area around Phong Nha-Ke Bang National Park offers incredible cave systems (some of the world's largest) and lush jungle landscapes ideal for trekking, at a fraction of the cost of similar adventures elsewhere. Nearby, the historic city of Hue provides a deep dive into Vietnam's imperial past with its citadel and tombs, offering rich cultural experiences without the intense crowds of Hoi An or Hanoi.本章探讨了如何有效地提示 LLM 生成列表。通过提供关于列表主题、项目标准、所需数量和格式的清晰指示,您可以利用 LLM 庞大的知识库创建精心策划的列表,用于广泛的目的,从简单的头脑风暴到更结构化的数据收集。