Skip to main content

Posts

聊天模型中的少量镜头示例

 聊天模型中的少量镜头示例 本笔记本介绍了如何在聊天模型中使用少量镜头示例。 关于如何最好地进行少量镜头提示,目前似乎还没有达成一致意见。因此,我们还没有围绕这个问题建立任何抽象概念,而是使用现有的抽象概念。 人类/人工智能信息交替 第一种进行少量镜头提示的方法是交替使用人类/人工智能信息。请看下面的示例。 from langchain.chat_models import ChatOpenAI from langchain import PromptTemplate, LLMChain from langchain.prompts.chat import (     ChatPromptTemplate,     SystemMessagePromptTemplate,     AIMessagePromptTemplate,     HumanMessagePromptTemplate, ) from langchain.schema import AIMessage, HumanMessage, SystemMessage chat = ChatOpenAI(temperature=0) template = "You are a helpful assistant that translates english to pirate." system_message_prompt = SystemMessagePromptTemplate.from_template(template) example_human = HumanMessagePromptTemplate.from_template("Hi") example_ai = AIMessagePromptTemplate.from_template("Argh me mateys") human_template = "{text}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages(    ...
Recent posts

少量镜头提示模板

 少量镜头提示模板 在本教程中,我们将学习如何创建使用少量示例的提示模板。少量镜头提示模板可以从一组示例或示例选择器对象中创建。 使用实例 在本教程中,我们将为带有搜索功能的自问自答配置少量镜头示例。 使用示例集 创建示例集 要开始使用,请创建一个包含少量示例的列表。每个示例都应是一个字典,键是输入变量,值是这些输入变量的值。 from langchain.prompts.few_shot import FewShotPromptTemplate from langchain.prompts.prompt import PromptTemplate 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: Craigsl...

自定义提示模板

 自定义提示模板 假设我们希望 LLM 能根据函数名称生成该函数的英文解释。为完成这一任务,我们将创建一个自定义提示模板,将函数名称作为输入,并格式化提示模板,以提供函数的源代码。 为什么需要自定义提示模板? LangChain 提供了一组默认提示模板,可用于为各种任务生成提示。但是,在某些情况下,默认提示模板可能无法满足您的需求。例如,您可能希望为自己的语言模型创建一个带有特定动态指令的提示模板。在这种情况下,您可以创建自定义提示模板。 在此查看当前的默认提示模板集。 创建自定义提示模板 基本上有两种不同的提示模板--字符串提示模板和聊天提示模板。字符串提示模板提供了字符串格式的简单提示,而聊天提示模板则生成了更有条理的提示,可与聊天 API 配合使用。 在本指南中,我们将使用字符串提示模板创建自定义提示。 要创建自定义字符串提示模板,有两个要求: 它有一个 input_variables 属性,用于公开提示模板期望使用的输入变量。 它有一个 format 方法,该方法接收与预期输入变量相对应的关键字参数,并返回格式化后的提示信息。 我们将创建一个自定义提示模板,将函数名称作为输入,并格式化提示,以提供函数的源代码。为此,我们首先创建一个函数,该函数将返回函数名的源代码。 import inspect def get_source_code(function_name):     # Get the source code of the function     return inspect.getsource(function_name) 接下来,我们将创建一个自定义提示模板,将函数名称作为输入,并格式化提示模板,以提供函数的源代码。 from langchain.prompts import StringPromptTemplate from pydantic import BaseModel, validator PROMPT = """\ Given the function name and source code, generate an English language explanation of the function. Function Name: {function_name} Source ...

