prompt_template = PromptTemplate.from_template("Tell me a joke") prompt_template
1
PromptTemplate(input_variables=[], template='Tell me a joke')
1
prompt_template.format()
'Tell me a joke'
在操作字符串提示时,各个模板将被连续地组合起来,不过列表中的首个元素需要是一个提示。
1 2 3 4 5 6 7 8
from langchain_core.prompts import PromptTemplate
prompt = ( PromptTemplate.from_template("Tell me a joke about {topic}") + ", make it funny" + "\n\nand in {language}" ) prompt
1
PromptTemplate(input_variables=['language', 'topic'], template='Tell me a joke about {topic}, make it funny\n\nand in {language}')
1
prompt.format(topic="sports", language="spanish")
'Tell me a joke about sports, make it funny\n\nand in spanish'
ChatPromptTemplate
ChatModels 的提示是一个包含多条聊天消息的列表。
每条聊天消息都关联一段内容,并有一个称作 role 的附加参数。
像这样创建一个 ChatPromptTemplate:
1 2 3 4 5 6 7 8 9 10 11 12 13
from langchain_core.prompts import ChatPromptTemplate
chat_template = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful AI bot. Your name is {name}."), ("human", "Hello, how are you doing?"), ("ai", "I'm doing well, thanks!"), ("human", "{user_input}"), ] )
messages = chat_template.format_messages(name="Bob", user_input="What is your name?") messages
1 2 3 4
[SystemMessage(content='You are a helpful AI bot. Your name is Bob.'), HumanMessage(content='Hello, how are you doing?'), AIMessage(content="I'm doing well, thanks!"), HumanMessage(content='What is your name?')]
from langchain_core.messages import SystemMessage from langchain_core.prompts import HumanMessagePromptTemplate
chat_template = ChatPromptTemplate.from_messages( [ SystemMessage( content=( "You are a helpful assistant that re-writes the user's text to " "sound more upbeat." ) ), HumanMessagePromptTemplate.from_template("{text}"), ] )
messages = chat_template.format_messages(text="I don't like eating tasty things") messages
1
[SystemMessage(content="You are a helpful assistant that re-writes the user's text to sound more upbeat."), HumanMessage(content="I don't like eating tasty things")]
1 2 3 4 5 6 7 8
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
prompt = SystemMessage(content="You are a nice pirate") new_prompt = ( prompt + HumanMessage(content="hi") + AIMessage(content="what?") + "{input}" ) messages = new_prompt.format_messages(input="i said hi") messages
1 2 3 4
[SystemMessage(content='You are a nice pirate'), HumanMessage(content='hi'), AIMessage(content='what?'), HumanMessage(content='i said hi')]
Message Prompts
LangChain 提供了不同种类的 MessagePromptTemplate。
其中,最常用的模板包括 AIMessagePromptTemplate、SystemMessagePromptTemplate 和 HumanMessagePromptTemplate,它们分别用于生成 AI 消息、系统消息和面向人类的消息。
来比较一下 PromptTemplate 和 Message 之间的区别:
1 2 3 4 5 6
from langchain_core.prompts import SystemMessagePromptTemplate from langchain_core.messages import SystemMessage
smpt = SystemMessagePromptTemplate.from_template("You are a helpful assistant, {etc}") sm = SystemMessage(content="You are a nice pirate") smpt, sm
1 2
(SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=['etc'], template='You are a helpful assistant, {etc}')), SystemMessage(content='You are a nice pirate'))
from langchain_core.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, )
human_prompt = "Summarize our conversation so far in {word_count} words." human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)
from langchain_core.messages import AIMessage, HumanMessage
human_message = HumanMessage(content="What is the best way to learn programming?") ai_message = AIMessage( content="""\ 1. Choose a programming language: Decide on a programming language that you want to learn. 2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures. 3. Practice, practice, practice: The best way to learn programming is through hands-on experience\ """ )
[HumanMessage(content='What is the best way to learn programming?'), AIMessage(content='1. Choose a programming language: Decide on a programming language that you want to learn.\n\n2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\n\n3. Practice, practice, practice: The best way to learn programming is through hands-on experience'), HumanMessage(content='Summarize our conversation so far in 10 words.')]
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)
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?", ) )
def_get_datetime(): now = datetime.now() return now.strftime("%m/%d/%Y, %H:%M:%S")
prompt = PromptTemplate( template="Tell me a {adjective} joke about the day {date}", input_variables=["adjective", "date"], ) partial_prompt = prompt.partial(date=_get_datetime) partial_prompt
1
PromptTemplate(input_variables=['adjective'], partial_variables={'date': <function _get_datetime at 0x7ac5c5b988b0>}, template='Tell me a {adjective} joke about the day {date}')
1
print(partial_prompt.format(adjective="funny"))
Tell me a funny joke about the day 05/07/2024, 07:24:11
也可以这么写:
1 2 3 4 5 6
prompt = PromptTemplate( template="Tell me a {adjective} joke about the day {date}", input_variables=["adjective"], partial_variables={"date": _get_datetime}, ) print(prompt.format(adjective="funny"))
Tell me a funny joke about the day 05/07/2024, 07:24:31