查看: 1064|回复: 0

大模型工具:LangChain原理与案例

[复制链接]

247

主题

276

帖子

770

积分

高级飞友

Rank: 4

积分
770
飞币
492
注册时间
2017-8-2
发表于 2023-7-14 13:34:59 | 显示全部楼层 |阅读模式
LangChain 是什么?

LangChain是一个用于开发由语言模型驱动的应用程序的框架。它使得可以构建以下类型的应用程序:
    数据感知:将语言模型与其他数据源连接起来智能:允许语言模型与其环境进行交互


LangChain的主要价值在于:
    组件:提供了处理语言模型的抽象,以及每个抽象的多个实现。组件是模块化且易于使用的,无论您是否使用LangChain的其他部分。现成的链:结构化组件的组合,用于完成特定的高层任务。
LangChain 安装

要安装LangChain,请运行以下命令:
    使用Pip使用Conda
pip install langchain

这是安装LangChain的最基本要求。LangChain的真正价值在于将其与各种模型提供者、数据存储等进行集成时产生。默认情况下,安装LangChain不会安装这些依赖项。不过,有两种其他方法可以安装带有这些依赖项的LangChain。

要安装与常见的LLM提供者相关的模块,请运行:
pip install langchain[llms]

要安装所有集成所需的模块,请运行:
pip install langchain[all]

请注意,如果您使用的是zsh,在将方括号作为命令参数传递时,需要使用引号括起来,例如:
pip install 'langchain[all]'
LangChain 基础案例


OpenAI Key设置
from langchain.llms import OpenAI
llm = OpenAI(openai_api_key="...")

使用LLMs进行预测,并通过设置温度参数来调整生成的文本的随机性。
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.9)
llm.predict("What would be a good company name for a company that makes colorful socks?")
# >> Feetful of Fun

使用聊天模型进行对话的方法,包括使用不同类型的消息对象进行输入和输出。
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

chat = ChatOpenAI(temperature=0)
chat.predict_messages([HumanMessage(content="Translate this sentence from English to French. I love programming.")])
# >> AIMessage(content="J'aime programmer.", additional_kwargs={})

使用提示模板将用户输入和指令结合起来,以提供更多上下文信息。
from langchain.prompts import PromptTemplate
prompt = PromptTemplate.from_template("What is a good name for a company that makes {product}?")
prompt.format(product="colorful socks")

使用链将模型和提示模板连接起来,实现更复杂的工作流程。
from langchain import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
chat = ChatOpenAI(temperature=0)
template = "You are a helpful assistant that translates {input_language} to {output_language}."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
chain = LLMChain(llm=chat, prompt=chat_prompt)
chain.run(input_language="English", output_language="French", text="I love programming.")

代理可以根据输入动态选择不同的操作。
from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")

使用存储器来保持应用程序的状态,并在下一次运行时使用存储的状态。
from langchain.prompts import (
    ChatPromptTemplate,
    MessagesPlaceholder,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate
)
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory

prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template(
        "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."
    ),
    MessagesPlaceholder(variable_name="history"),
    HumanMessagePromptTemplate.from_template("{input}")
])

llm = ChatOpenAI(temperature=0)
memory = ConversationBufferMemory(return_messages=True)
conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)
conversation.predict(input="Hi there!")
LangChain 组成结构

LangChain提供了标准、可扩展的接口和外部集成,用于以下模块,按照从简单到复杂的顺序排列。
Model I/O

Model I/O是任何语言模型应用程序的核心元素。LangChain提供了与任何语言模型进行交互的构建模块。

大模型工具:LangChain原理与案例w2.jpg

    Prompts(提示):将模型输入进行模板化、动态选择和管理。Language models(语言模型):通过通用接口调用语言模型。Output parsers(输出解析器):从模型输出中提取信息。

使用这些模块,可以有效地管理和处理与语言模型的交互。模板化输入、调用模型和解析输出是构建语言模型应用程序的关键步骤。
Data connection

Data connection是许多LLM应用程序所需的一部分,它涉及用户特定的数据,这些数据不是模型的训练集的一部分。LangChain提供了构建模块,通过以下方式加载、转换、存储和查询数据:

