File size: 7,312 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "32e022a2",
   "metadata": {},
   "source": [
    "# PAL\n",
    "\n",
    "Implements Program-Aided Language Models, as in https://arxiv.org/pdf/2211.10435.pdf.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1370e40f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import PALChain\n",
    "from langchain import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a58e15e",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(temperature=0, max_tokens=512)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "095adc76",
   "metadata": {},
   "source": [
    "## Math Prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "beddcac7",
   "metadata": {},
   "outputs": [],
   "source": [
    "pal_chain = PALChain.from_math_prompt(llm, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e2eab9d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3ef64b27",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new PALChain chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mdef solution():\n",
      "    \"\"\"Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?\"\"\"\n",
      "    cindy_pets = 4\n",
      "    marcia_pets = cindy_pets + 2\n",
      "    jan_pets = marcia_pets * 3\n",
      "    total_pets = cindy_pets + marcia_pets + jan_pets\n",
      "    result = total_pets\n",
      "    return result\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'28'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pal_chain.run(question)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0269d20a",
   "metadata": {},
   "source": [
    "## Colored Objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e524f81f",
   "metadata": {},
   "outputs": [],
   "source": [
    "pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "03a237b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses. If I remove all the pairs of sunglasses from the desk, how many purple items remain on it?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a84a4352",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new PALChain chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m# Put objects into a list to record ordering\n",
      "objects = []\n",
      "objects += [('booklet', 'blue')] * 2\n",
      "objects += [('booklet', 'purple')] * 2\n",
      "objects += [('sunglasses', 'yellow')] * 2\n",
      "\n",
      "# Remove all pairs of sunglasses\n",
      "objects = [object for object in objects if object[0] != 'sunglasses']\n",
      "\n",
      "# Count number of purple objects\n",
      "num_purple = len([object for object in objects if object[1] == 'purple'])\n",
      "answer = num_purple\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished PALChain chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'2'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pal_chain.run(question)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc3d7f10",
   "metadata": {},
   "source": [
    "## Intermediate Steps\n",
    "You can also use the intermediate steps flag to return the code executed that generates the answer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9d2d9c61",
   "metadata": {},
   "outputs": [],
   "source": [
    "pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True, return_intermediate_steps=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b29b971b",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses. If I remove all the pairs of sunglasses from the desk, how many purple items remain on it?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a2c40c28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new PALChain chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m# Put objects into a list to record ordering\n",
      "objects = []\n",
      "objects += [('booklet', 'blue')] * 2\n",
      "objects += [('booklet', 'purple')] * 2\n",
      "objects += [('sunglasses', 'yellow')] * 2\n",
      "\n",
      "# Remove all pairs of sunglasses\n",
      "objects = [object for object in objects if object[0] != 'sunglasses']\n",
      "\n",
      "# Count number of purple objects\n",
      "num_purple = len([object for object in objects if object[1] == 'purple'])\n",
      "answer = num_purple\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = pal_chain({\"question\": question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "efddd033",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"# Put objects into a list to record ordering\\nobjects = []\\nobjects += [('booklet', 'blue')] * 2\\nobjects += [('booklet', 'purple')] * 2\\nobjects += [('sunglasses', 'yellow')] * 2\\n\\n# Remove all pairs of sunglasses\\nobjects = [object for object in objects if object[0] != 'sunglasses']\\n\\n# Count number of purple objects\\nnum_purple = len([object for object in objects if object[1] == 'purple'])\\nanswer = num_purple\""
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['intermediate_steps']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfd88594",
   "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
}