组合(Composition)
LangChain 提供了一个用户友好的接口,用于把提示的不同部分组合在一起。你可以用字符串提示或聊天提示来做到这一点。以这种方式构造提示可以轻松复用组件。
字符串提示组合
处理字符串提示时,每个模板会被拼接在一起。你可以直接使用提示或字符串(列表中的第一个元素需要是一个提示)。
from langchain_core.prompts import PromptTemplateprompt = (
PromptTemplate.from_template("Tell me a joke about {topic}")
+ ", make it funny"
+ "\n\nand in {language}"
)promptPromptTemplate(input_variables=['language', 'topic'], template='Tell me a joke about {topic}, make it funny\n\nand in {language}')prompt.format(topic="sports", language="spanish")'Tell me a joke about sports, make it funny\n\nand in spanish'你也可以像以前一样在 LLMChain 中使用它。
from langchain.chains import LLMChain
from langchain_openai import ChatOpenAImodel = ChatOpenAI()chain = LLMChain(llm=model, prompt=prompt)chain.run(topic="sports", language="spanish")'¿Por qué el futbolista llevaba un paraguas al partido?\n\nPorque pronosticaban lluvia de goles.'聊天提示组合
聊天提示由消息列表组成。纯粹为了开发者体验,我们加入了一种创建这些提示的便捷方式。在这个管道中,每个新元素都是最终提示中的一条新消息。
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage首先,让我们用一个系统消息初始化基础的 ChatPromptTemplate。它不一定要以系统消息开始,但这通常是好习惯。
prompt = SystemMessage(content="You are a nice pirate")然后你可以轻松地创建一个管道,把它与其它消息 或 消息模板组合起来。
当没有需要格式化的变量时,使用 Message;当有需要格式化的变量时,使用 MessageTemplate。你也可以只用一个字符串(注意:这会自动被推断为 HumanMessagePromptTemplate)。
new_prompt = (
prompt + HumanMessage(content="hi") + AIMessage(content="what?") + "{input}"
)在底层,这会创建 ChatPromptTemplate 类的一个实例,因此你可以像以前一样使用它!
new_prompt.format_messages(input="i said hi")[SystemMessage(content='You are a nice pirate', additional_kwargs={}),
HumanMessage(content='hi', additional_kwargs={}, example=False),
AIMessage(content='what?', additional_kwargs={}, example=False),
HumanMessage(content='i said hi', additional_kwargs={}, example=False)]你也可以像以前一样在 LLMChain 中使用它。
from langchain.chains import LLMChain
from langchain_openai import ChatOpenAImodel = ChatOpenAI()chain = LLMChain(llm=model, prompt=new_prompt)chain.run("i said hi")'Oh, hello! How can I assist you today?'使用 PipelinePrompt
LangChain 包含一个抽象 PipelinePromptTemplate (opens in a new tab),当你想复用提示的某些部分时,它很有用。一个 PipelinePrompt 由两个主要部分组成:
- 最终提示(Final prompt):被返回的最终提示
- 管道提示(Pipeline prompts):一个元组列表,每个元组由一个字符串名称和一个提示模板组成。每个提示模板会被格式化,然后作为同名变量传给后续的提示模板。
from langchain_core.prompts.pipeline import PipelinePromptTemplate
from langchain_core.prompts.prompt import PromptTemplatefull_template = """{introduction}
{example}
{start}"""
full_prompt = PromptTemplate.from_template(full_template)introduction_template = """You are impersonating {person}."""
introduction_prompt = PromptTemplate.from_template(introduction_template)example_template = """Here's an example of an interaction:
Q: {example_q}
A: {example_a}"""
example_prompt = PromptTemplate.from_template(example_template)start_template = """Now, do this for real!
Q: {input}
A:"""
start_prompt = PromptTemplate.from_template(start_template)input_prompts = [
("introduction", introduction_prompt),
("example", example_prompt),
("start", start_prompt),
]
pipeline_prompt = PipelinePromptTemplate(
final_prompt=full_prompt, pipeline_prompts=input_prompts
)pipeline_prompt.input_variables['example_q', 'person', 'input', 'example_a']print(
pipeline_prompt.format(
person="Elon Musk",
example_q="What's your favorite car?",
example_a="Tesla",
input="What's your favorite social media site?",
)
)You are impersonating Elon Musk.
Here's an example of an interaction:
Q: What's your favorite car?
A: Tesla
Now, do this for real!
Q: What's your favorite social media site?
A: