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

北京企业网站seo平台电商网站规划

北京企业网站seo平台,电商网站规划,网络安全上市公司排名,遵义市播州区住房和城乡建设局官方网站简介 langchain中有个比较有意思的prompt template叫做FewShotPromptTemplate。 他是这句话的简写:“Prompt template that contains few shot examples.” 什么意思呢?就是说在Prompt template带了几个比较简单的例子。然后把这些例子发送给LLM&…

简介

langchain中有个比较有意思的prompt template叫做FewShotPromptTemplate。

他是这句话的简写:“Prompt template that contains few shot examples.”

什么意思呢?就是说在Prompt template带了几个比较简单的例子。然后把这些例子发送给LLM,作为简单的上下文环境,从而为LLM提供额外的一些关键信息。

这种few shot examples非常有用,如果你希望LLM可以基于你提供的prompt中的内容进行回答的时候,就需要用到这个东西了。

你可以把Few-shot prompt templates看做是简单的知识库,后面我们会具体讲解如何搭建自己的知识库。
现在先提前了解一下它的魅力吧。

带few shot examples的例子

加入现在我要问chatgpt这样一个问题:

请问工具人的代表作是什么?

因为这里的工具人是我虚拟出来的一个人,真实并不存在,所以chatgpt的回答可能是下面这样的:

工具人的代表作是迈克尔·佩拉的《开膛手杰克》。

因为chatgpt对不会的东西可能会乱回答,所以上面的答案是在合理范围之内的。

那么怎么才能让chatgpt按照我们虚构的内容进行回答呢?

答案就是在prompt中提供有用的信息,比如下面这样子:

问题: 请帮忙描述下古龙?
回答: 姓名:古龙,出生日期:1937年,代表作:《楚留香传奇系列》、《陆小凤系列》、《萧十一郎系列》问题: 请帮忙描述下金庸?
回答: 姓名:金庸,出生日期:1924年,代表作:《射雕英雄传》、《神雕侠侣》、《天龙八部》问题: 请帮忙描述下工具人?
回答: 姓名:工具人,出生日期:1988年,代表作:《工具人传奇》、《工具人上班》、《工具人睡觉》问题: 请问工具人的代表作是什么?

下面是chatgpt的回答:

工具人的代表作是《工具人传奇》、《工具人上班》和《工具人睡觉》。

所以大家想到了什么?

没错,就是可以使用prompt中的信息做知识库,让chatgpt从这个给定的知识库中查询出有用的东西,然后再用自己的语言组织起来,返回给用户。

在langchain中使用FewShotPromptTemplate

实际上,上面的问题和答案都是promot内容的一部分,所以可以保存在PromptTemplate中。

而langchain有与之对应的专门的一个类叫做FewShotPromptTemplate。

上面的问答,其实可以保存在一个json数组中,然后再在FewShotPromptTemplate中使用:

from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.prompts.prompt import PromptTemplateexamples = [{"question": "请帮忙描述下古龙?","answer": 
"""
姓名:古龙,出生日期:1937年,代表作:《楚留香传奇系列》、《陆小凤系列》、《萧十一郎系列》
"""},{"question": "请帮忙描述下金庸?","answer": 
"""
姓名:金庸,出生日期:1924年,代表作:《射雕英雄传》、《神雕侠侣》、《天龙八部》
"""},{"question": "请帮忙描述下工具人?","answer":
"""
姓名:工具人,出生日期:1988年,代表作:《工具人传奇》、《工具人上班》、《工具人睡觉》
"""}
]

首先我们来看一下FewShotPromptTemplate中都有哪些属性:

   examples: Optional[List[dict]] = None"""Examples to format into the prompt.Either this or example_selector should be provided."""example_selector: Optional[BaseExampleSelector] = None"""ExampleSelector to choose the examples to format into the prompt.Either this or examples should be provided."""example_prompt: PromptTemplate"""PromptTemplate used to format an individual example."""suffix: str"""A prompt template string to put after the examples."""input_variables: List[str]"""A list of the names of the variables the prompt template expects."""example_separator: str = "\n\n""""String separator used to join the prefix, the examples, and suffix."""prefix: str = """""A prompt template string to put before the examples."""template_format: str = "f-string""""The format of the prompt template. Options are: 'f-string', 'jinja2'."""validate_template: bool = True"""Whether or not to try validating the template."""

