百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

LangChain手册(Python版)20模块Chains链操作指南

toyiye 2024-06-21 12:25 14 浏览 0 评论

单独使用 LLM 对于一些简单的应用程序来说很好,但许多更复杂的应用程序需要链接 LLM - 彼此或与其他专家链接。LangChain 提供了 Chains 的标准接口,以及一些常见的 Chain 实现,方便使用。

提供了以下文档部分:

  • 入门:链条入门指南,可帮助您快速启动和运行。
  • 操作指南:操作指南的集合。这些重点介绍了如何使用各种类型的链。
  • 参考:所有 Chain 类的 API 参考文档。

开始

在本教程中,我们将学习如何在 LangChain 中创建简单的链。我们将学习如何创建链、向其添加组件并运行它。

在本教程中,我们将介绍:

  • 使用简单的 LLM 链
  • 创建顺序链
  • 创建自定义链

为什么我们需要链条?

链允许我们将多个组件组合在一起以创建一个单一的、连贯的应用程序。例如,我们可以创建一个接受用户输入的链,使用 PromptTemplate 对其进行格式化,然后将格式化后的响应传递给 LLM。我们可以通过将多个链组合在一起,或者通过将链与其他组件组合来构建更复杂的链。

快速开始:使用LLMChain

这LLMChain是一个简单的链,它接受一个提示模板,用用户输入格式化它并返回来自 LLM 的响应。

要使用LLMChain,首先创建一个提示模板。

from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}?",
)

我们现在可以创建一个非常简单的链,它将接受用户输入,用它格式化提示,然后将它发送给 LLM。

from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)

# Run the chain only specifying the input variable.
print(chain.run("colorful socks"))
Colorful Toes Co.

如果有多个变量,您可以使用字典一次输入它们。

prompt = PromptTemplate(
    input_variables=["company", "product"],
    template="What is a good name for {company} that makes {product}?",
)
chain = LLMChain(llm=llm, prompt=prompt)
print(chain.run({
    'company': "ABC Startup",
    'product': "colorful socks"
    }))
Socktopia Colourful Creations.

LLMChain您也可以使用聊天模型:

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
)
human_message_prompt = HumanMessagePromptTemplate(
        prompt=PromptTemplate(
            template="What is a good name for a company that makes {product}?",
            input_variables=["product"],
        )
    )
chat_prompt_template = ChatPromptTemplate.from_messages([human_message_prompt])
chat = ChatOpenAI(temperature=0.9)
chain = LLMChain(llm=chat, prompt=chat_prompt_template)
print(chain.run("colorful socks"))
Rainbow Socks Co.

调用链的不同方式

所有继承自的类都Chain提供了几种运行链逻辑的方法。最直接的一种是使用__call__:

chat = ChatOpenAI(temperature=0)
prompt_template = "Tell me a {adjective} joke"
llm_chain = LLMChain(
    llm=chat,
    prompt=PromptTemplate.from_template(prompt_template)
)

llm_chain(inputs={"adjective":"corny"})
{'adjective': 'corny',
 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}

默认情况下,__call__返回输入和输出键值。return_only_outputs您可以通过设置为将其配置为仅返回输出键值True。

