{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73f9bf40",
   "metadata": {},
   "source": [
    "# Serialization\n",
    "\n",
    "LangChain Python and LangChain JS share a serialization scheme. You can check if a LangChain class is serializable by running with the `is_lc_serializable` class method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9c9fb6ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import OpenAI\n",
    "from langchain.llms.loading import load_llm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "441d28cb-e898-47fd-8f27-f620a9cd6c34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "OpenAI.is_lc_serializable()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "87b8a7c6-35b7-4fab-938b-4d05e9cc06f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(model=\"gpt-3.5-turbo-instruct\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88ce018b",
   "metadata": {},
   "source": [
    "## Dump\n",
    "\n",
    "Any serializable object can be serialized to a dict or json string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f12b28f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'lc': 1,\n",
       " 'type': 'constructor',\n",
       " 'id': ['langchain', 'llms', 'openai', 'OpenAI'],\n",
       " 'kwargs': {'model': 'gpt-3.5-turbo-instruct',\n",
       "  'openai_api_key': {'lc': 1, 'type': 'secret', 'id': ['OPENAI_API_KEY']}}}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.load import dumpd, dumps\n",
    "\n",
    "dumpd(llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "095b1d56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"lc\": 1, \"type\": \"constructor\", \"id\": [\"langchain\", \"llms\", \"openai\", \"OpenAI\"], \"kwargs\": {\"model\": \"gpt-3.5-turbo-instruct\", \"openai_api_key\": {\"lc\": 1, \"type\": \"secret\", \"id\": [\"OPENAI_API_KEY\"]}}}'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dumps(llm)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab3e4223",
   "metadata": {},
   "source": [
    "## Load\n",
    "\n",
    "Any serialized object can be loaded."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "68e45b1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.load import loads\n",
    "from langchain.load.load import load\n",
    "\n",
    "loaded_1 = load(dumpd(llm))\n",
    "loaded_2 = loads(dumps(llm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c9272667-7fe3-4e5f-a1cc-69e8829b9e8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "I am an AI and do not have the capability to experience emotions. But thank you for asking. Is there anything I can assist you with?\n"
     ]
    }
   ],
   "source": [
    "print(loaded_1.invoke(\"How are you doing?\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