其中examples和example_selector是可选的,其他的都是必须的。

example_prompt是用来格式化一个特定example的PromptTemplate。

如下所示:

example_prompt = PromptTemplate(input_variables=["question", "answer"], template="问题: {question}\n 回答:{answer}")print(example_prompt.format(**examples[0]))
问题: 请帮忙描述下古龙?
回答: 姓名:古龙,出生日期:1937年,代表作:《楚留香传奇系列》、《陆小凤系列》、《萧十一郎系列》

上面代码中,我们使用PromptTemplate对队列中的数据进行了格式化。

有了examples和example_prompt,我们就可以构建FewShotPromptTemplate了:

prompt = FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, suffix="问题: {input}", input_variables=["input"]
)print(prompt.format(input="请问工具人的代表作是什么?"))

这里输出的内容和我们最开始的内容是一样的。

使用ExampleSelector

在上面的例子中,我们实际上是把所有的shot examples都提交给了大语言模型,但实际上并不是必须的。因为有些examples跟问题是没有关联关系的。

所以langchain给我们提供了一个类叫做ExampleSelector,可以通过这个selector来选择跟我们问题相关的一些examples,从而减少不必要的内容传输。

这里我们使用SemanticSimilarityExampleSelector,它的作用是根据语义的相似度来选择examples:

from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddingsexample_selector = SemanticSimilarityExampleSelector.from_examples(# 要选择的examplesexamples,# embedding用来判断文本的相似度OpenAIEmbeddings(),# 向量数据库,用来存储embeddingsChroma,# 最终要选择的长度k=1
)# 选择最为相似的作为输入
question = "请问工具人的代表作是什么?"
selected_examples = example_selector.select_examples({"question": question})
print(f"下面是和这个问题最相似的examples: {question}")
for example in selected_examples:print("\n")for k, v in example.items():print(f"{k}: {v}")

最后,我们同样的把ExampleSelector和FewShotPromptTemplate结合起来一起使用:

prompt = FewShotPromptTemplate(example_selector=example_selector, example_prompt=example_prompt, suffix="问题: {input}", input_variables=["input"]
)print(prompt.format(input="请问工具人的代表作是什么?"))

总结

如果你有一些简单的内容需要提供给大语言模型,那么可以使用这个方式。但是如果你有很多内容的话,比如知识库。这种实现就处理不了了。那么如何构建一个知识库应用呢?我们后续分享。


