.ipynb
.pdf
Serialization
Serialization#
This notebook goes over how to serialize agents. For this notebook, it is important to understand the distinction we draw between agents and tools. An agent is the LLM powered decision maker that decides which actions to take and in which order. Tools are various instruments (functions) an agent has access to, through which an agent can interact with the outside world. When people generally use agents, they primarily talk about using an agent WITH tools. However, when we talk about serialization of agents, we are talking about the agent by itself. We plan to add support for serializing an agent WITH tools sometime in the future.
Let’s start by creating an agent with tools as we normally do:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
Let’s now serialize the agent. To be explicit that we are serializing ONLY the agent, we will call the save_agent method.
agent.save_agent('agent.json')
!cat agent.json
{
    "llm_chain": {
        "memory": null,
        "verbose": false,
        "prompt": {
            "input_variables": [
                "input",
                "agent_scratchpad"
            ],
            "output_parser": null,