llm_chain("corny", return_only_outputs=True)
{'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}

如果Chain只输出一个输出键(即只有一个元素在它的output_keys),你可以使用run方法。请注意,run输出的是字符串而不是字典。

# llm_chain only has one output key, so we can use run
llm_chain.output_keys
['text']
llm_chain.run({"adjective":"corny"})
'Why did the tomato turn red? Because it saw the salad dressing!'

在一个输入键的情况下,可以不指定输入映射直接输入字符串。

# These two are equivalent
llm_chain.run({"adjective":"corny"})
llm_chain.run("corny")

# These two are also equivalent
llm_chain("corny")
llm_chain({"adjective":"corny"})
{'adjective': 'corny',
 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}

提示:您可以通过其方法轻松地将Chain对象集成为Tool您的对象。请在此处查看示例。Agentrun

向链中添加内存

Chain支持将BaseMemory对象作为memory参数,允许Chain对象在多次调用中持久保存数据。换句话说,它创建了Chain一个有状态的对象。

from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

conversation = ConversationChain(
    llm=chat,
    memory=ConversationBufferMemory()
)

conversation.run("Answer briefly. What are the first 3 colors of a rainbow?")
# -> The first three colors of a rainbow are red, orange, and yellow.
conversation.run("And the next 4?")
# -> The next four colors of a rainbow are green, blue, indigo, and violet.
'The next four colors of a rainbow are green, blue, indigo, and violet.'

本质上,BaseMemory定义了一个如何langchain存储内存的接口。它允许通过方法读取存储的数据load_memory_variables并通过save_context方法存储新数据。您可以在内存部分了解更多信息。

调试链

由于大多数对象涉及大量输入提示预处理和 LLM 输出后处理,因此很难Chain仅从其输出调试对象。Chain设置verbose为将在运行时True打印出对象的一些内部状态。Chain

conversation = ConversationChain(
    llm=chat,
    memory=ConversationBufferMemory(),
    verbose=True
)
conversation.run("What is ChatGPT?")
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: What is ChatGPT?
AI:

> Finished chain.
'ChatGPT is an AI language model developed by OpenAI. It is based on the GPT-3 architecture and is capable of generating human-like responses to text prompts. ChatGPT has been trained on a massive amount of text data and can understand and respond to a wide range of topics. It is often used for chatbots, virtual assistants, and other conversational AI applications.'

组合链SequentialChain

调用语言模型后的下一步是对语言模型进行一系列调用。我们可以使用顺序链来做到这一点,顺序链是按预定义顺序执行其链接的链。具体来说,我们将使用SimpleSequentialChain. 这是最简单的顺序链类型,其中每个步骤都有一个输入/输出,一个步骤的输出是下一个步骤的输入。

在本教程中,我们的顺序链将:

  1. 首先,为产品创建公司名称。我们将重用LLMChain我们之前初始化的来创建这个公司名称。
  2. 然后,为产品创建一个标语。我们将初始化一个新的LLMChain来创建这个标语,如下所示。
second_prompt = PromptTemplate(
    input_variables=["company_name"],
    template="Write a catchphrase for the following company: {company_name}",
)
chain_two = LLMChain(llm=llm, prompt=second_prompt)

现在我们可以将这两个 LLMChain 结合起来,这样我们就可以一步创建一个公司名称和一个标语。

from langchain.chains import SimpleSequentialChain
overall_chain = SimpleSequentialChain(chains=[chain, chain_two], verbose=True)

# Run the chain specifying only the input variable for the first chain.
catchphrase = overall_chain.run("colorful socks")
print(catchphrase)
> Entering new SimpleSequentialChain chain...
Rainbow Socks Co.


"Put a little rainbow in your step!"

> Finished chain.


"Put a little rainbow in your step!"

Chain使用类创建自定义链

LangChain 提供了许多开箱即用的链,但有时您可能希望为您的特定用例创建自定义链。对于这个例子,我们将创建一个自定义链来连接 2 的输出LLMChain。

为了创建自定义链:

  1. 从类的子Chain类开始,
  2. 填写input_keys和output_keys属性,
  3. 添加_call显示如何执行链的方法。

这些步骤在下面的示例中进行了演示:

from langchain.chains import LLMChain
from langchain.chains.base import Chain

from typing import Dict, List


class ConcatenateChain(Chain):
    chain_1: LLMChain
    chain_2: LLMChain

    @property
    def input_keys(self) -> List[str]:
        # Union of the input keys of the two chains.
        all_input_vars = set(self.chain_1.input_keys).union(set(self.chain_2.input_keys))
        return list(all_input_vars)

    @property
    def output_keys(self) -> List[str]:
        return ['concat_output']

    def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
        output_1 = self.chain_1.run(inputs)
        output_2 = self.chain_2.run(inputs)
        return {'concat_output': output_1 + output_2}

现在,我们可以尝试运行我们调用的链。

prompt_1 = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}?",
)
chain_1 = LLMChain(llm=llm, prompt=prompt_1)

prompt_2 = PromptTemplate(
    input_variables=["product"],
    template="What is a good slogan for a company that makes {product}?",
)
chain_2 = LLMChain(llm=llm, prompt=prompt_2)

concat_chain = ConcatenateChain(chain_1=chain_1, chain_2=chain_2)
concat_output = concat_chain.run("colorful socks")
print(f"Concatenated output:\n{concat_output}")
Concatenated output:


Funky Footwear Company

"Brighten Up Your Day with Our Colorful Socks!"

操作指南

链由链接组成,链接可以是原始链或其他链。基元可以是prompts、models、任意函数或其他链。此处的示例分为三个部分:

通用功能

涵盖通用链(在广泛的应用程序中很有用)以及与这些链相关的通用功能。

  • Async API for Chain
  • Creating a custom Chain
  • Loading from LangChainHub
  • LLM Chain
  • Additional ways of running LLM Chain
  • Parsing the outputs
  • Initialize from string
  • Router Chains
  • Sequential Chains
  • Serialization
  • Transformation Chain

索引相关链

与使用索引相关的链。

  • Analyze Document
  • Chat Over Documents with Chat History
  • Graph QA
  • Hypothetical Document Embeddings
  • Question Answering with Sources
  • Question Answering
  • Summarization
  • Retrieval Question/Answering
  • Retrieval Question Answering with Sources
  • Vector DB Text Generation

所有其他相关链

所有其他类型的链条!

  • API Chains
  • Self-Critique Chain with Constitutional AI
  • FLARE
  • GraphCypherQAChain
  • BashChain
  • LLMCheckerChain
  • LLM Math
  • LLMRequestsChain
  • LLMSummarizationCheckerChain
  • Moderation
  • Router Chains: Selecting from multiple prompts with MultiPromptChain
  • Router Chains: Selecting from multiple prompts with MultiRetrievalQAChain
  • OpenAPI Chain
  • PAL
  • SQL Chain example

相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码