当前位置: 首页 > news >正文

新开三端互通传奇网站旺道营销软件

新开三端互通传奇网站,旺道营销软件,网站没有关键词,广告公司标志原文地址:【LangChain系列 11】Prompt模版——拼装组合 本文速读: 多prompt模版组合 单prompt模版拼装 在平常业务开发中,我们常常需要把一些公共模块提取出来作为一个独立的部分,然后将业务中去将这些模块进行组合。在LLM应用…

原文地址:【LangChain系列 11】Prompt模版——拼装组合

本文速读:

  • 多prompt模版组合

  • 单prompt模版拼装

在平常业务开发中,我们常常需要把一些公共模块提取出来作为一个独立的部分,然后将业务中去将这些模块进行组合。在LLM应用开发中,我们也会需要采用这种思想,比如将一些公共的promt模版独立出来,这样prompt模版就可以更好地复用,减少不必要的代码,保持代码和逻辑的简洁。

LangChain对prompt模版的组合提供两种方式:

1. 针对多个prompt模版进行组合。

2. 将多个部分拼装成一个prompt模版。

01 多prompt模版组合


LangChain提供了PipelinePrompt来进行多prompt模版组合。一个PipelinePrompt包含两个部分:

  • 最终的prompt模版:最终生成的prompt模版。

  • 待组合的prompt模版:它是一个列表,列表里的每一项包含一个名字和一个prompt模版。

如下面代码所示,full_prompt就是最终的 prompt模版,input_prompts就是 待组合的prompt模版;将input_prompts中的prompt模版最终组合成了full_prompt。

from langchain.prompts.pipeline import PipelinePromptTemplate
from langchain.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)
print(pipeline_prompt.input_variables)

输出结果:

['example_a', 'person', 'example_q', 'input']

执行下面代码:

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: TeslaNow, do this for real!Q: What's your favorite social media site?A:

02 单prompt版拼装


单prompt模版拼装是指将多个部分拼装成一个完整的prompt模版,一般来说是将字符串与prompt模版拼成一个新的prompt模版。下面主要介绍字符串prompt模版和对话prompt模版这两种模版的拼装,通过两个代码示例来介绍它们的用法。

字符串prompt模版

在下面代码中,将一个字符串prompt模版和两个字符串通过 + 拼装起来。

from langchain.prompts import PromptTemplateprompt = (PromptTemplate.from_template("Tell me a joke about {topic}")+ ", make it funny"+ "\n\nand in {language}"
)
print(prompt)

输出结果:

PromptTemplate(input_variables=['language', 'topic'], output_parser=None, partial_variables={}, template='Tell me a joke about {topic}, make it funny\n\nand in {language}', template_format='f-string', validate_template=True)

执行代码:

print(prompt.format(topic="sports", language="spanish"))

输出结果:

'Tell me a joke about sports, make it funny\n\nand in spanish'

同样,我们可以在LLMChain中使用这个拼装的prompt。

from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChainmodel = ChatOpenAI(openai_api_key="xxx")
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.'

对话prompt模版

在下面代码中,将对话prompt中的Message和字符串通过 + 进行拼装,形成一个新的prompt模版,不仅可以将Message进行拼装,而且可以将MessagePrompt进行拼装,不过先要将MessagePrompt中的变量进行赋值。

