# system message template = "You are a helpful assistant that translates {input_language} to {output_language}." system_message_prompt = SystemMessagePromptTemplate.from_template(template)
# human message human_template = "{text}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
# format result = chat_prompt.format(input_language="English", output_language="French", text="I love programming.")
# 或者 format_prompt 后 to_string result = chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_string()
# >> System: You are a helpful assistant that translates English to French.\nHuman: I love programming.
消息列表
1 2 3 4 5 6 7
# format_messages result = chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")
# format_prompt 后 to_messages result = chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages()
# >> [SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}),\nHumanMessage(content='I love programming.', additional_kwargs={})]
ChatPromptValue
1 2 3
result = chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.")
# >> ChatPromptValue(messages=[SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), HumanMessage(content='I love programming.', additional_kwargs={})])
examples = [ { "question": "Who lived longer, Muhammad Ali or Alan Turing?", "answer": """ Are follow up questions needed here: Yes. Follow up: How old was Muhammad Ali when he died? Intermediate answer: Muhammad Ali was 74 years old when he died. Follow up: How old was Alan Turing when he died? Intermediate answer: Alan Turing was 41 years old when he died. So the final answer is: Muhammad Ali """ }, { "question": "When was the founder of craigslist born?", "answer": """ Are follow up questions needed here: Yes. Follow up: Who was the founder of craigslist? Intermediate answer: Craigslist was founded by Craig Newmark. Follow up: When was Craig Newmark born? Intermediate answer: Craig Newmark was born on December 6, 1952. So the final answer is: December 6, 1952 """ }, { "question": "Who was the maternal grandfather of George Washington?", "answer": """ Are follow up questions needed here: Yes. Follow up: Who was the mother of George Washington? Intermediate answer: The mother of George Washington was Mary Ball Washington. Follow up: Who was the father of Mary Ball Washington? Intermediate answer: The father of Mary Ball Washington was Joseph Ball. So the final answer is: Joseph Ball """ }, { "question": "Are both the directors of Jaws and Casino Royale from the same country?", "answer": """ Are follow up questions needed here: Yes. Follow up: Who is the director of Jaws? Intermediate Answer: The director of Jaws is Steven Spielberg. Follow up: Where is Steven Spielberg from? Intermediate Answer: The United States. Follow up: Who is the director of Casino Royale? Intermediate Answer: The director of Casino Royale is Martin Campbell. Follow up: Where is Martin Campbell from? Intermediate Answer: New Zealand. So the final answer is: No """ } ]
Question: Who lived longer, Muhammad Ali or Alan Turing? Answer: Are follow up questions needed here: Yes. Follow up: How old was Muhammad Ali when he died? Intermediate answer: Muhammad Ali was 74 years oldwhen he died. Follow up: How old was Alan Turing when he died? Intermediate answer: Alan Turing was 41 years oldwhen he died. So the final answer is: Muhammad Ali
Question:Wholivedlonger,MuhammadAliorAlanTuring? Answer: Are follow up questions needed here:Yes. Follow up:HowoldwasMuhammadAliwhenhedied? Intermediate answer:MuhammadAliwas74yearsoldwhenhedied. Follow up:HowoldwasAlanTuringwhenhedied? Intermediate answer:AlanTuringwas41yearsoldwhenhedied. So the final answer is:MuhammadAli
Question:Whenwasthefounderofcraigslistborn? Answer: Are follow up questions needed here:Yes. Follow up:Whowasthefounderofcraigslist? Intermediate answer:CraigslistwasfoundedbyCraigNewmark. Follow up:WhenwasCraigNewmarkborn? Intermediate answer:CraigNewmarkwasbornonDecember6,1952. So the final answer is:December6,1952
Question:WhowasthematernalgrandfatherofGeorgeWashington? Answer: Are follow up questions needed here:Yes. Follow up:WhowasthemotherofGeorgeWashington? Intermediate answer:ThemotherofGeorgeWashingtonwasMaryBallWashington. Follow up:WhowasthefatherofMaryBallWashington? Intermediate answer:ThefatherofMaryBallWashingtonwasJosephBall. So the final answer is:JosephBall
Question:AreboththedirectorsofJawsandCasinoRoyalefromthesamecountry? Answer: Are follow up questions needed here:Yes. Follow up:WhoisthedirectorofJaws? Intermediate Answer:ThedirectorofJawsisStevenSpielberg. Follow up:WhereisStevenSpielbergfrom? Intermediate Answer:TheUnitedStates. Follow up:WhoisthedirectorofCasinoRoyale? Intermediate Answer:ThedirectorofCasinoRoyaleisMartinCampbell. Follow up:WhereisMartinCampbellfrom? Intermediate Answer:NewZealand. So the final answer is:No
from langchain.prompts import StringPromptTemplate from pydantic import v1 as pydantic_v1 import inspect
classFunctionExplainerPromptTemplate(StringPromptTemplate, BaseModel): """A custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function."""
# input_variables @pydantic_v1.validator("input_variables") defvalidate_input_variables(cls, v): """Validate that the input variables are correct.""" iflen(v) != 1or"function_name"notin v: raise ValueError("function_name must be the only input_variable.") return v
# format defformat(self, **kwargs) -> str: # Get the source code of the function source_code = get_source_code(kwargs["function_name"])
# Generate the prompt to be sent to the language model prompt = f""" 给定函数名称和源码,生成函数的英文解释 函数名称: {kwargs["function_name"].__name__} 源码: {source_code} 解释: """ return prompt
def_prompt_type(self): return"function-explainer"
# 获取源码字符串 defget_source_code(function_name): # Get the source code of the function return inspect.getsource(function_name)
使用
1 2 3 4
fn_explainer = FunctionExplainerPromptTemplate(input_variables=["function_name"]) # Generate a prompt for the function "get_source_code" prompt = fn_explainer.format(function_name=get_source_code) print(prompt)
需要注意这里官方文档给的示例可能存在问题,报错信息
1 2
class FunctionExplainerPromptTemplate(StringPromptTemplate, BaseModel): TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses ofall its bases
和 pydantic 库的版本有关,v2
版本下官方代码会存在不兼容问题,上面代码块使用
from pydantic import v1 as pydantic_v1 来指定了 v1 版本