File size: 14,455 Bytes
7b856a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
292
293
294
295
296
297
298
299
300
301
302
303
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c0381b39",
   "metadata": {},
   "source": [
    "Notebook to answer a math problem with code.\n",
    "Adapted from Dust [maths-generate-code](https://dust.tt/spolu/a/d12ac33169)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d709d448",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-02-27T14:14:11.694164Z",
     "iopub.status.busy": "2023-02-27T14:14:11.693422Z",
     "iopub.status.idle": "2023-02-27T14:14:11.909671Z",
     "shell.execute_reply": "2023-02-27T14:14:11.908948Z"
    },
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "import minichain"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ae06a28",
   "metadata": {},
   "source": [
    "Prompt that asks LLM for code from math."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "89496133",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-02-27T14:14:11.914563Z",
     "iopub.status.busy": "2023-02-27T14:14:11.913355Z",
     "iopub.status.idle": "2023-02-27T14:14:11.918659Z",
     "shell.execute_reply": "2023-02-27T14:14:11.918034Z"
    },
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "class MathPrompt(minichain.TemplatePrompt[str]):\n",
    "    template_file = \"math.pmpt.tpl\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22169567",
   "metadata": {},
   "source": [
    "Ask a question and run it as python code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6e122554",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-02-27T14:14:11.923599Z",
     "iopub.status.busy": "2023-02-27T14:14:11.922332Z",
     "iopub.status.idle": "2023-02-27T14:14:14.957037Z",
     "shell.execute_reply": "2023-02-27T14:14:14.956368Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "121\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with minichain.start_chain(\"math\") as backend:\n",
    "    question = \"What is the sum of the powers of 3 (3^i) that are smaller than 100?\"\n",
    "    prompt = MathPrompt(backend.OpenAI()).chain(minichain.SimplePrompt(backend.Python()))\n",
    "    result = prompt({\"question\": question})\n",
    "    print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c41d0936",
   "metadata": {},
   "source": [
    "View the prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "df25f904",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-02-27T14:14:14.960159Z",
     "iopub.status.busy": "2023-02-27T14:14:14.959694Z",
     "iopub.status.idle": "2023-02-27T14:14:15.006679Z",
     "shell.execute_reply": "2023-02-27T14:14:15.006127Z"
    },
    "tags": [
     "hide_inp"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<!-- <link rel=\"stylesheet\" href=\"https://cdn.rawgit.com/Chalarangelo/mini.css/v3.0.1/dist/mini-default.min.css\"> -->\n",
       " <main class=\"container\">\n",
       "\n",
       "<h3>MathPrompt</h3>\n",
       "\n",
       "<dl>\n",
       "  <dt>Input:</dt>\n",
       "  <dd>\n",
       "<div class=\"highlight\"><pre><span></span><span class=\"p\">{</span><span class=\"s1\">&#39;question&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;What is 10 + 12?&#39;</span><span class=\"p\">}</span>\n",
       "</pre></div>\n",
       "\n",
       "\n",
       "  </dd>\n",
       "\n",
       "  <dt> Full Prompt: </dt>\n",
       "  <dd>\n",
       "    <details>\n",
       "      <summary>Prompt</summary>\n",
       "      <p>Question:<br>What is 37593 * 67?<br>Code:<br>37593 * 67<br><br>Question:<br>Janet's ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers' market?<br>Code:<br>(16-3-4)*2<br><br>Question:<br>How many of the integers between 0 and 99 inclusive are divisible by 8?<br>Code:<br>count = 0<br>for i in range(0, 99+1):<br>  if i % 8 == 0: count += 1<br><br>Question:<br>A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?<br>Code:<br>2 + 2/2<br><br>Question:<br><div style='color:red'>What is 10 + 12?</div><br>Code:</p>\n",
       "    </details>\n",
       "  </dd>\n",
       "\n",
       "  <dt> Response: </dt>\n",
       "  <dd>\n",
       "    10 + 12\n",
       "  </dd>\n",
       "\n",
       "  <dt>Value:</dt>\n",
       "  <dd>\n",
       "<div class=\"highlight\"><pre><span></span><span class=\"mi\">10</span> <span class=\"o\">+</span> <span class=\"mi\">12</span>\n",
       "</pre></div>\n",
       "\n",
       "  </dd>\n",
       "</main>\n"
      ],
      "text/plain": [
       "HTML(html='\\n<!-- <link rel=\"stylesheet\" href=\"https://cdn.rawgit.com/Chalarangelo/mini.css/v3.0.1/dist/mini-default.min.css\"> -->\\n <main class=\"container\">\\n\\n<h3>MathPrompt</h3>\\n\\n<dl>\\n  <dt>Input:</dt>\\n  <dd>\\n<div class=\"highlight\"><pre><span></span><span class=\"p\">{</span><span class=\"s1\">&#39;question&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;What is 10 + 12?&#39;</span><span class=\"p\">}</span>\\n</pre></div>\\n\\n\\n  </dd>\\n\\n  <dt> Full Prompt: </dt>\\n  <dd>\\n    <details>\\n      <summary>Prompt</summary>\\n      <p>Question:<br>What is 37593 * 67?<br>Code:<br>37593 * 67<br><br>Question:<br>Janet\\'s ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers\\' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers\\' market?<br>Code:<br>(16-3-4)*2<br><br>Question:<br>How many of the integers between 0 and 99 inclusive are divisible by 8?<br>Code:<br>count = 0<br>for i in range(0, 99+1):<br>  if i % 8 == 0: count += 1<br><br>Question:<br>A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?<br>Code:<br>2 + 2/2<br><br>Question:<br><div style=\\'color:red\\'>What is 10 + 12?</div><br>Code:</p>\\n    </details>\\n  </dd>\\n\\n  <dt> Response: </dt>\\n  <dd>\\n    10 + 12\\n  </dd>\\n\\n  <dt>Value:</dt>\\n  <dd>\\n<div class=\"highlight\"><pre><span></span><span class=\"mi\">10</span> <span class=\"o\">+</span> <span class=\"mi\">12</span>\\n</pre></div>\\n\\n  </dd>\\n</main>\\n')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MathPrompt().show({\"question\": \"What is 10 + 12?\"}, \"10 + 12\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f726907",
   "metadata": {},
   "source": [
    "View the log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0aa84914",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-02-27T14:14:15.009395Z",
     "iopub.status.busy": "2023-02-27T14:14:15.008955Z",
     "iopub.status.idle": "2023-02-27T14:14:15.032165Z",
     "shell.execute_reply": "2023-02-27T14:14:15.031585Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[38;5;15md962c2e7-4475-4094-bd1d-24f450acac26\u001b[1m\u001b[0m\n",
      "└── \u001b[38;5;5m<class '__main__.MathPrompt'>\u001b[0m/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:12Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m2.713s\u001b[2m\u001b[0m\n",
      "    ├── \u001b[38;5;5mInput Function\u001b[0m/2/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:12Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m0.002s\u001b[2m\u001b[0m\n",
      "    │   ├── \u001b[38;5;4minput\u001b[0m: \u001b[0m\n",
      "    │   │   └── \u001b[38;5;4mquestion\u001b[0m: What is the sum of the powers of 3 (3^i) that are smaller than 100?\u001b[0m\n",
      "    │   └── \u001b[38;5;5mInput Function\u001b[0m/2/2\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:12Z\u001b[2m\u001b[0m\n",
      "    ├── \u001b[38;5;5mPrompted\u001b[0m/3/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:12Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m2.711s\u001b[2m\u001b[0m\n",
      "    │   ├── \u001b[38;5;4mprompt\u001b[0m: Question:⏎\n",
      "    │   │   What is 37593 * 67?⏎\n",
      "    │   │   Code:⏎\n",
      "    │   │   37593 * 67⏎\n",
      "    │   │   ⏎\n",
      "    │   │   Question:⏎\n",
      "    │   │   Janet's ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers' market?⏎\n",
      "    │   │   Code:⏎\n",
      "    │   │   (16-3-4)*2⏎\n",
      "    │   │   ⏎\n",
      "    │   │   Question:⏎\n",
      "    │   │   How many of the integers between 0 and 99 inclusive are divisible by 8?⏎\n",
      "    │   │   Code:⏎\n",
      "    │   │   count = 0⏎\n",
      "    │   │   for i in range(0, 99+1):⏎\n",
      "    │   │     if i % 8 == 0: count += 1⏎\n",
      "    │   │   ⏎\n",
      "    │   │   Question:⏎\n",
      "    │   │   A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?⏎\n",
      "    │   │   Code:⏎\n",
      "    │   │   2 + 2/2⏎\n",
      "    │   │   ⏎\n",
      "    │   │   Question:⏎\n",
      "    │   │   What is the sum of the powers of 3 (3^i) that are smaller than 100?⏎\n",
      "    │   │   Code:\u001b[0m\n",
      "    │   └── \u001b[38;5;5mPrompted\u001b[0m/3/2\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m\n",
      "    ├── \u001b[38;5;5mResult\u001b[0m/4/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m0.000s\u001b[2m\u001b[0m\n",
      "    │   ├── \u001b[38;5;4mresult\u001b[0m: ⏎\n",
      "    │   │   sum = 0⏎\n",
      "    │   │   for i in range(0, 100):⏎\n",
      "    │   │     if 3**i < 100:⏎\n",
      "    │   │       sum += 3**i⏎\n",
      "    │   │   ⏎\n",
      "    │   │   print(sum)\u001b[0m\n",
      "    │   └── \u001b[38;5;5mResult\u001b[0m/4/2\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m\n",
      "    └── \u001b[38;5;5m<class '__main__.MathPrompt'>\u001b[0m/5\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m\n",
      "\n",
      "\u001b[38;5;15m3ce24477-fa41-4ca1-b11e-f7d253a7c511\u001b[1m\u001b[0m\n",
      "└── \u001b[38;5;5m<class 'minichain.prompts.SimplePrompt'>\u001b[0m/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m0.000s\u001b[2m\u001b[0m\n",
      "    ├── \u001b[38;5;5mInput Function\u001b[0m/2/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m0.000s\u001b[2m\u001b[0m\n",
      "    │   ├── \u001b[38;5;4minput\u001b[0m: ⏎\n",
      "    │   │   sum = 0⏎\n",
      "    │   │   for i in range(0, 100):⏎\n",
      "    │   │     if 3**i < 100:⏎\n",
      "    │   │       sum += 3**i⏎\n",
      "    │   │   ⏎\n",
      "    │   │   print(sum)\u001b[0m\n",
      "    │   └── \u001b[38;5;5mInput Function\u001b[0m/2/2\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m\n",
      "    ├── \u001b[38;5;5mPrompted\u001b[0m/3/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m0.000s\u001b[2m\u001b[0m\n",
      "    │   ├── \u001b[38;5;4mprompt\u001b[0m: ⏎\n",
      "    │   │   sum = 0⏎\n",
      "    │   │   for i in range(0, 100):⏎\n",
      "    │   │     if 3**i < 100:⏎\n",
      "    │   │       sum += 3**i⏎\n",
      "    │   │   ⏎\n",
      "    │   │   print(sum)\u001b[0m\n",
      "    │   └── \u001b[38;5;5mPrompted\u001b[0m/3/2\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m\n",
      "    ├── \u001b[38;5;5mResult\u001b[0m/4/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m0.000s\u001b[2m\u001b[0m\n",
      "    │   ├── \u001b[38;5;4mresult\u001b[0m: 121⏎\n",
      "    │   │   \u001b[0m\n",
      "    │   └── \u001b[38;5;5mResult\u001b[0m/4/2\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m\n",
      "    └── \u001b[38;5;5m<class 'minichain.prompts.SimplePrompt'>\u001b[0m/5\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m\n",
      "\n",
      "\u001b[38;5;15m4d75cc49-ca9f-4512-8ca0-9393732c7d14\u001b[1m\u001b[0m\n",
      "└── \u001b[38;5;5mmath\u001b[0m/1\u001b[0m ⇒ \u001b[38;5;2mstarted\u001b[0m \u001b[38;5;15m2023-02-27 14:14:11Z\u001b[2m\u001b[0m ⧖ \u001b[38;5;4m3.028s\u001b[2m\u001b[0m\n",
      "    └── \u001b[38;5;5mmath\u001b[0m/2\u001b[0m ⇒ \u001b[38;5;2msucceeded\u001b[0m \u001b[38;5;15m2023-02-27 14:14:14Z\u001b[2m\u001b[0m\n",
      "\n"
     ]
    }
   ],
   "source": [
    "minichain.show_log(\"math.log\")"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "tags,-all"
  },
  "kernelspec": {
   "display_name": "minichain",
   "language": "python",
   "name": "minichain"
  },
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}