|
21 | 21 | "from langchain import OpenAI"
|
22 | 22 | ]
|
23 | 23 | },
|
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "id": "9a58e15e", |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "id": "095adc76", |
| 37 | + "metadata": {}, |
| 38 | + "source": [ |
| 39 | + "## Math Prompt" |
| 40 | + ] |
| 41 | + }, |
24 | 42 | {
|
25 | 43 | "cell_type": "code",
|
26 | 44 | "execution_count": 2,
|
27 | 45 | "id": "beddcac7",
|
28 | 46 | "metadata": {},
|
29 | 47 | "outputs": [],
|
30 | 48 | "source": [
|
31 |
| - "llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)\n", |
32 | 49 | "pal_chain = PALChain.from_math_prompt(llm, verbose=True)"
|
33 | 50 | ]
|
34 | 51 | },
|
|
64 | 81 | " result = total_pets\n",
|
65 | 82 | " return result\u001b[0m\n",
|
66 | 83 | "\n",
|
67 |
| - "\u001b[1m> Finished PALChain chain.\u001b[0m\n" |
| 84 | + "\u001b[1m> Finished chain.\u001b[0m\n" |
68 | 85 | ]
|
69 | 86 | },
|
70 | 87 | {
|
|
82 | 99 | "pal_chain.run(question)"
|
83 | 100 | ]
|
84 | 101 | },
|
| 102 | + { |
| 103 | + "cell_type": "markdown", |
| 104 | + "id": "0269d20a", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "## Colored Objects" |
| 108 | + ] |
| 109 | + }, |
85 | 110 | {
|
86 | 111 | "cell_type": "code",
|
87 | 112 | "execution_count": 5,
|
88 | 113 | "id": "e524f81f",
|
89 | 114 | "metadata": {},
|
90 | 115 | "outputs": [],
|
91 | 116 | "source": [
|
92 |
| - "llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)\n", |
93 | 117 | "pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True)"
|
94 | 118 | ]
|
95 | 119 | },
|
|
147 | 171 | "pal_chain.run(question)"
|
148 | 172 | ]
|
149 | 173 | },
|
| 174 | + { |
| 175 | + "cell_type": "markdown", |
| 176 | + "id": "fc3d7f10", |
| 177 | + "metadata": {}, |
| 178 | + "source": [ |
| 179 | + "## Intermediate Steps\n", |
| 180 | + "You can also use the intermediate steps flag to return the code executed that generates the answer." |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": 5, |
| 186 | + "id": "9d2d9c61", |
| 187 | + "metadata": {}, |
| 188 | + "outputs": [], |
| 189 | + "source": [ |
| 190 | + "pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True, return_intermediate_steps=True)" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": 6, |
| 196 | + "id": "b29b971b", |
| 197 | + "metadata": {}, |
| 198 | + "outputs": [], |
| 199 | + "source": [ |
| 200 | + "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?\"" |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "code", |
| 205 | + "execution_count": 8, |
| 206 | + "id": "a2c40c28", |
| 207 | + "metadata": {}, |
| 208 | + "outputs": [ |
| 209 | + { |
| 210 | + "name": "stdout", |
| 211 | + "output_type": "stream", |
| 212 | + "text": [ |
| 213 | + "\n", |
| 214 | + "\n", |
| 215 | + "\u001b[1m> Entering new PALChain chain...\u001b[0m\n", |
| 216 | + "\u001b[32;1m\u001b[1;3m# Put objects into a list to record ordering\n", |
| 217 | + "objects = []\n", |
| 218 | + "objects += [('booklet', 'blue')] * 2\n", |
| 219 | + "objects += [('booklet', 'purple')] * 2\n", |
| 220 | + "objects += [('sunglasses', 'yellow')] * 2\n", |
| 221 | + "\n", |
| 222 | + "# Remove all pairs of sunglasses\n", |
| 223 | + "objects = [object for object in objects if object[0] != 'sunglasses']\n", |
| 224 | + "\n", |
| 225 | + "# Count number of purple objects\n", |
| 226 | + "num_purple = len([object for object in objects if object[1] == 'purple'])\n", |
| 227 | + "answer = num_purple\u001b[0m\n", |
| 228 | + "\n", |
| 229 | + "\u001b[1m> Finished chain.\u001b[0m\n" |
| 230 | + ] |
| 231 | + } |
| 232 | + ], |
| 233 | + "source": [ |
| 234 | + "result = pal_chain({\"question\": question})" |
| 235 | + ] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "code", |
| 239 | + "execution_count": 11, |
| 240 | + "id": "efddd033", |
| 241 | + "metadata": {}, |
| 242 | + "outputs": [ |
| 243 | + { |
| 244 | + "data": { |
| 245 | + "text/plain": [ |
| 246 | + "\"# 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\"" |
| 247 | + ] |
| 248 | + }, |
| 249 | + "execution_count": 11, |
| 250 | + "metadata": {}, |
| 251 | + "output_type": "execute_result" |
| 252 | + } |
| 253 | + ], |
| 254 | + "source": [ |
| 255 | + "result['intermediate_steps']" |
| 256 | + ] |
| 257 | + }, |
150 | 258 | {
|
151 | 259 | "cell_type": "code",
|
152 | 260 | "execution_count": null,
|
153 |
| - "id": "4ab20fec", |
| 261 | + "id": "dfd88594", |
154 | 262 | "metadata": {},
|
155 | 263 | "outputs": [],
|
156 | 264 | "source": []
|
|
0 commit comments