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

做拍福利爱福利视频网站Wordpress主题 仿魅族

做拍福利爱福利视频网站,Wordpress主题 仿魅族,万网域名注册官网,网站建设电销话术开场白分类目录#xff1a;《自然语言处理从入门到应用》总目录 创建自定义提示模板 假设我们希望LLM根据函数名称生成该函数的英文语言解释。为了实现这个任务#xff0c;我们将创建一个自定义的提示模板#xff0c;以函数名称作为输入#xff0c;并格式化提示模板以提供函数的…分类目录《自然语言处理从入门到应用》总目录 创建自定义提示模板 假设我们希望LLM根据函数名称生成该函数的英文语言解释。为了实现这个任务我们将创建一个自定义的提示模板以函数名称作为输入并格式化提示模板以提供函数的源代码。LangChain提供了一组默认的提示模板可用于生成各种任务的提示。但是在某些情况下默认的提示模板可能无法满足我们的需求。例如我们可能希望创建一个具有特定动态指令的提示模板以适应我们的语言模型。在这种情况下我们可以创建自定义的提示模板。 有两种不同的提示模板 字符串提示模板提供一个简单的字符串格式提示聊天提示模板生成一个更结构化的聊天API使用的提示 在本文中我们将使用字符串提示模板创建一个自定义提示。要创建自定义字符串提示模板有两个要求 它具有input_variables属性用于公开提示模板期望的输入变量它公开一个format方法该方法接受与预期的input_variables相对应的关键字参数并返回格式化的提示 我们将创建一个自定义的提示模板它以函数名称作为输入并格式化提示以提供函数的源代码。为了实现这一点让我们首先创建一个函数该函数将根据函数名称返回函数的源代码。 import inspectdef get_source_code(function_name):# Get the source code of the functionreturn inspect.getsource(function_name)接下来我们将创建一个自定义的提示模板该模板以函数名称作为输入并格式化提示模板以提供函数的源代码 from langchain.prompts import StringPromptTemplate from pydantic import BaseModel, validatorclass FunctionExplainerPromptTemplate(StringPromptTemplate, BaseModel):一个自定义的提示模板接受函数名作为输入并格式化提示模板以提供函数的源代码。validator(input_variables)def validate_input_variables(cls, v):验证输入变量的正确性。if len(v) ! 1 or function_name not in v:raise ValueError(function_name必须是唯一的输入变量。)return vdef format(self, **kwargs) - str:# 获取函数的源代码source_code get_source_code(kwargs[function_name])# 生成要发送给语言模型的提示prompt f给定函数名和源代码生成一个关于函数的英文语言解释。函数名{kwargs[function_name].__name__}源代码{source_code}解释return promptdef _prompt_type(self):return function-explainer现在我们已经创建了一个自定义的提示模板我们可以使用它来生成我们任务的提示 fn_explainer FunctionExplainerPromptTemplate(input_variables[function_name])# 为函数get_source_code生成一个提示 prompt fn_explainer.format(function_nameget_source_code) print(prompt)输出 给定函数名和源代码生成一个关于函数的英文语言解释。 函数名get_source_code 源代码 def get_source_code(function_name):# Get the source code of the functionreturn inspect.getsource(function_name)解释创建含有Few-Shot示例的提示模板 在下文中我们将学习如何创建含有Few-Shot示例的提示模板。我们将使用FewShotPromptTemplate类来创建一个含有Few-Shot示例的提示模板。该类可以接受一组示例或者一个ExampleSelector对象。在下文中我们将分别为自我提问与搜索配置Few-Shot示例讨论这两种选项。 使用示例集 首先创建一个Few-Shot示例的列表。每个示例应该是一个字典其中键是输入变量值是这些输入变量的值。 from langchain.prompts.few_shot import FewShotPromptTemplate from langchain.prompts.prompt import PromptTemplateexamples [{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 } ]然后我们可以为Few Shot示例创建格式化程序。配置一个将Few Shot示例格式化为字符串的格式化程序。该格式化程序应该是一个PromptTemplate对象。 example_prompt PromptTemplate(input_variables[question, answer], templateQuestion: {question}\n{answer})print(example_prompt.format(**examples[0])) 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最后创建一个FewShotPromptTemplate对象。该对象接受Few Shot示例和Few Shot示例的格式化程序作为输入。 prompt FewShotPromptTemplate(examplesexamples, example_promptexample_prompt, suffixQuestion: {input}, input_variables[input] )print(prompt.format(inputWho was the father of Mary Ball Washington?))输出 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对象中我们将把它们输入到一个ExampleSelector对象中。在下文中我们将使用SemanticSimilarityExampleSelector类。该类根据示例与输入之间的相似度选择Few-Shot示例。它使用嵌入模型计算输入与Few-Shot示例之间的相似度并使用向量存储执行最近邻搜索。 from langchain.prompts.example_selector import SemanticSimilarityExampleSelector from langchain.vectorstores import Chroma from langchain.embeddings import OpenAIEmbeddingsexample_selector SemanticSimilarityExampleSelector.from_examples(# 这是可供选择的示例列表。examples,# 这是用于生成嵌入的嵌入类用于衡量语义相似度。OpenAIEmbeddings(),# 这是用于存储嵌入并进行相似度搜索的向量存储类。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})输出 Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient. Examples most similar to the input: Who was the father of Mary Ball Washington?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我们还可以将示例选择器应用于FewShotPromptTemplate。创建一个FewShotPromptTemplate对象。该对象接收示例选择器和用于Few-Shot示例的格式化程序 prompt FewShotPromptTemplate(example_selectorexample_selector, example_promptexample_prompt, suffixQuestion: {input}, input_variables[input] )print(prompt.format(inputWho was the father of Mary Ball Washington?))输出 Question: 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 Ball Question: Who was the father of Mary Ball Washington? 参考文献 [1] LangChain ️ 中文网跟着LangChain一起学LLM/GPT开发https://www.langchain.com.cn/ [2] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架http://www.cnlangchain.com/
http://www.w-s-a.com/news/503116/