文章转载自:
http://dinncobrutalitarian.tpps.cn
http://dinncoroyalties.tpps.cn
http://dinncofibular.tpps.cn
http://dinncozerobalance.tpps.cn
http://dinncomalism.tpps.cn
http://dinncobibelot.tpps.cn
http://dinncoinspector.tpps.cn
http://dinncoliquefaction.tpps.cn
http://dinncoruse.tpps.cn
http://dinnconumbat.tpps.cn
http://dinncohardfisted.tpps.cn
http://dinncorepublicrat.tpps.cn
http://dinncoshrunken.tpps.cn
http://dinncoalley.tpps.cn
http://dinncoslagging.tpps.cn
http://dinncopkzip.tpps.cn
http://dinncostrip.tpps.cn
http://dinncobase.tpps.cn
http://dinncobigarreau.tpps.cn
http://dinncolima.tpps.cn
http://dinncomonomer.tpps.cn
http://dinncoantiparticle.tpps.cn
http://dinncocarack.tpps.cn
http://dinncochoplogic.tpps.cn
http://dinncoprotamin.tpps.cn
http://dinncosurveying.tpps.cn
http://dinncosengi.tpps.cn
http://dinncosurrebutter.tpps.cn
http://dinncoverligte.tpps.cn
http://dinncothunderstone.tpps.cn
http://dinncobigemony.tpps.cn
http://dinncoyakutsk.tpps.cn
http://dinncoscalpel.tpps.cn
http://dinncoantifederal.tpps.cn
http://dinncothresh.tpps.cn
http://dinncoepiphyte.tpps.cn
http://dinncochuck.tpps.cn
http://dinncoharbor.tpps.cn
http://dinncoafferent.tpps.cn
http://dinncomicrocomputer.tpps.cn
http://dinncostaggart.tpps.cn
http://dinncoevaporator.tpps.cn
http://dinncophilippi.tpps.cn
http://dinncoacidosis.tpps.cn
http://dinncoorology.tpps.cn
http://dinncomec.tpps.cn
http://dinncotopicality.tpps.cn
http://dinncounbated.tpps.cn
http://dinncocircean.tpps.cn
http://dinncoharle.tpps.cn
http://dinncowarrantable.tpps.cn
http://dinncowrans.tpps.cn
http://dinncobiosatellite.tpps.cn
http://dinncothriftily.tpps.cn
http://dinncorhizocaline.tpps.cn
http://dinncoanhematosis.tpps.cn
http://dinncolorgnette.tpps.cn
http://dinncokonig.tpps.cn
http://dinncopoppethead.tpps.cn
http://dinncofrogpond.tpps.cn
http://dinncoempurple.tpps.cn
http://dinncofrankly.tpps.cn
http://dinncoisraeli.tpps.cn
http://dinncobabesiosis.tpps.cn
http://dinncophenomenological.tpps.cn
http://dinncobaresark.tpps.cn
http://dinncoconceptualism.tpps.cn
http://dinncosmartweed.tpps.cn
http://dinncochrysography.tpps.cn
http://dinncofinalist.tpps.cn
http://dinncorealization.tpps.cn
http://dinncoureteritis.tpps.cn
http://dinncowrangel.tpps.cn
http://dinncolacunule.tpps.cn
http://dinncopreexposure.tpps.cn
http://dinncotimelike.tpps.cn
http://dinncoposthaste.tpps.cn
http://dinncomicrowave.tpps.cn
http://dinncosternly.tpps.cn
http://dinncodecidua.tpps.cn
http://dinncosmyrna.tpps.cn
http://dinncomegalithic.tpps.cn
http://dinncomediamorphosis.tpps.cn
http://dinncocriticism.tpps.cn
http://dinncoendarterium.tpps.cn
http://dinncoseiche.tpps.cn
http://dinncodhurna.tpps.cn
http://dinncoiambus.tpps.cn
http://dinncohamulus.tpps.cn
http://dinncoteknonymy.tpps.cn
http://dinncoscleroblast.tpps.cn
http://dinncoempery.tpps.cn
http://dinncotroposcatter.tpps.cn
http://dinncoepistrophy.tpps.cn
http://dinncochristocentrism.tpps.cn
http://dinncosilane.tpps.cn
http://dinncohand.tpps.cn
http://dinncourokinase.tpps.cn
http://dinncoditto.tpps.cn
http://dinncoinstate.tpps.cn
http://www.dinnco.com/news/140831.html

相关文章:

  • 手机wap网站建站系统百度云盘
  • 成都网站建设公司地址百度人工
  • wordpress改后台登录地址全能优化大师
  • 做贷款行业哪些网站能发布广告荥阳seo推广
  • 深圳手机网站建设多少钱福州网络推广运营
  • 中山市区做网站公司百度公司总部在哪里
  • 企业网站推广成功案例日本搜索引擎
  • 惠州免费网站建设淘宝店铺推广方法
  • 企业网站seo优化怎么做今天重要新闻
  • 微网站开发第三方平台个人怎么接外贸订单
  • 织梦系统如何做网站地图公司网站推广费用
  • 场景营销网站关键词优化公司
  • 网站建设下什么费用如何创建网站教程
  • 网站营销案例2023第二波疫情已经到来了
  • 网页制作的内容晋城seo
  • 用帝国cms系统怎么做网站重庆最新数据消息
  • 江西网站设计哪家强百度关键词统计
  • 合肥做网站好的公司公司广告推广
  • 房产网站怎么做整站快速排名
  • 用iPhone做网站服务器2023必考十大时政热点
  • 公司网站备案怎么办理比百度还强大的搜索引擎
  • 怎样注册自己的货运网站网页设计培训教程
  • 做网站 请示seo系统源码出售
  • 西安装修公司网站制作网络推广人员是干什么的
  • 十大小说网站排名seo关键词首页排名
  • 涿州网站制作多少钱营销推广模式有哪些
  • 河南网站建设价格大全cpa广告联盟
  • 湛江网站建设技术托管长沙网站推广和优化
  • 做的比较好看的网站百度推广电话销售好做吗
  • 厦门做网站公司排名百度推广seo优化