LangChain 核心组件 [ 2 ]
提示词模板Prompt Template概念提示词模板Prompt Template是 LangChain 的核心抽象之一它被广泛应用于构建大语言模型LLM应用的各个环节。简单来说只要是需要动态、批量、或有结构地向大语言模型发送请求的地方几乎都会用到提示词模板。一个简单的例子假设我们想根据一个城市名询问 LLM 其历史按照之前的做法我们可以定义HumanMessage(请介绍上海的历史)、HumanMessage(请介绍西安的历史)消息等。可以发现每次询问都会编写重复的消息内容请介绍xxx的历史。在 LangChain 中针对这种情况可以定义一个模板固定文本模板请介绍{city}的历史。输入变量[city]定义好后可以使用该模板当我们需要查询北京时就将city变量赋值为北京模板引擎会生成请介绍北京的历史。当我们需要查询上海时就将city变量赋值为上海模板引擎会生成请介绍上海的历史。由此可见提示词模板就是一个可复用的提示词蓝图它允许我们动态地生成提示而不是每次都手动编写完整的提示词。它类似于编程中的字符串格式化功能。你创建一个带有 “占位符” 的模板然后在运行时用具体的值变量填充这些占位符从而生成一个最终发送给 LLM 的完整提示词。提示词模板解决了以下几个核心问题可复用性只需定义一个模板就可以用于无数个类似的查询。# 定义一次 template PromptTemplate.from_template(你好{name}欢迎学习 LangChain) # 无限用 template.invoke({name: 小明}) template.invoke({name: 小红}) template.invoke({name: 老板})关注点分离将提示词的结构和逻辑工程与具体的内容和数据分离开。提示工程师可以专注于优化模板而应用程序则负责提供变量值。# 提示工程师只负责写模板 template PromptTemplate.from_template( 你是一个助手请回答用户问题 用户问题{question} 请用中文回答。 ) # 业务开发只负责传变量 result template.invoke({ question: 什么是提示词模板 })一致性确保发送给 LLM 的提示词结构统一这有助于获得更稳定、可预测的输出结果。# 永远统一格式 template PromptTemplate.from_template( 系统你是一个数学老师 用户{question} 请一步步解答。 ) template.invoke({question: 11等于几}) template.invoke({question: 2×3等于几}) template.invoke({question: 什么是素数})可维护性如果需要修改提示词的风格或结构只需修改一个模板文件而不用在代码的无数个地方进行修改。# 原来 template PromptTemplate.from_template(你好{name}) # 想变温柔一点只改这里 template PromptTemplate.from_template(哈喽{name}呀欢迎你)用法字符串模板LangChain 提供了PromptTemplate类来轻松实现这一功能。PromptTemplate实现了标准的Runnable接口。示例如下# 1. 定义模板 from langchain_core.prompts import PromptTemplate prompt_template PromptTemplate.from_template(Translate the following into {language}) # 2. 实例化模板 print(prompt_template.invoke({language: Chinese}))textTranslate the following into Chinese说明class langchain_core.prompts.prompt.PromptTemplate类其参数如下template提示模板input_variables需要其值作为提示输入的变量的名称列表。内置方法from_template()从模板定义提示。方法返回了一个PromptTemplate实例。因此除了上面示例中PromptTemplate.from_template定义提示模板的方式外下面这种方法也可以直接初始化模板prompt_template PromptTemplate( input_variables[language], templateTranslate the following into {language}, )聊天消息模板ChatPromptTemplate模板专为 LangChain 聊天模型设计。可以方便地构建包含SystemPromptMessage、HumanMessage、AIMessage类型的消息模板。如下代码示例所示from langchain_core.prompts import ChatPromptTemplate # 1. 设置模板 prompt_template ChatPromptTemplate( [ (system, Translate the following into {language}.), (user, {text}) ] ) # 说明 # 在 0.2.24 版本后可以直接使用 ChatPromptTemplate() 来初始化模板 # 在 0.2.24 版本前需要使用 ChatPromptTemplate.from_messages() 来初始化模板 # 2. 实例化模板获取消息实例 messagesValue prompt_template.invoke( { language: Chinese, text: what is your name? } ) messages messagesValue.to_messages() print(messages)打印结果[ SystemMessage(contentTranslate the following into Chinese., additional_kwargs{}, response_metadata{}), HumanMessage(contentwhat is your name?, additional_kwargs{}, response_metadata{}) ]现在我们可以将该结果发送给任何一个 LLM 来获取答案。如下所示from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser # 定义大模型 model ChatOpenAI(modelgpt-4o-mini) # 1. 设置模板 prompt_template ChatPromptTemplate( [ (system, Translate the following into {language}.), (user, {text}) ] ) # 2. 实例化模板获取消息实例 messagesValue prompt_template.invoke( { language: Chinese, text: what is your name? } ) messages messagesValue.to_messages() print(messages) # 3. 输出解析 parser StrOutputParser() chain model | parser print(chain.invoke(messages))打印结果你的名字是什么由于ChatPromptTemplate同样也实现了标准的Runnable接口因此我们还可以通过链来完成调用。代码如下所示from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser # 定义大模型 model ChatOpenAI(modelgpt-4o-mini) # 1. 设置模板 prompt_template ChatPromptTemplate( [ (system, Translate the following into {language}.), (user, {text}) ] ) # 2. 定义输出解析器 parser StrOutputParser() # 3. 定义链 chain prompt_template | model | parser for token in chain.stream( { language: English, text: 你好我叫斯蒂芬很高兴认识你 } ): print(token, end|)打印结果|Hello, | my| name| is| Stephen|, | nice| to| meet| you|.|消息占位符在上面的ChatPromptTemplate中我们看到了如何格式化两条消息每条消息都是一个字符串。但如果我们希望将消息列表直接传入需要使用MessagesPlaceholder。代码如下from langchain_core.prompts import ChatPromptTemplate from langchain_core.prompts import HumanMessage, AIMessage, MessagesPlaceholder prompt_template ChatPromptTemplate( [ (system, 你是一个聊天助手), MessagesPlaceholder(msgs) # 消息占位符 ] ) messages_to_pass [ HumanMessage(content中国首都是哪里), AIMessage(content中国首都是北京。), HumanMessage(content那法国呢) ] formatted_prompt prompt_template.invoke({msgs: messages_to_pass}) print(formatted_prompt)打印结果messages[ SystemMessage(content你是一个聊天助手, additional_kwargs{}, response_metadata{}), HumanMessage(content中国首都是哪里, additional_kwargs{}, response_metadata{}), AIMessage(content中国首都是北京。, additional_kwargs{}, response_metadata{}), HumanMessage(content那法国呢, additional_kwargs{}, response_metadata{}) ]在不显式使用MessagesPlaceholder类也可以完成该能力from langchain_core.prompts import ChatPromptTemplate from langchain_core.prompts import HumanMessage, AIMessage prompt_template ChatPromptTemplate( [ (system, You are a helpful assistant), (placeholder, {msgs}) ] ) messages_to_pass [ HumanMessage(content中国首都是哪里), AIMessage(content中国首都是北京。), HumanMessage(content那法国呢) ] formatted_prompt prompt_template.invoke({msgs: messages_to_pass}) print(formatted_prompt)使用 LangChain Hub 的提示词模板目前收藏最高的提示词模板是hardkothari/prompt-maker。我们就以它为示例演示一下如何使用 LangChain Hub 上的提示。Prompt Maker模板是一个【提示生成器】它可以自动化优化提示的过程从而提高语言模型在各种应用中的质量和效果。要想使用该能力需要先申请并配置LangSmith环境变量LANGSMITH_API_KEY你的LangSmith API Key。接着需要从 hub 拉取相应的提示并使用代码如下from langchain_openai import ChatOpenAI from langsmith import Client # 1. 从 LangChain Hub 拉取提示词模板自动优化提示词 client Client() prompt client.pull_prompt(hardkothari/prompt-maker, include_modelTrue) # 2. 定义大模型 model ChatOpenAI(modelgpt-4o-mini) # 3. 构建调用链模板 → 模型 chain prompt | model # 4. 循环交互 while True: # 输入任务需求 task input(\n你的任务是什么? (输入 quit 退出聊天)\n) if task quit: break # 输入原始简陋提示词 lazy_prompt input(\n你当前的提示是什么? (输入 quit 退出聊天)\n) if lazy_prompt quit: break # 执行并输出优化后的提示词 print(\n 模型返回的【优化后提示词】) response chain.invoke({lazy_prompt: lazy_prompt, task: task}) response.pretty_print()你的任务是什么? (输入 quit 退出聊天) 写一个快排代码 你当前的提示是什么? (输入 quit 退出聊天) 开发专家需中文回复 模型返回的【优化后提示词】 Ai Message As a coding expert specialized in algorithms and data structures, please write a detailed implementation of the Quick Sort algorithm in Python. 【翻译】作为一名专攻算法与数据结构的开发专家请用 Python 实现详细的快速排序代码。 ### Instructions: 【翻译】实现要求 Your code should be well-structured, with clear comments explaining each step of the algorithm. Additionally, include an example of how to use the function to sort a list of integers and print the sorted result. 【翻译】代码结构要清晰每一步都要有详细注释同时提供使用示例对整数列表进行排序并打印结果。 ### Context: 【翻译】额外要求 The implementation should focus on efficiency and clarity, ideally spanning no more than 20 lines of code. Ensure that the prompt is in Chinese, and structure your response to be easily understandable for readers with a basic knowledge of programming. 【翻译】实现要兼顾效率与可读性代码尽量不超过20行必须用中文输出内容要让有编程基础的人能轻松看懂。 Example: 【翻译】示例格式 python def quick_sort(arr): # 快速排序函数实现 ... return sorted_arr # 使用示例 unsorted_list [34, 7, 23, 32, 5, 62] print(quick_sort(unsorted_list)) Please provide the complete code and any additional notes that could aid in understanding the Quick Sort algorithm. Thank you! 【翻译】请提供完整代码并补充帮助理解快速排序的说明谢谢通过使用这个模板可以大幅减少构建提示词所需的工作量从而节省时间和资源。Prompt Maker通过分析用户提供的结构和内容然后应用一系列的算法来优化提示以提高响应质量。清晰适用于代码、数据分析任务、质量检查等有很大的场景中均有应用比如客户服务机器人、对话代理或数据分析任务。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2570241.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!