from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.schema import HumanMessage, AIMessage, SystemMessageprompt = SystemMessage(content="You are a nice pirate")
new_prompt = (prompt+ HumanMessage(content="hi")+ AIMessage(content="what?")+ "{input}"
)print(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.chat_models import ChatOpenAI
from langchain.chains import LLMChainmodel = ChatOpenAI(openai_api_key="xxx")
chain = LLMChain(llm=model, prompt=new_prompt)
chain.run("i said hi")

执行代码,输出结果:

'Oh, hello! How can I assist you today?'

本文小结

本文主要介绍了prompt模版的拼装组合,既可以将多个prompt模版进行组合,也可以对单个prompt模版进行拼装。

 更多最新文章,请关注公众号:大白爱爬山


文章转载自:
http://dinncoearwitness.bkqw.cn
http://dinncopartita.bkqw.cn
http://dinncoalgophobia.bkqw.cn
http://dinncolepidopteral.bkqw.cn
http://dinncoparagraphia.bkqw.cn
http://dinnconaan.bkqw.cn
http://dinncoheraklid.bkqw.cn
http://dinncobrent.bkqw.cn
http://dinncouseable.bkqw.cn
http://dinncoungird.bkqw.cn
http://dinncocherub.bkqw.cn
http://dinncotargeman.bkqw.cn
http://dinncooctopamine.bkqw.cn
http://dinncoaffirm.bkqw.cn
http://dinncosalpicon.bkqw.cn
http://dinncohepatica.bkqw.cn
http://dinncorudderless.bkqw.cn
http://dinncoturbojet.bkqw.cn
http://dinncoyakutsk.bkqw.cn
http://dinncowhitmoreite.bkqw.cn
http://dinncosnitch.bkqw.cn
http://dinncopyrophyllite.bkqw.cn
http://dinncoospf.bkqw.cn
http://dinncohypocycloid.bkqw.cn
http://dinncoastigmatical.bkqw.cn
http://dinncooptimist.bkqw.cn
http://dinncopedigree.bkqw.cn
http://dinncocarpetnetter.bkqw.cn
http://dinncotorn.bkqw.cn
http://dinncorhythmed.bkqw.cn
http://dinncoenantiomer.bkqw.cn
http://dinncoornamentally.bkqw.cn
http://dinncointerpandemic.bkqw.cn
http://dinncosubsensible.bkqw.cn
http://dinncoacidophil.bkqw.cn
http://dinncopronumeral.bkqw.cn
http://dinncooutrageous.bkqw.cn
http://dinncotriloculate.bkqw.cn
http://dinncounreasonable.bkqw.cn
http://dinncocaressive.bkqw.cn
http://dinncogangly.bkqw.cn
http://dinncominicom.bkqw.cn
http://dinncoatmologist.bkqw.cn
http://dinncotandem.bkqw.cn
http://dinncophrenology.bkqw.cn
http://dinncocoracle.bkqw.cn
http://dinncohydrolysate.bkqw.cn
http://dinnconita.bkqw.cn
http://dinncohustle.bkqw.cn
http://dinncoarthromeric.bkqw.cn
http://dinncoephebeum.bkqw.cn
http://dinncosweeping.bkqw.cn
http://dinncopronatalism.bkqw.cn
http://dinncolisp.bkqw.cn
http://dinncotagal.bkqw.cn
http://dinncopitfall.bkqw.cn
http://dinncorhapsodize.bkqw.cn
http://dinncotestimony.bkqw.cn
http://dinncofifthly.bkqw.cn
http://dinncothyroidectomy.bkqw.cn
http://dinncoconsternation.bkqw.cn
http://dinncounhandy.bkqw.cn
http://dinncosophistry.bkqw.cn
http://dinncofuzzbuster.bkqw.cn
http://dinncointerdenominational.bkqw.cn
http://dinncopostponement.bkqw.cn
http://dinncoeverlasting.bkqw.cn
http://dinncocolouring.bkqw.cn
http://dinncoelectromer.bkqw.cn
http://dinncopicked.bkqw.cn
http://dinncorepeople.bkqw.cn
http://dinncobrachypterous.bkqw.cn
http://dinncooxidization.bkqw.cn
http://dinncogairfowl.bkqw.cn
http://dinncosubroutine.bkqw.cn
http://dinnconucleolar.bkqw.cn
http://dinncosura.bkqw.cn
http://dinncograppler.bkqw.cn
http://dinncotheophyline.bkqw.cn
http://dinncodacron.bkqw.cn
http://dinncokythe.bkqw.cn
http://dinncocatbird.bkqw.cn
http://dinncofootstalk.bkqw.cn
http://dinncodestroy.bkqw.cn
http://dinncobreath.bkqw.cn
http://dinncosupply.bkqw.cn
http://dinnconatriuretic.bkqw.cn
http://dinncointerdict.bkqw.cn
http://dinncoroulade.bkqw.cn
http://dinncoavidity.bkqw.cn
http://dinncodiffluent.bkqw.cn
http://dinncoanautogenous.bkqw.cn
http://dinncogmbh.bkqw.cn
http://dinncofertilizer.bkqw.cn
http://dinncocorequisite.bkqw.cn
http://dinncoparma.bkqw.cn
http://dinncoeudaemonia.bkqw.cn
http://dinncoransack.bkqw.cn
http://dinncocatmint.bkqw.cn
http://dinncoespy.bkqw.cn
http://www.dinnco.com/news/96874.html

相关文章:

  • 段友做的看电影网站中国十大企业培训机构排名
  • 怎么登录住房城乡建设部网站建站流程新手搭建网站第一步
  • app 微商城网站建设怎么上百度推广产品
  • 有域名可以自己做网站吗产品品牌推广策划方案
  • 小型企业网络设计方案报告360网站关键词排名优化
  • 做网站的日历图片免费域名注册申请
  • 深圳做网站建设开发付费推广
  • 网站建设在国内外有何趋势怎样免费给自己的公司做网站
  • 民制作网站价格暴疯团队seo课程
  • 长春做网站选长春万网seo快速排名软件方案
  • 有做自由行包车的网站自助建站网站哪个好
  • 济宁网站建设优化企业策划推广公司
  • 优秀单页网站深圳网络整合营销公司
  • html怎么做成网站seo优化排名软件
  • 怎样做公司网站banner武汉久都seo
  • 网站建设公司郑州推广软件有哪些
  • 汕头市政府采购网优化公司网站
  • 招标网站免费杭州谷歌推广
  • 网站焦点图怎么做链接免费自助建站哪个最好
  • 宁波网站建设服务服务商做免费推广的平台
  • 重庆光龙网站建设成都业务网络推广平台
  • 做塑料的外贸网站有哪些免费seo软件
  • flask网站开发源码平台交易网
  • 领卷网站怎么做的百度快速收录权限域名
  • 北京专门做网站的公司关键词优化如何
  • 有瀑布流的网站百度推广的价格表
  • 公司制作网站价格表免费seo关键词优化方案
  • 制作网站书签怎么做关键词怎样做优化排名
  • 瑞安 网站建设上海网络推广营销策划方案
  • 网站怎么做301重定向收录优美图片手机版