大模型工具:LangChain原理与案例w3.jpg

    Document loaders(文档加载器):从多种不同的来源加载文档。Document transformers(文档转换器):对文档进行分割、去除冗余文档等操作。Text embedding models(文本嵌入模型):将非结构化文本转换为浮点数列表。Vector stores(向量存储):存储和检索嵌入数据。Retrievers(检索器):对数据进行查询。

使用这些构建模块,可以有效地加载、转换、存储和查询用户特定的数据,为LLM应用程序提供必要的数据连接。
Chains

Chains是LangChain提供的用于构建“链式”应用程序的接口。在简单的应用程序中,单独使用LLM是可以的,但更复杂的应用程序需要将LLM进行链接,无论是与其他LLM还是其他组件进行链接。

LangChain为这种“链式”应用程序提供了Chain接口。我们将Chain定义为对组件的一系列调用,可以包括其他链。
Agents

Agents在一些应用程序中需要根据用户输入灵活地调用LLMs和其他工具。Agent接口提供了这种应用程序的灵活性。Agent可以访问一系列工具,并根据用户输入决定使用哪些工具。Agent可以使用多个工具,并将一个工具的输出作为下一个工具的输入。

Agent主要分为两种类型:
    Action agents(行动型Agent):在每个时间步骤中,根据之前所有行动的输出决定下一步的行动。Plan-and-execute agents(规划执行型Agent):在前期决定完整的行动序列,然后按计划依次执行,而无需更新计划。

Action agents适用于小任务,而plan-and-execute agents更适用于复杂或长时间运行的任务,这些任务需要维持长期目标和焦点。通常最佳方法是将action agent的动态性与plan-and-execute agent的规划能力相结合,让plan-and-execute agent使用action agents来执行计划。
Memory

Memory模块旨在处理应用程序中的状态,并在链式应用程序或代理的运行之间保留和持久化应用程序数据。它允许您记住和引用先前的交互,并在诸如聊天机器人等应用程序中保持上下文。
Callbacks

LangChain提供了一个回调系统,允许您在LLM应用程序的各个阶段进行钩子操作。这对于日志记录、监控、流式处理和其他任务非常有用。

可以根据需要实现这些方法来执行自定义的回调逻辑,例如记录日志、发送通知、保存输出等。这使您能够在应用程序执行过程中进行观察和干预,并根据需要采取相应的操作。
LangChain 优缺点

优点


    LangChain采用组件化的设计,提供了一系列模块和接口,使得开发语言模型应用程序变得简单和灵活。开发人员可以选择和组合各个组件,以构建符合自己需求的应用。

    LangChain支持外部集成和扩展,可以与各种语言模型提供商、数据存储和其他工具集成。这使得开发人员能够根据自己的需求选择最适合的组件和工具,提高应用的灵活性和功能性。

    LangChain提供了各种模块和工具,适用于不同的应用场景,包括问题回答、聊天机器人、智能代理等。无论是简单的应用还是复杂的应用,LangChain都提供了相应的组件和示例,帮助开发人员快速构建应用。

    LangChain提供了内存模块,可以在应用程序的不同运行周期中持久化应用状态。这对于需要记住之前交互的应用程序非常有用,如聊天机器人。内存模块使得开发人员可以方便地管理和访问之前的交互数据。
缺点

由于LangChain是一个功能强大且灵活的框架,对于新手开发人员来说,可能需要一定的学习曲线才能熟悉其各个组件和工作原理。对于没有经验的开发人员来说,可能需要花费一些时间来理解和掌握LangChain的使用方法。

目前,LangChain的文档尚未完全完善,有些部分还在施工中。这可能会给开发人员带来一些困扰,特别是在需要参考文档进行开发时。

# 竞赛交流群 邀请函  #

大模型工具:LangChain原理与案例w4.jpg

△长按添加竞赛小助手
每天大模型、算法竞赛、干货资讯

与 36000+来自竞赛爱好者一起交流~
您需要登录后才可以回帖 登录 | 加入联盟

本版积分规则

快速回复 返回顶部 返回列表