相关文章:

  • 申请一个域名后怎么做网站evernote wordpress
  • 网站左侧导航栏设计网站开发后台数据怎么来
  • 临西做网站报价网站建设需要写语句吗
  • 建设网站网站首页购物网站开发代码
  • 淘宝客怎么建立网站网站360优化
  • 安徽建海建设工程有限公司网站网站空间和域名价格
  • 农产品网站建设策划哪里有做枪网站的
  • 更改各网站企业信息怎么做张家港企业网站制作
  • 郑州网站建设咨询银川做网站哪家好
  • 微信网站 微信支付合肥seo排名收费
  • 织梦做的网站如何上线广东省广州市番禺区南村镇
  • 网站设计的导航栏怎么做太原有网站工程公司吗
  • 苏州虎丘区建设局网站如何在一个数据库做两个网站
  • 淘宝天猫优惠券网站建设费用腾讯邮箱企业邮箱登录
  • 深圳福田做网站公司海航科技网站建设
  • 网站降权查询wordpress更换文章背景色
  • 大型电商网站开发金融企业网站建设公司
  • 成都营销型网站建设价格化妆品品牌推广方案
  • 深圳公司手机网站制作苏州网站推广哪家好
  • 网站建设开发方式包括购买学校网站建设费计入什么科目
  • 做简单网站的框架图中小微企业查询平台
  • 哪些网站可以免费做产品推广建设建设部网站
  • 网站开发销售怎么做django做网站
  • 淘宝客网站做百度竞价万网域名怎么绑定网站
  • 建设网站找哪个公司北京知名大公司有哪些
  • 专业彩票网站开发网站流量在哪设置
  • 网站建设对应的岗位榆林做网站公司
  • 网站建设公司怎么算专业js网站分页怎么做
  • 网和网站的区别phpcms和帝国cms哪个好
  • wordpress改网站名字长沙网络营销外包