langchain介绍之-Prompt

LangChain 是一个基于语言模型开发应用程序的框架。它使得应用程序具备以下特点:1.数据感知:将语言模型与其他数据源连接起来。2.代理性:允许语言模型与其环境进行交互

LangChain 的主要价值在于:组件: 用于处理语言模型的抽象,以及每个抽象的多个实现集合。这些组件是模块化且易于使用的,无论您是否在使用 LangChain 框架的其他部分
现成的链式结构:由多个组件组成的结构化组合,用于完成特定的高级任务现成的链式结构使得入门变得轻松。对于更复杂的应用程序和微妙的用例,组件使得可以轻松定制现有链式结构或构建新的结构。此篇博客主要介绍Langchain的prompt相关内容。

Langchain中提供了哪些Prompt呢?具体如下图所示,是截至目前Langchain提供的所有模版,对于base类模版,在通过langchain构建应用时,一般很少用到,开发者主要用的还是ChatPromptTemplate,PromptTemplate,以及各类MessagePromptTemplate。

为什么Lanchain会提供不同类型的MessagePromptTemplate呢?因为Openai的原始接口中,对于chat completion这个接口,里面的user role就分为user,system,assistant三个角色,所以,这里的MessageTemplate也分为HumanMessagePromptTemplate,AIMessagePromptTemplate,SystemMessagePromptTemplate。

openai官方提供的chat completion的接口如下图所示,可以看到原始调用openai的接口中,需要传入role的信息,所以上面的三种messagePromptTemplate对应三种不同的角色。

了解了前面的基础知识后,来看看如何使用PromptTemplate。下面的代码中调用from_template

(...)传入了一份带变量的字符串,调用format信息后,打印出来的message就是将变量值于原有字符串merge后的值。另外,从结果也可以看到,PromptTemplate是一个报刊input_variables和template变量的的class。

python 复制代码
import openai
import os
from langchain.prompts import (PromptTemplate)

prompt_template = PromptTemplate.from_template(
    "Tell me a joke about {context}")
message = prompt_template.format(context="chidren")
print(prompt_template)
print(type(prompt_template))
print(message)

除了通过from_template()的方法初始化一个PromptTemplate的class外,还可以通过下面的方法初始化这个class

python 复制代码
prompt_template_two = PromptTemplate(
    input_variables=['name'],
    template="what is your {name}"
)
print(prompt_template_two)

接着来看看SystemMessagePromptTemplate的使用,在创建好一个PromptTemplate后,可以将prompt赋值给SystemMessagePromptTemplate。可以看到SystemMessagePromptTemplate除了prompt变量外,还有template_format,validate_template变量。

python 复制代码
prompt = PromptTemplate(
    template="You are a helpful assistant that translates {input_language} to {output_language}.",
    input_variables=["input_language", "output_language"],
)
system_message_prompt = SystemMessagePromptTemplate(prompt=prompt)
print(system_message_prompt)
print(type(system_message_prompt))

除了上面的方式初始化一个SystemMessageTemplate外,还可以通过调用from_template的方式进行初始化。可以看到初始化出来的对象是一样的。

python 复制代码
prompt_string = """Translate the text \
that is delimited by triple backticks \
into a style that is {style}. \
text: ```{text}```
"""
system_message_prompt_two = SystemMessagePromptTemplate.from_template(
    prompt_string)
print(system_message_prompt_two)

接下来再看看ChatPromptTemplate,这里先创建了一个HumanMessagePromptTemplate,然后通过from_message,将创建了promptTemplate赋值给了ChatPromptTemplate

python 复制代码
human_prompt_template = HumanMessagePromptTemplate.from_template(prompt_string)
print(human_prompt_template)
print('-------------------')
chat_prompt = ChatPromptTemplate.from_messages([human_prompt_template])
print(chat_prompt)

执行结果如下图所示,可以看到直接打印的话,HumanMessagePromptTemplate和前面的SystemMessagePromptTemplate无区别,class包含的字段都一样。组装出来的ChatPromptTemplate包含input_variables,output_parser,messages三个变量,messages的值就是生成的HumanMessagePromptTemplate.

调用ChatPromptTemplate的format_messages()方法,可以将变量值和原有的prompt中的文字进行合并。结果如下图所示,返回的message是一个List,List只有一个值就是HumanMessage对象,HumanMessage对象又包含content,additional_kwargs={},example变量。

python 复制代码
message = chat_prompt.format_messages(style="myStyle", text="mytext")
print(message)

可以看到不同promptTemplate之间有一点绕,这可能也和AI技术不断在更新,langchain也在不断迭代有关吧。

message对象生成好后,就可以调用model生成内容了,代码如下所示:

python 复制代码
chat = ChatOpenAI(model_name="gpt-3.5-turbo", verbose=True)
response = chat(message)
print(response)
print(response.content)

调用大模型生成的内容如下图所示:

以上就是对Langchain中Prompt的使用介绍。

相关推荐
lusasky6 小时前
AgentScope、LangChain、AutoGen 全方位对比 + 混用可行性指南
microsoft·langchain
前端阿森纳19 小时前
从产品经理视角拆解 LangChain 的抽象设计
langchain·llm·aigc
大模型真好玩1 天前
LangGraph1.0速通指南(一)—— LangGraph1.0 核心概念、点、边
人工智能·langchain·agent
阿里云云原生1 天前
AgentRun Sandbox SDK 正式开源!集成 LangChain 等主流框架,一键开启智能体沙箱新体验
阿里云·langchain·开源·serverless·agentarun
、、、、南山小雨、、、、1 天前
最简单的LangChain和RAG
langchain
sztomarch1 天前
Windows-PowerShell-prompt
windows·prompt
路边草随风1 天前
langchain agent动态变更系统prompt
人工智能·python·langchain·prompt
Jack___Xue1 天前
LangChain实战快速入门笔记(六)--LangChain使用之Agent
笔记·langchain·unix
CNRio2 天前
从“手搓Prompt“到“物理世界提示词“:Looki L1如何重塑AI交互范式
人工智能·prompt·交互
大模型教程2 天前
使用Langchain4j和Ollama3搭建RAG系统
langchain·llm·ollama