File size: 58,072 Bytes
6fe7c36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FastAPI Multi-Agent System Testing\n",
    "\n",
    "This notebook demonstrates how to test and interact with the FastAPI endpoints.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "Make sure the FastAPI server is running:\n",
    "```bash\n",
    "python api.py\n",
    "# OR\n",
    "uvicorn api:app --host 0.0.0.0 --port 8000 --reload\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup and Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "βœ… Imports successful\n",
      "🌐 API Base URL: http://localhost:8000\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "import asyncio\n",
    "import websockets\n",
    "from pathlib import Path\n",
    "import time\n",
    "from IPython.display import display, Markdown, HTML\n",
    "\n",
    "# API Base URL\n",
    "BASE_URL = \"http://localhost:8000\"\n",
    "\n",
    "print(\"βœ… Imports successful\")\n",
    "print(f\"🌐 API Base URL: {BASE_URL}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Health Check\n",
    "\n",
    "Test if the API is running and all agents are initialized."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ₯ Health Check Response:\n",
      "  Status: healthy\n",
      "  Initialized: True\n",
      "\n",
      "  Agent Status:\n",
      "    βœ… crypto: True\n",
      "    βœ… rag: True\n",
      "    βœ… stock: True\n",
      "    βœ… search: True\n",
      "    βœ… finance_tracker: True\n"
     ]
    }
   ],
   "source": [
    "def test_health_check():\n",
    "    \"\"\"Test the health check endpoint.\"\"\"\n",
    "    try:\n",
    "        response = requests.get(f\"{BASE_URL}/health\")\n",
    "        response.raise_for_status()\n",
    "        data = response.json()\n",
    "        \n",
    "        print(\"πŸ₯ Health Check Response:\")\n",
    "        print(f\"  Status: {data['status']}\")\n",
    "        print(f\"  Initialized: {data['initialized']}\")\n",
    "        print(\"\\n  Agent Status:\")\n",
    "        for agent, status in data['agents'].items():\n",
    "            emoji = \"βœ…\" if status else \"❌\"\n",
    "            print(f\"    {emoji} {agent}: {status}\")\n",
    "        \n",
    "        return data\n",
    "    except Exception as e:\n",
    "        print(f\"❌ Error: {e}\")\n",
    "        return None\n",
    "\n",
    "health_data = test_health_check()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Root Endpoint\n",
    "\n",
    "Get API information and available endpoints."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ“‹ API Information:\n",
      "{\n",
      "  \"message\": \"Multi-Agent Assistant API\",\n",
      "  \"version\": \"1.0.0\",\n",
      "  \"docs\": \"/docs\",\n",
      "  \"health\": \"/health\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "def test_root_endpoint():\n",
    "    \"\"\"Test the root endpoint.\"\"\"\n",
    "    try:\n",
    "        response = requests.get(f\"{BASE_URL}/\")\n",
    "        response.raise_for_status()\n",
    "        data = response.json()\n",
    "        \n",
    "        print(\"πŸ“‹ API Information:\")\n",
    "        print(json.dumps(data, indent=2))\n",
    "        \n",
    "        return data\n",
    "    except Exception as e:\n",
    "        print(f\"❌ Error: {e}\")\n",
    "        return None\n",
    "\n",
    "api_info = test_root_endpoint()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Streaming Chat with Server-Sent Events (SSE)\n",
    "\n",
    "Test the streaming chat endpoint that returns real-time updates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ’¬ Query: What is the current price of Bitcoin?\n",
      "\n",
      "πŸ“‘ Streaming Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking for the current price of Bitcoin. The `CALL_CRYPTO` agent is designed to get cryptocurrency market data and prices. This is the most direct and appropriate tool to answer the query.\n",
      "   Action: CRYPTO\n",
      "   Justification: This action will directly retrieve the current price of Bitcoin, which is precisely what the user is asking for.\n",
      "\n",
      "πŸ”§ Calling Crypto Agent...\n",
      "\n",
      "πŸ“Š Crypto Agent Results:\n",
      "   The current price of Bitcoin is $95,748.\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: I have already called the `CALL_CRYPTO` agent, which provided the current price of Bitcoin as $95,748. This directly answers the user's query. I have sufficient information to provide a final answer.\n",
      "   Action: FINISH\n",
      "   Justification: The `CALL_CRYPTO` agent has successfully retrieved the current price of Bitcoin, which was the user's original query. No further information is needed.\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "The current price of Bitcoin is $95,748.\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "def test_streaming_chat(message, history=None):\n",
    "    \"\"\"Test streaming chat with SSE.\"\"\"\n",
    "    if history is None:\n",
    "        history = []\n",
    "    \n",
    "    payload = {\n",
    "        \"message\": message,\n",
    "        \"history\": history\n",
    "    }\n",
    "    \n",
    "    print(f\"πŸ’¬ Query: {message}\")\n",
    "    print(\"\\nπŸ“‘ Streaming Response:\")\n",
    "    print(\"=\" * 80)\n",
    "    \n",
    "    try:\n",
    "        response = requests.post(\n",
    "            f\"{BASE_URL}/api/v1/chat/stream\",\n",
    "            json=payload,\n",
    "            headers={\"Accept\": \"text/event-stream\"},\n",
    "            stream=True\n",
    "        )\n",
    "        response.raise_for_status()\n",
    "        \n",
    "        final_answer = \"\"\n",
    "        \n",
    "        for line in response.iter_lines():\n",
    "            if line:\n",
    "                line_str = line.decode('utf-8')\n",
    "                if line_str.startswith('data: '):\n",
    "                    data_str = line_str[6:]  # Remove 'data: ' prefix\n",
    "                    try:\n",
    "                        event = json.loads(data_str)\n",
    "                        event_type = event.get('type', 'unknown')\n",
    "                        \n",
    "                        if event_type == 'thinking':\n",
    "                            print(f\"\\nπŸ’­ Step {event.get('step', '?')}: Reasoning\")\n",
    "                            print(f\"   Thought: {event.get('thought', 'N/A')}\")\n",
    "                            print(f\"   Action: {event.get('action', 'N/A').upper()}\")\n",
    "                            print(f\"   Justification: {event.get('justification', 'N/A')}\")\n",
    "                        \n",
    "                        elif event_type == 'action':\n",
    "                            agent = event.get('agent', 'unknown')\n",
    "                            print(f\"\\nπŸ”§ Calling {agent.title()} Agent...\")\n",
    "                        \n",
    "                        elif event_type == 'observation':\n",
    "                            agent = event.get('agent', 'unknown')\n",
    "                            summary = event.get('summary', '')\n",
    "                            print(f\"\\nπŸ“Š {agent.title()} Agent Results:\")\n",
    "                            print(f\"   {summary[:200]}...\" if len(summary) > 200 else f\"   {summary}\")\n",
    "                        \n",
    "                        elif event_type == 'final_start':\n",
    "                            print(\"\\n✨ Final Answer:\")\n",
    "                            print(\"-\" * 80)\n",
    "                        \n",
    "                        elif event_type == 'final_token':\n",
    "                            final_answer = event.get('accumulated', '')\n",
    "                        \n",
    "                        elif event_type == 'final_complete':\n",
    "                            print(final_answer)\n",
    "                            print(\"-\" * 80)\n",
    "                        \n",
    "                        elif event_type == 'error':\n",
    "                            print(f\"\\n❌ Error: {event.get('error', 'Unknown error')}\")\n",
    "                    \n",
    "                    except json.JSONDecodeError:\n",
    "                        continue\n",
    "        \n",
    "        print(\"\\n\" + \"=\" * 80)\n",
    "        return final_answer\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(f\"\\n❌ Error: {e}\")\n",
    "        return None\n",
    "\n",
    "# Test with a simple crypto query\n",
    "result = test_streaming_chat(\"What is the current price of Bitcoin?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test with Different Queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ’¬ Query: What are the top 5 cryptocurrencies by market cap?\n",
      "\n",
      "πŸ“‘ Streaming Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking for a list of the top 5 cryptocurrencies based on their market capitalization. This is a specific query about cryptocurrency market data. I have an action called `CALL_CRYPTO` which is designed to get cryptocurrency market data, prices, and trends. This is the most direct and specialized tool for the job.\n",
      "   Action: CRYPTO\n",
      "   Justification: The `CALL_CRYPTO` action is the most appropriate tool to get real-time market capitalization data for cryptocurrencies, which is exactly what is needed to answer the user's query.\n",
      "\n",
      "πŸ”§ Calling Crypto Agent...\n",
      "\n",
      "πŸ“Š Crypto Agent Results:\n",
      "   The top 5 cryptocurrencies by market cap are:\n",
      "\n",
      "1. Bitcoin (BTC) - Market Cap: $2,025,171,098,463 - Price: $101,536\n",
      "2. Ethereum (ETH) - Market Cap: $411,695,018,306 - Price: $3,410.68\n",
      "3. Tether (USDT) ...\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: The CRYPTO agent has provided a list of the top 5 cryptocurrencies by market cap, which directly answers the user's query. I have all the necessary information to provide the final answer.\n",
      "   Action: FINISH\n",
      "   Justification: The information gathered from the CRYPTO agent is sufficient and complete to answer the user's question. No further actions are required.\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "Based on current market data, here are the top 5 cryptocurrencies ranked by market capitalization:\n",
      "\n",
      "**1. Bitcoin (BTC)**\n",
      "*   **Market Cap:** $2,025,171,098,463\n",
      "*   **Price:** $101,536.00\n",
      "\n",
      "**2. Ethereum (ETH)**\n",
      "*   **Market Cap:** $411,695,018,306\n",
      "*   **Price:** $3,410.68\n",
      "\n",
      "**3. Tether (USDT)**\n",
      "*   **Market Cap:** $184,005,021,987\n",
      "*   **Price:** $0.999813\n",
      "\n",
      "**4. XRP (XRP)**\n",
      "*   **Market Cap:** $143,300,134,340*   **Price:** $2.38\n",
      "\n",
      "**5. BNB (BNB)**\n",
      "*   **Market Cap:** $131,225,068,810\n",
      "*   **Price:** $952.81\n",
      "\n",
      "**Key Insight:**\n",
      "\n",
      "Market capitalization is a key metric used to determine the relative size and value of a cryptocurrency. It is calculated by multiplying the current market price of a single coin by the total number of coins in circulation. As shown above, Bitcoin holds a dominant position with a market cap significantly larger than the others combined.\n",
      "\n",
      "*It is important to note that the cryptocurrency market is highly volatile, and these rankings, market caps, and prices are subject to change rapidly.*\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Based on current market data, here are the top 5 cryptocurrencies ranked by market capitalization:\\n\\n**1. Bitcoin (BTC)**\\n*   **Market Cap:** $2,025,171,098,463\\n*   **Price:** $101,536.00\\n\\n**2. Ethereum (ETH)**\\n*   **Market Cap:** $411,695,018,306\\n*   **Price:** $3,410.68\\n\\n**3. Tether (USDT)**\\n*   **Market Cap:** $184,005,021,987\\n*   **Price:** $0.999813\\n\\n**4. XRP (XRP)**\\n*   **Market Cap:** $143,300,134,340*   **Price:** $2.38\\n\\n**5. BNB (BNB)**\\n*   **Market Cap:** $131,225,068,810\\n*   **Price:** $952.81\\n\\n**Key Insight:**\\n\\nMarket capitalization is a key metric used to determine the relative size and value of a cryptocurrency. It is calculated by multiplying the current market price of a single coin by the total number of coins in circulation. As shown above, Bitcoin holds a dominant position with a market cap significantly larger than the others combined.\\n\\n*It is important to note that the cryptocurrency market is highly volatile, and these rankings, market caps, and prices are subject to change rapidly.*'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test crypto query\n",
    "test_streaming_chat(\"What are the top 5 cryptocurrencies by market cap?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ’¬ Query: What is the current stock price of Apple (AAPL)?\n",
      "\n",
      "πŸ“‘ Streaming Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking for the current stock price of Apple (AAPL). The `CALL_STOCK` action is the most appropriate tool for this query as it is designed to get stock market data. I will use this action to retrieve the price for the ticker \"AAPL\".\n",
      "   Action: STOCK\n",
      "   Justification: This action is specifically designed to retrieve stock market data, which is exactly what is needed to answer the user's query about Apple's stock price.\n",
      "\n",
      "πŸ”§ Calling Stock Agent...\n",
      "\n",
      "πŸ“Š Stock Agent Results:\n",
      "   The current stock price of Apple (AAPL) is $273.47.\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: I have successfully retrieved the current stock price of Apple (AAPL) from the STOCK agent. This information directly answers the user's query. Therefore, I have sufficient information and can provide the final answer.\n",
      "   Action: FINISH\n",
      "   Justification: The STOCK agent has provided the exact information requested by the user. No further actions are necessary.\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "Based on the real-time data retrieved, the current stock price of Apple Inc. (AAPL) is **$273.47**.\n",
      "\n",
      "Please be aware that this price reflects the market value at the time the query was made and is subject to constant fluctuation based on market activity.\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Based on the real-time data retrieved, the current stock price of Apple Inc. (AAPL) is **$273.47**.\\n\\nPlease be aware that this price reflects the market value at the time the query was made and is subject to constant fluctuation based on market activity.'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test stock query\n",
    "test_streaming_chat(\"What is the current stock price of Apple (AAPL)?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ’¬ Query: Search for the latest AI developments in 2025\n",
      "\n",
      "πŸ“‘ Streaming Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking about future AI developments in 2025. This is a general knowledge and predictive query. I have no information gathered yet. The best tool to find information about current events, news, and future trends is the web search tool. The other tools (CRYPTO, STOCK, FINANCE_TRACKER, RAG) are not relevant to this topic. Therefore, I should use CALL_SEARCH to find the requested information.\n",
      "   Action: SEARCH\n",
      "   Justification: This action is the most appropriate for finding information on future technological trends and developments like those anticipated for AI in 2025.\n",
      "\n",
      "πŸ”§ Calling Search Agent...\n",
      "\n",
      "πŸ“Š Search Agent Results:\n",
      "   I couldn't find any results for \"latest AI developments in 2025.\" Since 2025 is in the future, there are no \"developments\" yet. I can search for predictions or trends for AI in 2025 if you'd like.\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: The initial search for \"latest AI developments in 2025\" failed because 2025 is in the future. The SEARCH agent correctly pointed this out and suggested a better query: searching for \"predictions or trends for AI in 2025\". To answer the user's underlying question, I need to follow this suggestion and perform a new search with the revised query. This will gather the predictive information the user is likely looking for.\n",
      "   Action: SEARCH\n",
      "   Justification: The first search failed because the query was flawed. I will now use the refined query \"AI predictions and trends for 2025\" as suggested by the search agent to gather relevant information about what experts expect to see in the field of AI in the near future.\n",
      "\n",
      "πŸ”§ Calling Search Agent...\n",
      "\n",
      "πŸ“Š Search Agent Results:\n",
      "   I couldn't find any results for \"AI predictions and trends for 2025.\" This might be due to a temporary issue with the search engine or the way the query was phrased. I will try a more general search f...\n",
      "\n",
      "πŸ’­ Step 3: Reasoning\n",
      "   Thought: I have attempted two searches so far, and both have failed to return results. The first search was too specific about \"developments\" in the future. The second, more reasonable search for \"predictions and trends for AI in 2025\" also failed, which the agent noted might be a temporary issue. The agent suggested a broader search for \"future of AI\". This is a logical next step to gather some relevant information, as it's a broader topic that is more likely to yield results which can then be analyzed for trends relevant to 2025. I still have no information to answer the user's query, so another search is necessary.\n",
      "   Action: SEARCH\n",
      "   Justification: My previous two search attempts failed. The search agent suggested a broader query, \"future of AI,\" which is a logical next step to gather relevant information to answer the user's question about AI in 2025. I need to execute this search to obtain the necessary information.\n",
      "\n",
      "πŸ”§ Calling Search Agent...\n",
      "\n",
      "πŸ“Š Search Agent Results:\n",
      "   I am sorry, but I was unable to find any search results for \"latest AI developments in 2025\" or related queries like \"AI predictions and trends for 2025\" and \"future of AI.\" It seems there might be an...\n",
      "\n",
      "πŸ’­ Step 4: Reasoning\n",
      "   Thought: I have attempted to answer the user's query by using the `CALL_SEARCH` agent three times with progressively broader queries (\"latest AI developments in 2025\", \"AI predictions and trends for 2025\", and \"future of AI\"). Each attempt has failed, and the agent has explicitly stated that there might be an issue with the search tool itself. Since the only available tool capable of answering this query is not functioning, I cannot gather the necessary information. Further attempts to use the search tool are unlikely to succeed. Therefore, I must conclude the process and inform the user about the inability to retrieve the information due to a technical issue.\n",
      "   Action: FINISH\n",
      "   Justification: The `CALL_SEARCH` agent, which is the only relevant tool for this query, has failed multiple times and indicated a potential technical problem. I have exhausted all reasonable attempts to gather the information and cannot proceed further. The best course of action is to finish and communicate the issue to the user.\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "I was unable to find information on the latest AI developments for 2025.\n",
      "\n",
      "My attempt to answer your query involved a multi-step search process. Initially, I searched for \"latest AI developments in 2025,\" but since 2025 is in the future, no \"developments\" have occurred yet. I then broadened my search to look for \"AI predictions and trends for 2025\" and subsequently the \"future of AI.\"\n",
      "\n",
      "Unfortunately, all of these searches failed to return any results. This indicates a potential temporary issue with the search tool or that information specifically categorized for 2025 is not widely available at this moment.\n",
      "\n",
      "I recommend trying your query again later, as the issue with the search tool may be resolved.\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'I was unable to find information on the latest AI developments for 2025.\\n\\nMy attempt to answer your query involved a multi-step search process. Initially, I searched for \"latest AI developments in 2025,\" but since 2025 is in the future, no \"developments\" have occurred yet. I then broadened my search to look for \"AI predictions and trends for 2025\" and subsequently the \"future of AI.\"\\n\\nUnfortunately, all of these searches failed to return any results. This indicates a potential temporary issue with the search tool or that information specifically categorized for 2025 is not widely available at this moment.\\n\\nI recommend trying your query again later, as the issue with the search tool may be resolved.'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test web search query\n",
    "test_streaming_chat(\"Search for the latest AI developments in 2025\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test finance tracker - adding a transaction\n",
    "test_streaming_chat(\"Add 10 shares of AAPL that I bought at $150\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ’¬ Query: What's Bitcoin's price and what's the latest news about cryptocurrency?\n",
      "\n",
      "πŸ“‘ Streaming Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking for two pieces of information: the current price of Bitcoin and the latest news about cryptocurrency. I need to address both parts of the query. The `CALL_CRYPTO` action is the most direct way to get the price of Bitcoin. I will start with that.\n",
      "   Action: CRYPTO\n",
      "   Justification: This action is specifically designed to retrieve cryptocurrency market data and prices, which directly answers the first part of the user's query about Bitcoin's price.\n",
      "\n",
      "πŸ”§ Calling Crypto Agent...\n",
      "\n",
      "πŸ“Š Crypto Agent Results:\n",
      "   I can only fetch the price of Bitcoin at the moment. Bitcoin's current price is $101,568. I cannot get the latest news about cryptocurrency.\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: I have successfully obtained the price of Bitcoin from the CRYPTO agent. However, the user's query also asks for the latest news about cryptocurrency, which the CRYPTO agent was unable to provide. To fulfill the second part of the request, I need to use an agent that can find current news. The SEARCH agent is the most suitable tool for this purpose.\n",
      "   Action: SEARCH\n",
      "   Justification: This action will allow me to find the latest news about cryptocurrency, which is the remaining piece of information needed to fully answer the user's query.\n",
      "\n",
      "πŸ”§ Calling Search Agent...\n",
      "\n",
      "πŸ“Š Search Agent Results:\n",
      "   Bitcoin's current price is $101,568.\n",
      "\n",
      "For the latest cryptocurrency news, you can check out these sources:\n",
      "\n",
      "*   **Crypto News:** https://cryptonews.com/\n",
      "*   **Forbes Digital Assets:** https://www.forb...\n",
      "\n",
      "πŸ’­ Step 3: Reasoning\n",
      "   Thought: I have successfully gathered the necessary information to answer the user's query. The CRYPTO and SEARCH agents both provided Bitcoin's current price, which is consistent. The SEARCH agent also provided a list of reliable sources for the latest cryptocurrency news. I do not need to call any more agents as I have a complete answer. I can now synthesize this information and provide the final response.\n",
      "   Action: FINISH\n",
      "   Justification: I have the price of Bitcoin and a list of sources for the latest cryptocurrency news. This is sufficient to fully answer the user's query.\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "The current price of Bitcoin is **$101,568**.\n",
      "\n",
      "For the latest news and in-depth coverage of the cryptocurrency market, you can refer to these reputable sources:\n",
      "\n",
      "*   **CoinDesk:** [https://www.coindesk.com/](https://www.coindesk.com/)\n",
      "*   **Cointelegraph:** [https://cointelegraph.com/](https://cointelegraph.com/)\n",
      "*   **Crypto News:** [https://cryptonews.com/](https://cryptonews.com/)\n",
      "*   **Forbes Digital Assets:** [https://www.forbes.com/digital-assets/news/](https://www.forbes.com/digital-assets/news/)\n",
      "*   **Reuters Cryptocurrency:** [https://www.reuters.com/markets/cryptocurrency/](https://www.reuters.com/markets/cryptocurrency/)\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The current price of Bitcoin is **$101,568**.\\n\\nFor the latest news and in-depth coverage of the cryptocurrency market, you can refer to these reputable sources:\\n\\n*   **CoinDesk:** [https://www.coindesk.com/](https://www.coindesk.com/)\\n*   **Cointelegraph:** [https://cointelegraph.com/](https://cointelegraph.com/)\\n*   **Crypto News:** [https://cryptonews.com/](https://cryptonews.com/)\\n*   **Forbes Digital Assets:** [https://www.forbes.com/digital-assets/news/](https://www.forbes.com/digital-assets/news/)\\n*   **Reuters Cryptocurrency:** [https://www.reuters.com/markets/cryptocurrency/](https://www.reuters.com/markets/cryptocurrency/)'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test multi-agent query (requires multiple agents)\n",
    "test_streaming_chat(\"What's Bitcoin's price and what's the latest news about cryptocurrency?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. WebSocket Chat\n",
    "\n",
    "Test the WebSocket endpoint for real-time bidirectional communication."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ’¬ WebSocket Query: What is Ethereum's current price?\n",
      "\n",
      "πŸ“‘ WebSocket Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking for the current price of Ethereum. Ethereum is a cryptocurrency. The `CALL_CRYPTO` action is specifically designed to get cryptocurrency market data and prices. Therefore, this is the most direct and appropriate action to take.\n",
      "   Action: CRYPTO\n",
      "\n",
      "πŸ”§ Calling Crypto Agent...\n",
      "\n",
      "πŸ“Š Crypto Agent Results\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: The user asked for the current price of Ethereum. The CRYPTO agent has already provided this information: \"The current price of Ethereum is $3423.75.\" I have a direct and complete answer to the user's query. Therefore, I can now finish.\n",
      "   Action: FINISH\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "Based on the latest market data, the current price of Ethereum (ETH) is **$3,423.75**.\n",
      "\n",
      "It is important to note that cryptocurrency prices are highly volatile and can change rapidly. This price reflects a snapshot at the time the data was retrieved.\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "async def test_websocket_chat(message, history=None):\n",
    "    \"\"\"Test WebSocket chat endpoint.\"\"\"\n",
    "    if history is None:\n",
    "        history = []\n",
    "    \n",
    "    ws_url = BASE_URL.replace('http://', 'ws://') + '/ws/v1/chat'\n",
    "    \n",
    "    print(f\"πŸ’¬ WebSocket Query: {message}\")\n",
    "    print(\"\\nπŸ“‘ WebSocket Response:\")\n",
    "    print(\"=\" * 80)\n",
    "    \n",
    "    try:\n",
    "        async with websockets.connect(ws_url) as websocket:\n",
    "            # Send message\n",
    "            await websocket.send(json.dumps({\n",
    "                \"message\": message,\n",
    "                \"history\": history\n",
    "            }))\n",
    "            \n",
    "            final_answer = \"\"\n",
    "            \n",
    "            # Receive streaming updates\n",
    "            while True:\n",
    "                try:\n",
    "                    response = await asyncio.wait_for(websocket.recv(), timeout=60.0)\n",
    "                    event = json.loads(response)\n",
    "                    event_type = event.get('type', 'unknown')\n",
    "                    \n",
    "                    if event_type == 'thinking':\n",
    "                        print(f\"\\nπŸ’­ Step {event.get('step', '?')}: Reasoning\")\n",
    "                        print(f\"   Thought: {event.get('thought', 'N/A')}\")\n",
    "                        print(f\"   Action: {event.get('action', 'N/A').upper()}\")\n",
    "                    \n",
    "                    elif event_type == 'action':\n",
    "                        agent = event.get('agent', 'unknown')\n",
    "                        print(f\"\\nπŸ”§ Calling {agent.title()} Agent...\")\n",
    "                    \n",
    "                    elif event_type == 'observation':\n",
    "                        agent = event.get('agent', 'unknown')\n",
    "                        summary = event.get('summary', '')\n",
    "                        print(f\"\\nπŸ“Š {agent.title()} Agent Results\")\n",
    "                    \n",
    "                    elif event_type == 'final_start':\n",
    "                        print(\"\\n✨ Final Answer:\")\n",
    "                        print(\"-\" * 80)\n",
    "                    \n",
    "                    elif event_type == 'final_token':\n",
    "                        final_answer = event.get('accumulated', '')\n",
    "                    \n",
    "                    elif event_type == 'final_complete':\n",
    "                        print(final_answer)\n",
    "                        print(\"-\" * 80)\n",
    "                        break\n",
    "                    \n",
    "                    elif event_type == 'error':\n",
    "                        print(f\"\\n❌ Error: {event.get('error', 'Unknown error')}\")\n",
    "                        break\n",
    "                \n",
    "                except asyncio.TimeoutError:\n",
    "                    print(\"\\n⏱️ Timeout waiting for response\")\n",
    "                    break\n",
    "            \n",
    "            print(\"\\n\" + \"=\" * 80)\n",
    "            return final_answer\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(f\"\\n❌ Error: {e}\")\n",
    "        return None\n",
    "\n",
    "# Test WebSocket\n",
    "ws_result = await test_websocket_chat(\"What is Ethereum's current price?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Document Upload\n",
    "\n",
    "Test uploading a document to the RAG agent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_document_upload(file_path):\n",
    "    \"\"\"Test document upload endpoint.\"\"\"\n",
    "    try:\n",
    "        if not Path(file_path).exists():\n",
    "            print(f\"❌ File not found: {file_path}\")\n",
    "            return None\n",
    "        \n",
    "        print(f\"πŸ“€ Uploading: {file_path}\")\n",
    "        \n",
    "        with open(file_path, 'rb') as f:\n",
    "            files = {'file': (Path(file_path).name, f)}\n",
    "            response = requests.post(\n",
    "                f\"{BASE_URL}/api/v1/documents/upload\",\n",
    "                files=files\n",
    "            )\n",
    "        \n",
    "        response.raise_for_status()\n",
    "        data = response.json()\n",
    "        \n",
    "        print(\"\\nβœ… Upload Response:\")\n",
    "        print(f\"  Success: {data['success']}\")\n",
    "        print(f\"  Message: {data['message']}\")\n",
    "        \n",
    "        if data.get('details'):\n",
    "            print(\"\\n  Details:\")\n",
    "            for key, value in data['details'].items():\n",
    "                print(f\"    {key}: {value}\")\n",
    "        \n",
    "        return data\n",
    "    \n",
    "    except requests.exceptions.HTTPError as e:\n",
    "        print(f\"\\n❌ HTTP Error: {e}\")\n",
    "        print(f\"   Response: {e.response.text}\")\n",
    "        return None\n",
    "    except Exception as e:\n",
    "        print(f\"\\n❌ Error: {e}\")\n",
    "        return None\n",
    "\n",
    "# Example: Upload a test file (update path to your actual file)\n",
    "test_document_upload(\"PATH_TO_YOUR_DOCUEMNT\")\n",
    "\n",
    "print(\"⚠️ To test document upload, uncomment the line above and provide a valid file path\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "βœ… Created test file: test_document.txt\n",
      "πŸ“€ Uploading: test_document.txt\n",
      "\n",
      "βœ… Upload Response:\n",
      "  Success: True\n",
      "  Message: Document uploaded successfully\n",
      "\n",
      "  Details:\n",
      "    filename: tmphdduvt25.txt\n",
      "    file_type: .txt\n",
      "    chunks_added: 1\n",
      "    total_documents: 229\n"
     ]
    }
   ],
   "source": [
    "# Create a test text file and upload it\n",
    "test_file_path = \"test_document.txt\"\n",
    "\n",
    "with open(test_file_path, 'w') as f:\n",
    "    f.write(\"\"\"\n",
    "    Test Document for RAG Agent\n",
    "    \n",
    "    This is a sample document for testing the RAG agent's document upload functionality.\n",
    "    \n",
    "    Key Information:\n",
    "    - The multi-agent system uses Google Gemini 2.5 Pro\n",
    "    - It implements a ReAct (Reasoning + Acting) pattern\n",
    "    - Available agents: Crypto, Stock, RAG, Search, Finance Tracker\n",
    "    - Each agent uses specialized MCP servers for their domain\n",
    "    \n",
    "    The system can handle co plex queries that require multiple agents working together.\n",
    "    \"\"\")\n",
    "\n",
    "print(f\"βœ… Created test file: {test_file_path}\")\n",
    "\n",
    "# Upload the test file\n",
    "upload_result = test_document_upload(test_file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ’¬ Query: What applications of AI agents are described in my collection?\n",
      "\n",
      "πŸ“‘ Streaming Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking about applications of AI agents described in \"my collection\". This indicates that the information should be retrieved from the user's uploaded documents. The `CALL_RAG` tool is designed for this purpose.\n",
      "   Action: RAG\n",
      "   Justification: The query specifically asks for information from \"my collection,\" which implies searching the user's uploaded documents. `CALL_RAG` is the appropriate tool to retrieve information from these documents.\n",
      "\n",
      "πŸ”§ Calling Rag Agent...\n",
      "\n",
      "πŸ“Š Rag Agent Results:\n",
      "   Based on the provided document excerpts, there is no information available regarding the current status of any project. The documents discuss applications of AI agents, challenges in their integration...\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: The user asked for \"What applications of AI agents are described in my collection?\". I previously called `CALL_RAG`, and the observation stated: \"The documents discuss applications of AI agents, challenges in their integration, and various machine learning models and their use cases.\" This confirms that the information about AI agent applications *is* present in the documents. However, the previous `RAG` call did not actually list these applications. I need to call `CALL_RAG` again, but this time with a more specific prompt to extract and list the applications of AI agents mentioned in the documents.\n",
      "   Action: RAG\n",
      "   Justification: The previous `CALL_RAG` observation confirmed that the documents contain information about \"applications of AI agents\". I need to call `CALL_RAG` again to specifically extract and list these applications, as the previous call did not provide them.\n",
      "\n",
      "πŸ”§ Calling Rag Agent...\n",
      "\n",
      "πŸ“Š Rag Agent Results:\n",
      "   Based on the provided document excerpts, there is no information available regarding the benefits of the Mediterranean diet for brain health. The documents discuss applications of AI agents, challenge...\n",
      "\n",
      "πŸ’­ Step 3: Reasoning\n",
      "   Thought: The user is asking for a list of AI agent applications from their collection. I have called `CALL_RAG` twice. The first call indicated that the documents discuss \"applications of AI agents\". The second call was problematic, mentioning \"Mediterranean diet\" while still stating that \"The documents discuss applications of AI agents\". This indicates that the RAG tool is acknowledging the presence of the information but failing to extract and list the specific applications. I need to call `CALL_RAG` again, but this time, I must ensure the prompt is very explicit about *listing* the applications to overcome the previous failures.\n",
      "   Action: RAG\n",
      "   Justification: The `CALL_RAG` tool is the only way to access information from the user's uploaded documents (\"my collection\"). Despite previous attempts, the tool has not yet provided the actual list of AI agent applications, even though it confirms the information is present. I need to try again with a more precise prompt to extract and list these applications.\n",
      "\n",
      "πŸ”§ Calling Rag Agent...\n",
      "\n",
      "πŸ“Š Rag Agent Results:\n",
      "   Based on the provided document excerpts, the following applications of AI agents are described:\n",
      "\n",
      "For **Marketing Professionals**:\n",
      "*   Content generation agents\n",
      "*   Market research agents\n",
      "*   Performan...\n",
      "\n",
      "πŸ’­ Step 4: Reasoning\n",
      "   Thought: I have successfully retrieved the applications of AI agents described in the user's collection using `CALL_RAG`. The information gathered directly answers the user's query.\n",
      "   Action: FINISH\n",
      "   Justification: I have sufficient information to answer the user's query about the applications of AI agents described in their collection.\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "Based on the documents in your collection, AI agents are described with specific applications for both marketing and sales professionals:\n",
      "\n",
      "**For Marketing Professionals:**\n",
      "*   **Content generation agents:** To assist in creating various forms of marketing content.\n",
      "*   **Market research agents:** For conducting research to understand market trends and opportunities.\n",
      "*   **Performance analytics agents:** To analyze the effectiveness of marketing campaigns and strategies.\n",
      "*   **Audience insight agents:** For gathering and interpreting data about target audiences.\n",
      "\n",
      "**For Sales Professionals:**\n",
      "*   **Prospect research agents:** To identify and gather information on potential clients.\n",
      "*   **Outreach customization agents:** For tailoring communication to individual prospects.\n",
      "*   **Meeting preparation agents:** To assist in preparing for sales meetings.\n",
      "*   **Follow-up management agents:** For organizing and executing post-meeting follow-up tasks.\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "# Now query the uploaded document\n",
    "if upload_result and upload_result.get('success'):\n",
    "    test_streaming_chat(\"What applications of AI agents are described in my collection?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Conversation History\n",
    "\n",
    "Test chat with conversation history."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "====================================================================================================\n",
      "MESSAGE 1\n",
      "====================================================================================================\n",
      "πŸ’¬ Query: What is Bitcoin?\n",
      "\n",
      "πŸ“‘ Streaming Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking for a definition of \"Bitcoin\". This is a general knowledge question. I do not have any information yet. The best tool for answering general knowledge questions is `CALL_SEARCH`. The other tools are more specialized: `CALL_CRYPTO` is for market data, not definitions; `CALL_STOCK` and `CALL_FINANCE_TRACKER` are for traditional finance; and `CALL_RAG` requires pre-existing documents. Therefore, I should use `CALL_SEARCH` to find out what Bitcoin is.\n",
      "   Action: SEARCH\n",
      "   Justification: The user's query \"What is Bitcoin?\" is a general knowledge question. The `CALL_SEARCH` action is the most appropriate tool to find a definition and explanation from the web.\n",
      "\n",
      "πŸ”§ Calling Search Agent...\n",
      "\n",
      "πŸ“Š Search Agent Results:\n",
      "   Bitcoin is a decentralized digital currency that allows for direct transactions between users without the need for an intermediary like a bank. It was invented in 2008 by an unknown entity or group us...\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: I have successfully used the `CALL_SEARCH` agent to get a clear and comprehensive definition of Bitcoin. The information gathered explains what it is, who created it, and the technology behind it. This is sufficient to answer the user's query. I do not need to call any other agents. I can now provide the final answer.\n",
      "   Action: FINISH\n",
      "   Justification: I have a complete answer to the user's question \"What is Bitcoin?\" from the previous `CALL_SEARCH` action. No further information is required.\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "Based on the information gathered, here is a comprehensive answer to your query.\n",
      "\n",
      "### What is Bitcoin?\n",
      "\n",
      "Bitcoin is a decentralized digital currency that enables direct, peer-to-peer transactions between users without the need for an intermediary like a bank or financial institution. It was the first cryptocurrency to be created and remains the most well-known.\n",
      "\n",
      "#### Key Features:*   **Decentralized:** Unlike traditional currencies issued by governments (like the U.S. Dollar), Bitcoin is not controlled by any single entity. This means it is resistant to censorship and manipulation by a central authority.\n",
      "*   **Digital Nature:** Bitcoin exists only in the digital realm. It is not a physical coin or bill but a balance associated with a digital address.\n",
      "*   **Peer-to-Peer Technology:** Transactions are sent directly from one user to another across a global network of computers.\n",
      "*   **Blockchain Technology:** All transactions are recorded on a public, distributed ledger called the blockchain. This technology ensures transparency and security, as every transaction is permanent and visible to everyone on the network.\n",
      "*   **Cryptography:** Advanced cryptographic techniques are used to secure the network, verify transactions, and control the creation of new bitcoins.\n",
      "\n",
      "#### Origins:\n",
      "\n",
      "Bitcoin was invented in 2008 by an unknown person or group of people using the pseudonym **Satoshi Nakamoto**. The network went live in 2009, marking the beginning of its use as a currency.\n",
      "\n",
      "***\n",
      "*Sources: Forbes Advisor, Investopedia, Bitcoin.org, Analytics Insight, Wikipedia, Britannica Money, Crypto.com, Cointelegraph*\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n",
      "\n",
      "====================================================================================================\n",
      "MESSAGE 2 (with context from previous message)\n",
      "====================================================================================================\n",
      "πŸ’¬ Query: What is its current price?\n",
      "\n",
      "πŸ“‘ Streaming Response:\n",
      "================================================================================\n",
      "\n",
      "πŸ’­ Step 1: Reasoning\n",
      "   Thought: The user is asking for the current price of \"it\". Based on the previous turn in the conversation history, \"it\" refers to Bitcoin. To answer this question, I need to get the current market price of Bitcoin. The `CALL_CRYPTO` tool is specifically designed to retrieve cryptocurrency prices.\n",
      "   Action: CRYPTO\n",
      "   Justification: This action will provide the real-time price of Bitcoin, which is the specific information needed to answer the user's query.\n",
      "\n",
      "πŸ”§ Calling Crypto Agent...\n",
      "\n",
      "πŸ“Š Crypto Agent Results:\n",
      "   The current price of Bitcoin is $98,857. Over the last 24 hours, its price has changed by -2.74%, and its 24-hour trading volume is $98,820,417,484.34. The market cap for Bitcoin is $1,970,383,895,456...\n",
      "\n",
      "πŸ’­ Step 2: Reasoning\n",
      "   Thought: I have successfully retrieved the current price of Bitcoin using the `CALL_CRYPTO` agent. The information gathered directly answers the user's query. Therefore, I have sufficient information and should now provide the final answer.\n",
      "   Action: FINISH\n",
      "   Justification: The `CALL_CRYPTO` agent provided the current price of Bitcoin, which is exactly what the user asked for. No further information is needed.\n",
      "\n",
      "✨ Final Answer:\n",
      "--------------------------------------------------------------------------------\n",
      "Based on real-time market data, the current price of Bitcoin is **$98,857**.\n",
      "\n",
      "Here is some additional market information:\n",
      "\n",
      "*   **24-Hour Price Change:** -2.74%\n",
      "*   **Market Cap:** $1,970,383,895,456.65*   **24-Hour Trading Volume:** $98,820,417,484.34\n",
      "\n",
      "***\n",
      "*Please note: Cryptocurrency prices are highly volatile and can change rapidly. This information reflects the market at the time this query was made.*\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "# Start a conversation\n",
    "history = []\n",
    "\n",
    "# First message\n",
    "print(\"\\n\" + \"=\"*100)\n",
    "print(\"MESSAGE 1\")\n",
    "print(\"=\"*100)\n",
    "result1 = test_streaming_chat(\"What is Bitcoin?\", history=history)\n",
    "history.append({\"role\": \"user\", \"content\": \"What is Bitcoin?\"})\n",
    "history.append({\"role\": \"assistant\", \"content\": result1})\n",
    "\n",
    "time.sleep(2)\n",
    "\n",
    "# Follow-up question (with context)\n",
    "print(\"\\n\" + \"=\"*100)\n",
    "print(\"MESSAGE 2 (with context from previous message)\")\n",
    "print(\"=\"*100)\n",
    "result2 = test_streaming_chat(\"What is its current price?\", history=history)\n",
    "history.append({\"role\": \"user\", \"content\": \"What is its current price?\"})\n",
    "history.append({\"role\": \"assistant\", \"content\": result2})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Performance Testing\n",
    "\n",
    "Test response times and measure performance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "⏱️ Performance Metrics for: 'What is Bitcoin price?'\n",
      "   Time to first token: 14.65s\n",
      "   Total response time: 36.27s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'first_token': 14.646224975585938, 'total': 36.274803161621094}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "def measure_response_time(query):\n",
    "    \"\"\"Measure response time for a query.\"\"\"\n",
    "    payload = {\"message\": query, \"history\": []}\n",
    "    \n",
    "    start_time = time.time()\n",
    "    first_token_time = None\n",
    "    \n",
    "    try:\n",
    "        response = requests.post(\n",
    "            f\"{BASE_URL}/api/v1/chat/stream\",\n",
    "            json=payload,\n",
    "            headers={\"Accept\": \"text/event-stream\"},\n",
    "            stream=True\n",
    "        )\n",
    "        \n",
    "        for i, line in enumerate(response.iter_lines()):\n",
    "            if i == 0 and first_token_time is None:\n",
    "                first_token_time = time.time() - start_time\n",
    "            if line and b'final_complete' in line:\n",
    "                break\n",
    "        \n",
    "        total_time = time.time() - start_time\n",
    "        \n",
    "        print(f\"\\n⏱️ Performance Metrics for: '{query}'\")\n",
    "        print(f\"   Time to first token: {first_token_time:.2f}s\")\n",
    "        print(f\"   Total response time: {total_time:.2f}s\")\n",
    "        \n",
    "        return {\"first_token\": first_token_time, \"total\": total_time}\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(f\"❌ Error: {e}\")\n",
    "        return None\n",
    "\n",
    "# Test with simple query\n",
    "measure_response_time(\"What is Bitcoin price?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Error Handling Tests\n",
    "\n",
    "Test how the API handles various error conditions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test empty message\n",
    "print(\"Testing empty message:\")\n",
    "test_streaming_chat(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test invalid file upload\n",
    "print(\"Testing invalid file type:\")\n",
    "\n",
    "invalid_file_path = \"/tmp/test.exe\"\n",
    "with open(invalid_file_path, 'w') as f:\n",
    "    f.write(\"invalid content\")\n",
    "\n",
    "test_document_upload(invalid_file_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. API Documentation\n",
    "\n",
    "The FastAPI server provides automatic interactive documentation.\n",
    "\n",
    "**Access the documentation at:**\n",
    "- Swagger UI: http://localhost:8000/docs\n",
    "- ReDoc: http://localhost:8000/redoc\n",
    "\n",
    "You can test all endpoints directly from the browser using the Swagger UI!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "πŸ“š FastAPI Interactive Documentation\n",
      "\n",
      "πŸ”— Swagger UI: http://localhost:8000/docs\n",
      "πŸ”— ReDoc: http://localhost:8000/redoc\n"
     ]
    }
   ],
   "source": [
    "from IPython.display import IFrame\n",
    "\n",
    "# Display Swagger UI in notebook (if server is running)\n",
    "print(\"πŸ“š FastAPI Interactive Documentation\")\n",
    "print(f\"\\nπŸ”— Swagger UI: {BASE_URL}/docs\")\n",
    "print(f\"πŸ”— ReDoc: {BASE_URL}/redoc\")\n",
    "\n",
    "# Uncomment to embed in notebook\n",
    "# IFrame(f\"{BASE_URL}/docs\", width=1000, height=600)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv (3.11.5)",
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}