File size: 3,628 Bytes
cfd3735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73f9bf40",
   "metadata": {},
   "source": [
    "# How to serialize LLM classes\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 an 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 an 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.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}