{ "cells": [ { "cell_type": "markdown", "id": "45d62106", "metadata": {}, "source": [ "# Basic RAG Implementation with a local LLM" ] }, { "cell_type": "code", "execution_count": 1, "id": "4c312410", "metadata": {}, "outputs": [], "source": [ "from gpt4all import GPT4All\n", "from sentence_transformers import SentenceTransformer\n", "from chromadb import PersistentClient\n", "from docx import Document\n", "\n", "MODEL = \"Meta-Llama-3-8B-Instruct.Q4_0.gguf\"\n", "CONTEXT_SIZE = 8192\n", "EMBEDDER = \"all-MiniLM-L6-v2\"\n", "RAG_PATH = \"./build/rag_db\"\n", "DOCS_PATH = \"./build/documents/fNIRS_Glossary_Hardware.docx\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "90bae527", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "104f2001edc34aa5aff82734b3388041", "version_major": 2, "version_minor": 0 }, "text/plain": [ "modules.json: 0%| | 0.00/349 [00:00 max_context_length:\n", " context = context[:max_context_length] + \"...\"\n", "\n", " prompt = f\"\"\"\n", "Use the context to answer the question.\n", "Context:\n", "{context}\n", "Question:\n", "{query}\n", "Answer:\n", "\"\"\"\n", " print(f\"Prompt length: {len(prompt)}\")\n", " return model.generate(prompt, max_tokens=200)" ] }, { "cell_type": "code", "execution_count": 5, "id": "6fa9fd10", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of documents: 68\n", "Document lengths: [1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 63]\n", "Retrieved docs length: 1\n", "Prompt length: 627\n" ] } ], "source": [ "query = \"What can Frequency domain multidistance NIRS estimate?\"\n", "print(f\"Number of documents: {len(documents)}\")\n", "print(f\"Document lengths: {[len(doc) for doc in documents]}\")\n", "retrieved = retrieve(query)\n", "print(f\"Retrieved docs length: {len(retrieved)}\")\n", "response = rag_answer(query)" ] }, { "cell_type": "code", "execution_count": 6, "id": "5a82353e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Frequency-domain (FD) multidistance NIRS technique can estimate absolute values of absorption and scattering of the medium, and subsequently chromophore concentrations.'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.13.9" } }, "nbformat": 4, "nbformat_minor": 5 }