趋近智
许多大型语言模型(LLM)应用需要指令能够适应不断变化的情况、用户输入或获取的数据。尽管静态指令可以作为一种初始方法,但硬编码所有可能的变体是不切实际且不灵活的。因此,使用 Python 的功能动态生成指令变得必不可少。动态指令生成使您能够即时为大型语言模型(LLM)构建定制的说明,从而实现更相关、更个性化、更有效的交互。
设想一下需要生成一篇新闻文章的摘要,但同时又希望大型语言模型根据用户偏好采用特定的语气(正式、非正式、乐观)。或者考虑一个聊天机器人,需要将用户的姓名和之前的对话轮次纳入其下一条指令中。这些场景需要指令在发送给模型之前即时组合,融入具体、最新的信息。
Python 提供了几种简单的方法来动态构建指令。
Python 的 f-strings(格式化字符串字面量)通常是将变量和表达式嵌入到指令文本中最直接、最易读的方式。它们允许您创建模板,其中占位符在运行时填充。
user_name = "Alex" # 用户名
topic = "the future of renewable energy" # 话题
difficulty = "beginner" # 难度
# 使用 f-string 插入变量
prompt = f"""
Explain {topic} to me.
Assume I am a {difficulty} and my name is {user_name}.
Keep the explanation concise and focus on the main benefits.
"""
print(prompt)
这段代码会生成以下指令字符串:
请向我解释可再生能源的未来。
假设我是一名初学者,我的名字是 Alex。
请保持解释简洁,并侧重于主要益处。
您可以在 f-string 中的花括号 {} 内嵌入任何有效的 Python 表达式,使其成为进行简单动态调整的功能强大的工具。
通常,您会希望根据特定条件改变指令的一部分。标准的 Python if/elif/else 语句非常适合此目的。您可以构建不同的指令片段,并根据应用逻辑组合它们。
user_skill_level = "expert" # 用户技能水平(可来自用户资料)
query = "Explain the transformer architecture." # 查询
prompt_base = f"Question: {query}\nAnswer:"
prompt_suffix = "" # 初始化空后缀
if user_skill_level == "beginner":
prompt_suffix = "\n请简单解释,避免技术术语。"
elif user_skill_level == "intermediate":
prompt_suffix = "\n假设您对基本机器学习概念有一定了解。"
elif user_skill_level == "expert":
prompt_suffix = "\n请随意包含技术细节和数学符号。"
final_prompt = prompt_base + prompt_suffix
print(final_prompt)
user_skill_level = "expert" 的输出:
问题:解释 Transformer 架构。
回答:
请随意包含技术细节和数学符号。
这种方法允许根据运行时条件对指令进行显著的结构变化。
当您需要包含多条数据时,例如项目列表、用户评论或搜索结果,Python 的 for 循环是不可或缺的。您可以遍历数据并在指令中进行适当格式化。
product_name = "Quantum Leap Laptop" # 产品名称
reviews = [
"Amazing speed, boots up in seconds!", # 惊人的速度,几秒钟内启动!
"Battery life could be better, but overall solid.", # 电池续航可以更好,但整体表现稳定。
"A bit pricey, but the performance justifies it.", # 有点贵,但性能物有所值。
"Screen resolution is fantastic for graphic design work." # 屏幕分辨率对于图形设计工作来说非常棒。
]
# 将评论格式化为带序号的列表,用于指令
formatted_reviews = "\n".join([f"{i+1}. {review}" for i, review in enumerate(reviews)])
prompt = f"""
Summarize the main pros and cons for the product "{product_name}" based on these user reviews:
{formatted_reviews}
Provide the summary as bullet points under 'Pros' and 'Cons'.
"""
print(prompt)
这会生成:
请根据这些用户评论,总结产品“Quantum Leap Laptop”的主要优点和缺点:
1. 惊人的速度,几秒钟内启动!
2. 电池续航可以更好,但整体表现稳定。
3. 有点贵,但性能物有所值。
4. 屏幕分辨率对于图形设计工作来说非常棒。
请以“优点”和“缺点”为标题,提供要点总结。
LangChain 等库在设计时考虑了动态指令。它们的 PromptTemplate 对象明确定义了输入变量,使动态生成清晰且有条理。您在第四章中了解了 PromptTemplate;下面是集成动态数据的方法:
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI # 示例大型语言模型集成
# 假设 llm 已初始化:llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
template_string = """
Generate a short product description for a product with the following features.
Product Name: {product_name}
Category: {category}
Important Features:
{features_list}
Target Audience: {audience}
Tone: {tone}
"""
prompt_template = PromptTemplate(
input_variables=["product_name", "category", "features_list", "audience", "tone"],
template=template_string
)
# 动态数据
product_data = {
"product_name": "EcoGrow Smart Garden",
"category": "Home & Garden",
"features": ["Automated watering", "LED grow lights", "App connectivity", "Soil sensors"],
"audience": "Urban dwellers with limited space",
"tone": "Enthusiastic and eco-conscious"
}
# 使用循环格式化功能列表,然后传递给模板
formatted_features = "\n".join([f"- {feature}" for feature in product_data["features"]])
# 使用模板和动态数据生成最终指令
final_prompt_string = prompt_template.format(
product_name=product_data["product_name"],
category=product_data["category"],
features_list=formatted_features, # 传递格式化后的列表
audience=product_data["audience"],
tone=product_data["tone"]
)
print("--- 生成的指令 ---")
print(final_prompt_string)
# 示例:在简单链中使用此指令
# chain = LLMChain(llm=llm, prompt=prompt_template)
# response = chain.run(product_name=product_data["product_name"], ...)
# print("\n--- 大型语言模型响应 (示例) ---")
# print(response) # 这部分需要 API 密钥和实际执行
此示例展示了如何结合 Python 的字符串格式化(在 features_list 的循环内)与 LangChain 的 PromptTemplate 来创建结构化的动态指令。PromptTemplate 明确定义了预期的输入,Python 代码在填充模板之前准备数据。
此图展示了应用逻辑如何使用 Python 结合数据和模板生成供大型语言模型使用的最终指令的流程。
通过掌握 Python 动态指令生成,您能很大程度上控制大型语言模型交互,从而构建更精密、适应性更强、以用户为中心的应用。它将指令从静态说明转变为由实时上下文和数据塑造的灵活沟通工具。
简洁的语法。内置调试功能。从第一天起就可投入生产。
为 ApX 背后的 AI 系统而构建
这部分内容有帮助吗?
PromptTemplate 对象的创建和使用,用于结构化和动态提示构建。© 2026 ApX Machine Learning用心打造