aakash0017 commited on
Commit
7b5e0ec
·
1 Parent(s): 8729297

Upload folder using huggingface_hub

Browse files
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import git
3
  import sys
4
 
5
- sys.path.append("deployment_app")
6
 
7
 
8
  username = os.environ['GIT_USERNAME']
@@ -15,6 +15,7 @@ repo_url_with_token = f'https://{username}:{token}@{repo_url.split("://")[1]}'
15
  # Clone the repository
16
  repo = git.Repo.clone_from(repo_url_with_token, 'deployment_app')
17
  print("REPO CLONED")
 
18
 
19
  from deployment_app.conversation import make_conversation
20
  import gradio as gr
 
2
  import git
3
  import sys
4
 
5
+
6
 
7
 
8
  username = os.environ['GIT_USERNAME']
 
15
  # Clone the repository
16
  repo = git.Repo.clone_from(repo_url_with_token, 'deployment_app')
17
  print("REPO CLONED")
18
+ sys.path.append("deployment_app")
19
 
20
  from deployment_app.conversation import make_conversation
21
  import gradio as gr
deployment_app/.DS_Store ADDED
Binary file (6.15 kB). View file
 
deployment_app/__init__.py ADDED
File without changes
deployment_app/conv_app.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from conversation import make_conversation, auth_function
3
+ import random
4
+
5
+ demo = gr.ChatInterface(make_conversation).queue()
6
+
7
+ demo.launch(auth=auth_function, share=True)
deployment_app/conversation.ipynb ADDED
@@ -0,0 +1,1258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stderr",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "/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",
13
+ " from tqdm.autonotebook import tqdm\n"
14
+ ]
15
+ }
16
+ ],
17
+ "source": [
18
+ "from langchain.document_loaders import TextLoader\n",
19
+ "import pinecone\n",
20
+ "from langchain.vectorstores import Pinecone\n",
21
+ "import os\n",
22
+ "from transformers import AutoTokenizer, AutoModel\n",
23
+ "from langchain.agents.agent_toolkits import create_conversational_retrieval_agent\n",
24
+ "from langchain.agents.agent_toolkits import create_retriever_tool\n",
25
+ "from langchain.chat_models import ChatOpenAI\n",
26
+ "import torch\n",
27
+ "from langchain.agents.openai_functions_agent.agent_token_buffer_memory import (AgentTokenBufferMemory)\n",
28
+ "from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent\n",
29
+ "from langchain.schema.messages import SystemMessage\n",
30
+ "from langchain.prompts import MessagesPlaceholder\n",
31
+ "import gradio as gr\n",
32
+ "import time\n",
33
+ "\n",
34
+ "# loader = TextLoader(\"../../../../../docs/docs/modules/state_of_the_union.txt\")"
35
+ ]
36
+ },
37
+ {
38
+ "cell_type": "code",
39
+ "execution_count": 23,
40
+ "metadata": {},
41
+ "outputs": [
42
+ {
43
+ "name": "stderr",
44
+ "output_type": "stream",
45
+ "text": [
46
+ "/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",
47
+ " warnings.warn(\n"
48
+ ]
49
+ }
50
+ ],
51
+ "source": [
52
+ "import gradio as gr\n",
53
+ "from conversation import make_conversation, auth_function\n",
54
+ "import random"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "code",
59
+ "execution_count": 24,
60
+ "metadata": {},
61
+ "outputs": [],
62
+ "source": [
63
+ "demo = gr.ChatInterface(make_conversation).queue()"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": 25,
69
+ "metadata": {},
70
+ "outputs": [
71
+ {
72
+ "name": "stdout",
73
+ "output_type": "stream",
74
+ "text": [
75
+ "Running on local URL: http://127.0.0.1:7863\n"
76
+ ]
77
+ },
78
+ {
79
+ "name": "stderr",
80
+ "output_type": "stream",
81
+ "text": [
82
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
83
+ "To disable this warning, you can either:\n",
84
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
85
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
86
+ ]
87
+ },
88
+ {
89
+ "name": "stdout",
90
+ "output_type": "stream",
91
+ "text": [
92
+ "Running on public URL: https://36c80e8ac1f1026dec.gradio.live\n",
93
+ "\n",
94
+ "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"
95
+ ]
96
+ },
97
+ {
98
+ "data": {
99
+ "text/html": [
100
+ "<div><iframe src=\"https://36c80e8ac1f1026dec.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
101
+ ],
102
+ "text/plain": [
103
+ "<IPython.core.display.HTML object>"
104
+ ]
105
+ },
106
+ "metadata": {},
107
+ "output_type": "display_data"
108
+ },
109
+ {
110
+ "data": {
111
+ "text/plain": []
112
+ },
113
+ "execution_count": 25,
114
+ "metadata": {},
115
+ "output_type": "execute_result"
116
+ }
117
+ ],
118
+ "source": [
119
+ "demo.launch(share=True, auth=auth_function)"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "markdown",
124
+ "metadata": {},
125
+ "source": [
126
+ "## Retrieval"
127
+ ]
128
+ },
129
+ {
130
+ "cell_type": "code",
131
+ "execution_count": 2,
132
+ "metadata": {},
133
+ "outputs": [],
134
+ "source": [
135
+ "pinecone.init(\n",
136
+ " api_key=os.getenv(\"PINECONE_API_KEY\"), # find at app.pinecone.io\n",
137
+ " environment=os.getenv(\"PINECONE_ENV\"), # next to api key in console\n",
138
+ ")\n",
139
+ "\n",
140
+ "index_name = \"ophtal-knowledge-base\""
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 3,
146
+ "metadata": {},
147
+ "outputs": [],
148
+ "source": [
149
+ "model = AutoModel.from_pretrained(\"models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e\")\n",
150
+ "tokenizer = AutoTokenizer.from_pretrained(\"models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e\")"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 4,
156
+ "metadata": {},
157
+ "outputs": [],
158
+ "source": [
159
+ "prompt = open(\"prompts/version_2.txt\", \"r\").read()"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": 5,
165
+ "metadata": {},
166
+ "outputs": [],
167
+ "source": [
168
+ "def get_bert_embeddings(sentence):\n",
169
+ " embeddings = []\n",
170
+ " input_ids = tokenizer.encode(sentence, return_tensors=\"pt\")\n",
171
+ " with torch.no_grad():\n",
172
+ " output = model(input_ids)\n",
173
+ " embedding = output.last_hidden_state[:,0,:].numpy().tolist()\n",
174
+ " return embedding"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": 6,
180
+ "metadata": {},
181
+ "outputs": [
182
+ {
183
+ "name": "stderr",
184
+ "output_type": "stream",
185
+ "text": [
186
+ "/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",
187
+ " warnings.warn(\n"
188
+ ]
189
+ }
190
+ ],
191
+ "source": [
192
+ "index = pinecone.Index(index_name)\n",
193
+ "vectorstore = Pinecone(index, get_bert_embeddings, \"text\")"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "code",
198
+ "execution_count": 7,
199
+ "metadata": {},
200
+ "outputs": [],
201
+ "source": [
202
+ "retriever = vectorstore.as_retriever()"
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "code",
207
+ "execution_count": 8,
208
+ "metadata": {},
209
+ "outputs": [],
210
+ "source": [
211
+ "tool = create_retriever_tool(\n",
212
+ " retriever,\n",
213
+ " \"search_ophtal-knowledge-base\",\n",
214
+ " \"Searches and returns documents regarding the ophtal-knowledge-base.\",\n",
215
+ ")\n",
216
+ "tools = [tool]"
217
+ ]
218
+ },
219
+ {
220
+ "cell_type": "code",
221
+ "execution_count": 9,
222
+ "metadata": {},
223
+ "outputs": [],
224
+ "source": [
225
+ "llm = ChatOpenAI(openai_api_key=os.getenv(\"OPENAI_API_KEY\"), openai_organization=os.getenv(\"ORGANIZATION_KEY\"), model=\"gpt-4\", temperature=0.2)"
226
+ ]
227
+ },
228
+ {
229
+ "cell_type": "code",
230
+ "execution_count": 10,
231
+ "metadata": {},
232
+ "outputs": [],
233
+ "source": [
234
+ "system_message = SystemMessage(\n",
235
+ " content=prompt,\n",
236
+ ")\n",
237
+ "\n",
238
+ "memory_key='history'"
239
+ ]
240
+ },
241
+ {
242
+ "cell_type": "code",
243
+ "execution_count": 11,
244
+ "metadata": {},
245
+ "outputs": [],
246
+ "source": [
247
+ "prompt = OpenAIFunctionsAgent.create_prompt(\n",
248
+ " system_message=system_message,\n",
249
+ " extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],\n",
250
+ ")"
251
+ ]
252
+ },
253
+ {
254
+ "cell_type": "code",
255
+ "execution_count": 12,
256
+ "metadata": {},
257
+ "outputs": [],
258
+ "source": [
259
+ "agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=True, prompt=prompt)"
260
+ ]
261
+ },
262
+ {
263
+ "cell_type": "code",
264
+ "execution_count": 14,
265
+ "metadata": {},
266
+ "outputs": [
267
+ {
268
+ "name": "stdout",
269
+ "output_type": "stream",
270
+ "text": [
271
+ "\n",
272
+ "\n",
273
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
274
+ "\u001b[32;1m\u001b[1;3m\n",
275
+ "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
276
+ "\n",
277
+ "\n",
278
+ "\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",
279
+ "\n",
280
+ "\u001b[1m> Finished chain.\u001b[0m\n"
281
+ ]
282
+ }
283
+ ],
284
+ "source": [
285
+ "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\"})"
286
+ ]
287
+ },
288
+ {
289
+ "cell_type": "code",
290
+ "execution_count": 22,
291
+ "metadata": {},
292
+ "outputs": [
293
+ {
294
+ "data": {
295
+ "text/plain": [
296
+ "'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'"
297
+ ]
298
+ },
299
+ "execution_count": 22,
300
+ "metadata": {},
301
+ "output_type": "execute_result"
302
+ }
303
+ ],
304
+ "source": [
305
+ "result[\"intermediate_steps\"][0][1][0].page_content"
306
+ ]
307
+ },
308
+ {
309
+ "cell_type": "code",
310
+ "execution_count": 18,
311
+ "metadata": {},
312
+ "outputs": [],
313
+ "source": [
314
+ "def run(input_):\n",
315
+ " output = agent_executor({\"input\": input_})\n",
316
+ " output_text = output[\"output\"]\n",
317
+ " if len(output[\"intermediate_steps\"])>0:\n",
318
+ " documents = output[\"intermediate_steps\"][0][1]\n",
319
+ " sources = []\n",
320
+ " for doc in documents:\n",
321
+ " print(doc.metadata)\n",
322
+ " sources.append(doc.metadata[\"source\"])\n",
323
+ " \n",
324
+ " source_text = \"\"\n",
325
+ " for i in range(len(sources)):\n",
326
+ " source_text += f\"{i+1}. {sources[i]}\\n\"\n",
327
+ "\n",
328
+ " return f\"{output_text} \\n\\nSources: \\n{source_text}\"\n",
329
+ " return output_text"
330
+ ]
331
+ },
332
+ {
333
+ "cell_type": "code",
334
+ "execution_count": 19,
335
+ "metadata": {},
336
+ "outputs": [],
337
+ "source": [
338
+ "def slow_echo(message, history):\n",
339
+ " text_ = run(message)\n",
340
+ " for i in range(len(text_)):\n",
341
+ " time.sleep(0.001)\n",
342
+ " yield text_[: i+1]"
343
+ ]
344
+ },
345
+ {
346
+ "cell_type": "code",
347
+ "execution_count": 20,
348
+ "metadata": {},
349
+ "outputs": [],
350
+ "source": [
351
+ "def auth_function(username, password):\n",
352
+ " return username == password"
353
+ ]
354
+ },
355
+ {
356
+ "cell_type": "code",
357
+ "execution_count": 21,
358
+ "metadata": {},
359
+ "outputs": [
360
+ {
361
+ "name": "stdout",
362
+ "output_type": "stream",
363
+ "text": [
364
+ "\n",
365
+ "\n",
366
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
367
+ "\u001b[32;1m\u001b[1;3mThe transport of ascorbic acid to the lens is done by:\n",
368
+ "\n",
369
+ "A. Myoinositol\n",
370
+ "B. Choline\n",
371
+ "C. Taurine\n",
372
+ "D. Na/K ATPase\u001b[0m\n",
373
+ "\n",
374
+ "\u001b[1m> Finished chain.\u001b[0m\n"
375
+ ]
376
+ }
377
+ ],
378
+ "source": [
379
+ "ques = run(\"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\nReturn options\")"
380
+ ]
381
+ },
382
+ {
383
+ "cell_type": "code",
384
+ "execution_count": 17,
385
+ "metadata": {},
386
+ "outputs": [
387
+ {
388
+ "data": {
389
+ "text/plain": [
390
+ "'The transport of ascorbic acid to the lens is done by:\\n\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase'"
391
+ ]
392
+ },
393
+ "execution_count": 17,
394
+ "metadata": {},
395
+ "output_type": "execute_result"
396
+ }
397
+ ],
398
+ "source": [
399
+ "ques"
400
+ ]
401
+ },
402
+ {
403
+ "cell_type": "code",
404
+ "execution_count": 35,
405
+ "metadata": {},
406
+ "outputs": [],
407
+ "source": [
408
+ "demo = gr.ChatInterface(slow_echo).queue()"
409
+ ]
410
+ },
411
+ {
412
+ "cell_type": "code",
413
+ "execution_count": 36,
414
+ "metadata": {},
415
+ "outputs": [
416
+ {
417
+ "name": "stdout",
418
+ "output_type": "stream",
419
+ "text": [
420
+ "Running on local URL: http://127.0.0.1:7861\n",
421
+ "Running on public URL: https://c73fe7008b858e476a.gradio.live\n",
422
+ "\n",
423
+ "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"
424
+ ]
425
+ },
426
+ {
427
+ "data": {
428
+ "text/html": [
429
+ "<div><iframe src=\"https://c73fe7008b858e476a.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
430
+ ],
431
+ "text/plain": [
432
+ "<IPython.core.display.HTML object>"
433
+ ]
434
+ },
435
+ "metadata": {},
436
+ "output_type": "display_data"
437
+ },
438
+ {
439
+ "data": {
440
+ "text/plain": []
441
+ },
442
+ "execution_count": 36,
443
+ "metadata": {},
444
+ "output_type": "execute_result"
445
+ }
446
+ ],
447
+ "source": [
448
+ "demo.launch(share=True)"
449
+ ]
450
+ },
451
+ {
452
+ "cell_type": "code",
453
+ "execution_count": null,
454
+ "metadata": {},
455
+ "outputs": [],
456
+ "source": []
457
+ },
458
+ {
459
+ "cell_type": "code",
460
+ "execution_count": 16,
461
+ "metadata": {},
462
+ "outputs": [],
463
+ "source": [
464
+ "# gr.ChatInterface(\n",
465
+ "# run,\n",
466
+ "# ).launch(share=True)"
467
+ ]
468
+ },
469
+ {
470
+ "cell_type": "code",
471
+ "execution_count": null,
472
+ "metadata": {},
473
+ "outputs": [],
474
+ "source": []
475
+ },
476
+ {
477
+ "cell_type": "code",
478
+ "execution_count": 11,
479
+ "metadata": {},
480
+ "outputs": [
481
+ {
482
+ "name": "stdout",
483
+ "output_type": "stream",
484
+ "text": [
485
+ "\n",
486
+ "\n",
487
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
488
+ "\u001b[32;1m\u001b[1;3mYour name is Bob.\u001b[0m\n",
489
+ "\n",
490
+ "\u001b[1m> Finished chain.\u001b[0m\n"
491
+ ]
492
+ }
493
+ ],
494
+ "source": [
495
+ "result = agent_executor({\"input\": \"whats my name?\"})\n"
496
+ ]
497
+ },
498
+ {
499
+ "cell_type": "code",
500
+ "execution_count": 14,
501
+ "metadata": {},
502
+ "outputs": [
503
+ {
504
+ "name": "stdout",
505
+ "output_type": "stream",
506
+ "text": [
507
+ "Transport of ascorbic acid to lens is done by:\n",
508
+ "A. Myoinositol\n",
509
+ "B. Choline\n",
510
+ "C. Taurine\n",
511
+ "D. Na/K ATPase\n",
512
+ "\n",
513
+ "Return options\n"
514
+ ]
515
+ },
516
+ {
517
+ "name": "stdout",
518
+ "output_type": "stream",
519
+ "text": [
520
+ "\n",
521
+ "\n",
522
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
523
+ "\u001b[32;1m\u001b[1;3m\n",
524
+ "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
525
+ "\n",
526
+ "\n",
527
+ "\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",
528
+ "\n",
529
+ "\u001b[1m> Finished chain.\u001b[0m\n",
530
+ "\n",
531
+ "\n",
532
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
533
+ "\u001b[32;1m\u001b[1;3mYes, your name is Aakash.\u001b[0m\n",
534
+ "\n",
535
+ "\u001b[1m> Finished chain.\u001b[0m\n"
536
+ ]
537
+ }
538
+ ],
539
+ "source": [
540
+ "print(\"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\nReturn options\")"
541
+ ]
542
+ },
543
+ {
544
+ "cell_type": "code",
545
+ "execution_count": 15,
546
+ "metadata": {},
547
+ "outputs": [
548
+ {
549
+ "name": "stdout",
550
+ "output_type": "stream",
551
+ "text": [
552
+ "\n",
553
+ "\n",
554
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
555
+ "\u001b[32;1m\u001b[1;3m\n",
556
+ "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
557
+ "\n",
558
+ "\n",
559
+ "\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",
560
+ "\n",
561
+ "Here is the JSON output:\n",
562
+ "\n",
563
+ "{\"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",
564
+ "\n",
565
+ "\u001b[1m> Finished chain.\u001b[0m\n"
566
+ ]
567
+ }
568
+ ],
569
+ "source": [
570
+ "result = agent_executor(\n",
571
+ " {\n",
572
+ " \"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",
573
+ " }\n",
574
+ ")"
575
+ ]
576
+ },
577
+ {
578
+ "cell_type": "code",
579
+ "execution_count": 28,
580
+ "metadata": {},
581
+ "outputs": [
582
+ {
583
+ "data": {
584
+ "text/plain": [
585
+ "'data/book/text/Parsons_ Diseases of the Eye 22nd edition.pdf.txt'"
586
+ ]
587
+ },
588
+ "execution_count": 28,
589
+ "metadata": {},
590
+ "output_type": "execute_result"
591
+ }
592
+ ],
593
+ "source": [
594
+ "result[\"intermediate_steps\"][0][1][0].metadata['source']"
595
+ ]
596
+ },
597
+ {
598
+ "cell_type": "code",
599
+ "execution_count": 19,
600
+ "metadata": {},
601
+ "outputs": [
602
+ {
603
+ "data": {
604
+ "text/plain": [
605
+ "{'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",
606
+ " 'chat_history': [HumanMessage(content='hi, im bob'),\n",
607
+ " AIMessage(content='Hello Bob! How can I assist you today?'),\n",
608
+ " 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",
609
+ " AIMessage(content='', additional_kwargs={'function_call': {'name': 'search_ophtal-knowledge-base', 'arguments': '{\\n \"__arg1\": \"Transport of ascorbic acid to lens\"\\n}'}}),\n",
610
+ " 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",
611
+ " 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",
612
+ " '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",
613
+ " '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",
614
+ " [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",
615
+ " 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",
616
+ " 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",
617
+ " 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'})])]}"
618
+ ]
619
+ },
620
+ "execution_count": 19,
621
+ "metadata": {},
622
+ "output_type": "execute_result"
623
+ }
624
+ ],
625
+ "source": [
626
+ "result"
627
+ ]
628
+ },
629
+ {
630
+ "cell_type": "code",
631
+ "execution_count": 37,
632
+ "metadata": {},
633
+ "outputs": [
634
+ {
635
+ "name": "stdout",
636
+ "output_type": "stream",
637
+ "text": [
638
+ "\n",
639
+ "\n",
640
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
641
+ "\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",
642
+ "\n",
643
+ "\u001b[1m> Finished chain.\u001b[0m\n"
644
+ ]
645
+ }
646
+ ],
647
+ "source": [
648
+ "result = agent_executor(\n",
649
+ " {\n",
650
+ " \"input\": \"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\n\"\n",
651
+ " }\n",
652
+ ")"
653
+ ]
654
+ },
655
+ {
656
+ "cell_type": "code",
657
+ "execution_count": 38,
658
+ "metadata": {},
659
+ "outputs": [
660
+ {
661
+ "data": {
662
+ "text/plain": [
663
+ "{'input': 'Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\n',\n",
664
+ " '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",
665
+ " 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",
666
+ " '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",
667
+ " 'intermediate_steps': []}"
668
+ ]
669
+ },
670
+ "execution_count": 38,
671
+ "metadata": {},
672
+ "output_type": "execute_result"
673
+ },
674
+ {
675
+ "name": "stdout",
676
+ "output_type": "stream",
677
+ "text": [
678
+ "\n",
679
+ "\n",
680
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
681
+ "\u001b[32;1m\u001b[1;3m\n",
682
+ "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
683
+ "\n",
684
+ "\n",
685
+ "\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",
686
+ "\n",
687
+ "\u001b[1m> Finished chain.\u001b[0m\n"
688
+ ]
689
+ }
690
+ ],
691
+ "source": [
692
+ "result"
693
+ ]
694
+ },
695
+ {
696
+ "cell_type": "code",
697
+ "execution_count": 13,
698
+ "metadata": {},
699
+ "outputs": [
700
+ {
701
+ "name": "stdout",
702
+ "output_type": "stream",
703
+ "text": [
704
+ "\n",
705
+ "\n",
706
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
707
+ "\u001b[32;1m\u001b[1;3m\n",
708
+ "Invoking: `search_ophtal-knowledge-base` with `Myoinositol transport of ascorbic acid to lens`\n",
709
+ "\n",
710
+ "\n",
711
+ "\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",
712
+ "\n",
713
+ "\u001b[1m> Finished chain.\u001b[0m\n"
714
+ ]
715
+ }
716
+ ],
717
+ "source": [
718
+ "result = agent_executor(\n",
719
+ " {\n",
720
+ " \"input\": \"I belive the answer is A. Myoinositol\"\n",
721
+ " }\n",
722
+ ")"
723
+ ]
724
+ },
725
+ {
726
+ "cell_type": "code",
727
+ "execution_count": 16,
728
+ "metadata": {},
729
+ "outputs": [
730
+ {
731
+ "name": "stdout",
732
+ "output_type": "stream",
733
+ "text": [
734
+ "\n",
735
+ "\n",
736
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
737
+ "\u001b[32;1m\u001b[1;3m\n",
738
+ "Invoking: `search_ophtal-knowledge-base` with `Lens develops from`\n",
739
+ "\n",
740
+ "\n",
741
+ "\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",
742
+ "\n",
743
+ "\u001b[1m> Finished chain.\u001b[0m\n"
744
+ ]
745
+ }
746
+ ],
747
+ "source": [
748
+ "result = agent_executor(\n",
749
+ " {\n",
750
+ " \"input\": \"Question: Lens develops from?\\nA. Endoderm\\nB. Mesoderm\\nC. Surface ectoderm\\nD. Neuroectoderm\" \n",
751
+ " }\n",
752
+ ")"
753
+ ]
754
+ },
755
+ {
756
+ "cell_type": "code",
757
+ "execution_count": 8,
758
+ "metadata": {},
759
+ "outputs": [],
760
+ "source": [
761
+ "memory_key = \"history\""
762
+ ]
763
+ },
764
+ {
765
+ "cell_type": "code",
766
+ "execution_count": 10,
767
+ "metadata": {},
768
+ "outputs": [],
769
+ "source": [
770
+ "llm = ChatOpenAI(openai_api_key=os.getenv(\"OPENAI_API_KEY\"), openai_organization=os.getenv(\"ORGANIZATION_KEY\"), model=\"gpt-4\", temperature=0.2)"
771
+ ]
772
+ },
773
+ {
774
+ "cell_type": "code",
775
+ "execution_count": 11,
776
+ "metadata": {},
777
+ "outputs": [],
778
+ "source": [
779
+ "memory = AgentTokenBufferMemory(memory_key=memory_key, llm=llm)"
780
+ ]
781
+ },
782
+ {
783
+ "cell_type": "code",
784
+ "execution_count": 12,
785
+ "metadata": {},
786
+ "outputs": [],
787
+ "source": [
788
+ "from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent\n",
789
+ "from langchain.schema.messages import SystemMessage\n",
790
+ "from langchain.prompts import MessagesPlaceholder"
791
+ ]
792
+ },
793
+ {
794
+ "cell_type": "code",
795
+ "execution_count": 13,
796
+ "metadata": {},
797
+ "outputs": [],
798
+ "source": [
799
+ "prompt = open(\"prompts/version_2.txt\", \"r\").read()"
800
+ ]
801
+ },
802
+ {
803
+ "cell_type": "code",
804
+ "execution_count": 14,
805
+ "metadata": {},
806
+ "outputs": [],
807
+ "source": [
808
+ "system_message = SystemMessage(\n",
809
+ " content=prompt,\n",
810
+ ")"
811
+ ]
812
+ },
813
+ {
814
+ "cell_type": "code",
815
+ "execution_count": 15,
816
+ "metadata": {},
817
+ "outputs": [],
818
+ "source": [
819
+ "system_message = SystemMessage(\n",
820
+ " 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",
821
+ ")"
822
+ ]
823
+ },
824
+ {
825
+ "cell_type": "code",
826
+ "execution_count": 16,
827
+ "metadata": {},
828
+ "outputs": [],
829
+ "source": [
830
+ "prompt = OpenAIFunctionsAgent.create_prompt(\n",
831
+ " system_message=system_message,\n",
832
+ " extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],\n",
833
+ ")"
834
+ ]
835
+ },
836
+ {
837
+ "cell_type": "code",
838
+ "execution_count": 33,
839
+ "metadata": {},
840
+ "outputs": [],
841
+ "source": [
842
+ "agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)"
843
+ ]
844
+ },
845
+ {
846
+ "cell_type": "code",
847
+ "execution_count": 19,
848
+ "metadata": {},
849
+ "outputs": [],
850
+ "source": [
851
+ "agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=True)"
852
+ ]
853
+ },
854
+ {
855
+ "cell_type": "code",
856
+ "execution_count": 21,
857
+ "metadata": {},
858
+ "outputs": [
859
+ {
860
+ "name": "stdout",
861
+ "output_type": "stream",
862
+ "text": [
863
+ "\n",
864
+ "\n",
865
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
866
+ "\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",
867
+ "\n",
868
+ "\u001b[1m> Finished chain.\u001b[0m\n"
869
+ ]
870
+ }
871
+ ],
872
+ "source": [
873
+ "result = agent_executor({\"input\": \"can you answer questions about ophtalmology?\"})"
874
+ ]
875
+ },
876
+ {
877
+ "cell_type": "code",
878
+ "execution_count": 22,
879
+ "metadata": {},
880
+ "outputs": [
881
+ {
882
+ "name": "stdout",
883
+ "output_type": "stream",
884
+ "text": [
885
+ "\n",
886
+ "\n",
887
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
888
+ "\u001b[32;1m\u001b[1;3m\n",
889
+ "Invoking: `search_ophtal-knowledge-base` with `Transport of ascorbic acid to lens`\n",
890
+ "\n",
891
+ "\n",
892
+ "\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",
893
+ "\n",
894
+ "\u001b[1m> Finished chain.\u001b[0m\n"
895
+ ]
896
+ }
897
+ ],
898
+ "source": [
899
+ "result = agent_executor(\n",
900
+ " {\n",
901
+ " \"input\": \"Transport of ascorbic acid to lens is done by:\\nA. Myoinositol\\nB. Choline\\nC. Taurine\\nD. Na/K ATPase\\n\\n\"\n",
902
+ " }\n",
903
+ ")"
904
+ ]
905
+ },
906
+ {
907
+ "cell_type": "code",
908
+ "execution_count": 23,
909
+ "metadata": {},
910
+ "outputs": [
911
+ {
912
+ "data": {
913
+ "text/plain": [
914
+ "'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.'"
915
+ ]
916
+ },
917
+ "execution_count": 23,
918
+ "metadata": {},
919
+ "output_type": "execute_result"
920
+ }
921
+ ],
922
+ "source": [
923
+ "result['output']"
924
+ ]
925
+ },
926
+ {
927
+ "cell_type": "code",
928
+ "execution_count": 35,
929
+ "metadata": {},
930
+ "outputs": [
931
+ {
932
+ "name": "stdout",
933
+ "output_type": "stream",
934
+ "text": [
935
+ "\n",
936
+ "\n",
937
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
938
+ "\u001b[32;1m\u001b[1;3mAs an AI, I can assist with a variety of tasks, including but not limited to:\n",
939
+ "\n",
940
+ "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",
941
+ "\n",
942
+ "2. Setting reminders and alarms: I can help you remember important dates and tasks.\n",
943
+ "\n",
944
+ "3. Sending messages: I can send emails or text messages on your behalf.\n",
945
+ "\n",
946
+ "4. Making calls: I can dial numbers and make calls for you.\n",
947
+ "\n",
948
+ "5. Providing news and weather updates: I can give you the latest news headlines and weather forecasts.\n",
949
+ "\n",
950
+ "6. Managing your schedule: I can add events to your calendar and notify you of upcoming appointments.\n",
951
+ "\n",
952
+ "7. Providing entertainment: I can tell jokes, play games, and more.\n",
953
+ "\n",
954
+ "8. Conducting online searches: I can find and present information from the web.\n",
955
+ "\n",
956
+ "9. Providing recommendations: I can suggest movies, books, restaurants, and more based on your preferences.\n",
957
+ "\n",
958
+ "10. Learning and adapting: I can learn from our interactions to better assist you in the future.\n",
959
+ "\n",
960
+ "Please note that the exact capabilities can vary depending on the platform and the specific AI model.\u001b[0m\n",
961
+ "\n",
962
+ "\u001b[1m> Finished chain.\u001b[0m\n"
963
+ ]
964
+ }
965
+ ],
966
+ "source": [
967
+ "result = agent_executor(\n",
968
+ " {\n",
969
+ " \"input\": \"what all can you do?\"\n",
970
+ " }\n",
971
+ ")"
972
+ ]
973
+ },
974
+ {
975
+ "cell_type": "markdown",
976
+ "metadata": {},
977
+ "source": [
978
+ "## ReAct"
979
+ ]
980
+ },
981
+ {
982
+ "cell_type": "code",
983
+ "execution_count": 35,
984
+ "metadata": {},
985
+ "outputs": [],
986
+ "source": [
987
+ "from langchain.agents import load_tools\n",
988
+ "from langchain.agents import initialize_agent\n",
989
+ "from langchain.agents import AgentType\n",
990
+ "from langchain.llms import OpenAI\n",
991
+ "import os\n",
992
+ "from transformers import AutoTokenizer, AutoModel\n",
993
+ "import torch\n",
994
+ "from langchain.vectorstores import Pinecone\n",
995
+ "import pinecone\n",
996
+ "from langchain.agents.agent_toolkits import create_retriever_tool\n",
997
+ "from langchain.chat_models import ChatOpenAI\n",
998
+ "from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent\n",
999
+ "from langchain.schema.messages import SystemMessage\n",
1000
+ "from langchain.prompts import MessagesPlaceholder"
1001
+ ]
1002
+ },
1003
+ {
1004
+ "cell_type": "code",
1005
+ "execution_count": 27,
1006
+ "metadata": {},
1007
+ "outputs": [],
1008
+ "source": [
1009
+ "llm = ChatOpenAI(openai_api_key=os.getenv(\"OPENAI_API_KEY\"), openai_organization=os.getenv(\"ORGANIZATION_KEY\"), model=\"gpt-4\", temperature=0.2)"
1010
+ ]
1011
+ },
1012
+ {
1013
+ "cell_type": "code",
1014
+ "execution_count": 28,
1015
+ "metadata": {},
1016
+ "outputs": [],
1017
+ "source": [
1018
+ "pinecone.init(\n",
1019
+ " api_key=os.getenv(\"PINECONE_API_KEY\"), # find at app.pinecone.io\n",
1020
+ " environment=os.getenv(\"PINECONE_ENV\"), # next to api key in console\n",
1021
+ ")\n",
1022
+ "\n",
1023
+ "index_name = \"ophtal-knowledge-base\""
1024
+ ]
1025
+ },
1026
+ {
1027
+ "cell_type": "code",
1028
+ "execution_count": 29,
1029
+ "metadata": {},
1030
+ "outputs": [],
1031
+ "source": [
1032
+ "model = AutoModel.from_pretrained(\"models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e\")\n",
1033
+ "tokenizer = AutoTokenizer.from_pretrained(\"models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e\")"
1034
+ ]
1035
+ },
1036
+ {
1037
+ "cell_type": "code",
1038
+ "execution_count": 30,
1039
+ "metadata": {},
1040
+ "outputs": [],
1041
+ "source": [
1042
+ "def get_bert_embeddings(sentence):\n",
1043
+ " embeddings = []\n",
1044
+ " input_ids = tokenizer.encode(sentence, return_tensors=\"pt\")\n",
1045
+ " with torch.no_grad():\n",
1046
+ " output = model(input_ids)\n",
1047
+ " embedding = output.last_hidden_state[:,0,:].numpy().tolist()\n",
1048
+ " return embedding"
1049
+ ]
1050
+ },
1051
+ {
1052
+ "cell_type": "code",
1053
+ "execution_count": 31,
1054
+ "metadata": {},
1055
+ "outputs": [],
1056
+ "source": [
1057
+ "index = pinecone.Index(index_name)"
1058
+ ]
1059
+ },
1060
+ {
1061
+ "cell_type": "code",
1062
+ "execution_count": 32,
1063
+ "metadata": {},
1064
+ "outputs": [
1065
+ {
1066
+ "name": "stderr",
1067
+ "output_type": "stream",
1068
+ "text": [
1069
+ "/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",
1070
+ " warnings.warn(\n"
1071
+ ]
1072
+ }
1073
+ ],
1074
+ "source": [
1075
+ "vectorstore = Pinecone(index, get_bert_embeddings, \"text\")\n",
1076
+ "retriever = vectorstore.as_retriever()\n",
1077
+ "tool = create_retriever_tool(\n",
1078
+ " retriever,\n",
1079
+ " \"search_ophtal-knowledge-base\",\n",
1080
+ " \"Searches and returns documents regarding the ophtal-knowledge-base.\",\n",
1081
+ ")\n",
1082
+ "tools = [tool]"
1083
+ ]
1084
+ },
1085
+ {
1086
+ "cell_type": "code",
1087
+ "execution_count": 36,
1088
+ "metadata": {},
1089
+ "outputs": [],
1090
+ "source": [
1091
+ "system_message = SystemMessage(\n",
1092
+ " 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",
1093
+ ")"
1094
+ ]
1095
+ },
1096
+ {
1097
+ "cell_type": "code",
1098
+ "execution_count": 38,
1099
+ "metadata": {},
1100
+ "outputs": [],
1101
+ "source": [
1102
+ "memory_key = \"history\""
1103
+ ]
1104
+ },
1105
+ {
1106
+ "cell_type": "code",
1107
+ "execution_count": 41,
1108
+ "metadata": {},
1109
+ "outputs": [],
1110
+ "source": [
1111
+ "prompt = OpenAIFunctionsAgent.create_prompt(\n",
1112
+ " system_message=system_message,\n",
1113
+ " extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],\n",
1114
+ ")"
1115
+ ]
1116
+ },
1117
+ {
1118
+ "cell_type": "code",
1119
+ "execution_count": 42,
1120
+ "metadata": {},
1121
+ "outputs": [
1122
+ {
1123
+ "ename": "TypeError",
1124
+ "evalue": "'AgentType' object is not callable",
1125
+ "output_type": "error",
1126
+ "traceback": [
1127
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1128
+ "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
1129
+ "\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",
1130
+ "\u001b[0;31mTypeError\u001b[0m: 'AgentType' object is not callable"
1131
+ ]
1132
+ }
1133
+ ],
1134
+ "source": [
1135
+ "agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION(llm=llm, tools=tools, prompt=prompt)"
1136
+ ]
1137
+ },
1138
+ {
1139
+ "cell_type": "code",
1140
+ "execution_count": 33,
1141
+ "metadata": {},
1142
+ "outputs": [],
1143
+ "source": [
1144
+ "agent_executor = initialize_agent(\n",
1145
+ " tools, llm, agent=, verbose=True\n",
1146
+ ")"
1147
+ ]
1148
+ },
1149
+ {
1150
+ "cell_type": "code",
1151
+ "execution_count": 34,
1152
+ "metadata": {},
1153
+ "outputs": [
1154
+ {
1155
+ "name": "stdout",
1156
+ "output_type": "stream",
1157
+ "text": [
1158
+ "\n",
1159
+ "\n",
1160
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
1161
+ "\u001b[32;1m\u001b[1;3mI need to find out which layer of embryonic tissue the lens of the eye develops from.\n",
1162
+ "Action: search_ophtal-knowledge-base\n",
1163
+ "Action Input: Lens development embryonic tissue\u001b[0m\n",
1164
+ "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",
1165
+ "Thought:\u001b[32;1m\u001b[1;3mThe documents indicate that the lens develops from the surface ectoderm.\n",
1166
+ "Final Answer: C. Surface ectoderm\u001b[0m\n",
1167
+ "\n",
1168
+ "\u001b[1m> Finished chain.\u001b[0m\n"
1169
+ ]
1170
+ },
1171
+ {
1172
+ "data": {
1173
+ "text/plain": [
1174
+ "{'input': 'Question: Lens develops from?\\nA. Endoderm\\nB. Mesoderm\\nC. Surface ectoderm\\nD. Neuroectoderm',\n",
1175
+ " 'output': 'C. Surface ectoderm'}"
1176
+ ]
1177
+ },
1178
+ "execution_count": 34,
1179
+ "metadata": {},
1180
+ "output_type": "execute_result"
1181
+ }
1182
+ ],
1183
+ "source": [
1184
+ "agent_executor.invoke(\n",
1185
+ " {\n",
1186
+ " \"input\": \"Question: Lens develops from?\\nA. Endoderm\\nB. Mesoderm\\nC. Surface ectoderm\\nD. Neuroectoderm\" \n",
1187
+ " }\n",
1188
+ ")"
1189
+ ]
1190
+ },
1191
+ {
1192
+ "cell_type": "code",
1193
+ "execution_count": 43,
1194
+ "metadata": {},
1195
+ "outputs": [
1196
+ {
1197
+ "name": "stdout",
1198
+ "output_type": "stream",
1199
+ "text": [
1200
+ "\n",
1201
+ "\n",
1202
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
1203
+ "\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",
1204
+ "Final Answer: I'm sorry, but I can't provide the information you're looking for.\u001b[0m\n",
1205
+ "\n",
1206
+ "\u001b[1m> Finished chain.\u001b[0m\n"
1207
+ ]
1208
+ },
1209
+ {
1210
+ "data": {
1211
+ "text/plain": [
1212
+ "{'input': 'What question did I ask before?',\n",
1213
+ " 'output': \"I'm sorry, but I can't provide the information you're looking for.\"}"
1214
+ ]
1215
+ },
1216
+ "execution_count": 43,
1217
+ "metadata": {},
1218
+ "output_type": "execute_result"
1219
+ }
1220
+ ],
1221
+ "source": [
1222
+ "agent_executor.invoke(\n",
1223
+ " {\n",
1224
+ " \"input\": \"What question did I ask before?\" \n",
1225
+ " }\n",
1226
+ ")"
1227
+ ]
1228
+ },
1229
+ {
1230
+ "cell_type": "code",
1231
+ "execution_count": null,
1232
+ "metadata": {},
1233
+ "outputs": [],
1234
+ "source": []
1235
+ }
1236
+ ],
1237
+ "metadata": {
1238
+ "kernelspec": {
1239
+ "display_name": "myenv",
1240
+ "language": "python",
1241
+ "name": "python3"
1242
+ },
1243
+ "language_info": {
1244
+ "codemirror_mode": {
1245
+ "name": "ipython",
1246
+ "version": 3
1247
+ },
1248
+ "file_extension": ".py",
1249
+ "mimetype": "text/x-python",
1250
+ "name": "python",
1251
+ "nbconvert_exporter": "python",
1252
+ "pygments_lexer": "ipython3",
1253
+ "version": "3.10.9"
1254
+ }
1255
+ },
1256
+ "nbformat": 4,
1257
+ "nbformat_minor": 2
1258
+ }
deployment_app/conversation.py ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from langchain.document_loaders import TextLoader
2
+ # import pinecone
3
+ # from langchain.vectorstores import Pinecone
4
+ # import os
5
+ # from transformers import AutoTokenizer, AutoModel
6
+ # from langchain.agents.agent_toolkits import create_conversational_retrieval_agent
7
+ # from langchain.agents.agent_toolkits import create_retriever_tool
8
+ # from langchain.chat_models import ChatOpenAI
9
+ # import torch
10
+ # from langchain.agents.openai_functions_agent.agent_token_buffer_memory import (AgentTokenBufferMemory)
11
+ # from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
12
+ # from langchain.schema.messages import SystemMessage
13
+ # from langchain.prompts import MessagesPlaceholder
14
+ # import gradio as gr
15
+ # import time
16
+ # from db_func import insert_one
17
+ # from global_variable_module import gobal_input, global_output
18
+ # import random
19
+
20
+
21
+ # def get_bert_embeddings(sentence):
22
+ # embeddings = []
23
+ # input_ids = tokenizer.encode(sentence, return_tensors="pt")
24
+ # with torch.no_grad():
25
+ # output = model(input_ids)
26
+ # embedding = output.last_hidden_state[:,0,:].numpy().tolist()
27
+ # return embedding
28
+
29
+ # model_name = "BAAI/bge-base-en-v1.5"
30
+ # model = AutoModel.from_pretrained("/Users/aakashbhatnagar/Documents/masters/ophthal_llm/models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e")
31
+ # tokenizer = AutoTokenizer.from_pretrained("/Users/aakashbhatnagar/Documents/masters/ophthal_llm/models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e")
32
+ # prompt_file = open("prompts/version_2.txt", "r").read()
33
+
34
+ # pinecone.init(
35
+ # api_key=os.getenv("PINECONE_API_KEY"), # find at app.pinecone.io
36
+ # environment=os.getenv("PINECONE_ENV"), # next to api key in console
37
+ # )
38
+
39
+ # index_name = "ophtal-knowledge-base"
40
+ # index = pinecone.Index(index_name)
41
+ # vectorstore = Pinecone(index, get_bert_embeddings, "text")
42
+ # retriever = vectorstore.as_retriever()
43
+ # tool = create_retriever_tool(
44
+ # retriever,
45
+ # "search_ophtal-knowledge-base",
46
+ # "Searches and returns documents regarding the ophtal-knowledge-base.",
47
+ # )
48
+ # tools = [tool]
49
+ # system_message = SystemMessage(content=prompt_file)
50
+ # memory_key='history'
51
+
52
+ # llm = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4", temperature=0.2)
53
+ # prompt = OpenAIFunctionsAgent.create_prompt(
54
+ # system_message=system_message,
55
+ # extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],
56
+ # )
57
+
58
+ # agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=False, prompt=prompt)
59
+ # user_name = None
60
+
61
+ # def run(input_):
62
+ # output = agent_executor({"input": input_})
63
+ # output_text = output["output"]
64
+ # source_text = ""
65
+ # doc_text = ""
66
+ # global_input = input_
67
+ # global_output = output_text
68
+
69
+
70
+ # if len(output["intermediate_steps"])>0:
71
+ # documents = output["intermediate_steps"][0][1]
72
+ # sources = []
73
+ # docs = []
74
+
75
+ # for doc in documents:
76
+ # if doc.metadata["source"] not in sources:
77
+ # sources.append(doc.metadata["source"])
78
+ # docs.append(doc.page_content)
79
+
80
+ # for i in range(len(sources)):
81
+ # temp = sources[i].replace('.pdf', '').replace('.txt', '').replace("AAO", "").replace("2022-2023", "").replace("data/book", "").replace("text", "").replace(" ", " ")
82
+ # source_text += f"{i+1}. {temp}\n"
83
+ # doc_text += f"{i+1}. {docs[i]}\n"
84
+
85
+ # # output_text = f"{output_text} \n\nSources: \n{source_text}\n\nDocuments: \n{doc_text}"
86
+ # # output_text = f"{output_text}"
87
+
88
+ # doc_to_insert = {
89
+ # "user": user_name,
90
+ # "input": input_,
91
+ # "output": output_text,
92
+ # "source": source_text,
93
+ # "documents": doc_text
94
+ # }
95
+
96
+ # insert_one(doc_to_insert)
97
+
98
+ # return output_text
99
+
100
+ # def make_conversation(message, history):
101
+ # text_ = run(message)
102
+ # for i in range(len(text_)):
103
+ # time.sleep(0.001)
104
+ # yield text_[: i+1]
105
+
106
+ # def auth_function(username, password):
107
+ # user_name = username
108
+ # return username == password
109
+
110
+ # def random_response(message, accuracy, history):
111
+ # print(type(message))
112
+ # print(message)
113
+ # print(accuracy)
114
+ # out = random.choice(["Yes", "No"])
115
+ # gobal_input = out
116
+ # # open a txt file
117
+ # with open("function hit", "a+") as f:
118
+ # f.write(message)
119
+ # return out
120
+
121
+ from langchain.document_loaders import TextLoader
122
+ import pinecone
123
+ from langchain.vectorstores import Pinecone
124
+ import os
125
+ from transformers import AutoTokenizer, AutoModel
126
+ from langchain.agents.agent_toolkits import create_conversational_retrieval_agent
127
+ from langchain.agents.agent_toolkits import create_retriever_tool
128
+ from langchain.chat_models import ChatOpenAI
129
+ import torch
130
+ from langchain.agents.openai_functions_agent.agent_token_buffer_memory import (AgentTokenBufferMemory)
131
+ from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
132
+ from langchain.schema.messages import SystemMessage
133
+ from langchain.prompts import MessagesPlaceholder
134
+ import gradio as gr
135
+ import time
136
+ from db_func import insert_one
137
+ from langchain.agents import AgentExecutor
138
+ import wordninja
139
+
140
+ def clean_text(text):
141
+
142
+ text = text.strip().lower()
143
+ output_paragraph = ' '.join(''.join(text.split()).split(' '))
144
+ words = wordninja.split(output_paragraph)
145
+
146
+ return ' '.join(words)
147
+
148
+
149
+ def get_bert_embeddings(sentence):
150
+ embeddings = []
151
+ input_ids = tokenizer.encode(sentence, return_tensors="pt")
152
+ with torch.no_grad():
153
+ output = model(input_ids)
154
+ embedding = output.last_hidden_state[:,0,:].numpy().tolist()
155
+ return embedding
156
+
157
+ model_name = "BAAI/bge-base-en-v1.5"
158
+ model = AutoModel.from_pretrained(model_name)
159
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
160
+ # prompt_file = open("prompts/version_2.txt", "r").read()
161
+
162
+ pinecone.init(
163
+ api_key=os.getenv("PINECONE_API_KEY"), # find at app.pinecone.io
164
+ environment=os.getenv("PINECONE_ENV"), # next to api key in console
165
+ )
166
+
167
+ index_name = "ophtal-knowledge-base"
168
+ index = pinecone.Index(index_name)
169
+ vectorstore = Pinecone(index, get_bert_embeddings, "text")
170
+ retriever = vectorstore.as_retriever()
171
+ tool = create_retriever_tool(
172
+ retriever,
173
+ "search_ophtal-knowledge-base",
174
+ "Searches and returns documents regarding the ophtamology from ophtal-knowledge-base.",
175
+ )
176
+ tools = [tool]
177
+ system_message = SystemMessage(content="Your name is Dr. V AI. You are an assistant to Ophthamologists. Your task is to assist them with their questions. Communicate in mecial terms and be professional. You are working with a doctor so do not give the warning that you are an AI agent be confident.")
178
+ memory_key='history'
179
+
180
+ llm = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4", temperature=0.2)
181
+ # llm = ChatOpenAI(openai_api_key="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", model="gpt-4", temperature=0.2)
182
+ prompt = OpenAIFunctionsAgent.create_prompt(
183
+ system_message=system_message,
184
+ extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],
185
+ )
186
+
187
+ memory = AgentTokenBufferMemory(memory_key=memory_key, llm=llm, max_token_limit=4000)
188
+
189
+
190
+
191
+ # agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=False, prompt=prompt, )
192
+
193
+ agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
194
+
195
+ agent_executor = AgentExecutor(
196
+ agent=agent,
197
+ tools=tools,
198
+ memory=memory,
199
+ verbose=True,
200
+ return_intermediate_steps=True,
201
+ max_iterations = 2
202
+ )
203
+
204
+ # agent_executor.s
205
+
206
+ user_name = None
207
+
208
+ def run(input_):
209
+ output = agent_executor({"input": input_})
210
+ output_text = output["output"]
211
+ print(output_text)
212
+ source_text = ""
213
+ doc_text = ""
214
+
215
+ if len(output["intermediate_steps"])>0:
216
+ documents = output["intermediate_steps"][0][1]
217
+ sources = []
218
+ docs = []
219
+
220
+ for doc in documents:
221
+ if doc.metadata["source"] not in sources:
222
+ sources.append(doc.metadata["source"])
223
+ docs.append(doc.page_content)
224
+
225
+ for i in range(len(sources)):
226
+ temp = sources[i].replace('.pdf', '').replace('.txt', '').replace("AAO", "").replace("2022-2023", "").replace("data/book", "").replace("text", "").replace(" ", " ")
227
+ source_text += f"{i+1}. {temp}\n"
228
+ doc_text += f"{i+1}. {docs[i]}\n"
229
+
230
+ output_text = f"{output_text} \n\nSources: \n{source_text}\n\nDocuments: \n{doc_text}"
231
+ # output_text = f"{output_text} \n\nSources: \n{source_text}"
232
+ # output_text = f"{output_text}"
233
+
234
+ doc_to_insert = {
235
+ "user": user_name,
236
+ "input": input_,
237
+ "output": output_text,
238
+ "source": source_text,
239
+ "documents": doc_text
240
+ }
241
+
242
+ insert_one(doc_to_insert)
243
+
244
+ return output_text
245
+
246
+ def make_conversation(message, history = {}):
247
+ text_ = run(message)
248
+ for i in range(len(text_)):
249
+ time.sleep(0.001)
250
+ yield text_[: i+1]
251
+
252
+ # def auth_function(username, password):
253
+ # user_name = username
254
+ # return username == password
255
+
256
+
257
+
258
+
259
+
260
+
261
+
deployment_app/db_func.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pymongo
3
+
4
+ user_name = os.getenv("MONGO_DB_USERNAME")
5
+ password = os.getenv("MONGO_DB_PASSWORD")
6
+
7
+ # Replace these with your MongoDB cluster connection details
8
+ mongo_uri = uri = f"mongodb+srv://{user_name}:{password}@cluster0.mrrq3sf.mongodb.net/?retryWrites=true&w=majority"
9
+ database_name = "ophthal-llm"
10
+ collection_name = "drvai-deployment"
11
+
12
+
13
+ def insert_one(document):
14
+ client = pymongo.MongoClient(mongo_uri)
15
+ db = client[database_name]
16
+ collection = db[collection_name]
17
+ collection.insert_one(document)
18
+ client.close()
deployment_app/demo_rag_gpt_4.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv, find_dotenv
2
+ import os
3
+ import openai
4
+ from langchain.prompts import PromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate
5
+ from rag import fetch_top_k
6
+ import json
7
+ from langchain.chat_models import ChatOpenAI
8
+ from langchain.schema.messages import SystemMessage
9
+ import time
10
+
11
+ # langchain.chat_models.init(openai_api_key=os.getenv("OPENAI_API_KEY"))
12
+ class InValidOutput(ValueError):
13
+ def __init__(self, message='In valid result from the model'):
14
+ # Call the base class constructor with the parameters it needs
15
+ super(InValidOutput, self).__init__(message)
16
+
17
+ class PineConeException(Exception):
18
+ def __init__(self, message='Pinecone error'):
19
+ # Call the base class constructor with the parameters it needs
20
+ super(PineConeException, self).__init__(message)
21
+
22
+ class ApiException(Exception):
23
+ def __init__(self, message='API error'):
24
+ # Call the base class constructor with the parameters it needs
25
+ super(ApiException, self).__init__(message)
26
+
27
+
28
+ def create_openai_prompt(input_data, top_k, prompt_file):
29
+ #First do rag and find relevant context
30
+ try:
31
+ top_k_docuemnts = fetch_top_k(input_data=input_data, top_k=top_k)
32
+ # time.sleep(10)
33
+ context = '\n\n'.join(top_k_docuemnts)
34
+ except PineConeException as e:
35
+ print(f"Caught Pinecone Exception: {e}")
36
+
37
+ prompt = open(prompt_file).read()
38
+
39
+ # Create Chat Template
40
+
41
+ chat_template = ChatPromptTemplate.from_messages(
42
+ [
43
+ SystemMessage(
44
+ content=prompt,
45
+ ),
46
+ HumanMessagePromptTemplate.from_template("Context: {context}\nAnswer the following question.\n{input_data}"),
47
+ # HumanMessagePromptTemplate.from_template("Answer this question. Give only the option followed by a brief explanation as output\\n{text}"),
48
+ ]
49
+ )
50
+
51
+ complete_prompt = chat_template.format_messages(input_data=input_data, context=context)
52
+
53
+ return complete_prompt, top_k_docuemnts
54
+
55
+ def api_call(input_data, history={}):
56
+
57
+ top_k = 5
58
+ service="openai"
59
+ prompt_file="prompts/version_2.txt"
60
+
61
+ if service == "openai":
62
+
63
+ complete_prompt, top_k_docuemnts = create_openai_prompt(
64
+ input_data=input_data,
65
+ top_k=top_k,
66
+ prompt_file=prompt_file)
67
+ # print(top_k_docuemnts)
68
+ # time.sleep(5)
69
+
70
+ llm = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"), openai_organization=os.getenv("ORGANIZATION_KEY"), model="gpt-4", temperature=0.2)
71
+
72
+ try:
73
+ output = llm(complete_prompt).content
74
+ except ApiException as e:
75
+ print(f"API Error: {e}")
76
+
77
+ try:
78
+ output_dict = json.loads(output)
79
+ # document_index = output_dict["doc_no"].replace(" ","").split(",")
80
+ # document_index = [int(i)-1 for i in document_index]
81
+
82
+ # documents = []
83
+ # for i in document_index:
84
+ # documents.append(f"{top_k_docuemnts[i]}")
85
+
86
+ # docs = "\n\n".join(documents)
87
+
88
+ # output_text = f"Answer for the question is {output_dict['option']}.\n\nExplanation: {output_dict['explanation']}\n\nDocuments used for reference are\n\n{docs}"
89
+
90
+ return output_dict, top_k_docuemnts
91
+
92
+ except Exception as e:
93
+ docs = "\n\n".join(top_k_docuemnts)
94
+ output_text = f"{output}\n\nDocument fetched: {docs}"
95
+ # print(f"Invalid output from the model: {e}\n\nDocument fetched: {docs}")
96
+ return output_text
97
+
98
+ else:
99
+ print("Service not Supported")
100
+
101
+ # return output_text
deployment_app/prompts/system_messages/consumer_app/system_message_1.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ Your name is Dr. V AI. You are an Ophthamology assistant to provide patients with second opinion. Your task is to assist them with their questions. Your output will be converted to audio so be sympathetic and compassionate as you will deal with real patients.
deployment_app/prompts/system_messages/consumer_app/system_message_1_llama.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ <> Your name is Dr. V AI. You are an Ophthamology assistant to provide patients with second opinion. Your task is to assist them with their questions. Your output will be converted to audio so be sympathetic and compassionate as you will deal with real patients. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know.<>
deployment_app/prompts/system_messages/system_message_1.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ Your name is Dr. V AI. You are an assistant to Ophthamologists. Your task is to assist them with their questions. Communicate in mecial terms and be professional.
deployment_app/prompts/system_messages/system_message_1_llama.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ <> Your name is Dr. V AI. You are an assistant to Ophthamologists. Your task is to assist them with their questions. Communicate in mecial terms and be professional. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know.<>
deployment_app/prompts/version_1.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ You are an assistant to Ophthamologist. Your task is to help in diagnosis. You should use medical terms and professional language. You will also receive some documents as context, use those to answer the questions. If you think the context is very different from the question, ask for more questions. You should ask questions untill you can narrow down the answer.
2
+
3
+ In your output, you should include 3 things: Answer, Summary explaining the Documents used, and Document Number.
4
+
5
+ Give all outputs in json format for example {"option": "D", "explanation": "This is the explanation", "doc_no": "2,4"}
6
+
7
+ You can use multiple documents to answer the question. If you can't find answer in any document. Return {"error": "MORE CONTEXT NEEDED"}
8
+
deployment_app/prompts/version_1_wihtout_rag.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ You are an assistant to Ophthamologist. Your task is to help in diagnosis. You should use medical terms and professional language. You should ask questions untill you can narrow down the answer.
deployment_app/prompts/version_2.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ You are an assistant to Ophthamologist. Your task is to help in diagnosis. You should use medical terms and professional language. You will also receive some documents as context, use those to answer the questions. If you think the context is very different from the question, ask for more questions. You should ask questions untill you can narrow down the answer.
2
+
3
+ You can use multiple documents to answer the question. If you can't find answer in any document. Use your knowledge and answer the question.
4
+
5
+ In your output, you should include 3 things: Answer, explanation, and Document Number if used.
6
+
7
+ Give all outputs in json format for example {"option": "D", "explanation": "This is the explanation", "doc_no": "2,4"}
8
+
deployment_app/rag.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import getpass
3
+ import pinecone
4
+ from transformers import AutoTokenizer, AutoModel
5
+ import torch
6
+ # from tqdm import tqdm
7
+ from langchain.text_splitter import CharacterTextSplitter
8
+ from langchain.vectorstores import Pinecone
9
+ from langchain.document_loaders import TextLoader, DirectoryLoader
10
+ from dotenv import load_dotenv, find_dotenv
11
+
12
+
13
+ load_dotenv(find_dotenv(".env"), override=True)
14
+ #
15
+ # os.environ["PINECONE_API_KEY"] = getpass.getpass("Pinecone API Key:")
16
+ # os.environ["PINECONE_ENV"] = getpass.getpass("Pinecone Environment:")
17
+
18
+
19
+ print("Downloading model")
20
+ print()
21
+ # model = AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
22
+ model = AutoModel.from_pretrained("models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e")
23
+ tokenizer = AutoTokenizer.from_pretrained("models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e")
24
+
25
+ # tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
26
+ print()
27
+ print("Models downloaded")
28
+
29
+ pinecone.init(
30
+ api_key=os.getenv("PINECONE_API_KEY"), # find at app.pinecone.io
31
+ environment=os.getenv("PINECONE_ENV"), # next to api key in console
32
+ )
33
+
34
+ index_name = "ophtal-knowledge-base"
35
+
36
+
37
+ def get_bert_embeddings(sentence):
38
+ embeddings = []
39
+ input_ids = tokenizer.encode(sentence, return_tensors="pt")
40
+ with torch.no_grad():
41
+ output = model(input_ids)
42
+ embedding = output.last_hidden_state[:,0,:].numpy().tolist()
43
+ # embeddings.append((f"doc-{doc_no}-seg{i}", embedding, {"meta_data": text_input[i]}))
44
+ # embeddings.append((f"doc-{doc_no}-seg{i}", embedding, {"meta_data": text_input[i]}))
45
+
46
+ return embedding
47
+
48
+ def fetch_top_k(input_data, top_k):
49
+ # top_k=5
50
+
51
+
52
+ index = pinecone.Index(index_name)
53
+
54
+ # emb = get_bert_embeddings(input_data)
55
+
56
+ vectorstore = Pinecone(index, get_bert_embeddings, "text")
57
+
58
+ # query = index.query(
59
+ # vector=emb,
60
+ # top_k=top_k,
61
+ # include_values=True,
62
+ # pool_threads=100,
63
+ # )
64
+
65
+ query = vectorstore.similarity_search(input_data, k=top_k)
66
+
67
+ # id_list = []
68
+
69
+ # for i in query['matches']:
70
+ # # print(i)
71
+ # id_list.append(i['id'])
72
+
73
+ # fetched_data = index.fetch(id_list)
74
+
75
+ # topk_list = []
76
+ text_list = []
77
+ source_list = []
78
+
79
+ # for id_ in id_list:
80
+ # text = index.fetch(id_list)['vectors'][id_]['metadata']['text']
81
+ # source = index.fetch(id_list)['vectors'][id_]['metadata']['source']
82
+
83
+ # text_list.append(text)
84
+ # source_list.append(source)
85
+
86
+ for i in query:
87
+ text = i.page_content
88
+ source = i.metadata['source']
89
+ text_list.append(text)
90
+ source_list.append(source)
91
+
92
+ # print(text_list)
93
+ list_ = []
94
+ for i in range(top_k):
95
+ list_.append(f"Document: {i+1}.\n{text_list[i]}")
96
+
97
+
98
+ # return "hello"
99
+ # return '\n\n'.join(list_)
100
+ return list_
101
+
102
+ # if __name__ == "__main__":
103
+ # neet = "Which of the following is true regarding Mittendorf dot?\nA. Glial tissue projecting from optic disc\nB. Obliterated vessel running forward into the vitreous\nC. Associated with posterior polar cataract\nD. Commonest congenital anomaly of hyaloid system"
104
+ # text, source = fetch_top_k(neet)
105
+ # # print(text)
deployment_app/requirements.txt ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ aiohttp==3.8.6
3
+ wordninja
4
+ aiosignal==1.3.1
5
+ altair==5.1.2
6
+ annotated-types==0.6.0
7
+ anyio==3.7.1
8
+ appnope==0.1.3
9
+ asttokens==2.4.1
10
+ async-timeout==4.0.3
11
+ attrs==23.1.0
12
+ backoff==2.2.1
13
+ bcrypt==4.0.1
14
+ boto3==1.28.84
15
+ botocore==1.31.84
16
+ cachetools==5.3.2
17
+ certifi==2023.7.22
18
+ charset-normalizer==3.3.2
19
+ chroma-hnswlib==0.7.3
20
+ chromadb==0.4.15
21
+ click==8.1.7
22
+ colorama==0.4.6
23
+ coloredlogs==15.0.1
24
+ comm==0.1.4
25
+ contourpy==1.1.1
26
+ cycler==0.12.1
27
+ dataclasses-json==0.6.1
28
+ datasets==2.14.6
29
+ debugpy==1.8.0
30
+ decorator==5.1.1
31
+ Deprecated==1.2.14
32
+ dill==0.3.7
33
+ dnspython==2.4.2
34
+ dotenv-python==0.0.1
35
+ einops==0.7.0
36
+ encodec==0.1.1
37
+ exceptiongroup==1.1.3
38
+ executing==2.0.1
39
+ fastapi==0.104.1
40
+ ffmpy==0.3.1
41
+ filelock==3.13.1
42
+ flatbuffers==23.5.26
43
+ fonttools==4.43.1
44
+ frozenlist==1.4.0
45
+ fsspec==2023.10.0
46
+ funcy==2.0
47
+ google-auth==2.23.4
48
+ googleapis-common-protos==1.61.0
49
+ gradio==4.0.2
50
+ gradio_client==0.7.0
51
+ grpcio==1.59.2
52
+ h11==0.14.0
53
+ httpcore==0.18.0
54
+ httptools==0.6.1
55
+ httpx==0.25.0
56
+ huggingface-hub==0.17.3
57
+ humanfriendly==10.0
58
+ idna==3.4
59
+ importlib-metadata==6.8.0
60
+ importlib-resources==6.1.0
61
+ ipykernel==6.26.0
62
+ ipython==8.17.2
63
+ jedi==0.19.1
64
+ Jinja2==3.1.2
65
+ jmespath==1.0.1
66
+ joblib==1.3.2
67
+ jsonpatch==1.33
68
+ jsonpointer==2.4
69
+ jsonschema==4.19.2
70
+ jsonschema-specifications==2023.7.1
71
+ jupyter_client==8.5.0
72
+ jupyter_core==5.5.0
73
+ kiwisolver==1.4.5
74
+ kubernetes==28.1.0
75
+ langchain==0.0.327
76
+ langchainhub==0.1.13
77
+ langsmith==0.0.56
78
+ llvmlite==0.41.1
79
+ loguru==0.7.2
80
+ markdown-it-py==3.0.0
81
+ MarkupSafe==2.1.3
82
+ marshmallow==3.20.1
83
+ matplotlib==3.8.1
84
+ matplotlib-inline==0.1.6
85
+ mdurl==0.1.2
86
+ monotonic==1.6
87
+ more-itertools==10.1.0
88
+ mpmath==1.3.0
89
+ multidict==6.0.4
90
+ multiprocess==0.70.15
91
+ mypy-extensions==1.0.0
92
+ nest-asyncio==1.5.8
93
+ networkx==3.2.1
94
+ nltk==3.8.1
95
+ numba==0.58.1
96
+ numpy==1.26.1
97
+ oauthlib==3.2.2
98
+ onnxruntime==1.16.1
99
+ openai==0.28.1
100
+ openai-whisper @ git+https://github.com/openai/whisper.git@fcfeaf1b61994c071bba62da47d7846933576ac9
101
+ opentelemetry-api==1.20.0
102
+ opentelemetry-exporter-otlp-proto-common==1.20.0
103
+ opentelemetry-exporter-otlp-proto-grpc==1.20.0
104
+ opentelemetry-proto==1.20.0
105
+ opentelemetry-sdk==1.20.0
106
+ opentelemetry-semantic-conventions==0.41b0
107
+ orjson==3.9.10
108
+ overrides==7.4.0
109
+ packaging==23.2
110
+ pandas==2.1.2
111
+ parso==0.8.3
112
+ pexpect==4.8.0
113
+ Pillow==10.1.0
114
+ pinecone-client==2.2.4
115
+ platformdirs==3.11.0
116
+ posthog==3.0.2
117
+ prompt-toolkit==3.0.39
118
+ protobuf==4.25.0
119
+ psutil==5.9.6
120
+ ptyprocess==0.7.0
121
+ pulsar-client==3.3.0
122
+ pure-eval==0.2.2
123
+ pyarrow==14.0.0
124
+ pyasn1==0.5.0
125
+ pyasn1-modules==0.3.0
126
+ pydantic==2.4.2
127
+ pydantic_core==2.10.1
128
+ pydub==0.25.1
129
+ Pygments==2.16.1
130
+ pymongo==4.6.0
131
+ pyparsing==3.1.1
132
+ PyPika==0.48.9
133
+ python-dateutil==2.8.2
134
+ python-dotenv==1.0.0
135
+ python-multipart==0.0.6
136
+ pytz==2023.3.post1
137
+ PyYAML==6.0.1
138
+ pyzmq==25.1.1
139
+ referencing==0.30.2
140
+ regex==2023.10.3
141
+ requests==2.31.0
142
+ requests-oauthlib==1.3.1
143
+ rich==13.6.0
144
+ rpds-py==0.10.6
145
+ rsa==4.9
146
+ s3transfer==0.7.0
147
+ safetensors==0.4.0
148
+ scikit-learn==1.3.2
149
+ scipy==1.11.3
150
+ semantic-version==2.10.0
151
+ sentence-transformers==2.2.2
152
+ sentencepiece==0.1.99
153
+ shellingham==1.5.4
154
+ six==1.16.0
155
+ sniffio==1.3.0
156
+ SQLAlchemy==2.0.22
157
+ stack-data==0.6.3
158
+ starlette==0.27.0
159
+ suno-bark @ git+https://github.com/suno-ai/bark.git@773624d26db84278a55aacae9a16d7b25fbccab8
160
+ sympy==1.12
161
+ tenacity==8.2.3
162
+ threadpoolctl==3.2.0
163
+ tiktoken==0.5.1
164
+ tokenizers==0.14.1
165
+ tomlkit==0.12.0
166
+ toolz==0.12.0
167
+ torch==2.1.0
168
+ torchaudio==2.1.0
169
+ torchvision==0.16.0
170
+ tornado==6.3.3
171
+ tqdm==4.66.1
172
+ traitlets==5.13.0
173
+ transformers==4.34.1
174
+ typer==0.9.0
175
+ types-requests==2.31.0.10
176
+ typing-inspect==0.9.0
177
+ typing_extensions==4.8.0
178
+ tzdata==2023.3
179
+ urllib3==2.0.7
180
+ uvicorn==0.23.2
181
+ uvloop==0.19.0
182
+ watchfiles==0.21.0
183
+ wcwidth==0.2.9
184
+ websocket-client==1.6.4
185
+ websockets==11.0.3
186
+ wrapt==1.15.0
187
+ xxhash==3.4.1
188
+ yarl==1.9.2
189
+ zipp==3.17.0
deployment_app/temp.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
deployment_app/wiki_scrapper.ipynb ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 71,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import requests\n",
10
+ "from bs4 import BeautifulSoup\n",
11
+ "\n",
12
+ "def get_wikipedia_content(title):\n",
13
+ " \"\"\"\n",
14
+ " Fetch the content of a Wikipedia article by its title.\n",
15
+ " \"\"\"\n",
16
+ " # Create the URL\n",
17
+ " try:\n",
18
+ " url = f\"https://en.wikipedia.org/wiki/{title}\"\n",
19
+ "\n",
20
+ " # Get the page\n",
21
+ " response = requests.get(url)\n",
22
+ " response.raise_for_status() # Raise an exception for HTTP errors\n",
23
+ "\n",
24
+ " # Parse with BeautifulSoup\n",
25
+ " soup = BeautifulSoup(response.text, 'html.parser')\n",
26
+ "\n",
27
+ " paragraphs = soup.find_all('p')\n",
28
+ "\n",
29
+ "\n",
30
+ " # Here, I'm just returning the plain text, but you can return the HTML or specific parts if you prefer\n",
31
+ " return paragraphs\n",
32
+ " except Exception as e:\n",
33
+ " print(f\"Error occured {e} on title: {title}\")\n"
34
+ ]
35
+ },
36
+ {
37
+ "cell_type": "code",
38
+ "execution_count": 73,
39
+ "metadata": {},
40
+ "outputs": [],
41
+ "source": [
42
+ "path = \"wiki_titles.txt\"\n",
43
+ "output_path = \"data/wiki/\"\n",
44
+ "with open(path, 'r') as file: \n",
45
+ " contents = file.read()\n",
46
+ " for title in contents.split(\"\\n\"):\n",
47
+ " # print(title)\n",
48
+ " file_name = f\"{output_path}{title}.txt\"\n",
49
+ " para_list = get_wikipedia_content(title)\n",
50
+ " for para in para_list:\n",
51
+ " # print(file_name)\n",
52
+ " f = open(file_name,\"a+\") \n",
53
+ " f.write(para.get_text()) \n",
54
+ " f.close() \n",
55
+ " "
56
+ ]
57
+ },
58
+ {
59
+ "cell_type": "code",
60
+ "execution_count": 54,
61
+ "metadata": {},
62
+ "outputs": [],
63
+ "source": [
64
+ "para_list"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "code",
69
+ "execution_count": 35,
70
+ "metadata": {},
71
+ "outputs": [
72
+ {
73
+ "name": "stdout",
74
+ "output_type": "stream",
75
+ "text": [
76
+ "Ophthalmology (/ˌɒfθælˈmɒlədʒi/ OFF-thal-MOL-ə-jee)[1] is a surgical subspecialty within medicine that deals with the diagnosis and treatment of eye disorders.[2]\n",
77
+ "\n",
78
+ "An ophthalmologist is a physician who undergoes subspecialty training in medical and surgical eye care.[3] Following a medical degree, a doctor specialising in ophthalmology must pursue additional postgraduate residency training specific to that field. This may include a one-year integrated internship that involves more general medical training in other fields such as internal medicine or general surgery. Following residency, additional specialty training (or fellowship) may be sought in a particular aspect of eye pathology.[4]\n",
79
+ "\n",
80
+ "Ophthalmologists prescribe medications to treat ailments, such as eye diseases, implement laser therapy, and perform surgery when needed.[5] Ophthalmologists provide both primary and specialty eye care - medical and surgical.[5] Most ophthalmologists participate in academic research on eye diseases at some point in their training and many include research as part of their career.[6]\n",
81
+ "Ophthalmology has always been at the forefront of medical research with a long history of advancement and innovation in eye care.[7]\n",
82
+ "\n",
83
+ "A brief list of some of the most common diseases treated by ophthalmologists:[8][9]\n",
84
+ "\n",
85
+ "The most valued pharmaceutical companies worldwide whose leading products are in Ophthalmology are Regeneron (United States) for AMD treatment and Bausch Health (Canada) for Front of eye.[10]\n",
86
+ "\n",
87
+ "Following are examples of examination methods performed during an eye examination that enables diagnosis[citation needed]\n",
88
+ "\n",
89
+ "Optical coherence tomography (OCT) is a medical technological platform used to assess ocular structures. The information is then used by physicians to assess staging of pathological processes and confirm clinical diagnoses. Subsequent OCT scans are used to assess the efficacy of managing diabetic retinopathy, age-related macular degeneration, and glaucoma.\n",
90
+ "\n",
91
+ "Optical coherence tomography angiography (OCTA) and Fluorescein angiography to visualize the vascular networks of the retina and choroid.\n",
92
+ "\n",
93
+ "Electroretinography (ERG) measures the electrical responses of various cell types in the retina, including the photoreceptors (rods and cones), inner retinal cells (bipolar and amacrine cells), and the ganglion cells.\n",
94
+ "\n",
95
+ "Electrooculography (EOG) is a technique for measuring the corneo-retinal standing potential that exists between the front and the back of the human eye. The resulting signal is called the electrooculogram. Primary applications are in ophthalmological diagnosis and in recording eye movements.\n",
96
+ "\n",
97
+ "Visual field testing to detect dysfunction in central and peripheral vision which may be caused by various medical conditions such as glaucoma, stroke, pituitary disease, brain tumours or other neurological deficits.\n",
98
+ "\n",
99
+ "Corneal topography is a non-invasive medical imaging technique for mapping the anterior curvature of the cornea, the outer structure of the eye.\n",
100
+ "\n",
101
+ "Ultrasonography of the eyes may be performed by an ophthalmologist.\n",
102
+ "\n",
103
+ "Eye surgery, also known as ocular surgery, is surgery performed on the eye or its adnexa by an ophthalmologist. The eye is a fragile organ, and requires extreme care before, during, and after a surgical procedure. An eye surgeon is responsible for selecting the appropriate surgical procedure for the patient and for taking the necessary safety precautions.\n",
104
+ "\n",
105
+ "Ophthalmology includes subspecialities that deal either with certain diseases or diseases of certain parts of the eye. Some of them are:[11]\n",
106
+ "\n",
107
+ "Medical retina and vitreo-retinal surgery sometimes are combined and together they are called posterior segment subspecialisation\n",
108
+ "\n",
109
+ "The Greek roots of the word ophthalmology are ὀφθαλμός (ophthalmos, \"eye\") and -λoγία (-logia, \"study, discourse\"),[14][15] i.e., \"the study of eyes\". The discipline applies to all animal eyes, whether human or not, since the practice and procedures are quite similar with respect to disease processes, although there are differences in the anatomy or disease prevalence.\n",
110
+ "\n",
111
+ "In the Ebers Papyrus from ancient Egypt dating to 1550 BC, a section is devoted to eye diseases.[2]\n",
112
+ "\n",
113
+ "Prior to Hippocrates, physicians largely based their anatomical conceptions of the eye on speculation, rather than empiricism.[2] They recognized the sclera and transparent cornea running flushly as the outer coating of the eye, with an inner layer with pupil, and a fluid at the centre. It was believed, by Alcamaeon (fifth century BC) and others, that this fluid was the medium of vision and flowed from the eye to the brain by a tube. Aristotle advanced such ideas with empiricism. He dissected the eyes of animals, and discovering three layers (not two), found that the fluid was of a constant consistency with the lens forming (or congealing) after death, and the surrounding layers were seen to be juxtaposed. He and his contemporaries further put forth the existence of three tubes leading from the eye, not one. One tube from each eye met within the skull.\n",
114
+ "\n",
115
+ "The Greek physician Rufus of Ephesus (first century AD) recognised a more modern concept of the eye, with conjunctiva, extending as a fourth epithelial layer over the eye.[2] Rufus was the first to recognise a two-chambered eye, with one chamber from cornea to lens (filled with water), the other from lens to retina (filled with a substance resembling egg whites).\n",
116
+ "\n",
117
+ "Celsus the Greek philosopher of the second century AD gave a detailed description of cataract surgery by the couching method.\n",
118
+ "\n",
119
+ "The Greek physician Galen (second century AD) remedied some mistaken descriptions, including about the curvature of the cornea and lens, the nature of the optic nerve, and the existence of a posterior chamber. Although this model was a roughly correct modern model of the eye, it contained errors. Still, it was not advanced upon again until after Vesalius. A ciliary body was then discovered and the sclera, retina, choroid, and cornea were seen to meet at the same point. The two chambers were seen to hold the same fluid, as well as the lens being attached to the choroid. Galen continued the notion of a central canal, but he dissected the optic nerve and saw that it was solid. He mistakenly counted seven optical muscles, one too many. He also knew of the tear ducts.\n",
120
+ "\n",
121
+ "The Indian surgeon Sushruta wrote the Sushruta Samhita in Sanskrit in approximately the sixth century BC,[16] which describes 76 ocular diseases (of these, 51 surgical) as well as several ophthalmological surgical instruments and techniques.[17][18] His description of cataract surgery was compatible with the method of couching.[19] He has been described as one of the first cataract surgeons.[20][21]\n",
122
+ "\n",
123
+ "Medieval Islamic Arabic and Persian scientists (unlike their classical predecessors) considered it normal to combine theory and practice, including the crafting of precise instruments, and therefore, found it natural to combine the study of the eye with the practical application of that knowledge.[22] Hunayn ibn Ishaq, and others beginning with the medieval Arabic period, taught that the crystalline lens is in the exact center of the eye.[23] This idea was propagated until the end of the 1500s.[23]\n",
124
+ "\n",
125
+ "Ibn al-Haytham (Alhazen), in his Book of Optics explained that vision occurs when light lands on an object, bounces off, and is directed to one's eyes.\n",
126
+ "\n",
127
+ "Ibn al-Nafis, an Arabic native of Damascus, wrote a large textbook, The Polished Book on Experimental Ophthalmology, divided into two parts, On the Theory of Ophthalmology and Simple and Compounded Ophthalmic Drugs.[24]\n",
128
+ "\n",
129
+ "Avicenna wrote in his Canon \"rescheth\", which means \"retiformis\", and Gerard of Cremona translated this at approximately 1150 into the new term \"retina\".[25]\n",
130
+ "\n",
131
+ "In the seventeenth and eighteenth centuries, hand lenses were used by Malpighi, microscopes by Leeuwenhoek, preparations for fixing the eye for study by Ruysch, and later the freezing of the eye by Petit. This allowed for detailed study of the eye and an advanced model. Some mistakes persisted, such as: why the pupil changed size (seen to be vessels of the iris filling with blood), the existence of the posterior chamber, and the nature of the retina. Unaware of their functions, Leeuwenhoek noted the existence of photoreceptors,[26] however, they were not properly described until Gottfried Reinhold Treviranus in 1834.\n",
132
+ "\n",
133
+ "Approximately 1750, Jacques Daviel advocated a new treatment for cataract by extraction instead of the traditional method of couching.[27] Georg Joseph Beer (1763–1821) was an Austrian ophthalmologist and leader of the First Viennese School of Medicine. He introduced a flap operation for treatment of cataract (Beer's operation), as well as having popularized the instrument used to perform the surgery (Beer's knife).[28]\n",
134
+ "\n",
135
+ "In North America, indigenous healers treated some eye diseases by rubbing or scraping the eyes or eyelids.[29]\n",
136
+ "\n",
137
+ "The first ophthalmic surgeon in Great Britain was John Freke, appointed to the position by the governors of St. Bartholomew's Hospital in 1727. A major breakthrough came with the appointment of Baron de Wenzel (1724–90), a German who became the oculist to King George III of Great Britain in 1772. His skill at removing cataracts legitimized the field.[27] The first dedicated ophthalmic hospital opened in 1805 in London; it is now called Moorfields Eye Hospital. Clinical developments at Moorfields and the founding of the Institute of Ophthalmology (now part of the University College London) by Sir Stewart Duke-Elder established the site as the largest eye hospital in the world and a nexus for ophthalmic research.[30]\n",
138
+ "\n",
139
+ "In Berlin, ophthalmologist Albrecht von Graefe introduced iridectomy as a treatment for glaucoma and improved cataract surgery, he is also considered the founding father of the German Ophthalmological Society.\n",
140
+ "\n",
141
+ "Numerous ophthalmologists fled Germany after 1933 as the Nazis began to persecute those of Jewish descent. A representative leader was Joseph Igersheimer (1879–1965), best known for his discoveries with arsphenamine for the treatment of syphilis. He fled to Turkey in 1933. As one of eight emigrant directors in the Faculty of Medicine at the University of Istanbul, he built a modern clinic and trained students. In 1939, he went to the United States, becoming a professor at Tufts University.[31] German ophthalmologist, Gerhard Meyer-Schwickerath is widely credited with developing the predecessor of laser coagulation, photocoagulation.\n",
142
+ "\n",
143
+ "In 1946, Igersheimer conducted the first experiments on light coagulation. In 1949, he performed the first successful treatment of a retinal detachment with a light beam (light coagulation) with a self-constructed device on the roof of the ophthalmic clinic at the University of Hamburg-Eppendorf.[32][33]\n",
144
+ "\n",
145
+ "Polish ophthalmology dates to the thirteenth century. The Polish Ophthalmological Society was founded in 1911. A representative leader was Adam Zamenhof (1888–1940), who introduced certain diagnostic, surgical, and nonsurgical eye-care procedures. He was executed by the German Nazis in 1940.[34]\n",
146
+ "\n",
147
+ "Zofia Falkowska (1915–93) head of the Faculty and Clinic of Ophthalmology in Warsaw from 1963 to 1976, was the first to use lasers in her practice.\n",
148
+ "\n",
149
+ "The prominent physicists of the late nineteenth and early twentieth centuries included Ernst Abbe (1840–1905), a co-owner of at the Zeiss Jena factories in Germany, where he developed numerous optical instruments. Hermann von Helmholtz (1821-1894) was a polymath who made contributions to many fields of science and invented the ophthalmoscope in 1851. They both made theoretical calculations on image formation in optical systems and also had studied the optics of the eye.\n",
150
+ "\n",
151
+ "Ophthalmologists are physicians (MD/DO in the U.S. or MBBS in the UK and elsewhere or DO/DOMS/DNB, who typically complete an undergraduate degree, general medical school, followed by a residency in ophthalmology. Ophthalmologists typically perform optical, medical and surgical eye care.\n",
152
+ "\n",
153
+ "In Australia and New Zealand, the FRACO or FRANZCO is the equivalent postgraduate specialist qualification. The structured training system takes place over five years of postgraduate training. Overseas-trained ophthalmologists are assessed using the pathway published on the RANZCO website. Those who have completed their formal training in the UK and have the CCST or CCT, usually are deemed to be comparable.\n",
154
+ "\n",
155
+ "In Bangladesh to be an ophthalmologist the basic degree is an MBBS. Then they have to obtain a postgraduate degree or diploma in an ophthalmology specialty. In Bangladesh, these are diploma in ophthalmology, diploma in community ophthalmology, fellow or member of the College of Physicians and Surgeons in ophthalmology, and Master of Science in ophthalmology.\n",
156
+ "\n",
157
+ "In Canada, after medical school an ophthalmology residency is undertaken. The residency typically lasts five years, which culminates in fellowship of the Royal College of Surgeons of Canada (FRCSC). Subspecialty training is undertaken by approximately 30% of fellows (FRCSC) in a variety of fields from anterior segment, cornea, glaucoma, vision rehabilitation, uveitis, oculoplastics, medical and surgical retina, ocular oncology, Ocular pathology, or neuro-ophthalmology. Approximately 35 vacancies open per year for ophthalmology residency training in all of Canada. These numbers fluctuate per year, ranging from 30 to 37 spots. Of these, up to ten spots are at French-speaking universities in Quebec. At the end of the five years, the graduating ophthalmologist must pass the oral and written portions of the Royal College exam in either English or French.\n",
158
+ "\n",
159
+ "In India, after completing MBBS degree, postgraduate study in ophthalmology is required. The degrees are doctor of medicine, master of surgery, diploma in ophthalmic medicine and surgery, and diplomate of national board. The concurrent training and work experience are in the form of a junior residency at a medical college, eye hospital, or institution under the supervision of experienced faculty. Further work experience in the form of fellowship, registrar, or senior resident refines the skills of these eye surgeons. All members of the India Ophthalmologist Society and various state-level ophthalmologist societies hold regular conferences and actively promote continuing medical education.\n",
160
+ "\n",
161
+ "In Nepal, to become an ophthalmologist, three years of postgraduate study is required after completing an MBBS degree. The postgraduate degree in ophthalmology is called medical doctor in ophthalmology. Currently, this degree is provided by Tilganga Institute of Ophthalmology, Tilganga, Kathmandu, BPKLCO, Institute of Medicine, TU, Kathmandu, BP Koirala Institute of Health Sciences, Dharan, Kathmandu University, Dhulikhel, and National Academy of Medical Science, Kathmandu. A few Nepalese citizens also study this subject in Bangladesh, China, India, Pakistan, and other countries. All graduates have to pass the Nepal Medical Council Licensing Exam to become a registered ophthalmologists in Nepal. The concurrent residency training is in the form of a PG student (resident) at a medical college, eye hospital, or institution according to the degree providing university's rules and regulations. Nepal Ophthalmic Society holds regular conferences and actively promotes continuing medical education.\n",
162
+ "\n",
163
+ "In Ireland, the Royal College of Surgeons of Ireland grants membership (MRCSI (Ophth)) and fellowship (FRCSI (Ophth)) qualifications in conjunction with the Irish College of Ophthalmologists. Total postgraduate training involves an intern year, a minimum of three years of basic surgical training, and a further 4.5 years of higher surgical training. Clinical training takes place within public, Health Service Executive-funded hospitals in Dublin, Sligo, Limerick, Galway, Waterford, and Cork. A minimum of 8.5 years of training is required before eligibility to work in consultant posts. Some trainees take extra time to obtain MSc, MD or PhD degrees and to undertake clinical fellowships in the UK, Australia, and the United States.\n",
164
+ "\n",
165
+ "In Pakistan, after MBBS, a four-year full-time residency program leads to an exit-level FCPS examination in ophthalmology, held under the auspices of the College of Physicians and Surgeons, Pakistan. The tough examination is assessed by both highly qualified Pakistani and eminent international ophthalmic consultants. As a prerequisite to the final examinations, an intermediate module, an optics and refraction module, and a dissertation written on a research project carried out under supervision is also assessed.\n",
166
+ "\n",
167
+ "Moreover, a two-and-a-half-year residency program leads to an MCPS while a two-year training of DOMS is also being offered.[35] For candidates in the military, a stringent two-year graded course, with quarterly assessments, is held under Armed Forces Post Graduate Medical Institute in Rawalpindi.\n",
168
+ "\n",
169
+ "The M.S. in ophthalmology is also one of the specialty programs. In addition to programs for physicians, various diplomas and degrees for allied eyecare personnel are also being offered to produce competent optometrists, orthoptists, ophthalmic nurses, ophthalmic technologists, and ophthalmic technicians in this field. These programs are being offered, notably by the College of Ophthalmology and Allied Vision Sciences, in Lahore and the Pakistan Institute of Community Ophthalmology in Peshawar.[36] Subspecialty fellowships also are being offered in the fields of pediatric ophthalmology and vitreoretinal ophthalmology. King Edward Medical University, Al Shifa Trust Eye Hospital Rawalpindi, and Al- Ibrahim Eye Hospital Karachi also have started a degree program in this field.\n",
170
+ "\n",
171
+ "In the Philippines, Ophthalmology is considered a medical specialty that uses medicine and surgery to treat diseases of the eye. There is only one professional organization in the country that is duly recognized by the PMA and the PCS: the Philippine Academy of Ophthalmology (PAO).[37] PAO and the state-standard Philippine Board of Ophthalmology (PBO) regulates ophthalmology residency programs and board certification. To become a general ophthalmologist in the Philippines, a candidate must have completed a doctor of medicine degree (MD) or its equivalent (e.g. MBBS), have completed an internship in Medicine, have passed the physician licensure exam, and have completed residency training at a hospital accredited by the Philippine Board of Ophthalmology (accrediting arm of PAO).[38] Attainment of board certification in ophthalmology from the PBO is essential in acquiring privileges in most major health institutions. Graduates of residency programs can receive further training in ophthalmology subspecialties, such as neuro-ophthalmology, retina, etc. by completing a fellowship program that varies in length depending on each program's requirements.\n",
172
+ "\n",
173
+ "In the United Kingdom, three colleges grant postgraduate degrees in ophthalmology. The Royal College of Ophthalmologists (RCOphth) and the Royal College of Surgeons of Edinburgh grant MRCOphth/FRCOphth and MRCSEd/FRCSEd, (although membership is no longer a prerequisite for fellowship), the Royal College of Glasgow grants FRCS. Postgraduate work as a specialist registrar and one of these degrees is required for specialization in eye diseases. Such clinical work is within the NHS, with supplementary private work for some consultants.\n",
174
+ "\n",
175
+ "Only 2.3 ophthalmologists exist per 100,000 population in the UK – fewer pro rata than in any nations in the European Union.[39]\n",
176
+ "\n",
177
+ "Ophthalmologists typically complete four years of undergraduate studies, four years of medical school and four years of eye-specific training (residency). Some pursue additional training, known as a fellowship - typically one to two years. Ophthalmologists are physicians who specialize in the eye and related structures. They perform medical and surgical eye care and may also write prescriptions for corrective lenses. They often manage late stage eye disease, which typically involves surgery.[40]\n",
178
+ "\n",
179
+ "Ophthalmologists must complete the requirements of continuing medical education to maintain licensure and for recertification.\n",
180
+ "\n",
181
+ "The following is a list of physicians who have significantly contributed to the field of ophthalmology:\n",
182
+ "\n"
183
+ ]
184
+ }
185
+ ],
186
+ "source": [
187
+ "for i in a:\n",
188
+ " print(i.get_text())"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type": "code",
193
+ "execution_count": null,
194
+ "metadata": {},
195
+ "outputs": [],
196
+ "source": []
197
+ }
198
+ ],
199
+ "metadata": {
200
+ "kernelspec": {
201
+ "display_name": "myenv",
202
+ "language": "python",
203
+ "name": "python3"
204
+ },
205
+ "language_info": {
206
+ "codemirror_mode": {
207
+ "name": "ipython",
208
+ "version": 3
209
+ },
210
+ "file_extension": ".py",
211
+ "mimetype": "text/x-python",
212
+ "name": "python",
213
+ "nbconvert_exporter": "python",
214
+ "pygments_lexer": "ipython3",
215
+ "version": "3.10.9"
216
+ },
217
+ "orig_nbformat": 4
218
+ },
219
+ "nbformat": 4,
220
+ "nbformat_minor": 2
221
+ }
temp.ipynb CHANGED
@@ -36,7 +36,7 @@
36
  },
37
  {
38
  "cell_type": "code",
39
- "execution_count": 6,
40
  "metadata": {},
41
  "outputs": [
42
  {
 
36
  },
37
  {
38
  "cell_type": "code",
39
+ "execution_count": 3,
40
  "metadata": {},
41
  "outputs": [
42
  {