|
132 | 132 | },
|
133 | 133 | {
|
134 | 134 | "cell_type": "code",
|
135 |
| - "execution_count": 3, |
136 | 135 | "id": "eb420fcd-1a4d-4cc8-90cf-39a92cf28060",
|
137 |
| - "metadata": {}, |
138 |
| - "outputs": [], |
| 136 | + "metadata": { |
| 137 | + "jupyter": { |
| 138 | + "is_executing": true |
| 139 | + } |
| 140 | + }, |
139 | 141 | "source": [
|
140 | 142 | "# 使用了retry库,指定在请求失败时的重试策略。\n",
|
141 | 143 | "# 这里设定的是指数等待(wait_random_exponential),时间间隔的最大值为40秒,并且最多重试3次(stop_after_attempt(3))。\n",
|
142 | 144 | "# 定义一个函数chat_completion_request,主要用于发送 聊天补全 请求到OpenAI服务器\n",
|
| 145 | + "\n", |
| 146 | + "#### 【新增】\n", |
| 147 | + "# 智谱模型 Function Calling\n", |
| 148 | + "# 参考: https://www.bigmodel.cn/dev/howuse/functioncall\n", |
| 149 | + "\n", |
| 150 | + "# deepseek-chat 模型 Function Calling\n", |
| 151 | + "# 注意:当前版本 deepseek-chat 模型 Function Calling 功能效果不稳定,会出现循环调用、空回复的情况。\n", |
| 152 | + "# 参考: https://api-docs.deepseek.com/zh-cn/guides/function_calling\n", |
| 153 | + "\n", |
| 154 | + "### 使用 智谱模型 Function Calling 和 deepseek-chat 模型 Function Calling 注意替换模型名称,调用地址,和API KEY\n", |
143 | 155 | "@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))\n",
|
144 | 156 | "def chat_completion_request(messages, functions=None, function_call=None, model=GPT_MODEL):\n",
|
145 | 157 | "\n",
|
|
175 | 187 | " print(\"Unable to generate ChatCompletion response\")\n",
|
176 | 188 | " print(f\"Exception: {e}\")\n",
|
177 | 189 | " return e\n"
|
178 |
| - ] |
| 190 | + ], |
| 191 | + "outputs": [], |
| 192 | + "execution_count": null |
179 | 193 | },
|
180 | 194 | {
|
181 | 195 | "cell_type": "code",
|
|
328 | 342 | "name": "stdout",
|
329 | 343 | "output_type": "stream",
|
330 | 344 | "text": [
|
331 |
| - "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
332 |
| - "\u001b[0m\n", |
333 |
| - "\u001b[32muser: What's the weather like today\n", |
334 |
| - "\u001b[0m\n", |
335 |
| - "\u001b[34massistant[content]: Sure, could you please provide me with the location for which you would like to know the current weather?\n", |
336 |
| - "\u001b[0m\n" |
| 345 | + "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
| 346 | + "\u001B[0m\n", |
| 347 | + "\u001B[32muser: What's the weather like today\n", |
| 348 | + "\u001B[0m\n", |
| 349 | + "\u001B[34massistant[content]: Sure, could you please provide me with the location for which you would like to know the current weather?\n", |
| 350 | + "\u001B[0m\n" |
337 | 351 | ]
|
338 | 352 | }
|
339 | 353 | ],
|
|
429 | 443 | "name": "stdout",
|
430 | 444 | "output_type": "stream",
|
431 | 445 | "text": [
|
432 |
| - "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
433 |
| - "\u001b[0m\n", |
434 |
| - "\u001b[32muser: What's the weather like today\n", |
435 |
| - "\u001b[0m\n", |
436 |
| - "\u001b[34massistant[content]: Sure, could you please provide me with the location for which you would like to know the current weather?\n", |
437 |
| - "\u001b[0m\n", |
438 |
| - "\u001b[32muser: I'm in Shanghai, China.\n", |
439 |
| - "\u001b[0m\n", |
440 |
| - "\u001b[34massistant[function_call]: {'name': 'get_current_weather', 'arguments': '{\"location\":\"Shanghai, China\",\"format\":\"celsius\"}'}\n", |
441 |
| - "\u001b[0m\n" |
| 446 | + "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
| 447 | + "\u001B[0m\n", |
| 448 | + "\u001B[32muser: What's the weather like today\n", |
| 449 | + "\u001B[0m\n", |
| 450 | + "\u001B[34massistant[content]: Sure, could you please provide me with the location for which you would like to know the current weather?\n", |
| 451 | + "\u001B[0m\n", |
| 452 | + "\u001B[32muser: I'm in Shanghai, China.\n", |
| 453 | + "\u001B[0m\n", |
| 454 | + "\u001B[34massistant[function_call]: {'name': 'get_current_weather', 'arguments': '{\"location\":\"Shanghai, China\",\"format\":\"celsius\"}'}\n", |
| 455 | + "\u001B[0m\n" |
442 | 456 | ]
|
443 | 457 | }
|
444 | 458 | ],
|
|
488 | 502 | "name": "stdout",
|
489 | 503 | "output_type": "stream",
|
490 | 504 | "text": [
|
491 |
| - "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
492 |
| - "\u001b[0m\n", |
493 |
| - "\u001b[32muser: what is the weather going to be like in Shanghai, China over the next x days\n", |
494 |
| - "\u001b[0m\n", |
495 |
| - "\u001b[34massistant[content]: Sure! Please provide the number of days you would like to know the weather forecast for in Shanghai, China.\n", |
496 |
| - "\u001b[0m\n" |
| 505 | + "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
| 506 | + "\u001B[0m\n", |
| 507 | + "\u001B[32muser: what is the weather going to be like in Shanghai, China over the next x days\n", |
| 508 | + "\u001B[0m\n", |
| 509 | + "\u001B[34massistant[content]: Sure! Please provide the number of days you would like to know the weather forecast for in Shanghai, China.\n", |
| 510 | + "\u001B[0m\n" |
497 | 511 | ]
|
498 | 512 | }
|
499 | 513 | ],
|
|
555 | 569 | "name": "stdout",
|
556 | 570 | "output_type": "stream",
|
557 | 571 | "text": [
|
558 |
| - "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
559 |
| - "\u001b[0m\n", |
560 |
| - "\u001b[32muser: what is the weather going to be like in Shanghai, China over the next x days\n", |
561 |
| - "\u001b[0m\n", |
562 |
| - "\u001b[34massistant[content]: Sure! Please provide the number of days you would like to know the weather forecast for in Shanghai, China.\n", |
563 |
| - "\u001b[0m\n", |
564 |
| - "\u001b[32muser: 5 days\n", |
565 |
| - "\u001b[0m\n", |
566 |
| - "\u001b[34massistant[function_call]: {'name': 'get_n_day_weather_forecast', 'arguments': '{\"location\":\"Shanghai, China\",\"format\":\"celsius\",\"num_days\":5}'}\n", |
567 |
| - "\u001b[0m\n" |
| 572 | + "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
| 573 | + "\u001B[0m\n", |
| 574 | + "\u001B[32muser: what is the weather going to be like in Shanghai, China over the next x days\n", |
| 575 | + "\u001B[0m\n", |
| 576 | + "\u001B[34massistant[content]: Sure! Please provide the number of days you would like to know the weather forecast for in Shanghai, China.\n", |
| 577 | + "\u001B[0m\n", |
| 578 | + "\u001B[32muser: 5 days\n", |
| 579 | + "\u001B[0m\n", |
| 580 | + "\u001B[34massistant[function_call]: {'name': 'get_n_day_weather_forecast', 'arguments': '{\"location\":\"Shanghai, China\",\"format\":\"celsius\",\"num_days\":5}'}\n", |
| 581 | + "\u001B[0m\n" |
568 | 582 | ]
|
569 | 583 | }
|
570 | 584 | ],
|
|
620 | 634 | "name": "stdout",
|
621 | 635 | "output_type": "stream",
|
622 | 636 | "text": [
|
623 |
| - "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
624 |
| - "\u001b[0m\n", |
625 |
| - "\u001b[32muser: Give me a weather report for San Diego, USA.\n", |
626 |
| - "\u001b[0m\n", |
627 |
| - "\u001b[34massistant[function_call]: {'name': 'get_n_day_weather_forecast', 'arguments': '{\"location\":\"San Diego, USA\",\"format\":\"celsius\",\"num_days\":1}'}\n", |
628 |
| - "\u001b[0m\n" |
| 637 | + "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
| 638 | + "\u001B[0m\n", |
| 639 | + "\u001B[32muser: Give me a weather report for San Diego, USA.\n", |
| 640 | + "\u001B[0m\n", |
| 641 | + "\u001B[34massistant[function_call]: {'name': 'get_n_day_weather_forecast', 'arguments': '{\"location\":\"San Diego, USA\",\"format\":\"celsius\",\"num_days\":1}'}\n", |
| 642 | + "\u001B[0m\n" |
629 | 643 | ]
|
630 | 644 | }
|
631 | 645 | ],
|
|
678 | 692 | "name": "stdout",
|
679 | 693 | "output_type": "stream",
|
680 | 694 | "text": [
|
681 |
| - "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
682 |
| - "\u001b[0m\n", |
683 |
| - "\u001b[32muser: Give me a weather report for San Diego, USA.\n", |
684 |
| - "\u001b[0m\n", |
685 |
| - "\u001b[34massistant[function_call]: {'name': 'get_current_weather', 'arguments': '{\"location\":\"San Diego, USA\",\"format\":\"celsius\"}'}\n", |
686 |
| - "\u001b[0m\n" |
| 695 | + "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
| 696 | + "\u001B[0m\n", |
| 697 | + "\u001B[32muser: Give me a weather report for San Diego, USA.\n", |
| 698 | + "\u001B[0m\n", |
| 699 | + "\u001B[34massistant[function_call]: {'name': 'get_current_weather', 'arguments': '{\"location\":\"San Diego, USA\",\"format\":\"celsius\"}'}\n", |
| 700 | + "\u001B[0m\n" |
687 | 701 | ]
|
688 | 702 | }
|
689 | 703 | ],
|
|
745 | 759 | "name": "stdout",
|
746 | 760 | "output_type": "stream",
|
747 | 761 | "text": [
|
748 |
| - "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
749 |
| - "\u001b[0m\n", |
750 |
| - "\u001b[32muser: Give me the current weather (use Celcius) for Toronto, Canada.\n", |
751 |
| - "\u001b[0m\n", |
752 |
| - "\u001b[34massistant[content]: Sure! Let me retrieve the current weather for Toronto, Canada in Celsius.\n", |
753 |
| - "\u001b[0m\n" |
| 762 | + "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", |
| 763 | + "\u001B[0m\n", |
| 764 | + "\u001B[32muser: Give me the current weather (use Celcius) for Toronto, Canada.\n", |
| 765 | + "\u001B[0m\n", |
| 766 | + "\u001B[34massistant[content]: Sure! Let me retrieve the current weather for Toronto, Canada in Celsius.\n", |
| 767 | + "\u001B[0m\n" |
754 | 768 | ]
|
755 | 769 | }
|
756 | 770 | ],
|
|
1105 | 1119 | "name": "stdout",
|
1106 | 1120 | "output_type": "stream",
|
1107 | 1121 | "text": [
|
1108 |
| - "\u001b[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n", |
1109 |
| - "\u001b[0m\n", |
1110 |
| - "\u001b[32muser: Hi, who are the top 5 artists by number of tracks?\n", |
1111 |
| - "\u001b[0m\n", |
1112 |
| - "\u001b[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT artists.Name, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM artists\\\\nJOIN albums ON artists.ArtistId = albums.ArtistId\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY artists.Name\\\\nORDER BY TrackCount DESC\\\\nLIMIT 5;\"}'}\n", |
1113 |
| - "\u001b[0m\n", |
1114 |
| - "\u001b[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n", |
1115 |
| - "\u001b[0m\n" |
| 1122 | + "\u001B[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n", |
| 1123 | + "\u001B[0m\n", |
| 1124 | + "\u001B[32muser: Hi, who are the top 5 artists by number of tracks?\n", |
| 1125 | + "\u001B[0m\n", |
| 1126 | + "\u001B[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT artists.Name, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM artists\\\\nJOIN albums ON artists.ArtistId = albums.ArtistId\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY artists.Name\\\\nORDER BY TrackCount DESC\\\\nLIMIT 5;\"}'}\n", |
| 1127 | + "\u001B[0m\n", |
| 1128 | + "\u001B[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n", |
| 1129 | + "\u001B[0m\n" |
1116 | 1130 | ]
|
1117 | 1131 | }
|
1118 | 1132 | ],
|
|
1240 | 1254 | "name": "stdout",
|
1241 | 1255 | "output_type": "stream",
|
1242 | 1256 | "text": [
|
1243 |
| - "\u001b[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n", |
1244 |
| - "\u001b[0m\n", |
1245 |
| - "\u001b[32muser: Hi, who are the top 5 artists by number of tracks?\n", |
1246 |
| - "\u001b[0m\n", |
1247 |
| - "\u001b[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT artists.Name, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM artists\\\\nJOIN albums ON artists.ArtistId = albums.ArtistId\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY artists.Name\\\\nORDER BY TrackCount DESC\\\\nLIMIT 5;\"}'}\n", |
1248 |
| - "\u001b[0m\n", |
1249 |
| - "\u001b[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n", |
1250 |
| - "\u001b[0m\n", |
1251 |
| - "\u001b[32muser: What is the name of the album with the most tracks?\n", |
1252 |
| - "\u001b[0m\n", |
1253 |
| - "\u001b[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT albums.Title, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM albums\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY albums.Title\\\\nORDER BY TrackCount DESC\\\\nLIMIT 1;\"}'}\n", |
1254 |
| - "\u001b[0m\n", |
1255 |
| - "\u001b[35mfunction (ask_database): [('Greatest Hits', 57)]\n", |
1256 |
| - "\u001b[0m\n" |
| 1257 | + "\u001B[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n", |
| 1258 | + "\u001B[0m\n", |
| 1259 | + "\u001B[32muser: Hi, who are the top 5 artists by number of tracks?\n", |
| 1260 | + "\u001B[0m\n", |
| 1261 | + "\u001B[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT artists.Name, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM artists\\\\nJOIN albums ON artists.ArtistId = albums.ArtistId\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY artists.Name\\\\nORDER BY TrackCount DESC\\\\nLIMIT 5;\"}'}\n", |
| 1262 | + "\u001B[0m\n", |
| 1263 | + "\u001B[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n", |
| 1264 | + "\u001B[0m\n", |
| 1265 | + "\u001B[32muser: What is the name of the album with the most tracks?\n", |
| 1266 | + "\u001B[0m\n", |
| 1267 | + "\u001B[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT albums.Title, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM albums\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY albums.Title\\\\nORDER BY TrackCount DESC\\\\nLIMIT 1;\"}'}\n", |
| 1268 | + "\u001B[0m\n", |
| 1269 | + "\u001B[35mfunction (ask_database): [('Greatest Hits', 57)]\n", |
| 1270 | + "\u001B[0m\n" |
1257 | 1271 | ]
|
1258 | 1272 | }
|
1259 | 1273 | ],
|
|
0 commit comments