《使用 LangChain 进行大模型应用开发》学习笔记(一)

前言

本文是 Harrison Chase (LangChain 创建者)和吴恩达(Andrew Ng)的视频课程《LangChain for LLM Application Development》(使用 LangChain 进行大模型应用开发)的学习笔记。由于原课程为全英文视频课程,国内访问较慢,同时我整理和替换了部分内容以便于国内学习。阅读本文可快速学习课程内容。

课程介绍

本课程介绍了强大且易于扩展的 LangChain 框架,LangChain 框架是一款用于开发大语言模型(LLM)应用的开源框架,其使用提示词、记忆、链、代理等简化了大语言模型应用的开发工作。由于 LangChain 仍处于快速发展期,部分 API 还不稳定,课程中的部分代码已过时,我使用了目前最新的 v0.2 版本进行讲解,所有代码均可在 v0.2 版本下执行。另外,课程使用的 OpenAI 在国内难以访问,我替换为国内的 Kimi 模型,对于学习没有影响。参考这篇文章来获取 Kimi 的 API 令牌。

课程一共分为三个部分:

  • 第一部分
  • 第二部分(待发布)
  • 第三部分(待发布)

在这里插入图片描述

课程链接

第一部分

LangChain 简介

LangChain 是一个可用于构建 LLM 应用的开源框架,支持 Python 和 Javascript。LangChain 关注组合和模块化,提供了链来组合构建复杂的任务。在核心模块之外,可以通过丰富的第三方扩展,集成 OpenAI 等服务。同时 LangChain 还提供了 LangChain Expression Language (LCEL) 来快速构建组件链。

接下来我们通过一些简单的例子来看看 LangChain 的使用。

模型、提示词和解析器

模型指的是大语言模型及相关支撑服务,提示词指的是创建给模型的输入,而解析器则是反过来,从模型获取输出,并转换为结构化的格式。在进行 LLM 应用开发时,我们总是需要反复将提示词提交给模型,并从它那里获取结果并解析。而 LangChain 则提供了一系列的抽象层来简化这部分的操作。

对话 API

我们可以直接使用大语言模型提供的 API 服务来构建 LLM 应用(课程使用的是 openai,但国内使用不便,这里替换为 Kimi)。首先我们来看一段代码。

import openai

base_url = 'https://api.moonshot.cn/v1'  # 替换为 Kimi 服务地址
api_key = 'sk-......'  # Kimi 的 API 令牌,获取方式见课程介绍
llm_model = 'moonshot-v1-8k'  # 模型名称

def get_completion(prompt, model=llm_model):
    messages = [{"role": "user", "content": prompt}]
    client = openai.OpenAI(
        api_key=api_key,
        base_url=base_url,
    )
    completion = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0.3,
    )
    return completion.choices[0].message.content

要运行这段代码,需要先安装 openai 依赖。

pip install openai

上面这段代码使用了 openai 库(替换为 Kimi 大模型)远程调用大模型的 API 接口进行提问,第一个参数就是我们的提示词。我们可以这样使用。

res = get_completion("1 + 1 等于几?")
print(res)

然后就会得到类似下面这样的回答。

1 + 1 等于 2。在数学中,这是一个基本的加法运算。当我们将两个相同的数相加时,结果是一个比原来数大1的数。在这个例子中,两个数都是1,所以相加后得到2。

LLM 并不总是返回相同的结果,每次执行可能会略有不同。

我们可以使用变量替换来构建更复杂的提示词。

customer_email = """
Arrr, I be fuming that me blender lid \
flew off and splattered me kitchen walls \
with smoothie! And to make matters worse,\
the warranty don't cover the cost of \
cleaning up me kitchen. I need yer help \
right now, matey!
"""

style = "中文,语气平和"

prompt = f"""请将下面双引号内的文本翻译为指定的样式:{style} \
文本: "{customer_email}"
"""
res = get_completion(prompt)
print(res)

可以获取到类似这样的回答。

哎呀,我真是气坏了,我的搅拌机盖子飞出去了,把我的厨房墙壁弄得到处都是奶昔!更糟糕的是,保修不包括清理厨房的费用。我现在就需要你的帮助,伙计!

现在让我们看看在 LangChain 中怎么使用。

首先需要安装依赖。

