Skip to content

Commit 66585d6

Browse files
updated some wording and typos. Also updated to not hard-code model name (#45)
* updated some wording and typos. Also updated to not hard-code model name * nbfmt and fixed chat notebook reference to text --------- Co-authored-by: Mark Daoust <[email protected]>
1 parent cadc8fa commit 66585d6

File tree

2 files changed

+105
-60
lines changed

2 files changed

+105
-60
lines changed

site/en/examples/chat_calculator.ipynb

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
{
1313
"cell_type": "code",
14-
"execution_count": 1,
14+
"execution_count": null,
1515
"metadata": {
1616
"cellView": "form",
1717
"id": "tuOe1ymfHZPu"
@@ -65,25 +65,43 @@
6565
"id": "D64fjiFDke_P"
6666
},
6767
"source": [
68-
"In some cases you might want to stop the generation.\n",
68+
"For some use cases, you may want to stop the generation from a model to insert specific results. For example, language models may have trouble with complicated arithmetic problems like word problems.\n",
69+
"This tutorial shows an example of using an external tool with the `palm.chat` method to output the correct answer to a word problem.\n",
6970
"\n",
70-
"For example, models like this may have trouble with harder arithmetic, like this version of the problem:"
71+
"This particular example uses the [`numexpr`](https://github.com/pydata/numexpr) tool to perform the arithmetic but you can use this same procedure to integrate other tools specific to your use case. The following is an outline of the steps:\n",
72+
"\n",
73+
"1. Determine a `start` and `end` tag to demarcate the text to send the tool.\n",
74+
"1. Create a prompt instructing the model how to use the tags in its response.\n",
75+
"1. From the model response, take the text between the `start` and `end` tags as input to the tool.\n",
76+
"1. Drop everything after the `end` tag.\n",
77+
"1. Run the tool and add it's output as your reply.\n",
78+
"1. The model will take into account the tools's output in its reply."
7179
]
7280
},
7381
{
7482
"cell_type": "code",
75-
"execution_count": 24,
83+
"execution_count": null,
7684
"metadata": {
7785
"id": "v8d0FtO2KJ3O"
7886
},
79-
"outputs": [],
87+
"outputs": [
88+
{
89+
"name": "stdout",
90+
"output_type": "stream",
91+
"text": [
92+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m122.2/122.2 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
93+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m113.3/113.3 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
94+
"\u001b[?25h"
95+
]
96+
}
97+
],
8098
"source": [
8199
"pip install -q google.generativeai"
82100
]
83101
},
84102
{
85103
"cell_type": "code",
86-
"execution_count": 26,
104+
"execution_count": null,
87105
"metadata": {
88106
"id": "TxbqsnIWRheU"
89107
},
@@ -94,7 +112,7 @@
94112
},
95113
{
96114
"cell_type": "code",
97-
"execution_count": 27,
115+
"execution_count": null,
98116
"metadata": {
99117
"id": "BH9oQzP2SD4L"
100118
},
@@ -113,7 +131,7 @@
113131
},
114132
{
115133
"cell_type": "code",
116-
"execution_count": 28,
134+
"execution_count": null,
117135
"metadata": {
118136
"id": "VQK6KRDpKD8g"
119137
},
@@ -125,7 +143,20 @@
125143
},
126144
{
127145
"cell_type": "code",
128-
"execution_count": 101,
146+
"execution_count": null,
147+
"metadata": {
148+
"id": "UGy1ghOp0ibu"
149+
},
150+
"outputs": [],
151+
"source": [
152+
"models = [m for m in palm.list_models() if 'generateMessage' in m.supported_generation_methods]\n",
153+
"model = models[0].name\n",
154+
"print(model)"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": null,
129160
"metadata": {
130161
"id": "1TIekNZ1rrOd"
131162
},
@@ -144,7 +175,7 @@
144175
},
145176
{
146177
"cell_type": "code",
147-
"execution_count": 81,
178+
"execution_count": null,
148179
"metadata": {
149180
"id": "IRoDEoU1Bq_6"
150181
},
@@ -177,8 +208,8 @@
177208
],
178209
"source": [
179210
"response = retry_chat(\n",
180-
" model=\"models/chat-bison-001\",\n",
181-
" context=\"You are an expert at solving word problems.\"\n",
211+
" model=model,\n",
212+
" context=\"You are an expert at solving word problems.\",\n",
182213
" messages=question,\n",
183214
")\n",
184215
"\n",
@@ -191,15 +222,15 @@
191222
"id": "zG4bukqCIQ4f"
192223
},
193224
"source": [
194-
"Just like that, it's usually incorrect.\n",
225+
"The prompt as is usually generates incorrect results.\n",
195226
"It generally gets the steps right but the arithmetic wrong.\n",
196227
"\n",
197228
"The answer should be:"
198229
]
199230
},
200231
{
201232
"cell_type": "code",
202-
"execution_count": 82,
233+
"execution_count": null,
203234
"metadata": {
204235
"id": "9Jw0L9OoCAHZ"
205236
},
@@ -226,12 +257,12 @@
226257
"id": "91LETezvs6LU"
227258
},
228259
"source": [
229-
"So give the model access to a calculator. You can do that by adding something like this to the prompt:"
260+
"In this next attempt, give the model instructions on how to access the calculator. You can do that by specifying a `start` and `end` tag the model can use to indicate where a calculation is needed. Add something like the following to the prompt:"
230261
]
231262
},
232263
{
233264
"cell_type": "code",
234-
"execution_count": 102,
265+
"execution_count": null,
235266
"metadata": {
236267
"id": "kjrZYUE7N5N6"
237268
},
@@ -271,7 +302,7 @@
271302
},
272303
{
273304
"cell_type": "code",
274-
"execution_count": 84,
305+
"execution_count": null,
275306
"metadata": {
276307
"id": "lbLNQfHVK0bO"
277308
},
@@ -304,7 +335,7 @@
304335
],
305336
"source": [
306337
"chat = retry_chat(\n",
307-
" model=\"models/chat-bison-001\",\n",
338+
" model=model,\n",
308339
" messages=calc_prompt,\n",
309340
")\n",
310341
"\n",
@@ -322,7 +353,7 @@
322353
},
323354
{
324355
"cell_type": "code",
325-
"execution_count": 85,
356+
"execution_count": null,
326357
"metadata": {
327358
"id": "K9Y0Z-Lome0P"
328359
},
@@ -352,7 +383,7 @@
352383
},
353384
{
354385
"cell_type": "code",
355-
"execution_count": 93,
386+
"execution_count": null,
356387
"metadata": {
357388
"id": "sGDNIvkkQjw8"
358389
},
@@ -384,7 +415,7 @@
384415
},
385416
{
386417
"cell_type": "code",
387-
"execution_count": 94,
418+
"execution_count": null,
388419
"metadata": {
389420
"id": "BC7TfMIu-9Ci"
390421
},
@@ -419,15 +450,15 @@
419450
},
420451
{
421452
"cell_type": "code",
422-
"execution_count": 98,
453+
"execution_count": null,
423454
"metadata": {
424455
"id": "PFXRsjvNMUyv"
425456
},
426457
"outputs": [],
427458
"source": [
428459
"def solve():\n",
429460
" chat = retry_chat(\n",
430-
" model=\"models/chat-bison-001\",\n",
461+
" model=model,\n",
431462
" context=\"You are an expert at solving word probles.\",\n",
432463
" messages=calc_prompt,\n",
433464
" )\n",
@@ -456,7 +487,7 @@
456487
},
457488
{
458489
"cell_type": "code",
459-
"execution_count": 103,
490+
"execution_count": null,
460491
"metadata": {
461492
"id": "Syf10WrLmopr"
462493
},
@@ -522,7 +553,7 @@
522553
},
523554
{
524555
"cell_type": "code",
525-
"execution_count": 104,
556+
"execution_count": null,
526557
"metadata": {
527558
"id": "jM-TRySbOz3k"
528559
},
@@ -713,7 +744,7 @@
713744
},
714745
{
715746
"cell_type": "code",
716-
"execution_count": 105,
747+
"execution_count": null,
717748
"metadata": {
718749
"id": "5vaOADwUWgQd"
719750
},

0 commit comments

Comments
 (0)