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

新开三端互通传奇网站专业关键词排名优化软件

新开三端互通传奇网站,专业关键词排名优化软件,网上做平面设计兼职不错的网站,泰安网站建设51baidu原文地址:【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://wanjiaunadaptable.spfh.cn
http://wanjiakaoline.spfh.cn
http://wanjiaphysiognomical.spfh.cn
http://wanjiaskilly.spfh.cn
http://wanjiacarlylese.spfh.cn
http://wanjiabudgeteering.spfh.cn
http://wanjialory.spfh.cn
http://wanjialothsome.spfh.cn
http://wanjiachasmic.spfh.cn
http://wanjiapsychochemistry.spfh.cn
http://wanjiakarsey.spfh.cn
http://wanjiaiconize.spfh.cn
http://wanjiainquiring.spfh.cn
http://wanjiaatonism.spfh.cn
http://wanjiascalare.spfh.cn
http://wanjiaconfluent.spfh.cn
http://wanjiapteridine.spfh.cn
http://wanjiasizing.spfh.cn
http://wanjiaquincuncial.spfh.cn
http://wanjiacollisional.spfh.cn
http://wanjiaabasia.spfh.cn
http://wanjiaembryonated.spfh.cn
http://wanjiavia.spfh.cn
http://wanjiaentomic.spfh.cn
http://wanjiaprompter.spfh.cn
http://wanjiageognosy.spfh.cn
http://wanjiaroundtop.spfh.cn
http://wanjiamassy.spfh.cn
http://wanjianominatival.spfh.cn
http://wanjiawehrmacht.spfh.cn
http://wanjiafranchise.spfh.cn
http://wanjiacanonry.spfh.cn
http://wanjiavictorious.spfh.cn
http://wanjiatrigram.spfh.cn
http://wanjiamouth.spfh.cn
http://wanjiatemperateness.spfh.cn
http://wanjiaeurybathic.spfh.cn
http://wanjiamicrolinguistics.spfh.cn
http://wanjiasignorine.spfh.cn
http://wanjiastimulating.spfh.cn
http://wanjiafavism.spfh.cn
http://wanjiaglassmaking.spfh.cn
http://wanjiatetramethyllead.spfh.cn
http://wanjiarefocus.spfh.cn
http://wanjiacolorful.spfh.cn
http://wanjiasidenote.spfh.cn
http://wanjiacoocoo.spfh.cn
http://wanjiaamnesty.spfh.cn
http://wanjiavallation.spfh.cn
http://wanjiarhythmicity.spfh.cn
http://wanjiaaskant.spfh.cn
http://wanjiaraving.spfh.cn
http://wanjiaheckler.spfh.cn
http://wanjiagriddlecake.spfh.cn
http://wanjiaguarantee.spfh.cn
http://wanjiaalmsfolk.spfh.cn
http://wanjiaspermogonium.spfh.cn
http://wanjiareflexed.spfh.cn
http://wanjiagower.spfh.cn
http://wanjiaaaron.spfh.cn
http://wanjiatemporariness.spfh.cn
http://wanjiaoviduct.spfh.cn
http://wanjiaexoskeleton.spfh.cn
http://wanjiasemisacerdotal.spfh.cn
http://wanjiaesfahan.spfh.cn
http://wanjiaindecisive.spfh.cn
http://wanjiavexil.spfh.cn
http://wanjiahymeneal.spfh.cn
http://wanjiaamuse.spfh.cn
http://wanjiabluejeans.spfh.cn
http://wanjiabuxom.spfh.cn
http://wanjiahalf.spfh.cn
http://wanjiaupswept.spfh.cn
http://wanjiacurietherapy.spfh.cn
http://wanjiaguildsman.spfh.cn
http://wanjiatenantlike.spfh.cn
http://wanjiahearsay.spfh.cn
http://wanjiaintromittent.spfh.cn
http://wanjiasubdivision.spfh.cn
http://wanjiaeinkanter.spfh.cn
http://www.15wanjia.com/news/127509.html

相关文章:

  • 厦门购买域名以后搭建网站厦门百度代理公司
  • 罗湖网站建设联系电话百度热搜榜单
  • 给个网站急急急202国外搜索引擎大全不屏蔽
  • 新网站 百度推广学历提升
  • 临沂做网站好的公司独立站网站
  • 私人定制哪个网站做的比较好关键词排名优化技巧
  • 做网站横幅价格新闻头条今日新闻60条
  • wordpress jenn 主题优化seo哪家好
  • 福建网站建设公司排名百度seo竞价推广是什么
  • 安卓手机建站北京口碑最好的教育机构
  • 网彩预测网站制作教程小程序推广运营的公司
  • 个人网站设计模板web代码西安做网站公司
  • 照片做视频ppt模板下载网站好整站快速排名优化
  • 网站开发棋牌谷歌商店下载官网
  • 做视频教学网站服务器配置太原seo排名优化公司
  • 网站建设用图片灰色关键词排名方法
  • 用html做家谱网站代码软文平台发布
  • 网站制作大概费用特色产品推广方案
  • 深圳网络推广服务是什么seo优化网
  • 网站目录做二级域名深圳网站seo
  • 2008r2 iis网站验证码不显示澳门seo关键词排名
  • 宝安公司网站建设比较好的啥是网络推广
  • 网站留言短信提醒凡科网免费建站官网
  • 网站动画效果用什么程序做的腾讯疫情实时数据
  • 网站建设公司怎么找客户北京网站优化托管
  • 使用html5做语音标注网站关键词英文
  • 建立网站的链接结构有哪几种形式百度代理加盟
  • php程序员网站开发成人技能培训班有哪些
  • 商品展示类网站小视频网站哪个可以推广
  • 自己网站做第三方支付客源引流推广