pip install langchain langchain-core langchain-openai

然后看看如下代码,和上面第二种对话有类似的结果。

from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

template_string = """请将下面双引号内的文本翻译为指定的样式:{style} \
文本: "{text}"
"""
chat = ChatOpenAI(temperature=0.0, model=llm_model, base_url=base_url, api_key=api_key)
prompt_template = ChatPromptTemplate.from_template(template_string)
# print(prompt_template.messages[0].prompt)
# print(prompt_template.messages[0].prompt.input_variables)
customer_messages = prompt_template.format_messages(
    style=style,
    text=customer_email)
# print(type(customer_messages))
# print(type(customer_messages[0]))
customer_response = chat(customer_messages)
print(customer_response.content)

上面的代码可以看到,LangChain 包装了 openai 的接口,ChatOpenAI 就是我们和模型交互的对象,LangChan 还提供了其他很多模型的整合。而 ChatPromptTemplate 提供了一种声明和注入变量的方式,使用 ChatPromptTemplate 就无需 Python 的变量拼接了。使用 format_messages 生成提示词,并使用模型调用,即可获取结果。

上面的代码还不能体现 LangChain 的优势,下面我们来看看解析器的使用。

输出解析器

假设我们现在有如下一段文字,我们想从中获取提取这个物品的信息,提供 JSON 格式的结果。

customer_review = """\
This leaf blower is pretty amazing.  It has four settings:\
candle blower, gentle breeze, windy city, and tornado. \
It arrived in two days, just in time for my wife's \
anniversary present. \
I think my wife liked it so much she was speechless. \
So far I've been the only one using it, and I've been \
using it every other morning to clear the leaves on our lawn. \
It's slightly more expensive than the other leaf blowers \
out there, but I think it's worth it for the extra features.
"""

我们可以这样编辑提示词。

review_template = """\
对于下面的文本,请提取出其中的信息:

gift: 该物品是作为礼物为他人购买的吗? \
如果是回答 True, 否或者不清楚回答 False。

delivery_days: 该物品需要多少天才能到达? \
如果找不到这个信息则输出 -1。

price_value: 提取任何有关物品价值的句子,\
使用逗号分割的 Python list 样式输出,文本翻译为中文。

使用 JSON 格式化结果,包含以下键:
gift
delivery_days
price_value

文本: {text}
"""
prompt_template = ChatPromptTemplate.from_template(review_template)
print(prompt_template)
messages = prompt_template.format_messages(text=customer_review)
chat = ChatOpenAI(temperature=0.0, model=llm_model, base_url=base_url, api_key=api_key)
response = chat(messages)
print(response.content)

运行后可以获取类似下面的结果。

在这里插入图片描述

如果我们需要在代码中使用这个结果,需要使用字符串解析(如 JSON 字符串解析)等方式,重复工作且容易出错。而 LangChain 提供了结果解析器,可以方便地将结果转换为 Python 字典。

首先引入依赖。

from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParser

然后定义返回结构。

gift_schema = ResponseSchema(name="gift",
                             description="该物品是作为礼物为他人购买的吗?如果是回答 True, 否或者不清楚回答 False。")
delivery_days_schema = ResponseSchema(name="delivery_days",
                                      description="该物品需要多少天才能到达?如果找不到这个信息则输出 -1。")
price_value_schema = ResponseSchema(name="price_value",
                                    description="提取任何有关物品价值的句子,使用逗号分割的 Python list 样式输出,文本翻译为中文。")

response_schemas = [gift_schema,
                    delivery_days_schema,
                    price_value_schema]

接着创建解析器。

output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()
print(format_instructions)

最后调用模型,并使用解析器处理结果。

review_template_2 = """\
对于下面的文本,请提取出其中的信息:

gift: 该物品是作为礼物为他人购买的吗? \
如果是回答 True, 否或者不清楚回答 False。

delivery_days: 该物品需要多少天才能到达? \
如果找不到这个信息则输出 -1。

price_value: 提取任何有关物品价值的句子,\
使用逗号分割的 Python list 样式输出,文本翻译为中文。

文本: {text}

{format_instructions}
"""

prompt = ChatPromptTemplate.from_template(template=review_template_2)
messages = prompt.format_messages(text=customer_review,
                                  format_instructions=format_instructions)
