File size: 94,636 Bytes
7b5e0ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/aakashbhatnagar/Documents/masters/ophthal_llm/myenv/lib/python3.10/site-packages/pinecone/index.py:4: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from tqdm.autonotebook import tqdm\n"
     ]
    }
   ],
   "source": [
    "from langchain.document_loaders import TextLoader\n",
    "import pinecone\n",
    "from langchain.vectorstores import Pinecone\n",
    "import os\n",
    "from transformers import AutoTokenizer, AutoModel\n",
    "from langchain.agents.agent_toolkits import create_conversational_retrieval_agent\n",
    "from langchain.agents.agent_toolkits import create_retriever_tool\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "import torch\n",
    "from langchain.agents.openai_functions_agent.agent_token_buffer_memory import (AgentTokenBufferMemory)\n",
    "from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent\n",
    "from langchain.schema.messages import SystemMessage\n",
    "from langchain.prompts import MessagesPlaceholder\n",
    "import gradio as gr\n",
    "import time\n",
    "\n",
    "# loader = TextLoader(\"../../../../../docs/docs/modules/state_of_the_union.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/aakashbhatnagar/Documents/masters/ophthal_llm/myenv/lib/python3.10/site-packages/langchain/vectorstores/pinecone.py:59: UserWarning: Passing in `embedding` as a Callable is deprecated. Please pass in an Embeddings object instead.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "from conversation import make_conversation, auth_function\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "demo = gr.ChatInterface(make_conversation).queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7863\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on public URL: https://36c80e8ac1f1026dec.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co./spaces)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://36c80e8ac1f1026dec.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "demo.launch(share=True, auth=auth_function)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Retrieval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "pinecone.init(\n",
    "    api_key=os.getenv(\"PINECONE_API_KEY\"),  # find at app.pinecone.io\n",
    "    environment=os.getenv(\"PINECONE_ENV\"),  # next to api key in console\n",
    ")\n",
    "\n",
    "index_name = \"ophtal-knowledge-base\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = AutoModel.from_pretrained(\"models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e\")\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = open(\"prompts/version_2.txt\", \"r\").read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_bert_embeddings(sentence):\n",
    "    embeddings = []\n",
    "    input_ids = tokenizer.encode(sentence, return_tensors=\"pt\")\n",
    "    with torch.no_grad():\n",
    "        output = model(input_ids)\n",
    "    embedding = output.last_hidden_state[:,0,:].numpy().tolist()\n",
    "    return embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/aakashbhatnagar/Documents/masters/ophthal_llm/myenv/lib/python3.10/site-packages/langchain/vectorstores/pinecone.py:59: UserWarning: Passing in `embedding` as a Callable is deprecated. Please pass in an Embeddings object instead.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "index = pinecone.Index(index_name)\n",
    "vectorstore = Pinecone(index, get_bert_embeddings, \"text\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = vectorstore.as_retriever()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "tool = create_retriever_tool(\n",
    "    retriever,\n",
    "    \"search_ophtal-knowledge-base\",\n",
    "    \"Searches and returns documents regarding the ophtal-knowledge-base.\",\n",
    ")\n",
    "tools = [tool]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(openai_api_key=os.getenv(\"OPENAI_API_KEY\"), openai_organization=os.getenv(\"ORGANIZATION_KEY\"), model=\"gpt-4\", temperature=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "system_message = SystemMessage(\n",
    "    content=prompt,\n",
    ")\n",
    "\n",
    "memory_key='history'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = OpenAIFunctionsAgent.create_prompt(\n",
    "    system_message=system_message,\n",
    "    extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=True, prompt=prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[Document(page_content='The essential process is probably anaerobic and in the   \\nlens there are a number of enzymes which break down \\npyruvate to lactic acid and water. Lactic acid is found in \\nconsiderable quantity in the aqueous humour when the lens \\nis present; this is not so in the aphakic eye. Agents which \\nappear to participate in this process are glutathione and \\nascorbic acid (vitamin C) which, reacting together, proba-\\nbly participate in an internal auto-oxidative system. The \\nformer, both as reduced and oxidized glutathione, occurs \\nin very high concentration in the lens, particularly in the \\ncortex; the latter is specially secreted by the ciliary body. \\nNeither is present in a cataract. The metabolic activity of \\nthe lens is largely confined to the cortex; the older nucleus \\nis relatively inert.\\nThe metabolism of the lens is fairly complex but   \\ncan be simplified for an overview ( Fig. 2.3 ). The energy \\nrequirements are met by various pathways of glucose   \\nmetabolism, namely the glycolysis (Embden–Meyerhoff–\\nParnas [EMP]) pathway, pentose phosphate pathway   \\nand the tricarboxylic acid (TCA) cycle. Energy is stored \\nin the form of adenosine triphosphate (ATP). Most of', metadata={'source': 'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'}), Document(page_content='chloride and bicarbonate. Phosphate is also present in the aqueous (aqueous-to-plasma ratio,\\n≈0.5 or lower), but its concentration is too low to have significant buffering capacity. Iron,\\n319\\ncopper, and zinc are all found in the aqueous humor at essentially the same levels as in plasma:\\napproximately 1 mg/mL.\\nOrganic Anions\\nLactate is the most abundant organic anion in the aqueous, and its concentration there is always\\nhigher than that in plasma. The high lactate level in aqueous is a result of glycolytic metabolism,\\nupon which the avascular lens depends.\\nAscorbate (vitamin C) levels in aqueous are much higher (some 10–50 times higher) than\\nthose in plasma. Ascorbate has antioxidant properties, and its high concentration in the aqueous\\nprotects intraocular structures by blocking UV light.\\nCarbohydrates\\nGlucose concentration in the aqueous is roughly 50%–70% of that in plasma. The rate of entry\\nof glucose into the posterior chamber is much more rapid than would be expected from its\\nmolecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\nepithelium occurs by facilitated diffusion.\\nCLINICAL PEARL', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='of intraocular hemorrhage. In both conditions, excess Fe2+ can accumulate in the trabecular\\nmeshwork, neurosensory retina, and retinal pigment epithelium (RPE), leading to secondary\\ndysfunction. See BCSC Section 11, Lens and Cataract, and Section 12, Retina and Vitreous, for\\nadditional discussion of siderosis bulbi.\\nOxidative Damage to the Lens and Protective Mechanisms\\nAs stated earlier, ROS are generated by metabolic processes, inflammatory responses, and\\nexposure to UV light. The lens relies almost entirely on anaerobic metabolism and is shielded\\nfrom the immune system. Thus, the major source of ROS in the lens is exposure to UV light.\\nAlthough most UVB radiation (<320-nm wavelength) striking the human eye is absorbed either\\nby the cornea or by the ascorbate present at high levels in the aqueous humor, a certain amount\\nreaches the lens epithelium, where it can cause damage. UVA light (320–400-nm wavelength)\\ncan penetrate more deeply into the lens, where it can react with various chromophores to\\ngenerate H2O2, O2•−, and 1O2.\\nAlthough repair and regeneration mechanisms are active in the lens epithelium and superficial', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='for evaluation of general health and for preoperative planning for cataract surgery.\\n 2. d.  After fetal development, the lens has no blood supply or organelles and depends on the \\naqueous humor for removal of metabolic waste. Changes in lens size occur throughout life \\nas the lens epithelial cells (LECs) at the equator continue to divide.\\n 3. d. Superoxide dismutase, catalase, and glutathione perioxidase work together to destroy \\nthe superoxide anion (O−\\n2), protecting the lens against oxidative and  free radical damage. \\n There are no known repair mechanisms for  free radical damage to proteins or membrane \\nlipids in the lens cortex. Glutathione acts as a major  free radical scavenger along with \\nvitamin E and ascorbic acid in the lens.  These 3 enzymes have no effect on the pH of the \\nlens and do not participate in the carbohydrate metabolism of the lens. Conversion from \\nwater- soluble to water- insoluble proteins appears to be a natu ral (age- relate d) pro cess in \\nlens fiber maturation and does not involve an enzymatic function.\\n 4. a. When glucose concentration increases in the lens, the sorbitol pathway is activated;', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'})]\u001b[0m\u001b[32;1m\u001b[1;3mThe transport of ascorbic acid to the lens is not directly mentioned in the documents. However, ascorbic acid (vitamin C) is known to be present in high concentrations in the aqueous humor, which nourishes the lens. This suggests that the transport mechanism could involve diffusion from the aqueous humor, but the specific transport molecule or mechanism is not specified in the provided documents. Therefore, based on the available information, a definitive answer cannot be given.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor({\"input\": \"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\n\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The essential process is probably anaerobic and in the   \\nlens there are a number of enzymes which break down \\npyruvate to lactic acid and water. Lactic acid is found in \\nconsiderable quantity in the aqueous humour when the lens \\nis present; this is not so in the aphakic eye. Agents which \\nappear to participate in this process are glutathione and \\nascorbic acid (vitamin C) which, reacting together, proba-\\nbly participate in an internal auto-oxidative system. The \\nformer, both as reduced and oxidized glutathione, occurs \\nin very high concentration in the lens, particularly in the \\ncortex; the latter is specially secreted by the ciliary body. \\nNeither is present in a cataract. The metabolic activity of \\nthe lens is largely confined to the cortex; the older nucleus \\nis relatively inert.\\nThe metabolism of the lens is fairly complex but   \\ncan be simplified for an overview ( Fig. 2.3 ). The energy \\nrequirements are met by various pathways of glucose   \\nmetabolism, namely the glycolysis (Embden–Meyerhoff–\\nParnas [EMP]) pathway, pentose phosphate pathway   \\nand the tricarboxylic acid (TCA) cycle. Energy is stored \\nin the form of adenosine triphosphate (ATP). Most of'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"intermediate_steps\"][0][1][0].page_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run(input_):\n",
    "    output = agent_executor({\"input\": input_})\n",
    "    output_text = output[\"output\"]\n",
    "    if len(output[\"intermediate_steps\"])>0:\n",
    "        documents = output[\"intermediate_steps\"][0][1]\n",
    "        sources = []\n",
    "        for doc in documents:\n",
    "            print(doc.metadata)\n",
    "            sources.append(doc.metadata[\"source\"])\n",
    "        \n",
    "        source_text = \"\"\n",
    "        for i in range(len(sources)):\n",
    "            source_text += f\"{i+1}. {sources[i]}\\n\"\n",
    "\n",
    "        return f\"{output_text} \\n\\nSources: \\n{source_text}\"\n",
    "    return output_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def slow_echo(message, history):\n",
    "    text_ = run(message)\n",
    "    for i in range(len(text_)):\n",
    "        time.sleep(0.001)\n",
    "        yield text_[: i+1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def auth_function(username, password):\n",
    "    return username == password"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThe transport of ascorbic acid to the lens is done by:\n",
      "\n",
      "A. Myoinositol\n",
      "B. Choline\n",
      "C. Taurine\n",
      "D. Na/K ATPase\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "ques = run(\"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\nReturn options\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The transport of ascorbic acid to the lens is done by:\\n\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ques"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "demo = gr.ChatInterface(slow_echo).queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7861\n",
      "Running on public URL: https://c73fe7008b858e476a.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co./spaces)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://c73fe7008b858e476a.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "demo.launch(share=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# gr.ChatInterface(\n",
    "#     run,\n",
    "#     ).launch(share=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mYour name is Bob.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor({\"input\": \"whats my name?\"})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transport of ascorbic acid to lens is done by:\n",
      "A. Myoinositol\n",
      "B. Choline\n",
      "C. Taurine\n",
      "D. Na/K ATPase\n",
      "\n",
      "Return options\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[Document(page_content='The essential process is probably anaerobic and in the   \\nlens there are a number of enzymes which break down \\npyruvate to lactic acid and water. Lactic acid is found in \\nconsiderable quantity in the aqueous humour when the lens \\nis present; this is not so in the aphakic eye. Agents which \\nappear to participate in this process are glutathione and \\nascorbic acid (vitamin C) which, reacting together, proba-\\nbly participate in an internal auto-oxidative system. The \\nformer, both as reduced and oxidized glutathione, occurs \\nin very high concentration in the lens, particularly in the \\ncortex; the latter is specially secreted by the ciliary body. \\nNeither is present in a cataract. The metabolic activity of \\nthe lens is largely confined to the cortex; the older nucleus \\nis relatively inert.\\nThe metabolism of the lens is fairly complex but   \\ncan be simplified for an overview ( Fig. 2.3 ). The energy \\nrequirements are met by various pathways of glucose   \\nmetabolism, namely the glycolysis (Embden–Meyerhoff–\\nParnas [EMP]) pathway, pentose phosphate pathway   \\nand the tricarboxylic acid (TCA) cycle. Energy is stored \\nin the form of adenosine triphosphate (ATP). Most of', metadata={'source': 'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'}), Document(page_content='chloride and bicarbonate. Phosphate is also present in the aqueous (aqueous-to-plasma ratio,\\n≈0.5 or lower), but its concentration is too low to have significant buffering capacity. Iron,\\n319\\ncopper, and zinc are all found in the aqueous humor at essentially the same levels as in plasma:\\napproximately 1 mg/mL.\\nOrganic Anions\\nLactate is the most abundant organic anion in the aqueous, and its concentration there is always\\nhigher than that in plasma. The high lactate level in aqueous is a result of glycolytic metabolism,\\nupon which the avascular lens depends.\\nAscorbate (vitamin C) levels in aqueous are much higher (some 10–50 times higher) than\\nthose in plasma. Ascorbate has antioxidant properties, and its high concentration in the aqueous\\nprotects intraocular structures by blocking UV light.\\nCarbohydrates\\nGlucose concentration in the aqueous is roughly 50%–70% of that in plasma. The rate of entry\\nof glucose into the posterior chamber is much more rapid than would be expected from its\\nmolecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\nepithelium occurs by facilitated diffusion.\\nCLINICAL PEARL', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='of intraocular hemorrhage. In both conditions, excess Fe2+ can accumulate in the trabecular\\nmeshwork, neurosensory retina, and retinal pigment epithelium (RPE), leading to secondary\\ndysfunction. See BCSC Section 11, Lens and Cataract, and Section 12, Retina and Vitreous, for\\nadditional discussion of siderosis bulbi.\\nOxidative Damage to the Lens and Protective Mechanisms\\nAs stated earlier, ROS are generated by metabolic processes, inflammatory responses, and\\nexposure to UV light. The lens relies almost entirely on anaerobic metabolism and is shielded\\nfrom the immune system. Thus, the major source of ROS in the lens is exposure to UV light.\\nAlthough most UVB radiation (<320-nm wavelength) striking the human eye is absorbed either\\nby the cornea or by the ascorbate present at high levels in the aqueous humor, a certain amount\\nreaches the lens epithelium, where it can cause damage. UVA light (320–400-nm wavelength)\\ncan penetrate more deeply into the lens, where it can react with various chromophores to\\ngenerate H2O2, O2•−, and 1O2.\\nAlthough repair and regeneration mechanisms are active in the lens epithelium and superficial', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='for evaluation of general health and for preoperative planning for cataract surgery.\\n 2. d.  After fetal development, the lens has no blood supply or organelles and depends on the \\naqueous humor for removal of metabolic waste. Changes in lens size occur throughout life \\nas the lens epithelial cells (LECs) at the equator continue to divide.\\n 3. d. Superoxide dismutase, catalase, and glutathione perioxidase work together to destroy \\nthe superoxide anion (O−\\n2), protecting the lens against oxidative and  free radical damage. \\n There are no known repair mechanisms for  free radical damage to proteins or membrane \\nlipids in the lens cortex. Glutathione acts as a major  free radical scavenger along with \\nvitamin E and ascorbic acid in the lens.  These 3 enzymes have no effect on the pH of the \\nlens and do not participate in the carbohydrate metabolism of the lens. Conversion from \\nwater- soluble to water- insoluble proteins appears to be a natu ral (age- relate d) pro cess in \\nlens fiber maturation and does not involve an enzymatic function.\\n 4. a. When glucose concentration increases in the lens, the sorbitol pathway is activated;', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'})]\u001b[0m\u001b[32;1m\u001b[1;3mThe transport of ascorbic acid (vitamin C) to the lens is not directly facilitated by any of the options you provided (Myoinositol, Choline, Taurine, Na/K ATPase). Ascorbic acid is secreted by the ciliary body and it is present in high concentrations in the aqueous humor, which bathes the lens. It serves as an antioxidant and helps protect intraocular structures by blocking UV light. The lens, which lacks its own blood supply, depends on the aqueous humor for the delivery of nutrients and removal of metabolic waste.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mYes, your name is Aakash.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "print(\"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\nReturn options\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[Document(page_content='The essential process is probably anaerobic and in the   \\nlens there are a number of enzymes which break down \\npyruvate to lactic acid and water. Lactic acid is found in \\nconsiderable quantity in the aqueous humour when the lens \\nis present; this is not so in the aphakic eye. Agents which \\nappear to participate in this process are glutathione and \\nascorbic acid (vitamin C) which, reacting together, proba-\\nbly participate in an internal auto-oxidative system. The \\nformer, both as reduced and oxidized glutathione, occurs \\nin very high concentration in the lens, particularly in the \\ncortex; the latter is specially secreted by the ciliary body. \\nNeither is present in a cataract. The metabolic activity of \\nthe lens is largely confined to the cortex; the older nucleus \\nis relatively inert.\\nThe metabolism of the lens is fairly complex but   \\ncan be simplified for an overview ( Fig. 2.3 ). The energy \\nrequirements are met by various pathways of glucose   \\nmetabolism, namely the glycolysis (Embden–Meyerhoff–\\nParnas [EMP]) pathway, pentose phosphate pathway   \\nand the tricarboxylic acid (TCA) cycle. Energy is stored \\nin the form of adenosine triphosphate (ATP). Most of', metadata={'source': 'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'}), Document(page_content='chloride and bicarbonate. Phosphate is also present in the aqueous (aqueous-to-plasma ratio,\\n≈0.5 or lower), but its concentration is too low to have significant buffering capacity. Iron,\\n319\\ncopper, and zinc are all found in the aqueous humor at essentially the same levels as in plasma:\\napproximately 1 mg/mL.\\nOrganic Anions\\nLactate is the most abundant organic anion in the aqueous, and its concentration there is always\\nhigher than that in plasma. The high lactate level in aqueous is a result of glycolytic metabolism,\\nupon which the avascular lens depends.\\nAscorbate (vitamin C) levels in aqueous are much higher (some 10–50 times higher) than\\nthose in plasma. Ascorbate has antioxidant properties, and its high concentration in the aqueous\\nprotects intraocular structures by blocking UV light.\\nCarbohydrates\\nGlucose concentration in the aqueous is roughly 50%–70% of that in plasma. The rate of entry\\nof glucose into the posterior chamber is much more rapid than would be expected from its\\nmolecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\nepithelium occurs by facilitated diffusion.\\nCLINICAL PEARL', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='of intraocular hemorrhage. In both conditions, excess Fe2+ can accumulate in the trabecular\\nmeshwork, neurosensory retina, and retinal pigment epithelium (RPE), leading to secondary\\ndysfunction. See BCSC Section 11, Lens and Cataract, and Section 12, Retina and Vitreous, for\\nadditional discussion of siderosis bulbi.\\nOxidative Damage to the Lens and Protective Mechanisms\\nAs stated earlier, ROS are generated by metabolic processes, inflammatory responses, and\\nexposure to UV light. The lens relies almost entirely on anaerobic metabolism and is shielded\\nfrom the immune system. Thus, the major source of ROS in the lens is exposure to UV light.\\nAlthough most UVB radiation (<320-nm wavelength) striking the human eye is absorbed either\\nby the cornea or by the ascorbate present at high levels in the aqueous humor, a certain amount\\nreaches the lens epithelium, where it can cause damage. UVA light (320–400-nm wavelength)\\ncan penetrate more deeply into the lens, where it can react with various chromophores to\\ngenerate H2O2, O2•−, and 1O2.\\nAlthough repair and regeneration mechanisms are active in the lens epithelium and superficial', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='for evaluation of general health and for preoperative planning for cataract surgery.\\n 2. d.  After fetal development, the lens has no blood supply or organelles and depends on the \\naqueous humor for removal of metabolic waste. Changes in lens size occur throughout life \\nas the lens epithelial cells (LECs) at the equator continue to divide.\\n 3. d. Superoxide dismutase, catalase, and glutathione perioxidase work together to destroy \\nthe superoxide anion (O−\\n2), protecting the lens against oxidative and  free radical damage. \\n There are no known repair mechanisms for  free radical damage to proteins or membrane \\nlipids in the lens cortex. Glutathione acts as a major  free radical scavenger along with \\nvitamin E and ascorbic acid in the lens.  These 3 enzymes have no effect on the pH of the \\nlens and do not participate in the carbohydrate metabolism of the lens. Conversion from \\nwater- soluble to water- insoluble proteins appears to be a natu ral (age- relate d) pro cess in \\nlens fiber maturation and does not involve an enzymatic function.\\n 4. a. When glucose concentration increases in the lens, the sorbitol pathway is activated;', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'})]\u001b[0m\u001b[32;1m\u001b[1;3mBased on the information from the ophthalmology knowledge base, the transport of ascorbic acid (vitamin C) to the lens is not directly facilitated by any of the options provided (Myoinositol, Choline, Taurine, Na/K ATPase). However, ascorbic acid is secreted by the ciliary body and is present in high concentrations in the aqueous humor, which bathes the lens. This suggests that the transport of ascorbic acid to the lens is facilitated by the aqueous humor.\n",
      "\n",
      "Here is the JSON output:\n",
      "\n",
      "{\"options\": \"None of the above\", \"explanation\": \"The transport of ascorbic acid to the lens is not directly facilitated by Myoinositol, Choline, Taurine, or Na/K ATPase. Instead, ascorbic acid is secreted by the ciliary body and is present in high concentrations in the aqueous humor, which bathes the lens.\"}\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor(\n",
    "    {\n",
    "        \"input\": \"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\nReturn options, and explanation in json format. Output should look like: {\\\"options\\\": \\\"a\\\", \\\"explanation\\\": \\\"This is the explanation\\\"}\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"intermediate_steps\"][0][1][0].metadata['source']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\nReturn options, and explanation in json format. Output should look like: {\"options\": \"a\", \"explanation\": \"This is the explanation\"}',\n",
       " 'chat_history': [HumanMessage(content='hi, im bob'),\n",
       "  AIMessage(content='Hello Bob! How can I assist you today?'),\n",
       "  HumanMessage(content='Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\nReturn options, and explanation in json format. Output should look like: {\"options\": \"a\", \"explanation\": \"This is the explanation\"}'),\n",
       "  AIMessage(content='', additional_kwargs={'function_call': {'name': 'search_ophtal-knowledge-base', 'arguments': '{\\n  \"__arg1\": \"Transport of ascorbic acid to lens\"\\n}'}}),\n",
       "  FunctionMessage(content=\"[Document(page_content='The essential process is probably anaerobic and in the   \\\\nlens there are a number of enzymes which break down \\\\npyruvate to lactic acid and water. Lactic acid is found in \\\\nconsiderable quantity in the aqueous humour when the lens \\\\nis present; this is not so in the aphakic eye. Agents which \\\\nappear to participate in this process are glutathione and \\\\nascorbic acid (vitamin C) which, reacting together, proba-\\\\nbly participate in an internal auto-oxidative system. The \\\\nformer, both as reduced and oxidized glutathione, occurs \\\\nin very high concentration in the lens, particularly in the \\\\ncortex; the latter is specially secreted by the ciliary body. \\\\nNeither is present in a cataract. The metabolic activity of \\\\nthe lens is largely confined to the cortex; the older nucleus \\\\nis relatively inert.\\\\nThe metabolism of the lens is fairly complex but   \\\\ncan be simplified for an overview ( Fig. 2.3 ). The energy \\\\nrequirements are met by various pathways of glucose   \\\\nmetabolism, namely the glycolysis (Embden–Meyerhoff–\\\\nParnas [EMP]) pathway, pentose phosphate pathway   \\\\nand the tricarboxylic acid (TCA) cycle. Energy is stored \\\\nin the form of adenosine triphosphate (ATP). Most of', metadata={'source': 'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'}), Document(page_content='chloride and bicarbonate. Phosphate is also present in the aqueous (aqueous-to-plasma ratio,\\\\n≈0.5 or lower), but its concentration is too low to have significant buffering capacity. Iron,\\\\n319\\\\ncopper, and zinc are all found in the aqueous humor at essentially the same levels as in plasma:\\\\napproximately 1 mg/mL.\\\\nOrganic Anions\\\\nLactate is the most abundant organic anion in the aqueous, and its concentration there is always\\\\nhigher than that in plasma. The high lactate level in aqueous is a result of glycolytic metabolism,\\\\nupon which the avascular lens depends.\\\\nAscorbate (vitamin C) levels in aqueous are much higher (some 10–50 times higher) than\\\\nthose in plasma. Ascorbate has antioxidant properties, and its high concentration in the aqueous\\\\nprotects intraocular structures by blocking UV light.\\\\nCarbohydrates\\\\nGlucose concentration in the aqueous is roughly 50%–70% of that in plasma. The rate of entry\\\\nof glucose into the posterior chamber is much more rapid than would be expected from its\\\\nmolecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\\\nepithelium occurs by facilitated diffusion.\\\\nCLINICAL PEARL', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='of intraocular hemorrhage. In both conditions, excess Fe2+ can accumulate in the trabecular\\\\nmeshwork, neurosensory retina, and retinal pigment epithelium (RPE), leading to secondary\\\\ndysfunction. See BCSC Section 11, Lens and Cataract, and Section 12, Retina and Vitreous, for\\\\nadditional discussion of siderosis bulbi.\\\\nOxidative Damage to the Lens and Protective Mechanisms\\\\nAs stated earlier, ROS are generated by metabolic processes, inflammatory responses, and\\\\nexposure to UV light. The lens relies almost entirely on anaerobic metabolism and is shielded\\\\nfrom the immune system. Thus, the major source of ROS in the lens is exposure to UV light.\\\\nAlthough most UVB radiation (<320-nm wavelength) striking the human eye is absorbed either\\\\nby the cornea or by the ascorbate present at high levels in the aqueous humor, a certain amount\\\\nreaches the lens epithelium, where it can cause damage. UVA light (320–400-nm wavelength)\\\\ncan penetrate more deeply into the lens, where it can react with various chromophores to\\\\ngenerate H2O2, O2•−, and 1O2.\\\\nAlthough repair and regeneration mechanisms are active in the lens epithelium and superficial', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='for evaluation of general health and for preoperative planning for cataract surgery.\\\\n 2. d.  After fetal development, the lens has no blood supply or organelles and depends on the \\\\naqueous humor for removal of metabolic waste. Changes in lens size occur throughout life \\\\nas the lens epithelial cells (LECs) at the equator continue to divide.\\\\n 3. d. Superoxide dismutase, catalase, and glutathione perioxidase work together to destroy \\\\nthe superoxide anion (O−\\\\n2), protecting the lens against oxidative and  free radical damage. \\\\n There are no known repair mechanisms for  free radical damage to proteins or membrane \\\\nlipids in the lens cortex. Glutathione acts as a major  free radical scavenger along with \\\\nvitamin E and ascorbic acid in the lens.  These 3 enzymes have no effect on the pH of the \\\\nlens and do not participate in the carbohydrate metabolism of the lens. Conversion from \\\\nwater- soluble to water- insoluble proteins appears to be a natu ral (age- relate d) pro cess in \\\\nlens fiber maturation and does not involve an enzymatic function.\\\\n 4. a. When glucose concentration increases in the lens, the sorbitol pathway is activated;', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'})]\", name='search_ophtal-knowledge-base'),\n",
       "  AIMessage(content='Based on the information from the ophthalmology knowledge base, the transport of ascorbic acid (vitamin C) to the lens is not directly facilitated by any of the options provided (Myoinositol, Choline, Taurine, Na/K ATPase). However, ascorbic acid is secreted by the ciliary body and is present in high concentrations in the aqueous humor, which bathes the lens. This suggests that the transport of ascorbic acid to the lens is facilitated by the aqueous humor.\\n\\nHere is the JSON output:\\n\\n{\"options\": \"None of the above\", \"explanation\": \"The transport of ascorbic acid to the lens is not directly facilitated by Myoinositol, Choline, Taurine, or Na/K ATPase. Instead, ascorbic acid is secreted by the ciliary body and is present in high concentrations in the aqueous humor, which bathes the lens.\"}')],\n",
       " 'output': 'Based on the information from the ophthalmology knowledge base, the transport of ascorbic acid (vitamin C) to the lens is not directly facilitated by any of the options provided (Myoinositol, Choline, Taurine, Na/K ATPase). However, ascorbic acid is secreted by the ciliary body and is present in high concentrations in the aqueous humor, which bathes the lens. This suggests that the transport of ascorbic acid to the lens is facilitated by the aqueous humor.\\n\\nHere is the JSON output:\\n\\n{\"options\": \"None of the above\", \"explanation\": \"The transport of ascorbic acid to the lens is not directly facilitated by Myoinositol, Choline, Taurine, or Na/K ATPase. Instead, ascorbic acid is secreted by the ciliary body and is present in high concentrations in the aqueous humor, which bathes the lens.\"}',\n",
       " 'intermediate_steps': [(AgentActionMessageLog(tool='search_ophtal-knowledge-base', tool_input='Transport of ascorbic acid to lens', log='\\nInvoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\\n\\n\\n', message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'search_ophtal-knowledge-base', 'arguments': '{\\n  \"__arg1\": \"Transport of ascorbic acid to lens\"\\n}'}})]),\n",
       "   [Document(page_content='The essential process is probably anaerobic and in the   \\nlens there are a number of enzymes which break down \\npyruvate to lactic acid and water. Lactic acid is found in \\nconsiderable quantity in the aqueous humour when the lens \\nis present; this is not so in the aphakic eye. Agents which \\nappear to participate in this process are glutathione and \\nascorbic acid (vitamin C) which, reacting together, proba-\\nbly participate in an internal auto-oxidative system. The \\nformer, both as reduced and oxidized glutathione, occurs \\nin very high concentration in the lens, particularly in the \\ncortex; the latter is specially secreted by the ciliary body. \\nNeither is present in a cataract. The metabolic activity of \\nthe lens is largely confined to the cortex; the older nucleus \\nis relatively inert.\\nThe metabolism of the lens is fairly complex but   \\ncan be simplified for an overview ( Fig. 2.3 ). The energy \\nrequirements are met by various pathways of glucose   \\nmetabolism, namely the glycolysis (Embden–Meyerhoff–\\nParnas [EMP]) pathway, pentose phosphate pathway   \\nand the tricarboxylic acid (TCA) cycle. Energy is stored \\nin the form of adenosine triphosphate (ATP). Most of', metadata={'source': 'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'}),\n",
       "    Document(page_content='chloride and bicarbonate. Phosphate is also present in the aqueous (aqueous-to-plasma ratio,\\n≈0.5 or lower), but its concentration is too low to have significant buffering capacity. Iron,\\n319\\ncopper, and zinc are all found in the aqueous humor at essentially the same levels as in plasma:\\napproximately 1 mg/mL.\\nOrganic Anions\\nLactate is the most abundant organic anion in the aqueous, and its concentration there is always\\nhigher than that in plasma. The high lactate level in aqueous is a result of glycolytic metabolism,\\nupon which the avascular lens depends.\\nAscorbate (vitamin C) levels in aqueous are much higher (some 10–50 times higher) than\\nthose in plasma. Ascorbate has antioxidant properties, and its high concentration in the aqueous\\nprotects intraocular structures by blocking UV light.\\nCarbohydrates\\nGlucose concentration in the aqueous is roughly 50%–70% of that in plasma. The rate of entry\\nof glucose into the posterior chamber is much more rapid than would be expected from its\\nmolecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\nepithelium occurs by facilitated diffusion.\\nCLINICAL PEARL', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}),\n",
       "    Document(page_content='of intraocular hemorrhage. In both conditions, excess Fe2+ can accumulate in the trabecular\\nmeshwork, neurosensory retina, and retinal pigment epithelium (RPE), leading to secondary\\ndysfunction. See BCSC Section 11, Lens and Cataract, and Section 12, Retina and Vitreous, for\\nadditional discussion of siderosis bulbi.\\nOxidative Damage to the Lens and Protective Mechanisms\\nAs stated earlier, ROS are generated by metabolic processes, inflammatory responses, and\\nexposure to UV light. The lens relies almost entirely on anaerobic metabolism and is shielded\\nfrom the immune system. Thus, the major source of ROS in the lens is exposure to UV light.\\nAlthough most UVB radiation (<320-nm wavelength) striking the human eye is absorbed either\\nby the cornea or by the ascorbate present at high levels in the aqueous humor, a certain amount\\nreaches the lens epithelium, where it can cause damage. UVA light (320–400-nm wavelength)\\ncan penetrate more deeply into the lens, where it can react with various chromophores to\\ngenerate H2O2, O2•−, and 1O2.\\nAlthough repair and regeneration mechanisms are active in the lens epithelium and superficial', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}),\n",
       "    Document(page_content='for evaluation of general health and for preoperative planning for cataract surgery.\\n 2. d.  After fetal development, the lens has no blood supply or organelles and depends on the \\naqueous humor for removal of metabolic waste. Changes in lens size occur throughout life \\nas the lens epithelial cells (LECs) at the equator continue to divide.\\n 3. d. Superoxide dismutase, catalase, and glutathione perioxidase work together to destroy \\nthe superoxide anion (O−\\n2), protecting the lens against oxidative and  free radical damage. \\n There are no known repair mechanisms for  free radical damage to proteins or membrane \\nlipids in the lens cortex. Glutathione acts as a major  free radical scavenger along with \\nvitamin E and ascorbic acid in the lens.  These 3 enzymes have no effect on the pH of the \\nlens and do not participate in the carbohydrate metabolism of the lens. Conversion from \\nwater- soluble to water- insoluble proteins appears to be a natu ral (age- relate d) pro cess in \\nlens fiber maturation and does not involve an enzymatic function.\\n 4. a. When glucose concentration increases in the lens, the sorbitol pathway is activated;', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'})])]}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThe transport of ascorbic acid to the lens is done by Na/K ATPase. This enzyme helps to maintain the balance of sodium and potassium ions in the cells, which is necessary for the absorption of ascorbic acid.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor(\n",
    "    {\n",
    "        \"input\": \"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\n\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\n',\n",
       " 'chat_history': [HumanMessage(content='Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\n'),\n",
       "  AIMessage(content='The transport of ascorbic acid to the lens is done by Na/K ATPase. This enzyme helps to maintain the balance of sodium and potassium ions in the cells, which is necessary for the absorption of ascorbic acid.')],\n",
       " 'output': 'The transport of ascorbic acid to the lens is done by Na/K ATPase. This enzyme helps to maintain the balance of sodium and potassium ions in the cells, which is necessary for the absorption of ascorbic acid.',\n",
       " 'intermediate_steps': []}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[Document(page_content='The essential process is probably anaerobic and in the   \\nlens there are a number of enzymes which break down \\npyruvate to lactic acid and water. Lactic acid is found in \\nconsiderable quantity in the aqueous humour when the lens \\nis present; this is not so in the aphakic eye. Agents which \\nappear to participate in this process are glutathione and \\nascorbic acid (vitamin C) which, reacting together, proba-\\nbly participate in an internal auto-oxidative system. The \\nformer, both as reduced and oxidized glutathione, occurs \\nin very high concentration in the lens, particularly in the \\ncortex; the latter is specially secreted by the ciliary body. \\nNeither is present in a cataract. The metabolic activity of \\nthe lens is largely confined to the cortex; the older nucleus \\nis relatively inert.\\nThe metabolism of the lens is fairly complex but   \\ncan be simplified for an overview ( Fig. 2.3 ). The energy \\nrequirements are met by various pathways of glucose   \\nmetabolism, namely the glycolysis (Embden–Meyerhoff–\\nParnas [EMP]) pathway, pentose phosphate pathway   \\nand the tricarboxylic acid (TCA) cycle. Energy is stored \\nin the form of adenosine triphosphate (ATP). Most of', metadata={'source': 'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'}), Document(page_content='chloride and bicarbonate. Phosphate is also present in the aqueous (aqueous-to-plasma ratio,\\n≈0.5 or lower), but its concentration is too low to have significant buffering capacity. Iron,\\n319\\ncopper, and zinc are all found in the aqueous humor at essentially the same levels as in plasma:\\napproximately 1 mg/mL.\\nOrganic Anions\\nLactate is the most abundant organic anion in the aqueous, and its concentration there is always\\nhigher than that in plasma. The high lactate level in aqueous is a result of glycolytic metabolism,\\nupon which the avascular lens depends.\\nAscorbate (vitamin C) levels in aqueous are much higher (some 10–50 times higher) than\\nthose in plasma. Ascorbate has antioxidant properties, and its high concentration in the aqueous\\nprotects intraocular structures by blocking UV light.\\nCarbohydrates\\nGlucose concentration in the aqueous is roughly 50%–70% of that in plasma. The rate of entry\\nof glucose into the posterior chamber is much more rapid than would be expected from its\\nmolecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\nepithelium occurs by facilitated diffusion.\\nCLINICAL PEARL', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='of intraocular hemorrhage. In both conditions, excess Fe2+ can accumulate in the trabecular\\nmeshwork, neurosensory retina, and retinal pigment epithelium (RPE), leading to secondary\\ndysfunction. See BCSC Section 11, Lens and Cataract, and Section 12, Retina and Vitreous, for\\nadditional discussion of siderosis bulbi.\\nOxidative Damage to the Lens and Protective Mechanisms\\nAs stated earlier, ROS are generated by metabolic processes, inflammatory responses, and\\nexposure to UV light. The lens relies almost entirely on anaerobic metabolism and is shielded\\nfrom the immune system. Thus, the major source of ROS in the lens is exposure to UV light.\\nAlthough most UVB radiation (<320-nm wavelength) striking the human eye is absorbed either\\nby the cornea or by the ascorbate present at high levels in the aqueous humor, a certain amount\\nreaches the lens epithelium, where it can cause damage. UVA light (320–400-nm wavelength)\\ncan penetrate more deeply into the lens, where it can react with various chromophores to\\ngenerate H2O2, O2•−, and 1O2.\\nAlthough repair and regeneration mechanisms are active in the lens epithelium and superficial', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='for evaluation of general health and for preoperative planning for cataract surgery.\\n 2. d.  After fetal development, the lens has no blood supply or organelles and depends on the \\naqueous humor for removal of metabolic waste. Changes in lens size occur throughout life \\nas the lens epithelial cells (LECs) at the equator continue to divide.\\n 3. d. Superoxide dismutase, catalase, and glutathione perioxidase work together to destroy \\nthe superoxide anion (O−\\n2), protecting the lens against oxidative and  free radical damage. \\n There are no known repair mechanisms for  free radical damage to proteins or membrane \\nlipids in the lens cortex. Glutathione acts as a major  free radical scavenger along with \\nvitamin E and ascorbic acid in the lens.  These 3 enzymes have no effect on the pH of the \\nlens and do not participate in the carbohydrate metabolism of the lens. Conversion from \\nwater- soluble to water- insoluble proteins appears to be a natu ral (age- relate d) pro cess in \\nlens fiber maturation and does not involve an enzymatic function.\\n 4. a. When glucose concentration increases in the lens, the sorbitol pathway is activated;', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'})]\u001b[0m\u001b[32;1m\u001b[1;3mThe transport of ascorbic acid (vitamin C) to the lens is not directly done by any of the options provided. However, ascorbic acid is present in high concentrations in the aqueous humor, which nourishes the lens. This is facilitated by the ciliary body, not directly by Myoinositol, Choline, Taurine, or Na/K ATPase.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `search_ophtal-knowledge-base` with `Myoinositol transport of ascorbic acid to lens`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[Document(page_content='. Anterior and Posterior. Its levels are reduced in patients of cataract. Most common cause of acquired cataract is senile cataract. \\uf077 Myoinositol is actively transported into the lens by sodium dependent carrier mediated CONGENITAL AND DEVELOPMENTAL mechanism. Its levels are markedly CATARACT reduced in cataract. It is a precursor of membrane phosphoinositides which are Etiology involved in Na/Ka ATPase function. It also participates in ascorbic acid transport in a. Heredity: Usually dominant. the lens. b. Maternal factors: Lens 1. Malnutrition. Water 99% 66% 2. TORCHS infections, i.e., Toxoplas- Na+ 144 20 mosis, rubella, cytomegalovirus, Herpes and syphilis. Na + K + AT Pase K+ 4.5 125 3. Drug: Thalidomide, Corticosteroid. Glucose 6 1 4. Radiation. Lactic acid 7.4 14 c. Foetal or Infantile factor: Proteins 0.04% (S) 33% 1. Anoxia. Active Transport Diffusion 2. Metabolic: (S) Synthesis a. Galactosemia–Galactokinase defi- Fig. 1.2 ciency. Development of lens: Lens develops from b', metadata={'source': 'data/book/Diseases of Lens'}), Document(page_content='molecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\nepithelium occurs by facilitated diffusion.\\nCLINICAL PEARL\\nIn individuals with diabetes mellitus, glucose levels in the aqueous humor are increased. This leads\\nto higher glucose concentrations in the lens, which has short-term refractive and longer-term\\ncataract implications.\\nInositol, which is important for phospholipid synthesis in the anterior segment, is found in the\\naqueous at a concentration approximately 10 times higher than that in plasma.\\nGlutathione and Urea\\nGlutathione, an important tripeptide with a reactive sulfhydryl group, is also found in the\\naqueous humor. Its concentration in primates ranges from 1 to 10 μmol/L. Blood contains a high\\nconcentration of glutathione; however, virtually all glutathione resides within the erythrocytes,\\nand plasma has a low concentration of only 5 μmol/L or less. Glutathione stabilizes the\\noxidation-reduction (redox) state of the aqueous by reconverting ascorbate to its functional form\\nafter oxidation, as well as by removing excess hydrogen peroxide. Glutathione also serves as a', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='chloride and bicarbonate. Phosphate is also present in the aqueous (aqueous-to-plasma ratio,\\n≈0.5 or lower), but its concentration is too low to have significant buffering capacity. Iron,\\n319\\ncopper, and zinc are all found in the aqueous humor at essentially the same levels as in plasma:\\napproximately 1 mg/mL.\\nOrganic Anions\\nLactate is the most abundant organic anion in the aqueous, and its concentration there is always\\nhigher than that in plasma. The high lactate level in aqueous is a result of glycolytic metabolism,\\nupon which the avascular lens depends.\\nAscorbate (vitamin C) levels in aqueous are much higher (some 10–50 times higher) than\\nthose in plasma. Ascorbate has antioxidant properties, and its high concentration in the aqueous\\nprotects intraocular structures by blocking UV light.\\nCarbohydrates\\nGlucose concentration in the aqueous is roughly 50%–70% of that in plasma. The rate of entry\\nof glucose into the posterior chamber is much more rapid than would be expected from its\\nmolecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\nepithelium occurs by facilitated diffusion.\\nCLINICAL PEARL', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='Endothelium 312\\nCHAPTER 9 314\\nAqueous Humor, Iris, and Ciliary Body 314\\nHighlights 314\\nPhysiology of the Iris and Ciliary Body 314\\n533\\nDynamics of the Aqueous Humor 315\\nBlood–Aqueous Barrier 315\\nAqueous Humor Formation and Secretion 315\\nComposition of the Aqueous Humor 318\\nInorganic Ions 319\\nOrganic Anions 320\\nCarbohydrates 320\\nGlutathione and Urea 320\\nProteins 320\\nProteinases and inhibitors 321\\nEnzymes 321\\nNeurotrophic and neuroendocrine proteins 321\\nGrowth-Modulatory Factors 322\\nVascular endothelial growth factors 322\\nOxygen and Carbon Dioxide 323\\nClinical Implications of Breakdown of the Blood–Aqueous Barrier 323\\nCHAPTER 10 325\\nLens 325\\nHighlights 325\\nOverview 325\\nStructure of the Lens 325\\nCapsule 325\\nEpithelium 326\\nCortex and Nucleus 327\\nChemical Composition of the Lens 327\\nPlasma Membranes 327\\nLens Proteins 328\\nCrystallins 328\\nCytoskeletal and membrane proteins 329\\nPosttranslational modifications to lens proteins 329\\nTransparency and Physiologic Aspects of the Lens 329\\nLens Transparency 329\\nLens Physiology 331\\nLens Metabolism and Formation of Sugar Cataracts 333\\nEnergy Production 333\\nCarbohydrate Cataracts 334\\nCHAPTER 11 336\\n534\\nVitreous 336\\nHighlights 336\\nOverview 336', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'})]\u001b[0m\u001b[32;1m\u001b[1;3mYou are correct, Bob. Myoinositol is actively transported into the lens by a sodium-dependent carrier-mediated mechanism. It participates in ascorbic acid transport in the lens. My apologies for the oversight.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor(\n",
    "    {\n",
    "        \"input\": \"I belive the answer is A. Myoinositol\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `search_ophtal-knowledge-base` with `Lens develops from`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[Document(page_content='The mechanisms responsible for the precise formation and changing organ ization of the \\nsuture pattern remain obscure.\\nThe  human lens weighs approximately 90\\xa0 mg at birth, and it increases in mass \\nby approximately 2\\xa0 mg per year throughout life as new fibers form. With increas -\\ning age, the central, or oldest, lens fibers gradually become less malleable, and the \\nlens nucleus becomes more rigid. This pro cess progressively reduces the amplitude o f \\naccommodation.CHAPTER 4: Embryology and Developmental\\xa0Defects ● 33 \\nTunica Vasculosa Lentis\\nAround 1 month of gestation, the hyaloid artery, which enters the eye at the optic nerve head \\n(also called the optic disc), branches to form a network of capillaries, the tunica vasculosa \\nlentis, on the posterior surface of the lens capsule ( Fig\\xa04-3 ).  These capillaries grow  toward the \\nequator of the lens, where they anastomose with a second network of capillaries, called th e \\nanterior pupillary membrane,  which derives from the ciliary veins and covers the anterior sur -\\nface of the lens. At approximately 9 weeks of gestation, the capillary network surrounding the', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'}), Document(page_content='developing lens fiber extends posteriorly along the capsule,  toward the posterior pole of \\nthe lens. In this manner, new lens fibers are continually formed, layer upon layer. As each \\nsecondary fiber cell detaches from the capsule, it loses its nucleus and membrane- bound \\norganelles. The secondary lens fibers formed between 2 and 8 months of gestation make \\nup the fetal nucleus.\\nLens Sutures and the Fetal Nucleus\\nAs lens fibers grow anteriorly and posteriorly, a pattern emerges where the ends of the \\nfibers meet and interdigitate with the ends of fibers arising on the opposite side of th e \\nlens, near the anterior and posterior poles.  These patterns of cell association are known as \\nsutures.  Y- shaped sutures are recognizable at approximately 8 weeks of gestation; an erect \\nY- suture appears anteriorly and an inverted Y- suture appears posteriorly ( Fig\\xa04-2 ). As the \\nlens fibers continue to form and the lens continues to grow, the pattern of lens sutures \\nbecomes increasingly complex, resulting in 12 or more suture branches in the adult eye. \\nThe mechanisms responsible for the precise formation and changing organ ization of the \\nsuture pattern remain obscure.', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'}), Document(page_content='they become closely apposed and adherent to the surface ectoderm,  a single layer of cuboidal \\ncells, in 2 patches on  either side of the head. (See BCSC Section\\xa02, Fundamentals and Princi ples \\nof Ophthalmology,  for additional discussion and illustrations of ocular development.)\\nVIDEO\\xa04-1  Lens development.\\nCourtesy of Charles Cole, MD.\\nGo to www . aao . org / bcscvideo _ section11  to access all videos in \\nSection\\xa011 .\\nLens Placode\\nAt approximately 27 days of gestation, the ectoderm cells that overlie the optic vesicles \\nbecome columnar. This area of thickened cells is called the lens placode.  Growth  factors \\n30 ● Lens and Cataract\\nOptic vesicle\\nSurface\\nectoderm\\nA. Optic vesicle formation (25 days)\\nLens placode\\nLens pit\\nB. Lens placode formation (27–29 days)\\nForming\\nlens\\nvesicle \\nEctoderm\\nC. Lens vesicle formation\\n     (30 days)\\nD. Lens vesicle completed\\n     (33 days)\\nFigure\\xa04-1  Embryologic development of the lens. (Illustration by Christine Gralapp.)\\n(Continued)\\nof the bone morphoge ne tic protein (BMP)   family are required for formation of the lens \\nplacode and, subsequently, the lens.\\nLens Pit', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'}), Document(page_content='The elongated cells are called the primary lens fibers.  As the fiber cells mature, their nuclei \\nand other membrane- bound organelles undergo degradation, a pro cess that reduces light \\nscattering. The primary lens fibers make up the embryonic nucleus that  will ultimately \\noccupy the central area of the adult lens.\\nThe cells of the anterior lens vesicle give rise to the lens epithelium,  a monolayer of \\ncuboidal cells . Proliferation within the epithelium  causes subsequent growth of the lens. \\nThe lens capsule  develops as a basement membrane elaborated by the lens epithelium an -\\nteriorly and by lens fibers posteriorly.\\nSecondary Lens Fibers\\n After they proliferate, the epithelial cells near the lens equator elongate to form secon dary \\nlens fibers. The anterior aspect of each developing lens fiber extends anteriorly beneath \\nthe lens epithelium,  toward the anterior pole of the lens. The posterior aspect of each \\ndeveloping lens fiber extends posteriorly along the capsule,  toward the posterior pole of \\nthe lens. In this manner, new lens fibers are continually formed, layer upon layer. As each', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'})]\u001b[0m\u001b[32;1m\u001b[1;3mThe lens develops from the surface ectoderm. This process begins around 27 days of gestation when the ectoderm cells that overlie the optic vesicles become columnar, forming an area of thickened cells known as the lens placode. So, the correct answer is C. Surface ectoderm.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor(\n",
    "    {\n",
    "        \"input\": \"Question: Lens develops from?\\nA. Endoderm\\nB. Mesoderm\\nC. Surface ectoderm\\nD. Neuroectoderm\" \n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "memory_key = \"history\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(openai_api_key=os.getenv(\"OPENAI_API_KEY\"), openai_organization=os.getenv(\"ORGANIZATION_KEY\"), model=\"gpt-4\", temperature=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "memory = AgentTokenBufferMemory(memory_key=memory_key, llm=llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent\n",
    "from langchain.schema.messages import SystemMessage\n",
    "from langchain.prompts import MessagesPlaceholder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = open(\"prompts/version_2.txt\", \"r\").read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "system_message = SystemMessage(\n",
    "    content=prompt,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "system_message = SystemMessage(\n",
    "    content=\"You are an assistant to Ophthamologists and that's your only job. You will be interacting with doctors. Keep your language medical and professional at all times. You can help doctors in answering their questions and also lookup the knowledge base to find something.\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = OpenAIFunctionsAgent.create_prompt(\n",
    "    system_message=system_message,\n",
    "    extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mYes, I can answer questions about ophthalmology to the best of my knowledge and abilities. However, please note that while I strive to provide accurate and up-to-date information, my responses should not be used as a replacement for professional medical advice.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor({\"input\": \"can you answer questions about ophtalmology?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[Document(page_content='The essential process is probably anaerobic and in the   \\nlens there are a number of enzymes which break down \\npyruvate to lactic acid and water. Lactic acid is found in \\nconsiderable quantity in the aqueous humour when the lens \\nis present; this is not so in the aphakic eye. Agents which \\nappear to participate in this process are glutathione and \\nascorbic acid (vitamin C) which, reacting together, proba-\\nbly participate in an internal auto-oxidative system. The \\nformer, both as reduced and oxidized glutathione, occurs \\nin very high concentration in the lens, particularly in the \\ncortex; the latter is specially secreted by the ciliary body. \\nNeither is present in a cataract. The metabolic activity of \\nthe lens is largely confined to the cortex; the older nucleus \\nis relatively inert.\\nThe metabolism of the lens is fairly complex but   \\ncan be simplified for an overview ( Fig. 2.3 ). The energy \\nrequirements are met by various pathways of glucose   \\nmetabolism, namely the glycolysis (Embden–Meyerhoff–\\nParnas [EMP]) pathway, pentose phosphate pathway   \\nand the tricarboxylic acid (TCA) cycle. Energy is stored \\nin the form of adenosine triphosphate (ATP). Most of', metadata={'source': 'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'}), Document(page_content='chloride and bicarbonate. Phosphate is also present in the aqueous (aqueous-to-plasma ratio,\\n≈0.5 or lower), but its concentration is too low to have significant buffering capacity. Iron,\\n319\\ncopper, and zinc are all found in the aqueous humor at essentially the same levels as in plasma:\\napproximately 1 mg/mL.\\nOrganic Anions\\nLactate is the most abundant organic anion in the aqueous, and its concentration there is always\\nhigher than that in plasma. The high lactate level in aqueous is a result of glycolytic metabolism,\\nupon which the avascular lens depends.\\nAscorbate (vitamin C) levels in aqueous are much higher (some 10–50 times higher) than\\nthose in plasma. Ascorbate has antioxidant properties, and its high concentration in the aqueous\\nprotects intraocular structures by blocking UV light.\\nCarbohydrates\\nGlucose concentration in the aqueous is roughly 50%–70% of that in plasma. The rate of entry\\nof glucose into the posterior chamber is much more rapid than would be expected from its\\nmolecular size and lipid solubility, suggesting that the transport of glucose across the ciliary\\nepithelium occurs by facilitated diffusion.\\nCLINICAL PEARL', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='of intraocular hemorrhage. In both conditions, excess Fe2+ can accumulate in the trabecular\\nmeshwork, neurosensory retina, and retinal pigment epithelium (RPE), leading to secondary\\ndysfunction. See BCSC Section 11, Lens and Cataract, and Section 12, Retina and Vitreous, for\\nadditional discussion of siderosis bulbi.\\nOxidative Damage to the Lens and Protective Mechanisms\\nAs stated earlier, ROS are generated by metabolic processes, inflammatory responses, and\\nexposure to UV light. The lens relies almost entirely on anaerobic metabolism and is shielded\\nfrom the immune system. Thus, the major source of ROS in the lens is exposure to UV light.\\nAlthough most UVB radiation (<320-nm wavelength) striking the human eye is absorbed either\\nby the cornea or by the ascorbate present at high levels in the aqueous humor, a certain amount\\nreaches the lens epithelium, where it can cause damage. UVA light (320–400-nm wavelength)\\ncan penetrate more deeply into the lens, where it can react with various chromophores to\\ngenerate H2O2, O2•−, and 1O2.\\nAlthough repair and regeneration mechanisms are active in the lens epithelium and superficial', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='for evaluation of general health and for preoperative planning for cataract surgery.\\n 2. d.  After fetal development, the lens has no blood supply or organelles and depends on the \\naqueous humor for removal of metabolic waste. Changes in lens size occur throughout life \\nas the lens epithelial cells (LECs) at the equator continue to divide.\\n 3. d. Superoxide dismutase, catalase, and glutathione perioxidase work together to destroy \\nthe superoxide anion (O−\\n2), protecting the lens against oxidative and  free radical damage. \\n There are no known repair mechanisms for  free radical damage to proteins or membrane \\nlipids in the lens cortex. Glutathione acts as a major  free radical scavenger along with \\nvitamin E and ascorbic acid in the lens.  These 3 enzymes have no effect on the pH of the \\nlens and do not participate in the carbohydrate metabolism of the lens. Conversion from \\nwater- soluble to water- insoluble proteins appears to be a natu ral (age- relate d) pro cess in \\nlens fiber maturation and does not involve an enzymatic function.\\n 4. a. When glucose concentration increases in the lens, the sorbitol pathway is activated;', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'})]\u001b[0m\u001b[32;1m\u001b[1;3mThe transport of ascorbic acid (vitamin C) to the lens is not directly facilitated by any of the options provided (Myoinositol, Choline, Taurine, Na/K ATPase). Ascorbic acid is present in high concentrations in the aqueous humor, which nourishes the lens. This high concentration of ascorbic acid in the aqueous humor helps protect intraocular structures by blocking UV light. However, the specific transport mechanism of ascorbic acid to the lens is not mentioned in the options provided.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor(\n",
    "    {\n",
    "        \"input\": \"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\n\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The transport of ascorbic acid (vitamin C) to the lens is not directly facilitated by any of the options provided (Myoinositol, Choline, Taurine, Na/K ATPase). Ascorbic acid is present in high concentrations in the aqueous humor, which nourishes the lens. This high concentration of ascorbic acid in the aqueous humor helps protect intraocular structures by blocking UV light. However, the specific transport mechanism of ascorbic acid to the lens is not mentioned in the options provided.'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['output']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mAs an AI, I can assist with a variety of tasks, including but not limited to:\n",
      "\n",
      "1. Answering questions: I can provide information on a wide range of topics. If I don't know the answer, I can look it up for you.\n",
      "\n",
      "2. Setting reminders and alarms: I can help you remember important dates and tasks.\n",
      "\n",
      "3. Sending messages: I can send emails or text messages on your behalf.\n",
      "\n",
      "4. Making calls: I can dial numbers and make calls for you.\n",
      "\n",
      "5. Providing news and weather updates: I can give you the latest news headlines and weather forecasts.\n",
      "\n",
      "6. Managing your schedule: I can add events to your calendar and notify you of upcoming appointments.\n",
      "\n",
      "7. Providing entertainment: I can tell jokes, play games, and more.\n",
      "\n",
      "8. Conducting online searches: I can find and present information from the web.\n",
      "\n",
      "9. Providing recommendations: I can suggest movies, books, restaurants, and more based on your preferences.\n",
      "\n",
      "10. Learning and adapting: I can learn from our interactions to better assist you in the future.\n",
      "\n",
      "Please note that the exact capabilities can vary depending on the platform and the specific AI model.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "result = agent_executor(\n",
    "    {\n",
    "        \"input\": \"what all can you do?\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ReAct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import load_tools\n",
    "from langchain.agents import initialize_agent\n",
    "from langchain.agents import AgentType\n",
    "from langchain.llms import OpenAI\n",
    "import os\n",
    "from transformers import AutoTokenizer, AutoModel\n",
    "import torch\n",
    "from langchain.vectorstores import Pinecone\n",
    "import pinecone\n",
    "from langchain.agents.agent_toolkits import create_retriever_tool\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent\n",
    "from langchain.schema.messages import SystemMessage\n",
    "from langchain.prompts import MessagesPlaceholder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(openai_api_key=os.getenv(\"OPENAI_API_KEY\"), openai_organization=os.getenv(\"ORGANIZATION_KEY\"), model=\"gpt-4\", temperature=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "pinecone.init(\n",
    "    api_key=os.getenv(\"PINECONE_API_KEY\"),  # find at app.pinecone.io\n",
    "    environment=os.getenv(\"PINECONE_ENV\"),  # next to api key in console\n",
    ")\n",
    "\n",
    "index_name = \"ophtal-knowledge-base\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = AutoModel.from_pretrained(\"models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e\")\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_bert_embeddings(sentence):\n",
    "    embeddings = []\n",
    "    input_ids = tokenizer.encode(sentence, return_tensors=\"pt\")\n",
    "    with torch.no_grad():\n",
    "        output = model(input_ids)\n",
    "    embedding = output.last_hidden_state[:,0,:].numpy().tolist()\n",
    "    return embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pinecone.Index(index_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/aakashbhatnagar/Documents/masters/ophthal_llm/myenv/lib/python3.10/site-packages/langchain/vectorstores/pinecone.py:59: UserWarning: Passing in `embedding` as a Callable is deprecated. Please pass in an Embeddings object instead.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "vectorstore = Pinecone(index, get_bert_embeddings, \"text\")\n",
    "retriever = vectorstore.as_retriever()\n",
    "tool = create_retriever_tool(\n",
    "    retriever,\n",
    "    \"search_ophtal-knowledge-base\",\n",
    "    \"Searches and returns documents regarding the ophtal-knowledge-base.\",\n",
    ")\n",
    "tools = [tool]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "system_message = SystemMessage(\n",
    "    content=\"You are an assistant to Ophthamologists and that's your only job. You will be interacting with doctors. Keep your language medical and professional at all times. You can help doctors in answering their questions and also lookup the knowledge base to find something.\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "memory_key = \"history\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = OpenAIFunctionsAgent.create_prompt(\n",
    "    system_message=system_message,\n",
    "    extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'AgentType' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m/Users/aakashbhatnagar/Documents/masters/ophthal_llm/conversation.ipynb Cell 44\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/aakashbhatnagar/Documents/masters/ophthal_llm/conversation.ipynb#Y104sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m agent \u001b[39m=\u001b[39m AgentType\u001b[39m.\u001b[39;49mZERO_SHOT_REACT_DESCRIPTION(llm\u001b[39m=\u001b[39;49mllm, tools\u001b[39m=\u001b[39;49mtools, prompt\u001b[39m=\u001b[39;49mprompt)\n",
      "\u001b[0;31mTypeError\u001b[0m: 'AgentType' object is not callable"
     ]
    }
   ],
   "source": [
    "agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION(llm=llm, tools=tools, prompt=prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = initialize_agent(\n",
    "    tools, llm, agent=, verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mI need to find out which layer of embryonic tissue the lens of the eye develops from.\n",
      "Action: search_ophtal-knowledge-base\n",
      "Action Input: Lens development embryonic tissue\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m[Document(page_content='115\\nFigure 2-29 Organization of the lens. At areas where lens cells converge and meet, sutures are\\nformed. A, Cutaway view of the adult lens showing an embryonic lens inside. The embryonal\\nnucleus has a Y-shaped suture at both the anterior and posterior poles; in the adult lens cortex, the\\norganization of the sutures is more complex. At the equator, the lens epithelium can divide, and the\\ncells become highly elongated and ribbonlike, sending processes anteriorly and posteriorly. As new\\nlens cells are formed, older cells come to lie in the deeper parts of the cortex. B, Cross section and\\ncorresponding surface view showing the difference in lens fibers at the anterior (A), intermediate\\n(B), and equatorial (C) zones. The lens capsule, or basement membrane of the lens epithelium (d),\\nis shown in relation to the zonular fibers (f) and their attachment to the lens (g). C, The diagram\\nshows a closer view of lens sutures. D and E, Optical sections of the lens of a young adult human\\n116\\n(25-year-old woman) demonstrated by Scheimpflug photography. The cornea is to the right. The\\nlens is in the nonaccommodative state in part D. The lens is shown during accommodation in part', metadata={'source': 'data/book/text/02 AAO Fundamentals and Principles of Ophthalmology.pdf.txt'}), Document(page_content='they become closely apposed and adherent to the surface ectoderm,  a single layer of cuboidal \\ncells, in 2 patches on  either side of the head. (See BCSC Section\\xa02, Fundamentals and Princi ples \\nof Ophthalmology,  for additional discussion and illustrations of ocular development.)\\nVIDEO\\xa04-1  Lens development.\\nCourtesy of Charles Cole, MD.\\nGo to www . aao . org / bcscvideo _ section11  to access all videos in \\nSection\\xa011 .\\nLens Placode\\nAt approximately 27 days of gestation, the ectoderm cells that overlie the optic vesicles \\nbecome columnar. This area of thickened cells is called the lens placode.  Growth  factors \\n30 ● Lens and Cataract\\nOptic vesicle\\nSurface\\nectoderm\\nA. Optic vesicle formation (25 days)\\nLens placode\\nLens pit\\nB. Lens placode formation (27–29 days)\\nForming\\nlens\\nvesicle \\nEctoderm\\nC. Lens vesicle formation\\n     (30 days)\\nD. Lens vesicle completed\\n     (33 days)\\nFigure\\xa04-1  Embryologic development of the lens. (Illustration by Christine Gralapp.)\\n(Continued)\\nof the bone morphoge ne tic protein (BMP)   family are required for formation of the lens \\nplacode and, subsequently, the lens.\\nLens Pit', metadata={'source': 'data/book/text/11.Lens and Cataract AAO 2022-2023.pdf.txt'}), Document(page_content='. Choroidal fissure-closed. 65-mm stage (3 months) Posterior lens cells growing forward Fig. 15.1 371372 Review of Ophthalmology DEVELOPMENT OF LENS Stages of growth of the embryonic lens CORNEAL EPITHELIUM Lens plate Lens vesicle (early) ANTERIOR WALL: Single layer of cuboidal epithelium Lens pit (early) POSTERIOR WALL: Increases Lens vesicle (late) in length and form elongated fibres that projects into lumen of vesicle (crystallin) NUCLEAR BOW: Nuclei of the lens fibers move anteriorly to form a convex line Lens pit (late) Embryonic lens nucleus Fig. 15.2 Structures Derived from Structures Derived from Cranial Neuroectoderm Neural Crest 1. Retina. 1. Corneal stroma. 2. Optic nerve. 2. Corneal endothelium. 3. Dilator pupillae. 3. Sclera (but temporal part of the sclera is derived from mesoderm). 4. Iris sphincter. 4. Trabecular meshwork. 5. Retinal pigment epithelium of retina. 5. Ciliary muscles. 6. Epithelial lining of ciliary body and iris. 6. Choroidal stroma. 7', metadata={'source': 'data/book/Embryology'}), Document(page_content='. Choroidal fissure-closed. 65-mm stage (3 months) Posterior lens cells growing forward Fig. 15.1 371372 Review of Ophthalmology DEVELOPMENT OF LENS Stages of growth of the embryonic lens CORNEAL EPITHELIUM Lens plate Lens vesicle (early) ANTERIOR WALL: Single layer of cuboidal epithelium Lens pit (early) POSTERIOR WALL: Increases Lens vesicle (late) in length and form elongated fibres that projects into lumen of vesicle (crystallin) NUCLEAR BOW: Nuclei of the lens fibers move anteriorly to form a convex line Lens pit (late) Embryonic lens nucleus Fig. 15.2 Structures Derived from Structures Derived from Cranial Neuroectoderm Neural Crest 1. Retina. 1. Corneal stroma. 2. Optic nerve. 2. Corneal endothelium. 3. Dilator pupillae. 3. Sclera (but temporal part of the sclera is derived from mesoderm). 4. Iris sphincter. 4. Trabecular meshwork. 5. Retinal pigment epithelium of retina. 5. Ciliary muscles. 6. Epithelial lining of ciliary body and iris. 6. Choroidal stroma. 7', metadata={'source': 'data/book/Recent Advances'})]\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mThe documents indicate that the lens develops from the surface ectoderm.\n",
      "Final Answer: C. Surface ectoderm\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': 'Question: Lens develops from?\\nA. Endoderm\\nB. Mesoderm\\nC. Surface ectoderm\\nD. Neuroectoderm',\n",
       " 'output': 'C. Surface ectoderm'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke(\n",
    "    {\n",
    "        \"input\": \"Question: Lens develops from?\\nA. Endoderm\\nB. Mesoderm\\nC. Surface ectoderm\\nD. Neuroectoderm\" \n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThis question is asking about a previous question, but there is no context provided. I can't answer this question accurately without knowing the previous question.\n",
      "Final Answer: I'm sorry, but I can't provide the information you're looking for.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': 'What question did I ask before?',\n",
       " 'output': \"I'm sorry, but I can't provide the information you're looking for.\"}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke(\n",
    "    {\n",
    "        \"input\": \"What question did I ask before?\" \n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv",
   "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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}