【LangChain】序列化(Serialization)--持久化到磁盘

28 篇文章 12 订阅

LangChain学习文档


概述

本笔记本介绍了如何将Chain序列化到磁盘从磁盘序列化回来。我们使用的序列化格式是json或yaml。目前,只有部分Chain支持这种类型的序列化。随着时间的推移,我们将增加支持的Chain的数量。

内容

将链保存到磁盘(Saving a chain to disk)

首先,我们来看看如何将Chain保存到磁盘。这可以通过 .save 方法并指定带有 json 或 yaml 扩展名的文件路径来完成。

from langchain import PromptTemplate, OpenAI, LLMChain

template = """Question: {question}

Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0), verbose=True)
# 关键点这里
llm_chain.save("llm_chain.json")

现在让我们看看这个保存的文件里面有什么

cat llm_chain.json

结果:

    {
        "memory": null,
        "verbose": true,
        "prompt": {
            "input_variables": [
                "question"
            ],
            "output_parser": null,
            "template": "Question: {question}\n\nAnswer: Let's think step by step.",
            "template_format": "f-string"
        },
        "llm": {
            "model_name": "text-davinci-003",
            "temperature": 0.0,
            "max_tokens": 256,
            "top_p": 1,
            "frequency_penalty": 0,
            "presence_penalty": 0,
            "n": 1,
            "best_of": 1,
            "request_timeout": null,
            "logit_bias": {},
            "_type": "openai"
        },
        "output_key": "text",
        "_type": "llm_chain"
    }

从磁盘中加载Chain(Loading a chain from disk)

我们可以使用 load_chain 方法从磁盘加载Chain

from langchain.chains import load_chain
chain = load_chain("llm_chain.json")
chain.run("whats 2 + 2")

结果:

    > Entering new LLMChain chain...
    Prompt after formatting:
    Question: whats 2 + 2
    
    Answer: Let's think step by step.
    
    > Finished chain.
    ' 2 + 2 = 4'

单独保存组件(Saving components separately)

在上面的例子中,我们可以看到promptllm配置信息与整体Chain保存在同一个json中。或者,我们可以将它们分开并单独保存。这通常有助于使保存的组件更加模块化。为此,我们只需指定 llm_pathPrompt_path

单独对Prompt组件进行保存

# 上面是:llm_chain.save("llm_chain.json")
llm_chain.prompt.save("prompt.json")

打印看结果:

cat prompt.json

    {
        "input_variables": [
            "question"
        ],
        "output_parser": null,
        "template": "Question: {question}\n\nAnswer: Let's think step by step.",
        "template_format": "f-string"
    }

单独对llm进行保存:

llm_chain.llm.save("llm.json")

查看结果:

cat llm.json
    {
        "model_name": "text-davinci-003",
        "temperature": 0.0,
        "max_tokens": 256,
        "top_p": 1,
        "frequency_penalty": 0,
        "presence_penalty": 0,
        "n": 1,
        "best_of": 1,
        "request_timeout": null,
        "logit_bias": {},
        "_type": "openai"
    }

这些独立序列化的组件,该如何组装呢?
就是指定相应组件的文件名

config = {
    "memory": None,
    "verbose": True,
    "prompt_path": "prompt.json",
    "llm_path": "llm.json",
    "output_key": "text",
    "_type": "llm_chain",
}
import json
# 将config编码写入到llm_chain_separate.json文件中
with open("llm_chain_separate.json", "w") as f:
    json.dump(config, f, indent=2)

查看效果:

cat llm_chain_separate.json
    {
      "memory": null,
      "verbose": true,
      "prompt_path": "prompt.json",
      "llm_path": "llm.json",
      "output_key": "text",
      "_type": "llm_chain"
    }

然后我们可以用同样的方式加载它

chain = load_chain("llm_chain_separate.json")

chain.run("whats 2 + 2")

结果:

    > Entering new LLMChain chain...
    Prompt after formatting:
    Question: whats 2 + 2
    
    Answer: Let's think step by step.
    
    > Finished chain.

    ' 2 + 2 = 4'

总结

本文讲述了,如何持久化、从磁盘加载、独立序列化组件、加载独立序列化组件的内容。

  1. 序列化:就是Chain对象调用save()方法。
  2. 从磁盘中加载:就是使用load_chain(),去读取文件,得到Chain对象。
  3. 独立组件序列化:llm_chain.prompt.save()llm_chain.llm.save()
  4. 加载独立序列化组件:
config = {
    "memory": None,
    "verbose": True,
    "prompt_path": "prompt.json",
    "llm_path": "llm.json",
    "output_key": "text",
    "_type": "llm_chain",
}
import json

with open("llm_chain_separate.json", "w") as f:
    json.dump(config, f, indent=2)

参考地址:

Serialization

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值