LangChain

How to Save and Load a Chain from a Disk Using LangChain?

LangChain is an Artificial Intelligence or AI framework that builds machine learning models to develop chatbots or question-answering software. It is beneficial in managing natural language processing problems that require storing large files. These files are stored as the prompt in JSON-like format to train models to process commands in natural languages.

This guide will explain the process of saving and loading a chain from a disk using LangChain.

How to Save and Load a Chain from Disk Using LangChain?

To save and load a chain from disk in LangChain, simply install the LangChain, OpenAI modules. After that, follow the below-mentioned procedure that is implemented in Python language:

Setup Prerequisites

Install the LangChain module to start the process of saving and loading a chain from disk using LangChain:

pip install langchain

 

Now, Install the OpenAI framework to use it in the example for loading chain from disk:

pip install openai

 

After installing the OpenAI module, simply import “os” and “getpass” libraries to provide the OpenAI API key:

import os
import getpass

os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')

 

After completing the initial importing modules, let’s explore the method to save the chain from a disk:

Save a Chain from a Disk in LangChain

After setting up the prerequisites, import PromptTemplate, OpenAI, and LLMChain libraries. After that, create the prompt template that is stored in the “prompt” variable, and the “llm_chain” variable is used to create a chain:

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)

 
Now, simply run the following code to use the save() function. It stores the chain from the local disk:

llm_chain.save("llm_chain.json")

 

Use the following code to check the content of the “llm_chain.json” file which was stored in the previous step:

cat llm_chain.json

 
Running the above code that displays the content of the JSON document as in the following screenshot:


Let’s load the chain from the disk in Langchain:

Load a Chain from a Disk in LangChain

After saving a chain from the disk, simply import the load_chain library. It uses for loading the chain from the disk:

from langchain.chains import load_chain

 
Use the “chain” variable to load JSON document using the load_chain() method:

chain = load_chain("llm_chain.json")

 
Execute the following code with any query of the user’s choice to run the chain using the chain.run() method:

chain.run("whats 10 + 5")

 
The following screenshot displays the answer to the provided query after completing the chain:

Bonus Tip: How to Save Components from a Chain Separately?

The user can also save different components from a chain separately as the above example saves and loads data as a complete chain. Use the following code to execute the “prompt.save()” method with the prompt file in the JSON format:

llm_chain.prompt.save("prompt.json")

 
After that, simply display the contents of the JSON file named prompt using the “cat” command:

cat prompt.json

 
The following screenshot displays the contents of the “prompt.json” file from the chain:


After that, use the “llm.save()” method to save the llm.json file as the following code suggests:

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

 
Now, display the content of the llm.json file using the following code:

cat llm.json

 
The following screenshot displays the contents of the file saved on the llm_chain:


After saving the file in the LLM chain, create a directory object named “config” with different parameters. After that, use the JSON library to open the JSON file in write mode to edit the document using the dump() function:

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)

 
After that, simply display the contents of the “llm_chain_separate.json” file:

cat llm_chain_separate.json

 
The following screenshot displays the contents of the file after editing the document:


After saving and editing the file, simply load the json file and use the “load_chain()” function:

chain = load_chain("llm_chain_separate.json")

 
Use the following code to execute the query by using the “chain.run()” function to get a step-by-step answer:

chain.run("whats 5 * 5")

 

That is all about saving and loading a chain from disk using LangChain.

Conclusion

To save and load a chain from a disk using LangChain, simply install the LangChain and OpenAI frameworks. After that, simply save a chain and configure the “prompt” and “llm_chain” variables to load the chain using a command. After saving the chain, simply load the chain from the disk using LangChain to execute the command written in natural language. This guide has explained the process of saving and loading a chain from disk using LangChain.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.