提示用于优化模型输入,提升生成结果的质量。LangChain 提供了强大的提示管理工具,包括提示模板和提示优化功能。提示模板是预定义的格式,包含静态文本和动态变量,用于生成发送给语言模型的输入。它们有助于确保交互的一致性和可控性。
1. 提示词结构
大语言模型的提示词(输入)结构通常包含三个核心部分:
- 系统指令(System Prompt):设置模型的角色、语气、风格等基础行为。
- 用户指令(User Prompt):说明具体要完成的任务(做什么、怎么做)。
- 用户输入(User Input):提供任务所需的上下文、文本、数据或其他原材料。
例如:
系统指令:你是一个翻译助手。 用户指令:请把下面的中文翻译成英文。 用户输入:你好。
为什么在向模型输入的时候,建议我们遵循这样的输入设计呢?
这是因为我们使用的 LLM(大语言模型)通常是通用领域模型,它并不具备明确的任务边界。为了让模型更准确地生成符合期望的内容,我们采用结构化的提示设计,通常包括以下三层:
- 首先,通过 系统指令 设定模型的角色、语气或知识背景,限定模型的行为边界
- 其次,通过 用户命令 明确指定任务目标,告诉模型要做什么
- 最后,通过 用户输入 告诉模型基于什么信息完成任务
注意:在实际应用中,用户指令 和 用户输入 往往是合并在一起构建的,也就是作为一个完整的 user message 提交给模型。
2. 提示词构建
在 LangChain 中,提供了三种构建输入提示词的方式。
from langchain_deepseek import ChatDeepSeek from langchain.schema import SystemMessage, HumanMessage, ChatMessage, AIMessage from dotenv import load_dotenv # 加载环境变量 load_dotenv('llm.env') # 1. 文本补全输入 # 文本生成:用于根据给定的提示(prompt)生成后续的文本内容,不需要显式地输入历史对话内容。 def demo01(): model = ChatDeepSeek(model='deepseek-chat', temperature=0) response = model.invoke('你是一个历史专家,善于回答各种历史知识。请问美国的首都是哪个城市?只需回答城市,不要回复任何解释内容。') print(response.content) # 2. 多轮对话输入 # 多轮对话:用于模拟人与人之间的对话。模型需要根据当前的输入和历史对话内容生成回答。 def demo02(): # 1. 方式一 messages = [ {'role': 'system', 'content': '你是一个翻译家,擅长将中文翻译成英文,回答要简洁准确。'}, {'role': 'user', 'content': '今天天气真好。'}, {'role': 'assistant', 'content': 'The weather is really nice today.'}, {'role': 'user', 'content': '你刚提到的天气这个单词是哪个?'} ] # 2. 方式二 messages = [ChatMessage(role='system', content='你是一个翻译家,擅长将中文翻译成英文,回答要简洁准确。'), ChatMessage(role='user', content='需要翻译的内容:今天天气真好。'), ChatMessage(role='assistant', content='The weather is really nice today.'), ChatMessage(role='user', content='你刚提到的天气这个单词是哪个?')] # 3. 方式三 messages = [SystemMessage('你是一个翻译家,擅长将中文翻译成英文,回答要简洁准确。'), HumanMessage('需要翻译的内容:今天天气真好。'), AIMessage('The weather is really nice today.'), HumanMessage('你刚提到的天气这个单词是哪个?')] model = ChatDeepSeek(model='deepseek-chat') response: BaseMessage = model.invoke(messages) print(response.content) if __name__ == '__main__': demo01() # demo02()
3. 提示词模板
前面构建的提示词是固定的,通过 LangChain 提供的提示词模板,我们可以构建动态的输入提示词。
from langchain.prompts import SystemMessagePromptTemplate from langchain.prompts import HumanMessagePromptTemplate from langchain.prompts import ChatPromptTemplate from langchain.prompts import PromptTemplate from langchain.prompts import MessagesPlaceholder def demo01(): model = ChatDeepSeek(model='deepseek-chat') template = PromptTemplate.from_template('你是一个历史专家,善于回答各种历史知识。请问{country}的首都是哪个城市?只需回答城市,不要回复任何解释内容。') prompt = template.invoke({'country': '德国'}) print(prompt) response = model.invoke(prompt) print(response.content) def demo02(): # 定义模板 # 方式一 messages = [('system', '你是一个翻译家,擅长将{src_lang}翻译成{tgt_lang},回答要简洁准确。'), ('user', '需要翻译的内容:{input}')] template = ChatPromptTemplate.from_messages(messages) # 方式二 # sys_template = SystemMessagePromptTemplate.from_template('你是一个翻译家,擅长将{src_lang}翻译成{tgt_lang},回答要简洁准确。') # usr_template = HumanMessagePromptTemplate.from_template('需要翻译的内容:{input}') # template = ChatPromptTemplate.from_messages([sys_template, usr_template]) # 使用模板 prompt = template.invoke({'src_lang': '中文', 'tgt_lang': '英文', 'input': '今天天气真好。'}) print(prompt) # 3. 占位符 def demo03(): # messages = [('system', '你是一个翻译家,擅长将{src_lang}翻译成{tgt_lang},回答要简洁准确。'), # MessagesPlaceholder(variable_name='chat_history'), # ('user', '需要翻译的内容:{input}')] # template = ChatPromptTemplate.from_messages(messages) sys_template = SystemMessagePromptTemplate.from_template('你是一个翻译家,擅长将{src_lang}翻译成{tgt_lang},回答要简洁准确。') usr_template = HumanMessagePromptTemplate.from_template('需要翻译的内容:{input}') template = ChatPromptTemplate.from_messages([sys_template, MessagesPlaceholder(variable_name='chat_history'), usr_template]) # 使用模板 history = [('user', '需要翻译的内容:我爱你'), ('ai', 'I love u')] prompt = template.invoke({'src_lang': '中文', 'tgt_lang': '英文', 'input': '今天天气真好。', 'chat_history': history}) print(prompt) if __name__ == '__main__': # demo01() # demo02() demo03()