Spaces:
Running
Running
File size: 38,618 Bytes
3e01cf7 |
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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">I push updates regularly. As people ask questions or have problems, I add more examples and improve explanations. As a result, the code below might not be identical to the videos, as I've added more steps and better comments. Consider this like an interactive book that accompanies the lectures.<br/><br/>\n",
" I try to send emails regularly with important updates related to the course. You can find this in the 'Announcements' section of Udemy in the left sidebar. You can also choose to receive my emails via your Notification Settings in Udemy. I'm respectful of your inbox and always try to add value with my emails!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### And please do remember to contact me if I can help\n",
"\n",
"And I love to connect: https://www.linkedin.com/in/eddonner/\n",
"\n",
"\n",
"### New to Notebooks like this one? Head over to the guides folder!\n",
"\n",
"Just to check you've already added the Python and Jupyter extensions to Cursor, if not already installed:\n",
"- Open extensions (View >> extensions)\n",
"- Search for python, and when the results show, click on the ms-python one, and Install it if not already installed\n",
"- Search for jupyter, and when the results show, click on the Microsoft one, and Install it if not already installed \n",
"Then View >> Explorer to bring back the File Explorer.\n",
"\n",
"And then:\n",
"1. Click where it says \"Select Kernel\" near the top right, and select the option called `.venv (Python 3.12.9)` or similar, which should be the first choice or the most prominent choice. You may need to choose \"Python Environments\" first.\n",
"2. Click in each \"cell\" below, starting with the cell immediately below this text, and press Shift+Enter to run\n",
"3. Enjoy!\n",
"\n",
"After you click \"Select Kernel\", if there is no option like `.venv (Python 3.12.9)` then please do the following: \n",
"1. On Mac: From the Cursor menu, choose Settings >> VS Code Settings (NOTE: be sure to select `VSCode Settings` not `Cursor Settings`); \n",
"On Windows PC: From the File menu, choose Preferences >> VS Code Settings(NOTE: be sure to select `VSCode Settings` not `Cursor Settings`) \n",
"2. In the Settings search bar, type \"venv\" \n",
"3. In the field \"Path to folder with a list of Virtual Environments\" put the path to the project root, like C:\\Users\\username\\projects\\agents (on a Windows PC) or /Users/username/projects/agents (on Mac or Linux). \n",
"And then try again.\n",
"\n",
"Having problems with missing Python versions in that list? Have you ever used Anaconda before? It might be interferring. Quit Cursor, bring up a new command line, and make sure that your Anaconda environment is deactivated: \n",
"`conda deactivate` \n",
"And if you still have any problems with conda and python versions, it's possible that you will need to run this too: \n",
"`conda config --set auto_activate_base false` \n",
"and then from within the Agents directory, you should be able to run `uv python list` and see the Python 3.12 version."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# First let's do an import\n",
"from dotenv import load_dotenv\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Next it's time to load the API keys into environment variables\n",
"\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Wait, did that just output `False`??\n",
"\n",
"If so, the most common reason is that you didn't save your `.env` file after adding the key! Be sure to have saved.\n",
"\n",
"Also, make sure the `.env` file is named precisely `.env` and is in the project root directory (`agents`)\n",
"\n",
"By the way, your `.env` file should have a stop symbol next to it in Cursor on the left, and that's actually a good thing: that's Cursor saying to you, \"hey, I realize this is a file filled with secret information, and I'm not going to send it to an external AI to suggest changes, because your keys should not be shown to anyone else.\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key not set - please head to the troubleshooting guide in the setup folder\n"
]
}
],
"source": [
"# Check the key - if you're not using OpenAI, check whichever key you're using! Ollama doesn't need a key.\n",
"\n",
"import os\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set - please head to the troubleshooting guide in the setup folder\")\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# And now - the all important import statement\n",
"# If you get an import error - head over to troubleshooting in the Setup folder\n",
"# If you get a Name Error - head over to Python Foundations guide (guide 6)\n",
"\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# And now we'll create an instance of the OpenAI class\n",
"# If you're not sure what it means to create an instance of a class - head over to the guides folder (guide 6)!\n",
"# If you get a NameError - head over to the guides folder (guide 6)to learn about NameErrors - always instantly fixable\n",
"# If you're not using OpenAI, you just need to slightly modify this - precise instructions are in the AI APIs guide (guide 9)\n",
"\n",
"import openai\n",
"\n",
"# استبدل هذا بـ مفتاح API الخاص بك\n",
"api_key = \"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\"\n",
"\n",
"openai.api_key = api_key\n",
"\n",
"# أو باستخدام الكود السابق لإنشاء الكائن (إذا كان موجودًا في كودك)\n",
"openai = OpenAI(api_key=api_key)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Create a list of messages in the familiar OpenAI format\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"What is 90%5?\"}]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"90%5 = 0\n",
"\n",
"The modulo operation (%) gives the remainder after division. Since 90 divided by 5 is exactly 18 with no remainder, the result is 0.\n"
]
}
],
"source": [
"# And now call it! Any problems, head to the troubleshooting guide\n",
"# This uses GPT 4.1 nano, the incredibly cheap model\n",
"# The APIs guide (guide 9) has exact instructions for using even cheaper or free alternatives to OpenAI\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-nano\",\n",
" messages=messages\n",
")\n",
"\n",
"print(response.choices[0].message.content)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# And now - let's ask for a question:\n",
"\n",
"question = \"Please propose a hard in arabic, challenging question to assess someone's IQ. Respond only with the question.\"\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"إذا كان عدد التفاحات في سلة يزيد بمقدار 3 أربعة أضعاف عدد التفاحات الأصلي، وبعد إزالة 6 تفاحات يصبح عدد التفاحات نصف العدد الأصلي، فكم عدد التفاحات التي كانت في السلة في البداية؟\n"
]
}
],
"source": [
"# ask it - this uses GPT 4.1 mini, still cheap but more powerful than nano\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"question = response.choices[0].message.content\n",
"\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"لنفترض أن عدد التفاحات الأصلي في السلة هو \\( x \\).\n",
"\n",
"المعادلة الأولى: \n",
"\"عدد التفاحات في السلة يزيد بمقدار 3 أربعة أضعاف عدد التفاحات الأصلي\"\n",
"هذا يعني:\n",
"عدد التفاحات الجديد = \\( 4x + 3 \\)\n",
"\n",
"المعادلة الثانية:\n",
"\"بعد إزالة 6 تفاحات يصبح عدد التفاحات نصف العدد الأصلي\"\n",
"أي:\n",
"\\( (4x + 3) - 6 = \\frac{x}{2} \\)\n",
"\n",
"نبسط المعادلة:\n",
"\\[\n",
"4x + 3 - 6 = \\frac{x}{2}\n",
"\\]\n",
"\\[\n",
"4x - 3 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"نضرب طرفي المعادلة في 2 للتخلص من المقام:\n",
"\\[\n",
"2 \\times (4x - 3) = 2 \\times \\frac{x}{2}\n",
"\\]\n",
"\\[\n",
"8x - 6 = x\n",
"\\]\n",
"\n",
"ننقل \\( x \\) للطرف الأيسر:\n",
"\\[\n",
"8x - x = 6\n",
"\\]\n",
"\\[\n",
"7x = 6\n",
"\\]\n",
"\n",
"نقسم الطرفين على 7:\n",
"\\[\n",
"x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"النتيجة تقول أن عدد التفاحات الأصلية هو \\( \\frac{6}{7} \\)، وهذا عدد غير صحيح لأن عدد التفاحات يجب أن يكون عددًا صحيحًا.\n",
"\n",
"إذن، ربما هناك خطأ في تفسير السؤال. لنراجع نص السؤال مرة أخرى ونفهمه بشكل صحيح.\n",
"\n",
"---\n",
"\n",
"**تحليل النص بعناية:**\n",
"\n",
"\"إذا كان عدد التفاحات في السلة يزيد بمقدار 3 أربعة أضعاف عدد التفاحات الأصلي\"\n",
"\n",
"هل المقصود أن عدد التفاحات الجديد هو (أربعة أضعاف العدد الأصلي زائد 3) أم (أي شيء آخر)؟\n",
"\n",
"يمكن أن يكون المعنى:\n",
"\n",
"- \"عدد التفاحات في السلة يزيد بمقدار 3 عن أربعة أضعاف عدد التفاحات الأصلي\"\n",
"أي:\n",
"\\[\n",
"\\text{عدد التفاحات الجديد} = 4x + 3\n",
"\\]\n",
"\n",
"أو\n",
"\n",
"- \"عدد التفاحات في السلة يزيد بمقدار 3 بحيث يصبح أربعة أضعاف عدد التفاحات الأصلي\"\n",
"أي:\n",
"\\[\n",
"\\text{عدد التفاحات الجديد} = 4x\n",
"\\]\n",
"\n",
"ويكون \"يزيد بمقدار 3\" هو زيادة عن العدد الأصلي.\n",
"\n",
"لكن في العادة، الصياغة \"يزيد بمقدار 3 أربعة أضعاف العدد الأصلي\" تعني:\n",
"\n",
"\\[\n",
"\\text{عدد التفاحات الجديد} = 4x + 3\n",
"\\]\n",
"\n",
"وهذا ما استخدمناه.\n",
"\n",
"---\n",
"\n",
"لنتحقق من الحل:\n",
"\n",
"عدد التفاحات الأصلي: \\( x \\)\n",
"\n",
"عدد التفاحات بعد الزيادة: \\( 4x + 3 \\)\n",
"\n",
"بعد إزالة 6 تفاحات: العدد يصبح نصف العدد الأصلي:\n",
"\n",
"\\[\n",
"4x + 3 - 6 = \\frac{x}{2}\n",
"\\]\n",
"\\[\n",
"4x - 3 = \\frac{x}{2}\n",
"\\]\n",
"\\[\n",
"8x - 6 = x\n",
"\\]\n",
"\\[\n",
"7x = 6\n",
"\\]\n",
"\\[\n",
"x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"عدد التفاحات غير صحيح.\n",
"\n",
"---\n",
"\n",
"**فحص الصياغة البديلة:**\n",
"\n",
"\"يزيد بمقدار 3\" أي \\(x + 3\\)\n",
"\n",
"\"أربعة أضعاف عدد التفاحات الأصلي\" أي \\(4x\\)\n",
"\n",
"هل يمكن أن يكون:\n",
"\n",
"\\[\n",
"x + 3 = 4x\n",
"\\]\n",
"وبعد ذلك، إزالة 6 تفاحات يصبح نصف العدد الأصلي:\n",
"\n",
"\\[\n",
"4x - 6 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"لكن لا، فهذه المعادلة لا تجمع المعطيات معًا.\n",
"\n",
"---\n",
"\n",
"قد يكون الفهم الصحيح:\n",
"\n",
"- بعد الزيادة بمقدار 3، يصبح عدد التفاحات 4 أضعاف العدد الأصلي:\n",
"\n",
"\\[\n",
"x + 3 = 4x\n",
"\\]\n",
"من هنا:\n",
"\n",
"\\[\n",
"3 = 3x \\implies x=1\n",
"\\]\n",
"\n",
"بعد ذلك، إزالة 6 تفاحات:\n",
"\n",
"\\[\n",
"(4x) - 6 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"إذا كان \\( x=1 \\):\n",
"\n",
"\\[\n",
"4 \\cdot 1 - 6 = \\frac{1}{2}\n",
"\\]\n",
"\\[\n",
"4 - 6 = 0.5\n",
"\\]\n",
"\\[\n",
"-2 = 0.5\n",
"\\]\n",
"\n",
"غير صحيحة.\n",
"\n",
"---\n",
"\n",
"أو العكس:\n",
"\n",
"\"عدد التفاحات بعد الزيادة التي قدرها 3، يعادل أربعة أضعاف العدد الأصلي\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"\\text{new number} = x + \\text{3}\n",
"\\]\n",
"و\n",
"\n",
"\\[\n",
"\\text{new number} = 4x\n",
"\\]\n",
"\n",
"إذن:\n",
"\n",
"\\[\n",
"x + 3 = 4x \\implies 3x = 3 \\implies x = 1\n",
"\\]\n",
"\n",
"بعد إزالة 6 تفاحات:\n",
"\n",
"\\[\n",
"4x - 6 = \\frac{x}{2} \\implies 4(1)-6 = \\frac{1}{2} \\implies -2=0.5 \\quad (خطأ)\n",
"\\]\n",
"\n",
"---\n",
"\n",
"بديل آخَر:\n",
"\n",
"\"عدد التفاحات في السلة يزيد بمقدار 3 إلى أن يصبح أربعة أضعاف عدد التفاحات الأصلي\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"\\text{new number} = 4x\n",
"\\]\n",
"و\n",
"\\[\n",
"\\text{new number} = x + 3\n",
"\\]\n",
"غير منطقي.\n",
"\n",
"---\n",
"\n",
"هل يمكن أن يكون المعنى:\n",
"\n",
"\"عدد التفاحات في السلة، إذا زاد ب 3، يصبح أربعة أضعاف عدد التفاحات الأصلي\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"x + 3 = 4x\n",
"\\]\n",
"\n",
"وبعد إزالة 6 تفاحات يصبح نصف العدد الأصلي:\n",
"\n",
"\\[\n",
"(x + 3) - 6 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"من المعادلة الأولى:\n",
"\n",
"\\[\n",
"x + 3 = 4x \\implies 3x = 3 \\implies x = 1\n",
"\\]\n",
"\n",
"من المعادلة الثانية:\n",
"\n",
"\\[\n",
"(1 + 3) - 6 = \\frac{1}{2} \\implies 4 - 6 = 0.5 \\implies -2 = 0.5\n",
"\\]\n",
"\n",
"خطأ.\n",
"\n",
"---\n",
"\n",
"ربما يجب تغيير المعنى بالشكل الآتي:\n",
"\n",
"\"عدد التفاحات في السلة \\( x \\) يزيد بأربعة أضعاف، ثم يزداد بمقدار 3\"\n",
"\n",
"عدد التفاحات الجديد:\n",
"\n",
"\\[\n",
"4x + 3\n",
"\\]\n",
"\n",
"بعد إزالة 6 تفاحات يصبح نصف العدد الأصلي:\n",
"\n",
"\\[\n",
"4x + 3 - 6 = \\frac{x}{2} \\implies 4x - 3 = \\frac{x}{2} \\implies 8x - 6 = x \\implies 7x = 6 \\implies x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"غير صحيح.\n",
"\n",
"---\n",
"\n",
"هل يمكن أن يكون \"يزيد بمقدار 3 إلى أربعة أضعاف\" تعني:\n",
"\n",
"\"عدد التفاحات في السلة يزيد بمقدار 3 ليصبح أربعة أضعاف العدد الأصلي\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"\\text{new} = 4x\n",
"\\]\n",
"و\n",
"\\[\n",
"\\text{new} = x + 3\n",
"\\]\n",
"\n",
"النتيجة واحدة كما في المحاولات السابقة.\n",
"\n",
"---\n",
"\n",
"**اقتراح حل منطقي:**\n",
"\n",
"لنفرض أن المعادلة هي:\n",
"\n",
"\\[\n",
"\\text{new} = 4x + 3\n",
"\\]\n",
"\n",
"وبعد إزالة 6 يصبح نصف الأصل:\n",
"\n",
"\\[\n",
"\\text{new} - 6 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"هذا يعطينا:\n",
"\n",
"\\[\n",
"4x + 3 - 6 = \\frac{x}{2} \\implies 4x - 3 = \\frac{x}{2} \\implies 8x - 6 = x \\implies 7x = 6 \\implies x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"لا يمكن أن يكون عدد التفاحات أصغر من 1.\n",
"\n",
"---\n",
"\n",
"**إذن السؤال قد يحتوي على خطأ أو نقص في المعلومات.**\n",
"\n",
"---\n",
"\n",
"**ملخص:**\n",
"\n",
"ربما يجب أن تصحح أو توضح السؤال أكثر.\n",
"\n",
"هل تريد مني افتراض المعادلة التالية وحلها؟\n",
"\n",
"- الأصل = \\( x \\)\n",
"- زيادة مقدار 3 تصبح أربعة أضعاف:\n",
"\n",
"\\[\n",
"x + 3 = 4x \\implies x = 1\n",
"\\]\n",
"\n",
"- بعد إزالة 6 تصبح نصف الأصل:\n",
"\n",
"\\[\n",
"(4x) - 6 = \\frac{x}{2} \\implies 4 - 6 = 0.5 \\quad \\rightarrow -2 = 0.5\n",
"\\]\n",
"\n",
"غير صحيحة.\n",
"\n",
"---\n",
"\n",
"**بديل:**\n",
"\n",
"\"عدد التفاحات في السلة زاد ليصبح أربعة أضعاف العدد الأصلي زائد 3\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"\\text{new} = 4x + 3\n",
"\\]\n",
"\n",
"وبعد إزالة 6 يصبح نصف الأصل:\n",
"\n",
"\\[\n",
"4x +3 - 6 = \\frac{x}{2} \\implies 4x - 3 = \\frac{x}{2} \\implies 8x - 6 = x \\implies 7x = 6 \\implies x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"بعد كل ذلك، الحل لا يعطي عددًا صحيحًا.\n",
"\n",
"---\n",
"\n",
"هل تريدني أن أفترض أرقامًا مختلفة أو أوضح السؤال؟\n"
]
}
],
"source": [
"# Ask it again\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"answer = response.choices[0].message.content\n",
"print(answer)\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"لنفترض أن عدد التفاحات الأصلي في السلة هو \\( x \\).\n",
"\n",
"المعادلة الأولى: \n",
"\"عدد التفاحات في السلة يزيد بمقدار 3 أربعة أضعاف عدد التفاحات الأصلي\"\n",
"هذا يعني:\n",
"عدد التفاحات الجديد = \\( 4x + 3 \\)\n",
"\n",
"المعادلة الثانية:\n",
"\"بعد إزالة 6 تفاحات يصبح عدد التفاحات نصف العدد الأصلي\"\n",
"أي:\n",
"\\( (4x + 3) - 6 = \\frac{x}{2} \\)\n",
"\n",
"نبسط المعادلة:\n",
"\\[\n",
"4x + 3 - 6 = \\frac{x}{2}\n",
"\\]\n",
"\\[\n",
"4x - 3 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"نضرب طرفي المعادلة في 2 للتخلص من المقام:\n",
"\\[\n",
"2 \\times (4x - 3) = 2 \\times \\frac{x}{2}\n",
"\\]\n",
"\\[\n",
"8x - 6 = x\n",
"\\]\n",
"\n",
"ننقل \\( x \\) للطرف الأيسر:\n",
"\\[\n",
"8x - x = 6\n",
"\\]\n",
"\\[\n",
"7x = 6\n",
"\\]\n",
"\n",
"نقسم الطرفين على 7:\n",
"\\[\n",
"x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"النتيجة تقول أن عدد التفاحات الأصلية هو \\( \\frac{6}{7} \\)، وهذا عدد غير صحيح لأن عدد التفاحات يجب أن يكون عددًا صحيحًا.\n",
"\n",
"إذن، ربما هناك خطأ في تفسير السؤال. لنراجع نص السؤال مرة أخرى ونفهمه بشكل صحيح.\n",
"\n",
"---\n",
"\n",
"**تحليل النص بعناية:**\n",
"\n",
"\"إذا كان عدد التفاحات في السلة يزيد بمقدار 3 أربعة أضعاف عدد التفاحات الأصلي\"\n",
"\n",
"هل المقصود أن عدد التفاحات الجديد هو (أربعة أضعاف العدد الأصلي زائد 3) أم (أي شيء آخر)؟\n",
"\n",
"يمكن أن يكون المعنى:\n",
"\n",
"- \"عدد التفاحات في السلة يزيد بمقدار 3 عن أربعة أضعاف عدد التفاحات الأصلي\"\n",
"أي:\n",
"\\[\n",
"\\text{عدد التفاحات الجديد} = 4x + 3\n",
"\\]\n",
"\n",
"أو\n",
"\n",
"- \"عدد التفاحات في السلة يزيد بمقدار 3 بحيث يصبح أربعة أضعاف عدد التفاحات الأصلي\"\n",
"أي:\n",
"\\[\n",
"\\text{عدد التفاحات الجديد} = 4x\n",
"\\]\n",
"\n",
"ويكون \"يزيد بمقدار 3\" هو زيادة عن العدد الأصلي.\n",
"\n",
"لكن في العادة، الصياغة \"يزيد بمقدار 3 أربعة أضعاف العدد الأصلي\" تعني:\n",
"\n",
"\\[\n",
"\\text{عدد التفاحات الجديد} = 4x + 3\n",
"\\]\n",
"\n",
"وهذا ما استخدمناه.\n",
"\n",
"---\n",
"\n",
"لنتحقق من الحل:\n",
"\n",
"عدد التفاحات الأصلي: \\( x \\)\n",
"\n",
"عدد التفاحات بعد الزيادة: \\( 4x + 3 \\)\n",
"\n",
"بعد إزالة 6 تفاحات: العدد يصبح نصف العدد الأصلي:\n",
"\n",
"\\[\n",
"4x + 3 - 6 = \\frac{x}{2}\n",
"\\]\n",
"\\[\n",
"4x - 3 = \\frac{x}{2}\n",
"\\]\n",
"\\[\n",
"8x - 6 = x\n",
"\\]\n",
"\\[\n",
"7x = 6\n",
"\\]\n",
"\\[\n",
"x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"عدد التفاحات غير صحيح.\n",
"\n",
"---\n",
"\n",
"**فحص الصياغة البديلة:**\n",
"\n",
"\"يزيد بمقدار 3\" أي \\(x + 3\\)\n",
"\n",
"\"أربعة أضعاف عدد التفاحات الأصلي\" أي \\(4x\\)\n",
"\n",
"هل يمكن أن يكون:\n",
"\n",
"\\[\n",
"x + 3 = 4x\n",
"\\]\n",
"وبعد ذلك، إزالة 6 تفاحات يصبح نصف العدد الأصلي:\n",
"\n",
"\\[\n",
"4x - 6 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"لكن لا، فهذه المعادلة لا تجمع المعطيات معًا.\n",
"\n",
"---\n",
"\n",
"قد يكون الفهم الصحيح:\n",
"\n",
"- بعد الزيادة بمقدار 3، يصبح عدد التفاحات 4 أضعاف العدد الأصلي:\n",
"\n",
"\\[\n",
"x + 3 = 4x\n",
"\\]\n",
"من هنا:\n",
"\n",
"\\[\n",
"3 = 3x \\implies x=1\n",
"\\]\n",
"\n",
"بعد ذلك، إزالة 6 تفاحات:\n",
"\n",
"\\[\n",
"(4x) - 6 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"إذا كان \\( x=1 \\):\n",
"\n",
"\\[\n",
"4 \\cdot 1 - 6 = \\frac{1}{2}\n",
"\\]\n",
"\\[\n",
"4 - 6 = 0.5\n",
"\\]\n",
"\\[\n",
"-2 = 0.5\n",
"\\]\n",
"\n",
"غير صحيحة.\n",
"\n",
"---\n",
"\n",
"أو العكس:\n",
"\n",
"\"عدد التفاحات بعد الزيادة التي قدرها 3، يعادل أربعة أضعاف العدد الأصلي\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"\\text{new number} = x + \\text{3}\n",
"\\]\n",
"و\n",
"\n",
"\\[\n",
"\\text{new number} = 4x\n",
"\\]\n",
"\n",
"إذن:\n",
"\n",
"\\[\n",
"x + 3 = 4x \\implies 3x = 3 \\implies x = 1\n",
"\\]\n",
"\n",
"بعد إزالة 6 تفاحات:\n",
"\n",
"\\[\n",
"4x - 6 = \\frac{x}{2} \\implies 4(1)-6 = \\frac{1}{2} \\implies -2=0.5 \\quad (خطأ)\n",
"\\]\n",
"\n",
"---\n",
"\n",
"بديل آخَر:\n",
"\n",
"\"عدد التفاحات في السلة يزيد بمقدار 3 إلى أن يصبح أربعة أضعاف عدد التفاحات الأصلي\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"\\text{new number} = 4x\n",
"\\]\n",
"و\n",
"\\[\n",
"\\text{new number} = x + 3\n",
"\\]\n",
"غير منطقي.\n",
"\n",
"---\n",
"\n",
"هل يمكن أن يكون المعنى:\n",
"\n",
"\"عدد التفاحات في السلة، إذا زاد ب 3، يصبح أربعة أضعاف عدد التفاحات الأصلي\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"x + 3 = 4x\n",
"\\]\n",
"\n",
"وبعد إزالة 6 تفاحات يصبح نصف العدد الأصلي:\n",
"\n",
"\\[\n",
"(x + 3) - 6 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"من المعادلة الأولى:\n",
"\n",
"\\[\n",
"x + 3 = 4x \\implies 3x = 3 \\implies x = 1\n",
"\\]\n",
"\n",
"من المعادلة الثانية:\n",
"\n",
"\\[\n",
"(1 + 3) - 6 = \\frac{1}{2} \\implies 4 - 6 = 0.5 \\implies -2 = 0.5\n",
"\\]\n",
"\n",
"خطأ.\n",
"\n",
"---\n",
"\n",
"ربما يجب تغيير المعنى بالشكل الآتي:\n",
"\n",
"\"عدد التفاحات في السلة \\( x \\) يزيد بأربعة أضعاف، ثم يزداد بمقدار 3\"\n",
"\n",
"عدد التفاحات الجديد:\n",
"\n",
"\\[\n",
"4x + 3\n",
"\\]\n",
"\n",
"بعد إزالة 6 تفاحات يصبح نصف العدد الأصلي:\n",
"\n",
"\\[\n",
"4x + 3 - 6 = \\frac{x}{2} \\implies 4x - 3 = \\frac{x}{2} \\implies 8x - 6 = x \\implies 7x = 6 \\implies x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"غير صحيح.\n",
"\n",
"---\n",
"\n",
"هل يمكن أن يكون \"يزيد بمقدار 3 إلى أربعة أضعاف\" تعني:\n",
"\n",
"\"عدد التفاحات في السلة يزيد بمقدار 3 ليصبح أربعة أضعاف العدد الأصلي\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"\\text{new} = 4x\n",
"\\]\n",
"و\n",
"\\[\n",
"\\text{new} = x + 3\n",
"\\]\n",
"\n",
"النتيجة واحدة كما في المحاولات السابقة.\n",
"\n",
"---\n",
"\n",
"**اقتراح حل منطقي:**\n",
"\n",
"لنفرض أن المعادلة هي:\n",
"\n",
"\\[\n",
"\\text{new} = 4x + 3\n",
"\\]\n",
"\n",
"وبعد إزالة 6 يصبح نصف الأصل:\n",
"\n",
"\\[\n",
"\\text{new} - 6 = \\frac{x}{2}\n",
"\\]\n",
"\n",
"هذا يعطينا:\n",
"\n",
"\\[\n",
"4x + 3 - 6 = \\frac{x}{2} \\implies 4x - 3 = \\frac{x}{2} \\implies 8x - 6 = x \\implies 7x = 6 \\implies x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"لا يمكن أن يكون عدد التفاحات أصغر من 1.\n",
"\n",
"---\n",
"\n",
"**إذن السؤال قد يحتوي على خطأ أو نقص في المعلومات.**\n",
"\n",
"---\n",
"\n",
"**ملخص:**\n",
"\n",
"ربما يجب أن تصحح أو توضح السؤال أكثر.\n",
"\n",
"هل تريد مني افتراض المعادلة التالية وحلها؟\n",
"\n",
"- الأصل = \\( x \\)\n",
"- زيادة مقدار 3 تصبح أربعة أضعاف:\n",
"\n",
"\\[\n",
"x + 3 = 4x \\implies x = 1\n",
"\\]\n",
"\n",
"- بعد إزالة 6 تصبح نصف الأصل:\n",
"\n",
"\\[\n",
"(4x) - 6 = \\frac{x}{2} \\implies 4 - 6 = 0.5 \\quad \\rightarrow -2 = 0.5\n",
"\\]\n",
"\n",
"غير صحيحة.\n",
"\n",
"---\n",
"\n",
"**بديل:**\n",
"\n",
"\"عدد التفاحات في السلة زاد ليصبح أربعة أضعاف العدد الأصلي زائد 3\"\n",
"\n",
"أي:\n",
"\n",
"\\[\n",
"\\text{new} = 4x + 3\n",
"\\]\n",
"\n",
"وبعد إزالة 6 يصبح نصف الأصل:\n",
"\n",
"\\[\n",
"4x +3 - 6 = \\frac{x}{2} \\implies 4x - 3 = \\frac{x}{2} \\implies 8x - 6 = x \\implies 7x = 6 \\implies x = \\frac{6}{7}\n",
"\\]\n",
"\n",
"بعد كل ذلك، الحل لا يعطي عددًا صحيحًا.\n",
"\n",
"---\n",
"\n",
"هل تريدني أن أفترض أرقامًا مختلفة أو أوضح السؤال؟"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import Markdown, display\n",
"\n",
"display(Markdown(answer))\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Congratulations!\n",
"\n",
"That was a small, simple step in the direction of Agentic AI, with your new environment!\n",
"\n",
"Next time things get more interesting..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (1044641152.py, line 16)",
"output_type": "error",
"traceback": [
" \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[20]\u001b[39m\u001b[32m, line 16\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mresponse =\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n"
]
}
],
"source": [
"# First create the messages:\n",
"question = \"Please propose a hard in arabic, challenging question to assess someone's IQ. Respond only with the question.\"\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"question = response.choices[0].message.content\n",
"\n",
"print(question)\n",
"\n",
"# Then make the first call:\n",
"\n",
"response =\n",
"\n",
"# Then read the business idea:\n",
"\n",
"business_idea = response.\n",
"\n",
"# And repeat! In the next message, include the business idea within the message"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|