langchain

1.概念

什么是LangChain?

源起:LangChain产生源于Harrison与领域内的一些人交谈,这些人正在构建复杂的LLM应用,他在开发方式

上看到了一些可以抽象的部分。一个应用可能需要多次提示LLM并解析其输出,因此需要编写大量的复制粘贴

LangChain使这个开发过程更加简单。一经推出后,在社区被广泛采纳,不仅有众多用户,还有许多贡献者参

与开源工作。

还有大模型本身的问题,无法感知实时数据,无法和当前世界进行交互。

LangChain是一个用于开发大语言模型的框架。

主要特性:

\1. 数据感知:能够将语⾔模型与其他数据源进⾏连接。

\2. 代理性:允许语⾔模型与其环境进⾏交互。可以通过写⼯具的⽅式做各种事情,数据的写⼊更新。

主要价值:

1、组件化了需要开发LLM所需要的功能,提供了很多工具,方便使用。

2、有一些现成的可以完整特定功能的链,也可以理解为提高了工具方便使用。

2.主要模块

LangChain 为以下模块提供了标准、可扩展的接口和外部集成,按照复杂程度从低到高列出:

模型输入/输出 (Model I/O)

与语言模型进行接口交互

数据连接 (Data connection)

与特定于应用程序的数据进行接口交互

链式组装 (Chains)

构造调用序列

代理 (Agents)

根据高级指令让链式组装选择要使用的工具

内存 (Memory)

在链式组装的多次运行之间持久化应用程序状态

回调 (Callbacks)

记录和流式传输任何链式组装的中间步骤

3.Memory

默认情况下,链式模型和代理模型都是无状态的,这意味着它们将每个传入的查询独立处理(就像底层的 LLMs 和聊天模型本身一样)。在某些应用程序中,比如聊天机器人,记住先前的交互是至关重要的。无论是短期还是长期,都要记住先前的交互。Memory 类正是做到了这一点。 LangChain 提供了两种形式的记忆组件。首先,LangChain 提供了用于管理和操作以前的聊天消息的辅助工具。这些工具被设计成模块化的,无论如何使用都很有用。其次,LangChain 提供了将这些工具轻松整合到链式模型中的方法。

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

conversation = ConversationChain(
    llm=model,
    verbose=True,
    memory=ConversationBufferMemory()
)
conversation.predict(input="你好啊!,我是张三")

conversation.predict(input="你知道我的姓名吗")

ConversationBufferMemory

memory = ConversationBufferMemory()
memory.save_context({"input": "你好啊!,我是张三"},
                    {"output": "你好,张三!很高兴认识你!你有什么问题想要问我吗?"})
memory.save_context({"input": "你知道我的姓名吗"},
                    {"output": "当然知道!您叫做张三。请问有什么我可以帮助您的?"})
memory.load_memory_variables({})

{‘history’: ‘Human: 你好啊!,我是张三\nAI: 你好,张三!很高兴认识你!你有什么问题想要问我吗?\nHuman: 你知道我的姓名吗\nAI: 当然知道!您叫做张三。请问有什么我可以帮助您的?’}

ConversationBufferWindowMemory

通过k控制记忆数量

from langchain.memory import ConversationBufferWindowMemory

memory = ConversationBufferWindowMemory(k=1)

memory.save_context({"input": "你好啊!,我是张三"},
                    {"output": "你好,张三!很高兴认识你!你有什么问题想要问我吗?"})
memory.save_context({"input": "你知道我的姓名吗"},
                    {"output": "当然知道!您叫做张三。请问有什么我可以帮助您的?"})
memory.load_memory_variables({})

{‘history’: ‘Human: 你知道我的姓名吗\nAI: 当然知道!您叫做张三。请问有什么我可以帮助您的?’}

ConversationTokenBufferMemory

保留token数量

from langchain.memory import ConversationTokenBufferMemory

memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=20)
memory.save_context({"input": "AI is what?!"},
                    {"output": "Amazing!"})
memory.save_context({"input": "Backpropagation is what?"},
                    {"output": "Beautiful!"})
memory.save_context({"input": "Chatbots are what?"},
                    {"output": "Charming!"})
memory.load_memory_variables({})

{‘history’: ‘AI: Beautiful!\nHuman: Chatbots are what?\nAI: Charming!’}

ConversationSummaryMemory

总结对话中的内容

from langchain.memory import ConversationSummaryBufferMemory

# create a long string
schedule = "There is a meeting at 8am with your product team. \
You will need your powerpoint presentation prepared. \
9am-12pm have time to work on your LangChain \
project which will go quickly because Langchain is such a powerful tool. \
At Noon, lunch at the italian resturant with a customer who is driving \
from over an hour away to meet you to understand the latest in AI. \
Be sure to bring your laptop to show the latest LLM demo."

memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)
memory.save_context({"input": "Hello"}, {"output": "What's up"})
memory.save_context({"input": "Not much, just hanging"},
                    {"output": "Cool"})
memory.save_context({"input": "What is on the schedule today?"},
                    {"output": f"{schedule}"})

memory.load_memory_variables({})

{‘history’: ‘System: The human asks the AI what is on the schedule today. The AI responds that it is not currently set up to provide a schedule.\nAI: There is a meeting at 8am with your product team. You will need your powerpoint presentation prepared. 9am-12pm have time to work on your LangChain project which will go quickly because Langchain is such a powerful tool. At Noon, lunch at the italian resturant with a customer who is driving from over an hour away to meet you to understand the latest in AI. Be sure to bring your laptop to show the latest LLM demo.’}