{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c1f56ae3-a056-4b31-bcab-27c2c97c00f1", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from dotenv import load_dotenv\n", "import numpy as np\n", "\n", "load_dotenv()\n", "\n", "from importlib.util import find_spec\n", "if find_spec(\"rag\") is None:\n", " import sys\n", " sys.path.append('..')\n", "from rag.parser.pdf import parser\n", "from rag.db.embeddings import Embeddings\n", "from rag.llm.encoder import Encoder\n", "from rag.db.documents import Documents" ] }, { "cell_type": "code", "execution_count": 2, "id": "6b5cb12e-df7e-4532-b78b-216e11ed6161", "metadata": {}, "outputs": [], "source": [ "path = Path(\"/home/aktersnurra/projects/library/quant/math/a-signal-processing-perspective-on-financial-engineering.pdf\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "f1a57be8-21a2-48d3-b99f-d1bbf7b8780a", "metadata": {}, "outputs": [], "source": [ "chunks = parser(path)" ] }, { "cell_type": "code", "execution_count": 4, "id": "14408e20-3dec-40b4-9dda-3397beb0c453", "metadata": {}, "outputs": [], "source": [ "encoder = Encoder()" ] }, { "cell_type": "code", "execution_count": 5, "id": "e94993c5-6b19-4bac-b7e8-c26a5200a626", "metadata": {}, "outputs": [], "source": [ "embs = encoder.encode_document(chunks)" ] }, { "cell_type": "code", "execution_count": 6, "id": "b845bb31-0909-42cb-9957-9a8b3bb0b5c4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1024,)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "embs[0].shape" ] }, { "cell_type": "code", "execution_count": 7, "id": "aa279b1a-465e-4820-ab56-b25fc513c0a1", "metadata": {}, "outputs": [], "source": [ "emb_db = Embeddings()" ] }, { "cell_type": "code", "execution_count": 8, "id": "1dc655de-2359-42ce-b705-76ec06c5f72f", "metadata": {}, "outputs": [], "source": [ "emb_db.add(embs)" ] }, { "cell_type": "code", "execution_count": 9, "id": "117d3416-e79f-436f-a33e-ffb45b972b72", "metadata": {}, "outputs": [], "source": [ "q = \"the variance of the portfolio\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "3a6ef474-678c-4525-8dcb-ece67aa9c7ea", "metadata": {}, "outputs": [], "source": [ "qe = encoder.query(q)" ] }, { "cell_type": "code", "execution_count": 11, "id": "4c8a16ba-6025-4a6e-95c2-bbba7a9a5de5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[122.61588, 127.4572 , 128.07301, 128.97739, 131.64783]],\n", " dtype=float32),\n", " array([[149, 47, 224, 255, 254]]))" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s, i = emb_db.search(qe, 5)\n", "s,i" ] }, { "cell_type": "code", "execution_count": 12, "id": "1c90dd20-c640-48b5-88c0-4ba93b60c5e6", "metadata": {}, "outputs": [], "source": [ "docs_db = Documents()" ] }, { "cell_type": "code", "execution_count": 14, "id": "ed69d8bf-93f1-4353-a4c2-c4aacbe25420", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "docs_db.add_document(chunks)" ] }, { "cell_type": "code", "execution_count": 16, "id": "40ebc825-2e2c-4110-93ff-ae6ec3dc1322", "metadata": {}, "outputs": [], "source": [ "docs_db.add_chunk(chunks[100].page_content)" ] }, { "cell_type": "code", "execution_count": null, "id": "4352b32d-c2e7-4fbf-aa05-fc46baf7c9f8", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }