File size: 3,188 Bytes
7b856a8
 
 
 
f5ec828
872b692
f5ec828
 
 
 
872b692
f5ec828
 
 
 
 
872b692
7b856a8
f5ec828
7b856a8
 
 
4e3dc76
 
 
f5ec828
4e3dc76
 
 
7b856a8
 
 
 
f5ec828
872b692
7b856a8
 
 
 
 
4e3dc76
7b856a8
 
 
 
f5ec828
872b692
7b856a8
 
 
 
 
4e3dc76
 
 
7b856a8
 
 
4e3dc76
f5ec828
872b692
4e3dc76
 
 
 
7b856a8
4e3dc76
 
f5ec828
7b856a8
 
 
 
f5ec828
872b692
f5ec828
4e3dc76
7b856a8
4e3dc76
 
7b856a8
 
 
 
f5ec828
872b692
f5ec828
4e3dc76
7b856a8
4e3dc76
 
 
 
7b856a8
 
 
 
f5ec828
872b692
f5ec828
4e3dc76
7b856a8
4e3dc76
 
 
 
f5ec828
4e3dc76
7b856a8
 
 
 
f5ec828
872b692
f5ec828
 
7b856a8
4e3dc76
 
7b856a8
 
 
 
 
f5ec828
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0075889",
   "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": "033f7bd9",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "desc = \"\"\"\n",
    "### Prompt-aided Language Models\n",
    "\n",
    "Chain for answering complex problems by code generation and execution. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/srush/MiniChain/blob/master/examples/pal.ipynb)\n",
    "\n",
    "(Adapted from Prompt-aided Language Models [PAL](https://arxiv.org/pdf/2211.10435.pdf)).\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ada25bcd",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "from minichain import prompt, show, OpenAI, Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3249f5ac",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "@prompt(OpenAI(), template_file=\"pal.pmpt.tpl\")\n",
    "def pal_prompt(model, question):\n",
    "    return model(dict(question=question))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37f265c9",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "@prompt(Python())\n",
    "def python(model, inp):\n",
    "    return float(model(inp + \"\\nprint(solution())\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e767c1eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def pal(question):\n",
    "    return python(pal_prompt(question))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2a4f241",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"Melanie is a door-to-door saleswoman. She sold a third of her \" \\\n",
    "    \"vacuum cleaners at the green house, 2 more to the red house, and half of \" \\\n",
    "    \"what was left at the orange house. If Melanie has 5 vacuum cleaners left, \" \\\n",
    "    \"how many did she start with?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c22e7837",
   "metadata": {},
   "outputs": [],
   "source": [
    "gradio = show(pal,\n",
    "              examples=[question],\n",
    "              subprompts=[pal_prompt, python],\n",
    "              description=desc,\n",
    "              out_type=\"json\",\n",
    "              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8790a65e",
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    gradio.launch()"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}