{ "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", "\n", "
\n", "\n", "

MathPrompt

\n", "\n", "
\n", "
Input:
\n", "
\n", "
{'question': 'What is 10 + 12?'}\n",
       "
\n", "\n", "\n", "
\n", "\n", "
Full Prompt:
\n", "
\n", "
\n", " Prompt\n", "

Question:
What is 37593 * 67?
Code:
37593 * 67

Question:
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?
Code:
(16-3-4)*2

Question:
How many of the integers between 0 and 99 inclusive are divisible by 8?
Code:
count = 0
for i in range(0, 99+1):
if i % 8 == 0: count += 1

Question:
A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?
Code:
2 + 2/2

Question:

What is 10 + 12?

Code:

\n", "
\n", "
\n", "\n", "
Response:
\n", "
\n", " 10 + 12\n", "
\n", "\n", "
Value:
\n", "
\n", "
10 + 12\n",
       "
\n", "\n", "
\n", "
\n" ], "text/plain": [ "HTML(html='\\n\\n
\\n\\n

MathPrompt

\\n\\n
\\n
Input:
\\n
\\n
{'question': 'What is 10 + 12?'}\\n
\\n\\n\\n
\\n\\n
Full Prompt:
\\n
\\n
\\n Prompt\\n

Question:
What is 37593 * 67?
Code:
37593 * 67

Question:
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?
Code:
(16-3-4)*2

Question:
How many of the integers between 0 and 99 inclusive are divisible by 8?
Code:
count = 0
for i in range(0, 99+1):
if i % 8 == 0: count += 1

Question:
A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?
Code:
2 + 2/2

Question:

What is 10 + 12?

Code:

\\n
\\n
\\n\\n
Response:
\\n
\\n 10 + 12\\n
\\n\\n
Value:
\\n
\\n
10 + 12\\n
\\n\\n
\\n
\\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\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\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\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\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 }