{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73f9bf40",
   "metadata": {},
   "source": [
    "# LLM Serialization\n",
    "\n",
    "This notebook walks through how to write and read an LLM Configuration to and from disk. This is useful if you want to save the configuration for a given LLM (e.g., the provider, the temperature, etc)."
   ]
  },
  {
   "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": "markdown",
   "id": "88ce018b",
   "metadata": {},
   "source": [
    "## Loading\n",
    "First, lets go over loading a LLM from disk. LLMs can be saved on disk in two formats: json or yaml. No matter the extension, they are loaded in the same way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f12b28f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "    \"model_name\": \"text-davinci-003\",\r\n",
      "    \"temperature\": 0.7,\r\n",
      "    \"max_tokens\": 256,\r\n",
      "    \"top_p\": 1.0,\r\n",
      "    \"frequency_penalty\": 0.0,\r\n",
      "    \"presence_penalty\": 0.0,\r\n",
      "    \"n\": 1,\r\n",
      "    \"best_of\": 1,\r\n",
      "    \"request_timeout\": null,\r\n",
      "    \"_type\": \"openai\"\r\n",
      "}"
     ]
    }
   ],
   "source": [
    "!cat llm.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9ab709fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = load_llm(\"llm.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "095b1d56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_type: openai\r\n",
      "best_of: 1\r\n",
      "frequency_penalty: 0.0\r\n",
      "max_tokens: 256\r\n",
      "model_name: text-davinci-003\r\n",
      "n: 1\r\n",
      "presence_penalty: 0.0\r\n",
      "request_timeout: null\r\n",
      "temperature: 0.7\r\n",
      "top_p: 1.0\r\n"
     ]
    }
   ],
   "source": [
    "!cat llm.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8cafaafe",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = load_llm(\"llm.yaml\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab3e4223",
   "metadata": {},
   "source": [
    "## Saving\n",
    "If you want to go from a LLM in memory to a serialized version of it, you can do so easily by calling the `.save` method. Again, this supports both json and yaml."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b38f685d",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm.save(\"llm.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b7365503",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm.save(\"llm.yaml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68e45b1c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