chat = ChatOpenAI(temperature=0.0, model=llm_model, base_url=base_url, api_key=api_key)
response = chat(messages)
print(response.content)
output_dict = output_parser.parse(response.content)
print(output_dict)
print(type(output_dict))

输出结果如下。

在这里插入图片描述
我们就可以获得 Python 字典类型的结果,这样我们就可以在 LLM 应用中获得结构化的结果了。

记忆

记忆(Memory)是一种用于存储数据的工具,由于 LLM 没有长期记忆,使用它在多次调用之间保存状态。

ConversationBufferMemory

我们先使用 ConversationBufferMemory 作为记忆的例子。

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

# 使用 Kimi 的 URL 和 API 令牌
llm = ChatOpenAI(temperature=0.0, model=llm_model, base_url=base_url, api_key=api_key)
# 创建 memory
memory = ConversationBufferMemory()
conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True  # 输出格式化后提交的提示词
)
print(conversation.predict(input="你好,我的名字是 wwtg99"))
print(conversation.predict(input="1 + 1 等于几?"))
# time.sleep(60) # 如果使用免费 Kimi API,有速度限制,需要手动限速
print(conversation.predict(input="我的名字是什么?"))

我们会得到类似下面这样的回答。我们首先告诉 LLM 自己的名字,然后问它一个其他的问题,然后再问它我的名字是什么?LLM 记得之前我告诉它的我的名字。

> 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: 你好,我的名字是 wwtg99
AI:

> Finished chain.
你好,wwtg99!很高兴认识你。我是你的人工智能助手,可以帮你解答问题和提供信息。请问有什么我可以帮助你的吗?


> 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: 你好,我的名字是 wwtg99
AI: 你好,wwtg99!很高兴认识你。我是你的人工智能助手,可以帮你解答问题和提供信息。请问有什么我可以帮助你的吗?
Human: 1 + 1 等于几?
AI:

> Finished chain.
1 + 1 等于 2。这是一个基本的数学加法问题,表示将两个相同的数值相加得到的结果。


> 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: 你好,我的名字是 wwtg99
AI: 你好,wwtg99!很高兴认识你。我是你的人工智能助手,可以帮你解答问题和提供信息。请问有什么我可以帮助你的吗?
Human: 1 + 1 等于几?
AI: 1 + 1 等于 2。这是一个基本的数学加法问题,表示将两个相同的数值相加得到的结果。
Human: 我的名字是什么?
AI:

> Finished chain.
你的名字是 wwtg99。在我们之前的对话中,你告诉我的。如果你有其他问题或需要帮助,请随时告诉我。

通过输出完整的提示词,我们可以看到,LangChain 将我们之前的对话整理之后作为对话上下文传递给了 LLM。LLM 是无状态的,每次对话都是独立的,记忆是通过将整个对话上下文传递给 LLM 来实现的。

可以查看 memory 的内容。

print(memory.buffer)
print(memory.load_memory_variables({}))

可以得到类似这样的对话上下文。

Human: 你好,我的名字是 wwtg99
AI: 你好,wwtg99!很高兴认识你。我是你的人工智能助手,可以帮你解答问题和提供信息。请问有什么我可以帮助你的吗?
Human: 1 + 1 等于几?
AI: 1 + 1 等于 2。这是一个基本的数学加法问题,表示将两个相同的数值相加得到的结果。

{'history': 'Human: 你好,我的名字是 wwtg99\nAI: 你好,wwtg99!很高兴认识你。我是你的人工智能助手,可以帮你解答问题和提供信息。请问有什么我可以帮助你的吗?\nHuman: 1 + 1 等于几?\nAI: 1 + 1 等于 2。这是一个基本的数学加法问题,表示将两个相同的数值相加得到的结果。'}

我们也可以使用 save_context 来手动添加记忆。

memory = ConversationBufferMemory()
memory.save_context({"input": "你好"},
                    {"output": "有什么事"})
print("---第一次对话----")
print(memory.load_memory_variables({}))
memory.save_context({"input": "没什么事"},
                    {"output": "好的"})
print("---第二次对话----")
print(memory.load_memory_variables({}))

会得到类似如下输出。记忆会不断积累,好让 LLM 记得我们之前的对话内容。

