织梦开发网站,建站工具有什么用,手机网站seo免费软件,男孩子怎么做网站本教程将介绍如何使用 LangChain 库中的 PromptTemplate 和 FewShotPromptTemplate 来构建和运行提示#xff08;prompt#xff09;#xff0c;并通过示例数据展示其应用。
安装依赖
首先#xff0c;确保你已经安装了 langchain 和相关依赖#xff1a;
pip install lan…本教程将介绍如何使用 LangChain 库中的 PromptTemplate 和 FewShotPromptTemplate 来构建和运行提示prompt并通过示例数据展示其应用。
安装依赖
首先确保你已经安装了 langchain 和相关依赖
pip install langchain langchain_core langchain_chroma langchain_community1. 创建 PromptTemplate
PromptTemplate 用于定义一个模板该模板可以动态地插入变量。
代码示例
from langchain_core.prompts import PromptTemplateexample_prompt PromptTemplate.from_template(Question: {question}\n{answer})解释
PromptTemplate.from_template 方法用于从给定的模板字符串创建一个 PromptTemplate 对象。模板字符串中的 {question} 和 {answer} 是占位符将在后续被实际数据替换。
2. 定义示例数据
示例数据是一个列表每个元素是一个包含 question 和 answer 的字典。
代码示例
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
,},
]解释
每个字典包含一个问题和对应的答案。答案部分详细描述了求解问题的中间步骤和最终答案。
3. 使用 PromptTemplate 调用示例
代码示例
print(example_prompt.invoke(examples[0]).to_string())输出
Question: Who lived longer, Muhammad Ali or Alan Turing?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解释
example_prompt.invoke(examples[0]) 方法将示例数据中的第一个元素插入到模板中。to_string() 方法将结果转换为字符串形式。
4. 创建 FewShotPromptTemplate
FewShotPromptTemplate 用于结合多个示例和一个后缀模板来生成最终的提示。
代码示例
from langchain_core.prompts import FewShotPromptTemplateprompt FewShotPromptTemplate(examplesexamples,example_promptexample_prompt,suffixQuestion: {input},input_variables[input],
)print(prompt.invoke({input: Who was the father of Mary Ball Washington?}).to_string()
)输出
Question: Who lived longer, Muhammad Ali or Alan Turing?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 AliQuestion: When was the founder of craigslist born?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, 1952Question: Who was the maternal grandfather of George Washington?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 BallQuestion: Are both the directors of Jaws and Casino Royale from the same country?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: NoQuestion: Who was the father of Mary Ball Washington?解释
FewShotPromptTemplate 结合了多个示例和一个后缀模板。examples 参数是之前定义的示例数据。example_prompt 是之前创建的 PromptTemplate。suffix 是一个后缀模板用于添加到所有示例之后。input_variables 定义了后缀模板中使用的变量。
5. 使用语义相似度选择示例
SemanticSimilarityExampleSelector 用于根据输入问题的语义相似度选择最相关的示例。
代码示例
from langchain_chroma import Chroma
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_community.embeddings import ZhipuAIEmbeddingsembed ZhipuAIEmbeddings(modelembedding-3,api_keyyour api key,
)example_selector SemanticSimilarityExampleSelector.from_examples(examples,embed,Chroma,k1,
)question Who was the father of Mary Ball Washington?
selected_examples example_selector.select_examples({question: question})
print(fExamples most similar to the input: {question})
for example in selected_examples:print(\n)for k, v in example.items():print(f{k}: {v})输出
Examples most similar to the input: Who was the father of Mary Ball 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 Ballquestion: Who was the maternal grandfather of George Washington?解释
ZhipuAIEmbeddings 用于生成问题的嵌入向量。SemanticSimilarityExampleSelector.from_examples 创建一个示例选择器用于根据语义相似度选择示例。k1 表示选择最相似的一个示例。select_examples 方法根据输入问题选择最相似的示例。
总结
本教程展示了如何使用 LangChain 中的 PromptTemplate 和 FewShotPromptTemplate 来构建和运行提示并通过语义相似度选择最相关的示例。这些工具在构建复杂的提示和生成高质量回答方面非常有用。希望这个教程对你有所帮助
参考链接https://python.langchain.com/v0.2/docs/how_to/few_shot_examples/
希望这个教程对你有所帮助如果有任何问题欢迎随时提问。 文章转载自: http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.rzcfg.cn.gov.cn.rzcfg.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.mgbcf.cn.gov.cn.mgbcf.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.lthtp.cn.gov.cn.lthtp.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.drnfc.cn.gov.cn.drnfc.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.rzjfn.cn.gov.cn.rzjfn.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.webpapua.com.gov.cn.webpapua.com http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.lsxabc.com.gov.cn.lsxabc.com http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.bqqzg.cn.gov.cn.bqqzg.cn http://www.morning.qbkw.cn.gov.cn.qbkw.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.jnhhc.cn.gov.cn.jnhhc.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.jnzfs.cn.gov.cn.jnzfs.cn http://www.morning.snygg.cn.gov.cn.snygg.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.mjpgl.cn.gov.cn.mjpgl.cn http://www.morning.yqgny.cn.gov.cn.yqgny.cn http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.bfcrp.cn.gov.cn.bfcrp.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.qgjwx.cn.gov.cn.qgjwx.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.kltsn.cn.gov.cn.kltsn.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.dansj.com.gov.cn.dansj.com http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn http://www.morning.dztp.cn.gov.cn.dztp.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn