File size: 36,030 Bytes
ade0520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "30e24ef3",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 如何让 Qwen-7b 使用 Langchain 中的 工具\n",
    "\n",
    "本文档主要介绍如何让千问调用 [LangChain](https://python.langchain.com/docs/get_started/introduction.html) 框架中实现好的谷歌搜索、 WolframAlpha 等工具。将主要基于 [ReAct Prompting](https://github.com/QwenLM/Qwen-7B/blob/main/examples/react_prompt.md) 技术,一种特殊的链式思考(Chain-of-Thought,简称 CoT)提示技巧,来实现这一目的。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "212979ec",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 安装依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e21c6728",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装千问的依赖\n",
    "!cd Qwen-7b\n",
    "!pip install -r requirements.txt\n",
    "\n",
    "# 安装 langchain 相关依赖\n",
    "!pip install langchain google-search-results wolframalpha arxiv;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b5e6ef9",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 第零步 - 导入 LangChain 的工具"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af7d0058",
   "metadata": {},
   "source": [
    "以下引入几个常用 APIs 作为演示:\n",
    " - [谷歌搜索API](https://serper.dev/?gclid=EAIaIQobChMIj9eqof7OgAMV44VbCh1F3QZoEAAYASABEgIh3fD_BwE#google-search-api)\n",
    " - [WolframAlpha](https://products.wolframalpha.com/api/)\n",
    " - arxiv论文搜索\n",
    " - python shell (需升级python至3.9以上使用)\n",
    "\n",
    "注1:此处推荐模仿此案例,细致地构造给千问看的工具描述。\n",
    "\n",
    "注2:谷歌搜索(SERPAPI), WolframAlpha 需自行申请它们的 API_KEY 后才能使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "07e49b98-9d6c-41f2-9b18-f043f2d13e1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import SerpAPIWrapper\n",
    "from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper\n",
    "from langchain.utilities import ArxivAPIWrapper\n",
    "from langchain.tools.python.tool import PythonAstREPLTool\n",
    "\n",
    "from typing import Dict, Tuple\n",
    "import os\n",
    "import json\n",
    "\n",
    "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
    "from transformers.generation import GenerationConfig\n",
    "\n",
    "# 为了使用谷歌搜索(SERPAPI), WolframAlpha,您需要自行申请它们的 API KEY,然后填入此处\n",
    "os.environ['SERPAPI_API_KEY'] = '重要!请在这里填入您的 SERPAPI_API_KEY!'\n",
    "os.environ['WOLFRAM_ALPHA_APPID'] = '重要!请在这里填入您的 WOLFRAM_ALPHA_APPID!'\n",
    "\n",
    "search = SerpAPIWrapper()\n",
    "WolframAlpha = WolframAlphaAPIWrapper()\n",
    "arxiv = ArxivAPIWrapper()\n",
    "python=PythonAstREPLTool()\n",
    "\n",
    "def tool_wrapper_for_qwen(tool):\n",
    "    def tool_(query):\n",
    "        query = json.loads(query)[\"query\"]\n",
    "        return tool.run(query)\n",
    "    return tool_\n",
    "\n",
    "# 以下是给千问看的工具描述:\n",
    "TOOLS = [\n",
    "    {\n",
    "        'name_for_human':\n",
    "            'google search',\n",
    "        'name_for_model':\n",
    "            'Search',\n",
    "        'description_for_model':\n",
    "            'useful for when you need to answer questions about current events.',\n",
    "        'parameters': [{\n",
    "            \"name\": \"query\",\n",
    "            \"type\": \"string\",\n",
    "            \"description\": \"search query of google\",\n",
    "            'required': True\n",
    "        }], \n",
    "        'tool_api': tool_wrapper_for_qwen(search)\n",
    "    },\n",
    "    {\n",
    "        'name_for_human':\n",
    "            'Wolfram Alpha',\n",
    "        'name_for_model':\n",
    "            'Math',\n",
    "        'description_for_model':\n",
    "            'Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life.',\n",
    "        'parameters': [{\n",
    "            \"name\": \"query\",\n",
    "            \"type\": \"string\",\n",
    "            \"description\": \"the problem to solved by Wolfram Alpha\",\n",
    "            'required': True\n",
    "        }], \n",
    "        'tool_api': tool_wrapper_for_qwen(WolframAlpha)\n",
    "    },  \n",
    "    {\n",
    "        'name_for_human':\n",
    "            'arxiv',\n",
    "        'name_for_model':\n",
    "            'Arxiv',\n",
    "        'description_for_model':\n",
    "            'A wrapper around Arxiv.org Useful for when you need to answer questions about Physics, Mathematics, Computer Science, Quantitative Biology, Quantitative Finance, Statistics, Electrical Engineering, and Economics from scientific articles on arxiv.org.',\n",
    "        'parameters': [{\n",
    "            \"name\": \"query\",\n",
    "            \"type\": \"string\",\n",
    "            \"description\": \"the document id of arxiv to search\",\n",
    "            'required': True\n",
    "        }], \n",
    "        'tool_api': tool_wrapper_for_qwen(arxiv)\n",
    "    },\n",
    "    {\n",
    "        'name_for_human':\n",
    "            'python',\n",
    "        'name_for_model':\n",
    "            'python',\n",
    "        'description_for_model':\n",
    "            \"A Python shell. Use this to execute python commands. When using this tool, sometimes output is abbreviated - Make sure it does not look abbreviated before using it in your answer. \"\n",
    "            \"Don't add comments to your python code.\",\n",
    "        'parameters': [{\n",
    "            \"name\": \"query\",\n",
    "            \"type\": \"string\",\n",
    "            \"description\": \"a valid python command.\",\n",
    "            'required': True\n",
    "        }],\n",
    "        'tool_api': tool_wrapper_for_qwen(python)\n",
    "    }\n",
    "\n",
    "]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7ec2027",
   "metadata": {},
   "source": [
    "## 第一步:让千问判断调用什么工具,生成工具入参"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a50d676",
   "metadata": {},
   "source": [
    "根据prompt模版、query、工具的信息构建prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4a8feb0e-22f7-4184-9ea0-b864812c9b09",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Answer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: Call this tool to interact with the google search API. What is the google search API useful for? useful for when you need to answer questions about current events. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"search query of google\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 加拿大2023年人口统计数字是多少?\n"
     ]
    }
   ],
   "source": [
    "TOOL_DESC = \"\"\"{name_for_model}: Call this tool to interact with the {name_for_human} API. What is the {name_for_human} API useful for? {description_for_model} Parameters: {parameters} Format the arguments as a JSON object.\"\"\"\n",
    "\n",
    "REACT_PROMPT = \"\"\"Answer the following questions as best you can. You have access to the following tools:\n",
    "\n",
    "{tool_descs}\n",
    "\n",
    "Use the following format:\n",
    "\n",
    "Question: the input question you must answer\n",
    "Thought: you should always think about what to do\n",
    "Action: the action to take, should be one of [{tool_names}]\n",
    "Action Input: the input to the action\n",
    "Observation: the result of the action\n",
    "... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\n",
    "Thought: I now know the final answer\n",
    "Final Answer: the final answer to the original input question\n",
    "\n",
    "Begin!\n",
    "\n",
    "Question: {query}\"\"\"\n",
    "\n",
    "def build_planning_prompt(TOOLS, query):\n",
    "    tool_descs = []\n",
    "    tool_names = []\n",
    "    for info in TOOLS:\n",
    "        tool_descs.append(\n",
    "            TOOL_DESC.format(\n",
    "                name_for_model=info['name_for_model'],\n",
    "                name_for_human=info['name_for_human'],\n",
    "                description_for_model=info['description_for_model'],\n",
    "                parameters=json.dumps(\n",
    "                    info['parameters'], ensure_ascii=False),\n",
    "            )\n",
    "        )\n",
    "        tool_names.append(info['name_for_model'])\n",
    "    tool_descs = '\\n\\n'.join(tool_descs)\n",
    "    tool_names = ','.join(tool_names)\n",
    "\n",
    "    prompt = REACT_PROMPT.format(tool_descs=tool_descs, tool_names=tool_names, query=query)\n",
    "    return prompt\n",
    "\n",
    "prompt_1 = build_planning_prompt(TOOLS[0:1], query=\"加拿大2023年人口统计数字是多少?\")\n",
    "print(prompt_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f22b002",
   "metadata": {},
   "source": [
    "将prompt作为输入获得response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f71b2577-118c-4ce2-a0ed-a45ec59ea35b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "A new version of the following files was downloaded from https://huggingface.co/Qwen/Qwen-7B-Chat:\n",
      "- tokenization_qwen.py\n",
      ". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n",
      "A new version of the following files was downloaded from https://huggingface.co/Qwen/Qwen-7B-Chat:\n",
      "- configuration_qwen.py\n",
      ". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n",
      "A new version of the following files was downloaded from https://huggingface.co/Qwen/Qwen-7B-Chat:\n",
      "- qwen_generation_utils.py\n",
      ". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n",
      "A new version of the following files was downloaded from https://huggingface.co/Qwen/Qwen-7B-Chat:\n",
      "- modeling_qwen.py\n",
      "- qwen_generation_utils.py\n",
      ". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "23435445dded44d6951aa6a7b771a963",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading shards:   0%|          | 0/8 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The model is automatically converting to bf16 for faster inference. If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\".\n",
      "Try importing flash-attention for faster inference...\n",
      "Warning: import flash_attn rotary fail, please install FlashAttention rotary to get higher efficiency https://github.com/Dao-AILab/flash-attention/tree/main/csrc/rotary\n",
      "Warning: import flash_attn rms_norm fail, please install FlashAttention layer_norm to get higher efficiency https://github.com/Dao-AILab/flash-attention/tree/main/csrc/layer_norm\n",
      "Warning: import flash_attn fail, please install FlashAttention to get higher efficiency https://github.com/Dao-AILab/flash-attention\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "728a1c13c2884291ade4cb4a1edfaaf2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/8 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 国内连 hugginface 网络不好,这段代码可能需要多重试\n",
    "checkpoint = \"Qwen/Qwen-7B-Chat\"\n",
    "TOKENIZER = AutoTokenizer.from_pretrained(checkpoint, trust_remote_code=True)\n",
    "MODEL = AutoModelForCausalLM.from_pretrained(checkpoint, device_map=\"auto\", trust_remote_code=True).eval()\n",
    "MODEL.generation_config = GenerationConfig.from_pretrained(checkpoint, trust_remote_code=True)\n",
    "MODEL.generation_config.do_sample = False  # greedy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dc0dbd6c-5a0f-44c9-a019-0ec0283ca92d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thought: 我应该使用搜索工具帮助我完成任务。search api能完成搜索的任务。\n",
      "Action: Search\n",
      "Action Input: {\"query\": \"加拿大 2023年人口统计数字\"}\n",
      "Observation:\n"
     ]
    }
   ],
   "source": [
    "stop = [\"Observation:\", \"Observation:\\n\"]\n",
    "react_stop_words_tokens = [TOKENIZER.encode(stop_) for stop_ in stop]\n",
    "response_1, _ = MODEL.chat(TOKENIZER, prompt_1, history=None, stop_words_ids=react_stop_words_tokens)\n",
    "print(response_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ebf47ac",
   "metadata": {},
   "source": [
    "## 第二步:从千问的输出中解析需要使用的工具和入参,并调用对应工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1a431670-a1f6-4afd-972f-1cfd6d06e8c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据加拿大统计局预测,加拿大人口今天(2023年6月16日)预计将超过4000万。 联邦统计局使用模型来实时估计加拿大的人口,该计数模型预计加拿大人口将在北美东部时间今天下午3点前达到4000万。 加拿大的人口增长率目前为2.7%。\n"
     ]
    }
   ],
   "source": [
    "def parse_latest_plugin_call(text: str) -> Tuple[str, str]:\n",
    "    i = text.rfind('\\nAction:')\n",
    "    j = text.rfind('\\nAction Input:')\n",
    "    k = text.rfind('\\nObservation:')\n",
    "    if 0 <= i < j:  # If the text has `Action` and `Action input`,\n",
    "        if k < j:  # but does not contain `Observation`,\n",
    "            # then it is likely that `Observation` is ommited by the LLM,\n",
    "            # because the output text may have discarded the stop word.\n",
    "            text = text.rstrip() + '\\nObservation:'  # Add it back.\n",
    "            k = text.rfind('\\nObservation:')\n",
    "    if 0 <= i < j < k:\n",
    "        plugin_name = text[i + len('\\nAction:'):j].strip()\n",
    "        plugin_args = text[j + len('\\nAction Input:'):k].strip()\n",
    "        return plugin_name, plugin_args\n",
    "    return '', ''\n",
    "\n",
    "def use_api(tools, response):\n",
    "    use_toolname, action_input = parse_latest_plugin_call(response)\n",
    "    if use_toolname == \"\":\n",
    "        return \"no tool founds\"\n",
    "\n",
    "    used_tool_meta = list(filter(lambda x: x[\"name_for_model\"] == use_toolname, tools))\n",
    "    if len(used_tool_meta) == 0:\n",
    "        return \"no tool founds\"\n",
    "    \n",
    "    api_output = used_tool_meta[0][\"tool_api\"](action_input)\n",
    "    return api_output\n",
    "\n",
    "api_output = use_api(TOOLS, response_1)\n",
    "print(api_output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "106a4ba0",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 第三步:让千问根据工具返回结果继续作答\n",
    "拼接上述返回答案,形成新的prompt,并获得生成最终结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a9d4d42d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Answer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: Call this tool to interact with the google search API. What is the google search API useful for? useful for when you need to answer questions about current events. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"search query of google\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 加拿大2023年人口统计数字是多少?Thought: 我应该使用搜索工具帮助我完成任务。search api能完成搜索的任务。\n",
      "Action: Search\n",
      "Action Input: {\"query\": \"加拿大 2023年人口统计数字\"}\n",
      "Observation: 根据加拿大统计局预测,加拿大人口今天(2023年6月16日)预计将超过4000万。 联邦统计局使用模型来实时估计加拿大的人口,该计数模型预计加拿大人口将在北美东部时间今天下午3点前达到4000万。 加拿大的人口增长率目前为2.7%。 Thought: I now know the final answer.\n",
      "Final Answer: 加拿大2023年人口统计数字预计为4000万。\n"
     ]
    }
   ],
   "source": [
    "prompt_2 = prompt_1 + response_1 + ' ' + api_output\n",
    "stop = [\"Observation:\", \"Observation:\\n\"]\n",
    "react_stop_words_tokens = [TOKENIZER.encode(stop_) for stop_ in stop]\n",
    "response_2, _ = MODEL.chat(TOKENIZER, prompt_2, history=None, stop_words_ids=react_stop_words_tokens)\n",
    "print(prompt_2, response_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b8da9fd",
   "metadata": {},
   "source": [
    "## 总结 - 串联起整个流程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1e51a8ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "def main(query, choose_tools):\n",
    "    prompt = build_planning_prompt(choose_tools, query) # 组织prompt\n",
    "    print(prompt)\n",
    "    stop = [\"Observation:\", \"Observation:\\n\"]\n",
    "    react_stop_words_tokens = [TOKENIZER.encode(stop_) for stop_ in stop]\n",
    "    response, _ = MODEL.chat(TOKENIZER, prompt, history=None, stop_words_ids=react_stop_words_tokens)\n",
    "\n",
    "    while \"Final Answer:\" not in response: # 出现final Answer时结束\n",
    "        api_output = use_api(choose_tools, response) # 抽取入参并执行api\n",
    "        api_output = str(api_output) # 部分api工具返回结果非字符串格式需进行转化后输出\n",
    "        if \"no tool founds\" == api_output:\n",
    "            break\n",
    "        print(\"\\033[32m\" + response + \"\\033[0m\" + \"\\033[34m\" + ' ' + api_output + \"\\033[0m\")\n",
    "        prompt = prompt + response + ' ' + api_output # 合并api输出\n",
    "        response, _ = MODEL.chat(TOKENIZER, prompt, history=None, stop_words_ids=react_stop_words_tokens) # 继续生成\n",
    "\n",
    "    print(\"\\033[32m\" + response + \"\\033[0m\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6dc38a34",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==========\n",
      "Answer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: Call this tool to interact with the google search API. What is the google search API useful for? useful for when you need to answer questions about current events. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"search query of google\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Math: Call this tool to interact with the Wolfram Alpha API. What is the Wolfram Alpha API useful for? Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"the problem to solved by Wolfram Alpha\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Arxiv: Call this tool to interact with the arxiv API. What is the arxiv API useful for? A wrapper around Arxiv.org Useful for when you need to answer questions about Physics, Mathematics, Computer Science, Quantitative Biology, Quantitative Finance, Statistics, Electrical Engineering, and Economics from scientific articles on arxiv.org. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"the document id of arxiv to search\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "python: Call this tool to interact with the python API. What is the python API useful for? A Python shell. Use this to execute python commands. When using this tool, sometimes output is abbreviated - Make sure it does not look abbreviated before using it in your answer. Don't add comments to your python code. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"a valid python command.\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search,Math,Arxiv,python]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 加拿大2022年的人口数量有多少?\n",
      "\u001B[32mThought: 我应该使用搜索工具帮助我完成任务。search api能完成搜索的任务。\n",
      "Action: Search\n",
      "Action Input: {\"query\": \"加拿大 2022年人口数量\"}\n",
      "Observation:\u001B[0m\u001B[34m 中新社多伦多3月22日电(记者余瑞冬)加拿大统计局3月22日公布的人口统计数据显示,截至今年1月1日,该国估算总人口约为3956.62万人,且2022年的人口增长数创纪录地突破100万人。 加统计局估算,该国人口在2022年增长105.011万人,年增长2.7%,创1957年以来最大增幅。\u001B[0m\n",
      "\u001B[32mThought: I now know the final answer.\n",
      "Final Answer: 加拿大2022年的人口数量约为3956.62万人。\u001B[0m\n",
      "==========\n",
      "Answer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: Call this tool to interact with the google search API. What is the google search API useful for? useful for when you need to answer questions about current events. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"search query of google\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Math: Call this tool to interact with the Wolfram Alpha API. What is the Wolfram Alpha API useful for? Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"the problem to solved by Wolfram Alpha\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Arxiv: Call this tool to interact with the arxiv API. What is the arxiv API useful for? A wrapper around Arxiv.org Useful for when you need to answer questions about Physics, Mathematics, Computer Science, Quantitative Biology, Quantitative Finance, Statistics, Electrical Engineering, and Economics from scientific articles on arxiv.org. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"the document id of arxiv to search\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "python: Call this tool to interact with the python API. What is the python API useful for? A Python shell. Use this to execute python commands. When using this tool, sometimes output is abbreviated - Make sure it does not look abbreviated before using it in your answer. Don't add comments to your python code. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"a valid python command.\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search,Math,Arxiv,python]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 求解方程 2x+5 = -3x + 7\n",
      "\u001B[32mThought: 我应该使用数学工具帮助我完成任务。Wolfram Alpha API应该能完成这项任务。\n",
      "Action: Math\n",
      "Action Input: {\"query\": \"2x+5 = -3x + 7\"}\n",
      "Observation:\u001B[0m\u001B[34m Assumption: 2 x + 5 = -3 x + 7 \n",
      "Answer: x = 2/5\u001B[0m\n",
      "\u001B[32mThought: I now know the final answer.\n",
      "Final Answer: x = 2/5\u001B[0m\n",
      "==========\n",
      "Answer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: Call this tool to interact with the google search API. What is the google search API useful for? useful for when you need to answer questions about current events. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"search query of google\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Math: Call this tool to interact with the Wolfram Alpha API. What is the Wolfram Alpha API useful for? Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"the problem to solved by Wolfram Alpha\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Arxiv: Call this tool to interact with the arxiv API. What is the arxiv API useful for? A wrapper around Arxiv.org Useful for when you need to answer questions about Physics, Mathematics, Computer Science, Quantitative Biology, Quantitative Finance, Statistics, Electrical Engineering, and Economics from scientific articles on arxiv.org. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"the document id of arxiv to search\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "python: Call this tool to interact with the python API. What is the python API useful for? A Python shell. Use this to execute python commands. When using this tool, sometimes output is abbreviated - Make sure it does not look abbreviated before using it in your answer. Don't add comments to your python code. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"a valid python command.\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search,Math,Arxiv,python]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 编号是1605.08386的论文讲了些什么?\n",
      "\u001B[32mThought: 我需要使用Arxiv API来搜索这篇论文。\n",
      "Action: Arxiv\n",
      "Action Input: {\"query\": \"1605.08386\"}\n",
      "Observation:\u001B[0m\u001B[34m Published: 2016-05-26\n",
      "Title: Heat-bath random walks with Markov bases\n",
      "Authors: Caprice Stanley, Tobias Windisch\n",
      "Summary: Graphs on lattice points are studied whose edges come from a finite set of\n",
      "allowed moves of arbitrary length. We show that the diameter of these graphs on\n",
      "fibers of a fixed integer matrix can be bounded from above by a constant. We\n",
      "then study the mixing behaviour of heat-bath random walks on these graphs. We\n",
      "also state explicit conditions on the set of moves so that the heat-bath random\n",
      "walk, a generalization of the Glauber dynamics, is an expander in fixed\n",
      "dimension.\u001B[0m\n",
      "\u001B[32mThought: I now know the final answer.\n",
      "Final Answer: 这篇论文的题目是《热浴随机游走的马尔可夫基》,作者是Caprice Stanley和Tobias Windisch。摘要中提到,该论文研究了在有限的允许移动集合中,由任意长度的边构成的图的边。我们证明了这些图在固定整数矩阵纤维上的直径可以被一个常数所限制。然后,我们研究了热浴随机游走在这类图上的混合行为。我们还给出了一个明确的条件,使得热浴随机游走(一个Glauber动力学的推广)在固定维度下是一个扩张。\u001B[0m\n",
      "==========\n",
      "Answer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: Call this tool to interact with the google search API. What is the google search API useful for? useful for when you need to answer questions about current events. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"search query of google\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Math: Call this tool to interact with the Wolfram Alpha API. What is the Wolfram Alpha API useful for? Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"the problem to solved by Wolfram Alpha\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Arxiv: Call this tool to interact with the arxiv API. What is the arxiv API useful for? A wrapper around Arxiv.org Useful for when you need to answer questions about Physics, Mathematics, Computer Science, Quantitative Biology, Quantitative Finance, Statistics, Electrical Engineering, and Economics from scientific articles on arxiv.org. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"the document id of arxiv to search\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "python: Call this tool to interact with the python API. What is the python API useful for? A Python shell. Use this to execute python commands. When using this tool, sometimes output is abbreviated - Make sure it does not look abbreviated before using it in your answer. Don't add comments to your python code. Parameters: [{\"name\": \"query\", \"type\": \"string\", \"description\": \"a valid python command.\", \"required\": true}] Format the arguments as a JSON object.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search,Math,Arxiv,python]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 使用python对下面的列表进行排序: [2, 4135, 523, 2, 3]\n",
      "\u001B[32mThought: 我应该使用python API来执行python命令。\n",
      "Action: python\n",
      "Action Input: {\"query\": \"sorted([2, 4135, 523, 2, 3])\"}\n",
      "Observation:\u001B[0m\u001B[34m [2, 2, 3, 523, 4135]\u001B[0m\n",
      "\u001B[32mThought: I now know the final answer.\n",
      "Final Answer: 使用python对给定的列表进行排序,结果为 [2, 2, 3, 523, 4135]。\u001B[0m\n"
     ]
    }
   ],
   "source": [
    "# 请尽可能控制备选工具数量\n",
    "query = \"加拿大2022年的人口数量有多少?\" # 所提问题\n",
    "choose_tools = TOOLS # 选择备选工具\n",
    "print(\"=\" * 10)\n",
    "main(query, choose_tools)\n",
    "\n",
    "query = \"求解方程 2x+5 = -3x + 7\" # 所提问题\n",
    "choose_tools = TOOLS # 选择备选工具\n",
    "print(\"=\" * 10)\n",
    "main(query, choose_tools)\n",
    "\n",
    "query = \"编号是1605.08386的论文讲了些什么?\" # 所提问题\n",
    "choose_tools = TOOLS # 选择备选工具\n",
    "print(\"=\" * 10)\n",
    "main(query, choose_tools)\n",
    "\n",
    "query =\"使用python对下面的列表进行排序: [2, 4135, 523, 2, 3]\"\n",
    "choose_tools = TOOLS # 选择备选工具\n",
    "print(\"=\" * 10)\n",
    "main(query, choose_tools)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}