---第一次对话----
{'history': 'Human: 你好\nAI: 有什么事'}
---第二次对话----
{'history': 'Human: 你好\nAI: 有什么事\nHuman: 没什么事\nAI: 好的'}

ConversationBufferWindowMemory

上面我们使用了 ConversationBufferMemory,还有其他类型的记忆。我们再来看看另一种记忆 ConversationBufferWindowMemory,它仅仅保留一个窗口的记忆。例如,如果设置 k = 1,那么只会记忆一次对话(我们提问一次和 LLM 回答一次)。

from langchain.memory import ConversationBufferWindowMemory

memory = ConversationBufferWindowMemory(k=1)
memory.save_context({"input": "你好"},
                    {"output": "有什么事"})
print("---第一次对话----")
print(memory.load_memory_variables({}))
memory.save_context({"input": "没什么事"},
                    {"output": "好的"})
print("---第二次对话----")
print(memory.load_memory_variables({}))

会得到这样的输出。

---第一次对话----
{'history': 'Human: 你好\nAI: 有什么事'}
---第二次对话----
{'history': 'Human: 没什么事\nAI: 好的'}

可以看到,由于 k = 1,因此只会保留一次对话的记忆,第二次对话就不会记得之前对话的内容了。同样的我们使用 ConversationBufferWindowMemory 且 k = 1,再来问 LLM 之前的三个问题(先报自己的名字,再问 1 + 1,再问我的名字是什么),由于 LLM 不记得之前的对话,它就不知道这个信息了。ConversationBufferWindowMemory 就好比人的记忆是有限的,不可能记得太久远的事情,因为太久远的对话内容可能对于当前的问题没有什么贡献。我们通过设置一个适当的 k 值,可以避免对话上下文无限扩大。

ConversationTokenBufferMemory

ConversationTokenBufferMemory 是另一种记忆实现,用来记忆有限数量的 tokens,tokens 和 LLM 的使用成本密切相关。关于 tokens 的概念,可以参考吴恩达的课程《给所有人的生成式 AI 课》 第二部分成本章节的介绍。

需要安装依赖 pip install tiktoken

from langchain.memory import ConversationTokenBufferMemory

llm = ChatOpenAI(temperature=0.0, model=llm_model)
memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=50, base_url=base_url, api_key=api_key)
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!"})
print(memory.load_memory_variables({}))

由于不同的 LLM 的 tokens 计算规则不同,因此我们需要使用 LLM 模型来创建 ConversationTokenBufferMemory。

ConversationSummaryMemory

相比于固定的对话次数或 tokens,ConversationSummaryMemory 提供了一种更适合较多内容的记忆实现,其对之前的对话进行总结并记忆。它是通过 LLM 将之前的对话进行总结压缩,提取关键信息,可以极大地减少对话上下文大小,同时尽可能地保留过去的记忆。

from langchain.memory import ConversationSummaryBufferMemory

memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)

总结

LangChain 提供了多种记忆实现,每种记忆可用于不同的应用场景。

  • ConversationBufferMemory:按原样存储整个对话历史记录,但随着对话增多,提示词大小也会无限膨胀
  • ConversationBufferWindowMemory:保存最近一个固定窗口的对话,可以有效避免提示词过大,但过去的记忆依赖 k 的设置
  • ConversationTokenBufferMemory:使用 token 长度来限制最近保存的对话,可以有效地控制 LLM 的使用成本
  • ConversationSummaryMemory:不限制固定的对话数量,而是将过去的对话提取成摘要记忆,可以有效降低提示词大小,同时也保留了过去的记忆,可设定记忆留存最高的 tokens 长度

除了上面四种,LangChain 还支持其他的记忆实现。

  • 向量数据存储(Vector data memory):通过嵌入(Embeddings)将内容转换为词向量进行存储
  • 实体记忆存储(Entity memories):适用于特定的实体信息(如人、单位、地区等),将实体详细信息进行存储,在需要时查询

LangChain 支持组合多种记忆类型,可创建更全面和定制化的解决方案。例如,可以使用 ConversationBufferMemory 或 ConversationSummaryBufferMemory 来维护整体对话上下文,同时还利用实体记忆来存储来调用有关对话中提到的个人或对象实体的特定详细信息。

(未完待续)

  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值