File size: 3,497 Bytes
f5ec828 872b692 f5ec828 872b692 f5ec828 872b692 f5ec828 872b692 f5ec828 872b692 f5ec828 872b692 f5ec828 872b692 f5ec828 872b692 f5ec828 872b692 f5ec828 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "a914a238",
"metadata": {},
"outputs": [],
"source": [
"!pip install -q git+https://github.com/srush/MiniChain\n",
"!git clone https://github.com/srush/MiniChain; cp -fr MiniChain/examples/* . "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa7b8a98",
"metadata": {
"lines_to_next_cell": 2,
"tags": [
"hide_inp"
]
},
"outputs": [],
"source": [
"desc = \"\"\"\n",
"### Word Problem Solver\n",
"\n",
"Chain that solves a math word problem by first generating and then running Python code. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/srush/MiniChain/blob/master/examples/math_demo.ipynb)\n",
"\n",
"(Adapted from Dust [maths-generate-code](https://dust.tt/spolu/a/d12ac33169))\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a34507a",
"metadata": {},
"outputs": [],
"source": [
"from minichain import show, prompt, OpenAI, Python"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43cdbfc0",
"metadata": {
"lines_to_next_cell": 1
},
"outputs": [],
"source": [
"@prompt(OpenAI(), template_file=\"math.pmpt.tpl\")\n",
"def math_prompt(model, question):\n",
" \"Prompt to call GPT with a Jinja template\"\n",
" return model(dict(question=question))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14d6eb95",
"metadata": {
"lines_to_next_cell": 1
},
"outputs": [],
"source": [
"@prompt(Python(), template=\"import math\\n{{code}}\")\n",
"def python(model, code):\n",
" \"Prompt to call Python interpreter\"\n",
" code = \"\\n\".join(code.strip().split(\"\\n\")[1:-1])\n",
" return int(model(dict(code=code)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dac2ba43",
"metadata": {},
"outputs": [],
"source": [
"def math_demo(question):\n",
" \"Chain them together\"\n",
" return python(math_prompt(question))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a4f5bbf",
"metadata": {
"lines_to_next_cell": 0,
"tags": [
"hide_inp"
]
},
"outputs": [],
"source": [
"gradio = show(math_demo,\n",
" examples=[\"What is the sum of the powers of 3 (3^i) that are smaller than 100?\",\n",
" \"What is the sum of the 10 first positive integers?\",],\n",
" # \"Carla is downloading a 200 GB file. She can download 2 GB/minute, but 40% of the way through the download, the download fails. Then Carla has to restart the download from the beginning. How load did it take her to download the file in minutes?\"],\n",
" subprompts=[math_prompt, python],\n",
" out_type=\"json\",\n",
" description=desc,\n",
" )\n",
"if __name__ == \"__main__\":\n",
" gradio.launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a478587c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "tags,-all",
"main_language": "python",
"notebook_metadata_filter": "-all"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|