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