连接到特征库

 连接到特征库 特征库是传统机器学习中的一个概念,可确保输入模型的数据是最新且相关的。有关这方面的更多信息,请参阅此处。 在考虑将 LLM 应用程序投入生产时,这一概念极为重要。为了个性化 LLM 应用程序,您可能希望将 LLM 与特定用户的最新信息结合起来。特征库是保持数据新鲜度的绝佳方式,而 LangChain 则提供了将这些数据与 LLM 结合起来的简便方法。 在本笔记本中,我们将展示如何将提示模板连接到特征库。其基本思想是在提示模板中调用特征库来检索值,然后将这些值格式化到提示中。 盛宴 首先,我们将使用流行的开源特征库框架 Feast。 我们假设你已经执行了 README 中的入门步骤。我们将以入门中的示例为基础,创建 LLMChain,向特定驱动程序写入有关其最新统计数据的说明。 加载盛宴存储 同样,应根据 Feast README 中的说明进行设置 from feast import FeatureStore # You may need to update the path depending on where you stored it feast_repo_path = "../../../../../my_feature_repo/feature_repo/" store = FeatureStore(repo_path=feast_repo_path) 提示 在这里,我们将设置一个自定义的 FeastPromptTemplate。该提示模板将输入一个驾驶员 ID,查找他们的统计数据,并将这些统计数据格式化为一个提示。 请注意,该提示模板的输入只有 driver_id,因为这是唯一一个用户定义的变量(所有其他变量都在提示模板内查找)。 from langchain.prompts import PromptTemplate, StringPromptTemplate template = """Given the driver's up to date stats, write them note relaying those stats to them. If they have a conversation rate above .5, give them a compliment....

模型输入/输出

模型输入/输出 任何语言模型应用程序的核心要素都是......模型。LangChain 为您提供了与任何语言模型接口的构建模块。 提示: 模板化、动态选择和管理模型输入 语言模型: 通过通用接口调用语言模型 输出解析器: 从模型输出中提取信息 提示 对模型进行编程的新方法是通过提示。提示指的是对模型的输入。这种输入通常由多个组件构成。LangChain 提供了多个类和函数,使提示的构建和使用变得简单。 提示模板: 参数化模型输入 示例选择器: 动态选择要包含在提示中的示例 提示模板 语言模型将文本作为输入--这些文本通常被称为提示。通常,提示不是简单的硬编码字符串,而是模板、一些示例和用户输入的组合。LangChain 提供了多个类和函数,可以轻松构建和使用提示。 什么是提示模板? 提示模板指的是生成提示的可重现方式。它包含一个文本字符串("模板"),可接收终端用户的一组参数并生成提示。 提示模板可以包含 对语言模型的说明、 帮助语言模型生成更好回复的一些示例、 向语言模型提出的问题。 下面是一个最简单的例子: from langchain import PromptTemplate template = """\ You are a naming consultant for new companies. What is a good name for a company that makes {product}? """ prompt = PromptTemplate.from_template(template) prompt.format(product="colorful socks") You are a naming consultant for new companies. What is a good name for a company that makes colorful socks? 创建提示模板 您可以使用 PromptTemplate 类创建简单的硬编码提示。提示模板可接受任意数量的输入变量,并可按格式生成提示。 from langchain import PromptTemplate # An example prompt with n...

模块

 模块 LangChain 为下列模块提供了标准的、可扩展的接口和外部集成,这些模块的复杂程度从低到高依次排列: 模型输入/输出 语言模型接口 数据连接 与应用程序特定数据的接口 链 构建调用序列 代理 让链根据高级指令选择要使用的工具 内存 在链运行之间保持应用程序状态 回调 记录任何链的中间步骤并进行流式处理 评估 评估链的性能

CentOS 安装 nginx

 sudo yum install epel-release sudo yum install nginx sudo systemctl start nginx sudo systemctl status nginx Output ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2022-01-24 20:14:24 UTC; 5s ago Process: 1898 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 1896 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 1895 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 1900 (nginx) CGroup: /system.slice/nginx.service ├─1900 nginx: master process /usr/sbin/nginx └─1901 nginx: worker process Jan 24 20:14:24 centos-updates systemd[1]: Starting The nginx HTTP and reverse proxy server... Jan 24 20:14:24 centos-updates nginx[1896]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Jan 24 20:14:24 centos-updates nginx[1896]: nginx...