{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"},{"sourceId":33551,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":28083}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Published on May 17, 2024. By Marília Prata, mpwolke","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-05-17T23:50:49.742211Z","iopub.execute_input":"2024-05-17T23:50:49.742916Z","iopub.status.idle":"2024-05-17T23:52:00.796354Z","shell.execute_reply.started":"2024-05-17T23:50:49.742875Z","shell.execute_reply":"2024-05-17T23:52:00.795283Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://media.licdn.com/dms/image/sync/D4E27AQGn4IqBa3ZSyg/articleshare-shrink_800/0/1711537540691?e=2147483647&v=beta&t=h2peVoq8KUe22f-EUnMnzR9RP2RQXtDRXQ2QJkcJ9yw)https://www.linkedin.com/posts/andrej-rusakov_remedylogics-abstract-was-selected-to-be-activity-6953735925440933888-XqgZ/","metadata":{}},{"cell_type":"markdown","source":"#Lumbar Spinal Stenosis (LSS) Questions\n\nWhat are the main/core conditions of Lumbar Spine degeneration?\n\nWhat are the severity levels of Spinal Canal Stenosis?\n\nWhat are the relevant vertebrae affected by Lumbar degeneration?\n\nWhich is the most frequent severity level in left_subarticular_stenosis_l2_l3?\n\nWhich is the percent of Moderate level in right_subarticular_stenosis_l1_l2?\n\nWhich is the percent of Severe cases in spinal_canal_stenosis_l4_l5?\n\nSpinal Stenosis\n\n\"Spinal stenosis happens when the space around your spinal cord becomes too narrow. This irritates your spinal cord and/or the nerves that branch off it. Spinal stenosis causes symptoms like back or neck pain and tingling in your arms or legs. There are several causes, as well as several treatment options.\"\n\nhttps://my.clevelandclinic.org/health/diseases/17499-spinal-stenosis","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-17T23:54:47.017755Z","iopub.execute_input":"2024-05-17T23:54:47.018766Z","iopub.status.idle":"2024-05-17T23:54:47.123621Z","shell.execute_reply.started":"2024-05-17T23:54:47.018728Z","shell.execute_reply":"2024-05-17T23:54:47.122513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"A neural network model for detection and classification of lumbar spinal stenosis on MRI\n\nAuthors: Vladislav Tumko, Jack Kim , Natalia Uspenskaia, Shaun Honig, Frederik Abel, Darren R Lebl, Irene Hotalen, Serhii Kolisnyk, Mikhail Kochnev, Andrej Rusakov, Raphaël Mourad \n\nDOI: 10.1007/s00586-023-08089-2\n\n\"The objective of that paper is to develop a three-stage convolutional neural network (CNN) approach to segment anatomical structures, classify the presence of lumbar spinal stenosis (LSS) for all 3 stenosis types: central, lateral recess and foraminal and assess its severity on spine MRI and to demonstrate its efficacy as an accurate and consistent diagnostic tool.\"\n\n\"The model showed comparable performance to the radiologist average both in terms of the determination of presence/absence of LSS as well as severity classification, for all 3 stenosis types. In the case of central canal stenosis, the sensitivity, specificity and AUROC of the CNN were (0.971, 0.864, 0.963) for binary (presence/absence) classification compared to the radiologist average of (0.786, 0.899, 0.842). For lateral recess stenosis, the sensitivity, specificity and AUROC of the CNN were (0.853, 0.787, 0.907) compared to the radiologist average of (0.713, 0.898, 805). For foraminal stenosis, the sensitivity, specificity and AUROC of the CNN were (0.942, 0.844, 0.950) compared to the radiologist average of (0.879, 0.877, 0.878). Multi-class severity classifications showed similarly comparable statistics.\"\n\n\"The CNN showed comparable performance to radiologist subspecialists for the detection and classification of LSS. The integration of neural network models in the detection of LSS (lumbar spinal stenosis) could bring higher accuracy, efficiency, consistency, and post-hoc interpretability in diagnostic practices.\"\n\nhttps://pubmed.ncbi.nlm.nih.gov/38150003/","metadata":{}},{"cell_type":"code","source":"!pip install transformers==4.33.0 accelerate==0.22.0 einops==0.6.1 langchain==0.0.300 xformers==0.0.21 \\\nbitsandbytes==0.41.1 sentence_transformers==2.2.2 chromadb==0.4.12","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-17T23:54:54.230991Z","iopub.execute_input":"2024-05-17T23:54:54.232049Z","iopub.status.idle":"2024-05-17T23:57:57.161243Z","shell.execute_reply.started":"2024-05-17T23:54:54.232013Z","shell.execute_reply":"2024-05-17T23:57:57.160028Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Import packages","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nfrom torch import cuda, bfloat16\nimport torch\nimport transformers\nfrom transformers import AutoTokenizer\nfrom time import time\n#import chromadb\n#from chromadb.config import Settings\nfrom langchain.llms import HuggingFacePipeline\nfrom langchain.document_loaders import PyPDFLoader\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter\nfrom langchain.embeddings import HuggingFaceEmbeddings\nfrom langchain.chains import RetrievalQA\nfrom langchain.vectorstores import Chroma","metadata":{"execution":{"iopub.status.busy":"2024-05-17T23:57:59.329265Z","iopub.execute_input":"2024-05-17T23:57:59.330813Z","iopub.status.idle":"2024-05-17T23:58:06.784710Z","shell.execute_reply.started":"2024-05-17T23:57:59.330764Z","shell.execute_reply":"2024-05-17T23:58:06.783884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nmodel_id = '/kaggle/input/llama-3/transformers/8b-chat-hf/1'\n\ndevice = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'\n\n# set quantization configuration to load large model with less GPU memory\n# this requires the `bitsandbytes` library\nbnb_config = transformers.BitsAndBytesConfig(\n    load_in_4bit=True,\n    bnb_4bit_quant_type='nf4',\n    bnb_4bit_use_double_quant=True,\n    bnb_4bit_compute_dtype=bfloat16\n)\n\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2024-05-17T23:58:11.115562Z","iopub.execute_input":"2024-05-17T23:58:11.116152Z","iopub.status.idle":"2024-05-17T23:58:11.176137Z","shell.execute_reply.started":"2024-05-17T23:58:11.116118Z","shell.execute_reply":"2024-05-17T23:58:11.175256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Turn On GPU\n\nSetting `pad_token_id` to `eos_token_id`:128001 for open-end generation.\n/opt/conda/lib/python3.10/site-packages/transformers/generation/utils.py:1268: UserWarning: Input length of input_ids is 1266, but `max_length` is set to 1024. This can lead to unexpected behavior. You should consider increasing `max_new_tokens`.\n  warnings.warn(","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\ntime_start = time()\nmodel_config = transformers.AutoConfig.from_pretrained(\n   model_id,\n    trust_remote_code=True,\n    max_new_tokens=2048 #Original is 1024\n)\nmodel = transformers.AutoModelForCausalLM.from_pretrained(\n    model_id,\n    trust_remote_code=True,\n    config=model_config,\n    quantization_config=bnb_config,\n    device_map='auto',\n)\ntokenizer = AutoTokenizer.from_pretrained(model_id)\ntime_end = time()\nprint(f\"Prepare model, tokenizer: {round(time_end-time_start, 3)} sec.\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-18T00:10:41.492656Z","iopub.execute_input":"2024-05-18T00:10:41.493692Z","iopub.status.idle":"2024-05-18T00:11:09.487381Z","shell.execute_reply.started":"2024-05-18T00:10:41.493647Z","shell.execute_reply":"2024-05-18T00:11:09.486281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\ntime_start = time()\nquery_pipeline = transformers.pipeline(\n        \"text-generation\",\n        model=model,\n        tokenizer=tokenizer,\n        torch_dtype=torch.float16,\n        max_length=2048,#Original was 1024\n        device_map=\"auto\",)\ntime_end = time()\nprint(f\"Prepare pipeline: {round(time_end-time_start, 3)} sec.\")","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:11:16.931462Z","iopub.execute_input":"2024-05-18T00:11:16.932349Z","iopub.status.idle":"2024-05-18T00:11:16.938971Z","shell.execute_reply.started":"2024-05-18T00:11:16.932313Z","shell.execute_reply":"2024-05-18T00:11:16.937860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\ndef test_model(tokenizer, pipeline, message):\n    \"\"\"\n    Perform a query\n    print the result\n    Args:\n        tokenizer: the tokenizer\n        pipeline: the pipeline\n        message: the prompt\n    Returns\n        None\n    \"\"\"    \n    time_start = time()\n    sequences = pipeline(\n        message,\n        do_sample=True,\n        top_k=10,\n        num_return_sequences=1,\n        eos_token_id=tokenizer.eos_token_id,\n        max_length=200,)\n    time_end = time()\n    total_time = f\"{round(time_end-time_start, 3)} sec.\"\n    \n    question = sequences[0]['generated_text'][:len(message)]\n    answer = sequences[0]['generated_text'][len(message):]\n    \n    return f\"Question: {question}\\nAnswer: {answer}\\nTotal time: {total_time}\"","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:11:23.223511Z","iopub.execute_input":"2024-05-18T00:11:23.223928Z","iopub.status.idle":"2024-05-18T00:11:23.231526Z","shell.execute_reply.started":"2024-05-18T00:11:23.223895Z","shell.execute_reply":"2024-05-18T00:11:23.230384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nfrom IPython.display import display, Markdown\ndef colorize_text(text):\n    for word, color in zip([\"Reasoning\", \"Question\", \"Answer\", \"Total time\"], [\"blue\", \"red\", \"green\", \"magenta\"]):\n        text = text.replace(f\"{word}:\", f\"\\n\\n**<font color='{color}'>{word}:</font>**\")\n    return text","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:01:11.201721Z","iopub.execute_input":"2024-05-18T00:01:11.202517Z","iopub.status.idle":"2024-05-18T00:01:11.208378Z","shell.execute_reply.started":"2024-05-18T00:01:11.202481Z","shell.execute_reply":"2024-05-18T00:01:11.207203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#What are the severity levels of Spinal Canal Stenosis?","metadata":{}},{"cell_type":"markdown","source":"#After changing max_new_tokens=1266 . Original is 1024","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nresponse = test_model(tokenizer,\n                    query_pipeline,\n                   \"What are the severity levels of Spinal Canal Stenosis?\")\ndisplay(Markdown(colorize_text(response)))","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:11:52.640681Z","iopub.execute_input":"2024-05-18T00:11:52.641475Z","iopub.status.idle":"2024-05-18T00:12:11.599427Z","shell.execute_reply.started":"2024-05-18T00:11:52.641438Z","shell.execute_reply":"2024-05-18T00:12:11.598425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Which is the percent of Moderate level in right_subarticular_stenosis_l1_l2?","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nresponse = test_model(tokenizer,\n                    query_pipeline,\n                   \"Which is the percent of Moderate level in right_subarticular_stenosis_l1_l2?\")\ndisplay(Markdown(colorize_text(response)))","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:12:30.101585Z","iopub.execute_input":"2024-05-18T00:12:30.102021Z","iopub.status.idle":"2024-05-18T00:12:48.320147Z","shell.execute_reply.started":"2024-05-18T00:12:30.101972Z","shell.execute_reply":"2024-05-18T00:12:48.319034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Retrieval Augmented Generation","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nllm = HuggingFacePipeline(pipeline=query_pipeline)\n\n# checking again that everything is working fine\ntime_start = time()\nquestion = \"What are the core conditions of Lumbar Spine degeneration?\"\nresponse = llm(prompt=question)\ntime_end = time()\ntotal_time = f\"{round(time_end-time_start, 3)} sec.\"\nfull_response =  f\"Question: {question}\\nAnswer: {response}\\nTotal time: {total_time}\"\ndisplay(Markdown(colorize_text(full_response)))","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:13:31.115792Z","iopub.execute_input":"2024-05-18T00:13:31.116181Z","iopub.status.idle":"2024-05-18T00:15:10.984213Z","shell.execute_reply.started":"2024-05-18T00:13:31.116153Z","shell.execute_reply":"2024-05-18T00:15:10.983140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Ingestion of data using TextLoader/Langchain","metadata":{}},{"cell_type":"code","source":"from langchain.document_loaders import TextLoader\n\n#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama-2-langchain-and-chromadb/\n\nloader = TextLoader(\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv\")\n                    #encoding=\"utf8\")\ndocuments = loader.load()","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:15:20.515087Z","iopub.execute_input":"2024-05-18T00:15:20.515986Z","iopub.status.idle":"2024-05-18T00:15:20.522061Z","shell.execute_reply.started":"2024-05-18T00:15:20.515950Z","shell.execute_reply":"2024-05-18T00:15:20.520824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Split data in chunks\n\nWe split data in chunks using a recursive character text splitter","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\ntext_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)\nall_splits = text_splitter.split_documents(documents)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:15:25.495124Z","iopub.execute_input":"2024-05-18T00:15:25.496066Z","iopub.status.idle":"2024-05-18T00:15:25.520721Z","shell.execute_reply.started":"2024-05-18T00:15:25.496034Z","shell.execute_reply":"2024-05-18T00:15:25.519885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Creating Embeddings and Storing in Vector Store","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nmodel_name = \"sentence-transformers/all-mpnet-base-v2\"\nmodel_kwargs = {\"device\": \"cuda\"}\n\nembeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:15:30.346965Z","iopub.execute_input":"2024-05-18T00:15:30.347818Z","iopub.status.idle":"2024-05-18T00:15:31.549524Z","shell.execute_reply.started":"2024-05-18T00:15:30.347778Z","shell.execute_reply":"2024-05-18T00:15:31.548632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Initialize ChromaDB with the document splits.\n\nThe embeddings defined previously and with the option to persist it locally.","metadata":{}},{"cell_type":"code","source":"vectordb = Chroma.from_documents(documents=all_splits, embedding=embeddings, persist_directory=\"chroma_db\")","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:15:44.140619Z","iopub.execute_input":"2024-05-18T00:15:44.141446Z","iopub.status.idle":"2024-05-18T00:15:53.267452Z","shell.execute_reply.started":"2024-05-18T00:15:44.141413Z","shell.execute_reply":"2024-05-18T00:15:53.266310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Initialize chain","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nretriever = vectordb.as_retriever()\n\nqa = RetrievalQA.from_chain_type(\n    llm=llm, \n    chain_type=\"stuff\", \n    retriever=retriever, \n    verbose=True\n)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:15:58.886260Z","iopub.execute_input":"2024-05-18T00:15:58.886977Z","iopub.status.idle":"2024-05-18T00:15:58.929912Z","shell.execute_reply.started":"2024-05-18T00:15:58.886942Z","shell.execute_reply":"2024-05-18T00:15:58.928703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Test est the Retrieval-Augmented Generation\n\nWe define a test function, that will run the query and time it.","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\ndef test_rag(qa, query):\n\n    time_start = time()\n    response = qa.run(query)\n    time_end = time()\n    total_time = f\"{round(time_end-time_start, 3)} sec.\"\n\n    full_response =  f\"Question: {query}\\nAnswer: {response}\\nTotal time: {total_time}\"\n    display(Markdown(colorize_text(full_response)))","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:16:05.466374Z","iopub.execute_input":"2024-05-18T00:16:05.466760Z","iopub.status.idle":"2024-05-18T00:16:05.472729Z","shell.execute_reply.started":"2024-05-18T00:16:05.466732Z","shell.execute_reply":"2024-05-18T00:16:05.471610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Check a few queries\n\nI changed max_length from 1024 to 1266 and didn't help anything","metadata":{}},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\n\nquery = \"Which is the percent of Severe cases in spinal_canal_stenosis_l4_l5?\"\ntest_rag(qa, query)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:16:12.039744Z","iopub.execute_input":"2024-05-18T00:16:12.040097Z","iopub.status.idle":"2024-05-18T00:16:15.904563Z","shell.execute_reply.started":"2024-05-18T00:16:12.040071Z","shell.execute_reply":"2024-05-18T00:16:15.903573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\nquery = \"Which is the percent of Moderate level in right_subarticular_stenosis_l1_l2?\"\ntest_rag(qa, query)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:17:20.098974Z","iopub.execute_input":"2024-05-18T00:17:20.099679Z","iopub.status.idle":"2024-05-18T00:17:23.935354Z","shell.execute_reply.started":"2024-05-18T00:17:20.099647Z","shell.execute_reply":"2024-05-18T00:17:23.934336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\nquery = \"What are the relevant vertebrae affected by Lumbar degeneration?\"\ntest_rag(qa, query)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:07:42.897796Z","iopub.execute_input":"2024-05-18T00:07:42.898591Z","iopub.status.idle":"2024-05-18T00:07:46.692765Z","shell.execute_reply.started":"2024-05-18T00:07:42.898555Z","shell.execute_reply":"2024-05-18T00:07:46.691793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\nquery = \"Which is the most frequent severity level in left_subarticular_stenosis_l2_l3?\"\ntest_rag(qa, query)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:08:04.239666Z","iopub.execute_input":"2024-05-18T00:08:04.240090Z","iopub.status.idle":"2024-05-18T00:08:08.047863Z","shell.execute_reply.started":"2024-05-18T00:08:04.240056Z","shell.execute_reply":"2024-05-18T00:08:08.046762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\nquery = \"Which is the most frequent severity level in right_subarticular_stenosis_l3_l4?\"\ntest_rag(qa, query)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:21:54.652732Z","iopub.execute_input":"2024-05-18T00:21:54.653137Z","iopub.status.idle":"2024-05-18T00:21:58.447035Z","shell.execute_reply.started":"2024-05-18T00:21:54.653108Z","shell.execute_reply":"2024-05-18T00:21:58.445963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Gabriel Preda https://www.kaggle.com/code/gpreda/rag-using-llama3-langchain-and-chromadb/notebook\nquery = \"Which is the most frequent severity level in left_neural_foraminal_narrowing_l4_l5?\"\ntest_rag(qa, query)","metadata":{"execution":{"iopub.status.busy":"2024-05-18T00:17:49.891684Z","iopub.execute_input":"2024-05-18T00:17:49.892101Z","iopub.status.idle":"2024-05-18T00:17:53.717171Z","shell.execute_reply.started":"2024-05-18T00:17:49.892069Z","shell.execute_reply":"2024-05-18T00:17:53.716225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The answers before The RAG were satisfactory, however after the Retrieval-Augmented Generation I got nothing that could be acceptable as an answer. Besides, I have No clue how to fix it. Increasing max_length, as mentioned in warnings, didn't help too.","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgements:\n\nGabriel Preda https://www.kaggle.com/code/gpreda/prompting-llama-3-like-a-pro","metadata":{}}]}