Skip to content

COLUMN 提示

请求表格化输出是一项强大的提示词工程技术,它使我们能够指示大语言模型(LLM),如 ChatGPT,以清晰、有条理的表格形式组织和格式化其回复。通过明确要求表格,通常会建议使用 Markdown 等格式,我们可以获得易于阅读和处理的结构化数据。

不同于通用的自由格式文本回复,你可以提示 LLM 以列和行的形式呈现信息。这对于比较、功能列表、数据摘要或任何结构化信息能增强清晰度的场景特别有用。

你通过在提示词中指定所需的列,有时还包括整体结构来引导 LLM。Markdown 是请求表格时一种常见且有效的格式,因为它易于 LLM 生成,也易于人类阅读。

以下是请求表格化输出的示例:

User: Can you compare the key features of the latest iPhone Pro and Samsung Galaxy Ultra? Present the comparison in a Markdown table with columns: 'Feature', 'iPhone Pro', and 'Samsung Galaxy Ultra'.
LLM: Sure! Here's a comparison of some key features in a Markdown table:
| Feature | iPhone Pro | Samsung Galaxy Ultra |
|------------------|---------------------|----------------------|
| Main Camera | 48MP | 200MP |
| Telephoto Zoom | 3x or 5x Optical | 3x & 10x Optical |
| Chipset | A17 Bionic (example)| Snapdragon Gen 3 (example) |
| Display Size | ~6.1" / 6.7" | ~6.8" |
| Operating System | iOS | Android |
| Stylus Support | No | Yes (S Pen included) |

在此示例中,用户明确请求一个比较表格并指定了列标题。然后 LLM 以请求的 Markdown 表格格式生成了信息。

为了最大化利用表格化输出请求:

  • 清晰定义列标题:指定每一列的准确名称。这为 LLM 提供了必要的上下文和结构。
  • 指定格式(例如,Markdown):明确要求“Markdown 表格”通常能为人类可读的表格带来最佳结果。对于机器可读的数据,你可以请求 CSV 或 JSON(尽管 JSON 更适合通过要求包含特定键的 JSON 对象来处理)。
  • 具体说明内容:必要时,说明每单元格或每行应包含哪种信息。
  • 尽可能保持简洁:过宽或行数过多的表格可能会被截断或不太连贯。必要时,分解复杂的原始数据请求。
  • 验证和迭代:LLM 有时可能会在表格格式或内容上出错。检查输出结果并在必要时优化你的提示词。

让我们通过一个实际示例,使用一个通过 OpenAI API 与 LLM 交互的 Python 脚本来请求表格化输出。

在此示例中,我们定义了一个函数 generate_tabular_response,该函数接收用户的请求,并要求 LLM 将回复格式化为 Markdown 表格。

import openai
import os
# 推荐:通过环境变量设置 API 密钥。
# openai.api_key = os.getenv("OPENAI_API_KEY")
# 对于本教程,使用占位符。请替换为你的实际密钥。
openai.api_key = 'YOUR_API_KEY'
def generate_tabular_response(user_query, table_description):
prompt_content = (
f"{user_query}. "
f"Please present this information in a Markdown table. {table_description}"
)
messages = [
{"role": "system", "content": "You are helpful assistant skilled at presenting information in well-structured Markdown tables."},
{"role": "user", "content": prompt_content}
]
try:
response = openai.chat.completions.create(
model="gpt-3.5-turbo", # 或使用更新的模型,如 gpt-4o-mini
messages=messages,
max_tokens=500, # 根据预期的表格大小进行调整
temperature=0.3, # 较低的 temperature 值用于获取更事实性、结构化的输出
n=1,
stop=None
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"An error occurred: {e}"
# 示例用法:
query = "Compare the top 3 cloud providers: AWS, Azure, and GCP."
description = "The table should have columns: 'Cloud Provider', 'Key Strengths', and 'Example Service'."
tabular_data = generate_tabular_response(query, description)
print("Generated Tabular Data:")
print(tabular_data)

运行脚本后,你将收到 LLM 的回复,其中包含按 Markdown 表格格式化的请求信息。对于云提供商的比较,输出可能看起来像这样:

生成的表格数据:

| Cloud Provider | Key Strengths | Example Service |
|----------------|------------------------------------------------|------------------------|
| AWS | Market leader, extensive service portfolio, mature ecosystem | Amazon EC2 (Compute) |
| Azure | Strong enterprise integration (Microsoft ecosystem), hybrid cloud capabilities | Azure Virtual Machines |
| GCP | Expertise in data analytics, machine learning, Kubernetes | Google Kubernetes Engine (GKE) |

在本章中,我们探讨了请求 LLM 提供表格化输出的技术。通过清晰地指示模型使用表格格式(如 Markdown)并指定列标题,你可以获得更易于理解和使用、结构清晰、组织有序的信息。

我们讨论了定义列、指定格式和迭代提示词等最佳实践。Python 示例展示了如何通过编程方式实现这一点。这项技能对于有效地呈现比较数据、功能列表和其他结构化信息至关重要。