趋近智
尽管基本提示词 (prompt)适用于简单的请求,但要指导大型语言模型(LLM)完成更复杂的任务,通常需要更具结构的方法。复杂任务可能涉及多步骤推理 (inference)、从大量文本中提取特定信息、整合多源数据或生成特定格式的输出。仅仅说明最终目标可能不够;你需要指导模型的工作流程。
有效地构建提示词涉及分解复杂性,并提供明确的限制或指令。这有助于LLM集中其能力,并减少生成不相关、不正确或格式不佳回复的可能性。接下来,我们看几种你可以直接在Python代码中实现的方法。
处理复杂性最直接的方法之一,是在提示词 (prompt)内部将任务分解为更小、按顺序的步骤。并非直接要求最终结果,你会指导模型达成该结果的过程。
考虑一个任务,例如总结一篇技术文章并提取其中提到的主要技术。一个简单的提示词可能含糊不清。结构化的提示词会将其分解:
article_text = """
Generative Adversarial Networks (GANs) are a class of machine learning frameworks designed by Ian Goodfellow and his colleagues in 2014. Two neural networks, the generator and the discriminator, contest with each other in a game. Given a training set, this technique learns to generate new data with the same statistics as the training set. For example, a GAN trained on photographs can generate new photographs that look at least superficially authentic to human observers, having many realistic characteristics. Though originally proposed as a form of generative model for unsupervised learning, GANs have also proven useful for semi-supervised learning, fully supervised learning, and reinforcement learning. Important advancements include DCGANs for stable image generation and CycleGAN for unpaired image-to-image translation.
"""
prompt_template = f"""
请严格按照以下步骤使用提供的文章文本:
1. 仔细阅读整篇文章。
2. 撰写文章的简洁摘要(2-3句话)。
3. 找出文章中提到的所有具体的机器学习技术或模型架构。
4. 将这些技术逐行列出。
文章文本:
'''
{article_text}
'''
指令:
1. 提供摘要。
2. 提供技术列表。
"""
# response = call_llm_api(prompt_template) # 假设此函数存在
# print(response)
这种结构化方法明确地指导模型,使其更有可能正确执行摘要和提取任务。
思维链提示是一种特定的分解方法,它鼓励模型在提供最终答案之前“大声思考”或概述其推理 (inference)步骤。这对于涉及算术、逻辑谜题或多步骤推断的任务特别有用,这些任务中过程与结果同等重要。你通常通过添加诸如“让我们一步一步来思考”的短语,或提供少量示例来展示这种推理过程来实现这一点。
提示词 (prompt)结构示例:
question = "A bookstore had 50 notebooks. They sold 15 on Monday and received a new shipment of 20 on Tuesday. How many notebooks do they have now?"
prompt = f"""
问题: {question}
让我们一步一步来解决这个问题:
1. 初始笔记本数量:50
2. 周一售出:15
3. 周一后剩余:50 - 15 = 35
4. 周二收到:20
5. 现在笔记本总数:35 + 20 = 55
最终答案:最终答案是 55
"""
# 在实际场景中,你可能只提供问题和“让我们一步一步来思考:”
# 然后让LLM生成推理过程和最终答案。
# prompt_for_llm = f"Question: {question}\n\nLet's think step by step:"
即使不自己提供完整的推理过程,添加“让我们一步一步来思考”通常会促使模型采用更系统(且通常更准确)的内部工作流程。
赋予LLM一个特定角色或身份可以有效限制其行为,并根据复杂情况调整其回复。这使模型专注于适合该任务的相关知识和风格。
user_query = "Explain the difference between TCP and UDP for someone building a real-time game."
prompt = f"""
你是一名资深网络工程师,为游戏开发者提供建议。
请解释TCP和UDP的主要区别,重点关注与实时多人游戏相关的方面(延迟、可靠性、顺序)。解释要清晰简洁。
用户查询: {user_query}
"""
# response = call_llm_api(prompt)
# print(response)
通过分配“资深网络工程师”角色,提示词 (prompt)鼓励生成使用适当术语并侧重于对目标受众(游戏开发者)实际影响的回复。
对于需要与数据交互或生成特定输出格式的任务,在提示词 (prompt)中明确地结构化输入和预期输出非常重要。
清晰的分隔符帮助模型区分指令、上下文 (context)数据、示例以及其回复区域。常见分隔符包括三引号 (''')、三反引号 (```)、类似XML的标签 (<instruction>, <data>),或简单的标记 (token),例如 ###。
context = "Patient Notes: John Doe, 45yo male, presents with persistent cough for 2 weeks. Mild fever reported yesterday. History of asthma. Current meds: Albuterol inhaler PRN."
instruction = "Extract patient age, symptoms, and relevant medical history."
output_format_hint = "Provide output as a JSON object with keys 'age', 'symptoms', 'history'."
prompt = f"""
### 指令 ###
{instruction}
### 上下文数据 ###
{context}
### 期望输出格式 ###
{output_format_hint}
### 输出 ###
{{
"age": 45,
"symptoms": ["persistent cough", "mild fever"],
"history": ["asthma"]
}}
"""
# 注意:上面的最终输出示例可能作为单次示例包含在内,
# 或者你可以在### Output ###部分之前停止提示词,让LLM完成。
# LLM生成示例:
prompt_for_llm = f"""
### 指令 ###
{instruction}
### 上下文数据 ###
{context}
### 期望输出格式 ###
{output_format_hint}
### 输出 ###
"""
# response = call_llm_api(prompt_for_llm) # 期望JSON输出
# print(response)
你可以直接要求模型生成采用JSON、XML、Markdown表格或列表等格式的输出。这对于将LLM输出集成到下游应用程序逻辑中非常重要。
report_text = "Sales increased by 15% in Q2, reaching $1.2M. Main drivers were the new product line and expansion into the European market. Q1 sales were $1.04M."
prompt = f"""
分析提供的销售报告文本。
提取以下信息:
- 第二季度销售额
- 第二季度增长百分比
- 第一季度销售额
- 提到的主要驱动因素
请以JSON对象形式提供结果。
报告文本:
'''
{report_text}
'''
JSON 输出:
"""
# response = call_llm_api(prompt)
# print(response)
# 期望的输出结构:
# {
# "q2_sales": 1200000,
# "q2_increase_percentage": 15,
# "q1_sales": 1040000,
# "key_drivers": ["new product line", "拓展欧洲市场"]
# }
这条明确指令大大增加了从LLM获得机器可读结构化数据的可能性。
通常,用于复杂任务最有效的提示词 (prompt)会结合其中几种结构化方法。你可以分配一个角色、提供分步指令、使用分隔符以提高清晰度,并指定精确的输出格式,所有这些都在一个提示词中完成。
图表说明了不同的结构化方法如何有助于引导LLM在复杂任务中生成期望的输出。
Python的字符串操作能力,尤其是f-string,非常适合动态构建这些结构化提示词 (prompt)。你可以轻松将包含用户输入、上下文 (context)数据或配置参数 (parameter)的变量插入到你的提示词模板中。
def create_analysis_prompt(report_text, requested_format="JSON"):
instruction = "Analyze the provided financial report text. Extract important financial figures and performance indicators."
format_instruction = f"Provide the result as a {requested_format} object."
prompt = f"""
### 角色 ###
你是一名金融分析师AI助手。
### 指令 ###
{instruction}
{format_instruction}
### 报告数据 ###
'''
{report_text}
'''
### 输出 ({requested_format}) ###
"""
return prompt
financial_report = "Company X reported revenue of $5M in Q3, a 10% increase YoY. Net profit was $500k."
prompt_string = create_analysis_prompt(financial_report, requested_format="JSON")
print(prompt_string)
# 现在你可以将 prompt_string 传递给你的LLM API调用
这种方法让你能够在Python应用程序中创建可复用且适应性强的提示词函数。
结构化提示词 (prompt)增加了清晰度,但同时也增加了提示词的长度。请注意你正在使用的LLM的上下文 (context)窗口限制。过长的提示词可能会被截断或变得计算成本高昂。此外,找到最佳结构通常需要进行实验。这里讨论的方法提供了一个很好的起点,但你可能需要根据LLM的回复进行迭代和改进你的提示词,这引出了迭代提示词改进的话题。
简洁的语法。内置调试功能。从第一天起就可投入生产。
为 ApX 背后的 AI 系统而构建
这部分内容有帮助吗?
© 2026 ApX Machine LearningAI伦理与透明度•