<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aleksei Mikhalev on Medium]]></title>
        <description><![CDATA[Stories by Aleksei Mikhalev on Medium]]></description>
        <link>https://medium.com/@mikhalev.aleksei1?source=rss-7dd83f542958------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2E8Lm1N-hU9qLABPviw2Lg.png</url>
            <title>Stories by Aleksei Mikhalev on Medium</title>
            <link>https://medium.com/@mikhalev.aleksei1?source=rss-7dd83f542958------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:14:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mikhalev.aleksei1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Local AI Library with Graph RAG and LLM: PDF Processing and Text Extraction]]></title>
            <link>https://medium.com/@mikhalev.aleksei1/building-a-local-ai-library-with-graph-rag-and-llm-pdf-processing-and-text-extraction-180f4e85f478?source=rss-7dd83f542958------2</link>
            <guid isPermaLink="false">https://medium.com/p/180f4e85f478</guid>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[neo4j]]></category>
            <category><![CDATA[graphrag]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Aleksei Mikhalev]]></dc:creator>
            <pubDate>Tue, 18 Mar 2025 01:10:28 GMT</pubDate>
            <atom:updated>2025-03-18T01:10:28.971Z</atom:updated>
            <content:encoded><![CDATA[<p>Welcome back to my little workshop of digital marvels. I’m continuing my series on building a local AI library that solves my personal pain point (and likely yours too, unless you enjoy drowning in unstructured information). Think of this as the intellectual equivalent of a custom-built supercar — powerful, elegant, and turns heads at academic conferences.</p><p>If you missed the introduction, check out:</p><p><a href="https://medium.com/@mikhalev.aleksei1/building-a-local-ai-library-with-graph-rag-and-llm-a-series-introduction-2e3c9267e842">Building a Local AI Library with Graph RAG and LLM: A Series Introduction</a></p><p>Don’t worry, I’ll wait… back? Great.</p><p>In this installment, I’ll walk you through how I engineered a PDF book processing and feature extraction service.</p><h3>Engineering the Project Structure: Because Chaos Is So Last Season</h3><p>First, we need architecture that doesn’t make future-you want to travel back in time to punch present-you in the face. How do we structure this beast to ensure it’s maintainable, extensible, and doesn’t become digital spaghetti?</p><p>After weighing the options (for approximately 37 seconds), I settled on Next.js for frontend and FastAPI for the backend — two powerhouse frameworks that deliver exceptional performance with minimal fuss. And…well, I’ve already worked with them recently. I’ve set up the repository with the backend as the main repo and frontend nested within, giving me the full picture within my IDE while allowing for seamless context switching.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*gTmUHJ3JSacvVZEVt9ORmg.png" /><figcaption>project repo root structure</figcaption></figure><p>I won’t belabor every atomic development step. That would be like explaining how I brew my morning coffee — technically fascinating but ultimately unnecessary. If there’s enough interest shown through reactions, I promise you, I’ll supplement with detailed step-by-step videos decomposed development into atoms.</p><h3>Backend (API) Architecture: Clean, Mean, and Ready to Process</h3><p>My backend follows a pragmatic, clean architecture that allow to find any component easily:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/0*_sRPg9NCHbZqNt3i" /><figcaption>api folders structure</figcaption></figure><p>Nothing fancy, just as simple as that. Quick overview:</p><pre>ai-library/<br>├── api/                  # API-related code and configurations<br>│   ├── .dockerignore     # Files to ignore in Docker builds<br>│   ├── .env              # Environment variables for development<br>│   ├── Dockerfile        # Instructions for building the Docker image<br>│   ├── Makefile          # Commands for building and managing the project<br>│   ├── docker-compose.dev.yml # Configuration for Docker Compose in development<br>│   ├── example.env       # Example environment variables<br>│   ├── pyproject.toml    # Project metadata and dependencies for Python<br>│   ├── pytest.ini        # Configuration for pytest<br>│   ├── src/              # Source code for the API<br>│   │   ├── __init__.py   # Marks the directory as a Python package<br>│   │   ├── config/       # Configuration files for the application<br>│   │   ├── database/     # Database initializers and migrations<br>│   │   ├── docs/         # Documentation files<br>│   │   ├── main.py       # Main entry point for the API<br>│   │   ├── models/       # Data models for the application<br>│   │   ├── pdf_uploads/  # Temp directory for uploaded PDF files<br>│   │   ├── repository/   # Data access layer for database interactions<br>│   │   ├── routers/      # API route definitions<br>│   │   ├── schemas/      # Data validation schemas<br>│   │   ├── services/     # Business logic<br>│   │   ├── tasks.py      # Background tasks<br>│   │   ├── utils/        # Utility functions and helpers<br>│   │   ├── worker.py     # Worker for background processing<br>│   ├── tests/            # Test cases for the API<br>│</pre><p>Pro tip: Rather than manually creating this structure like some digital caveman, just feed this description to your favorite LLM and request a bash script to generate it instantaneously. Better yet, if you’re using Windsurf or Cursor IDEs, they can generate it directly (now it smells vibe-coding).</p><h3>The Grand Design: PDF Processing Pipeline (Or How I Learned to Stop Worrying and Love Document Parsing)</h3><p>Let’s clarify our objectives. When a user uploads a PDF book, our backend needs to perform three critical tasks — consider this the algorithmic equivalent of turning lead into gold:</p><ol><li><strong>Read and parse the PDF</strong> from its source location, preserving its hierarchical organization. This isn’t just parsing; it’s digital archaeology — carefully extracting and structuring the knowledge buried within those digital pages, while tracking its document lineage through a unique ID.</li><li><strong>Extract concepts</strong> from each section — think of this as building a conceptual map of the book’s territory. This step requires Ollama and instruct LLM for efficient data extraction.</li><li><strong>Generate embeddings</strong> for these conceptually-enriched sections — creating multi-dimensional numerical representations that capture semantic meaning in a way machines can understand. These vectors are our compass for navigating the book’s knowledge landscape later.</li><li><strong>Store everything in Neo4j</strong>, our graph database of choice. Here, the book’s knowledge isn’t just stored — it’s connected. Each concept becomes a node in a vast web of meaning, with relationships that mirror how ideas connect in our own minds.</li></ol><p>Let’s examine each component of this processing pipeline, representing the core business logic domain that makes the magic happen:</p><pre># schemas/upload.py<br>...<br>class SectionData(BaseModel):<br>    section_name: str<br>    section_paragraphs_data: list[SectionParagraphData]<br>    section_text: str<br>    section_concepts_embedding: list[float] = None<br>    concepts: list[str] = []<br><br><br>class SectionsFeatures(BaseModel):<br>    sections_features: list[SectionData]<br><br><br>class ProcessedBook(BaseModel):<br>    document_id: str = Field(...)<br>    title: str = &quot;Untitled&quot;<br>    author: str = &quot;Unknown&quot;<br>    pages: int = 0<br>    published_date: datetime = None<br>    added_date: datetime = datetime.now()<br>    cover_image: str = &quot;&quot;<br>    description: str = &quot;&quot;<br>    concepts: list[str] = []<br>    sections: list[SectionData]<br>    status: str = &quot;&quot;<br><br><br><br><br># /services/pdf_processing.py<br>...<br>async def process_pdf(self, pdf_url: str, document_id: str) -&gt; ProcessedBook:<br>        try:<br>            # Save processing status<br>            await self.processing_repository.save_pdf_processing_metadata(<br>                ProcessedBookMongoDB(document_id=document_id, status=&quot;PROCESSING&quot;)<br>            )<br>            start_time = time.time()<br><br>            # Read PDF<br>            logging.info(f&quot;Step 1/3: Reading and parsing PDF: {pdf_url}&quot;)<br>            processed_document = await self._read_pdf(pdf_url, document_id)<br>            sections_features = processed_document.sections<br><br>            # Clear GPU memory before LLM processing<br>            self._manage_gpu_memory(force=True)<br><br>            # Extract concepts<br>            logging.info(&quot;Step 2/3: Extracting concepts&quot;)<br>            sections_with_concepts = await self._extract_concepts(sections_features)<br>            processed_document.sections = sections_with_concepts<br>            flatten_concepts_per_book = [<br>                item for sublist in sections_with_concepts for item in sublist.concepts<br>            ]<br>            processed_document.concepts = list(set(flatten_concepts_per_book))<br><br>            logging.info(&quot;Concepts extracted&quot;)<br><br>            # Get embeddings<br>            logging.info(&quot;Step 3/3: Getting embeddings for sections concepts&quot;)<br>            sections_with_concepts_with_embeddings = await self._get_embeddings(<br>                processed_document.sections<br>            )<br>            processed_document.sections = sections_with_concepts_with_embeddings<br><br>            # Store into Neo4j<br>            logging.info(&quot;Step 4/4: Storing extracted features into Neo4j&quot;)<br>            await self.processing_repository.store_features_in_neo4j(processed_document)<br><br>            end_time = time.time()<br><br>            logging.info(<br>                f&quot;Time spent on features extraction: {end_time - start_time:.2f} seconds&quot;<br>            )<br><br>            # Clear GPU memory<br>            self._manage_gpu_memory(force=True)<br><br>            # Update processing status<br>            updated_document = (<br>                await self.processing_repository.update_pdf_processing_metadata(<br>                    ProcessedBookMongoDB(<br>                        document_id=document_id,<br>                        status=&quot;COMPLETED&quot;,<br>                    )<br>                )<br>            )<br><br>            # Remove temp PDF file after processing<br>            file_path = Path(pdf_url)<br>            if file_path.exists():<br>                file_path.unlink()<br><br>            return ProcessedBookMongoDB(**updated_document)<br>        except Exception as e:<br>            logging.error(f&quot;Processing failed: {e}&quot;)<br>            updated_document = (<br>                await self.processing_repository.update_pdf_processing_metadata(<br>                    ProcessedBookMongoDB(document_id=document_id, status=&quot;FAILED&quot;)<br>                )<br>            )<br>            return ProcessedBookMongoDB(**updated_document)</pre><p>The entire backend infrastructure is containerized using Docker Compose, with Redis and Celery added to the mix. Why? I’ll detail their crucial roles in future installments:</p><pre>services:<br>  api:<br>    build: .<br>    container_name: api<br>    image: api:latest<br>    restart: on-failure:0<br>    ports:<br>      - &quot;8000:8000&quot;<br>    develop:<br>      watch:<br>        - action: sync<br>          path: .<br>          target: /app<br>          ignore:<br>            - .venv/<br>            - venv/<br>        - action: rebuild<br>          path: ./pyproject.toml<br>    depends_on:<br>      - neo4j<br>      - nlm-ingestor<br>      - mongo_db<br>      - ollama<br>    env_file:<br>      - .env<br>    volumes:<br>      - .:/app<br>    networks:<br>      - ai-library-network<br><br>  worker:<br>    build: .<br>    image: api:latest<br>    container_name: celery_worker<br>    command: [&quot;celery&quot;, &quot;-A&quot;, &quot;src.worker.celery_app&quot;, &quot;worker&quot;, &quot;--loglevel=info&quot;]<br>    restart: on-failure:0<br>    depends_on:<br>      redis:<br>        condition: service_healthy<br>      mongo_db:<br>        condition: service_healthy<br>    env_file:<br>      - .env<br>    environment:<br>      - broker_connection_retry_on_startup=${BROKER_CONNECTION_RETRY_ON_STARTUP}<br>    volumes:<br>      - .:/app<br>    networks:<br>      - ai-library-network<br><br>  redis:<br>    image: redis/redis-stack-server:latest<br>    container_name: redis<br>    restart: on-failure:0<br>    ports:<br>      - &quot;6379:6379&quot;<br>    networks:<br>      - ai-library-network<br>    healthcheck:<br>      test: [&quot;CMD&quot;, &quot;redis-cli&quot;, &quot;ping&quot;]<br>      interval: 5s<br>      retries: 3<br>  <br>  nlm-ingestor:<br>    image: ghcr.io/nlmatics/nlm-ingestor:latest<br>    container_name: nlm-ingestor<br>    ports:<br>      - &quot;5001:5001&quot;<br>    restart: on-failure:0<br>    networks:<br>      - ai-library-network<br>    depends_on:<br>      - ollama<br>    deploy:<br>      resources:<br>        reservations:<br>          devices:<br>            - driver: nvidia<br>              count: all<br>              capabilities: [gpu]<br><br>  ollama:<br>    image: ollama/ollama:latest<br>    container_name: ollama<br>    privileged: true<br>    ports:<br>      - &quot;7689:11434&quot;<br>    healthcheck:<br>      test: ollama --version || exit 1<br>    deploy:<br>      resources:<br>        reservations:<br>          devices:<br>            - driver: nvidia<br>              count: all<br>              capabilities: [gpu]<br>    restart: on-failure:0<br>    volumes:<br>      - ollama:/root/.ollama<br>      - ./ollama_entrypoint.sh:/ollama_entrypoint.sh<br>    entrypoint: [&quot;/usr/bin/bash&quot;, &quot;/ollama_entrypoint.sh&quot;]<br>    networks:<br>      - ai-library-network<br><br>  mongo_db:<br>    image: mongo<br>    container_name: mongodb<br>    command: &gt;<br>      --quiet<br>      --logpath /dev/stdout<br>      --logappend<br>      --setParameter logComponentVerbosity={network:1}<br>    restart: on-failure:0<br>    ports:<br>      - 27017:27017<br>    environment:<br>      - OLLAMA_HOST=${OLLAMA_HOST}<br>    volumes:<br>      - mongodb_data:/data/db<br>    networks:<br>      - ai-library-network<br>    healthcheck:<br>      test: [&quot;CMD&quot;, &quot;mongosh&quot;, &quot;--eval&quot;, &quot;db.runCommand({ping: 1})&quot;]<br>      interval: 10s<br>      timeout: 5s<br>      retries: 3<br><br>  neo4j:<br>    image: neo4j:5.25.1-community<br>    container_name: neo4j<br>    ports:<br>      - 7474:7474<br>      - 7687:7687<br>    volumes:<br>      - neo4j_data:/data<br>      - neo4j_import:/var/lib/neo4j/import/<br>    networks:<br>      - ai-library-network<br>    restart: on-failure:0<br>    environment:<br>      - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes<br>      - NEO4J_AUTH=${NEO4J_USER}/${NEO4J_PASSWORD}<br>      - NEO4J_apoc_export_file_enabled=true<br>      - NEO4J_apoc_import_file_enabled=true<br>      - NEO4J_apoc_import_file_use__neo4j=true<br>      - NEO4J_dbms_security_allow__csv__import__from__file__urls=true<br>      - NEO4J_PLUGINS=[&quot;apoc&quot;]<br>      - NEO4J_server_memory_pagecache_size=2G<br>      - NEO4J_server_memory_heap_max__size=2G<br>      - NEO4J_dbms_memory_transaction_total_max=6G<br>      - NEO4J_dbms_security_auth__enabled=true<br>      - NEO4J_dbms_routing_driver_logging_level=DEBUG<br>      - NEO4J_db_logs_query_enabled=VERBOSE<br>      - NEO4J_db_logs_query_parameter__logging__enabled=true<br>      - NEO4J_db_logs_query_transaction_enabled=VERBOSE<br>      - NEO4J_dbms_logs_http_enabled=true<br>      - NEO4J_server_logs_debug_enabled=true<br>      - NEO4J_server_threads_worker__count=8<br><br>volumes:<br>  ollama:<br>  mongodb_data:<br>  neo4j_data:<br>  neo4j_import:<br>    driver: local<br><br>networks:<br>  ai-library-network:<br>    driver: bridge</pre><p><strong>Step 1/3: Reading and parsing PDF</strong></p><p>For PDF extraction, I leveraged LLM Sherpa, a library specifically engineered for RAG applications. It’s like having a Sherpa guide you through the treacherous mountain of PDF formatting — hence the name, I assume. Explore this brilliant tool at <a href="https://github.com/nlmatics/llmsherpa"><em>LLM Sherpa</em></a><em> and </em><a href="https://llmsherpa.readthedocs.io/en/latest/"><em>LLM Sherpa API</em></a></p><p>To demonstrate, let’s process Giunio de Luca’s excellent <a href="https://www.amazon.com/FastAPI-Cookbook-Develop-high-performance-applications/dp/1805127853">FastAPI Cookbook</a>.</p><p>_read_pdf<em> </em>method returns parsed sections with paragraphs and their metadata:</p><pre>[<br>    {<br>        &quot;section_name&quot;: &quot;FastAPI Cookbook&quot;,<br>        &quot;section_paragraphs_data&quot;: [<br>            {<br>                &quot;level&quot;: 1,<br>                &quot;text&quot;: &quot;FastAPI is a cutting-edge Python framework that is revolutionizing the way web apps and APIs are built.\nKnown for its speed, simplicity, and scalability, FastAPI empowers developers to create high-performing applications with ease.\nThis book will help you leverage FastAPI\u2019s immense potential to handle high-traffic scenarios and integrate seamlessly with modern Python tools.&quot;,<br>                &quot;page&quot;: 1,<br>                &quot;parent_text&quot;: &quot;FastAPI Cookbook&quot;,<br>                &quot;parent_chain&quot;: [<br>                    &quot;FastAPI Cookbook&quot;<br>                ]<br>            },<br>            {<br>                &quot;level&quot;: 1,<br>                &quot;text&quot;: &quot;The book begins by familiarizing you with the basics of setting up and configuring your FastAPI environment before moving on to the intricacies of building RESTful APIs, managing data with SQL and NoSQL databases, and handling authentication and authorization.\nAfter that you\u2019ll focus on advanced topics such as custom middleware, WebSocket communication, and integration with various Python libraries.\nEach chapter is meticulously crafted with practical recipes, progressing from foundational concepts to advanced features and best practices.\nThe concluding chapters show you how to optimize performance, implement rate limiting, and execute background tasks, empowering you to become a proficient FastAPI developer.&quot;,<br>                &quot;page&quot;: 1,<br>                &quot;parent_text&quot;: &quot;FastAPI Cookbook&quot;,<br>                &quot;parent_chain&quot;: [<br>                    &quot;FastAPI Cookbook&quot;<br>                ]<br>            },<br>            {<br>                &quot;level&quot;: 1,<br>                &quot;text&quot;: &quot;By the end of this book, you\u2019ll have gained the skills you need to migrate existing apps to FastAPI and be equipped to tackle any challenge in the modern web development landscape, ensuring your apps are not only functional but also efficient, secure, and scalable.&quot;,<br>                &quot;page&quot;: 1,<br>                &quot;parent_text&quot;: &quot;FastAPI Cookbook&quot;,<br>                &quot;parent_chain&quot;: [<br>                    &quot;FastAPI Cookbook&quot;<br>                ]<br>            },<br>            {<br>                &quot;level&quot;: 2,<br>                &quot;text&quot;: &quot;S\nT\nE\nD I T\nI\nO N&quot;,<br>                &quot;page&quot;: 1,<br>                &quot;parent_text&quot;: &quot;FastAPI Cookbook&quot;,<br>                &quot;parent_chain&quot;: [<br>                    &quot;FastAPI Cookbook&quot;<br>                ]<br>            }<br>        ],<br>        &quot;section_text&quot;: &quot;FastAPI Cookbook\nFastAPI is a cutting-edge Python framework that is revolutionizing the way web apps and APIs are built.\nKnown for its speed, simplicity, and scalability, FastAPI empowers developers to create high-performing applications with ease.\nThis book will help you leverage FastAPI\u2019s immense potential to handle high-traffic scenarios and integrate seamlessly with modern Python tools.\nThe book begins by familiarizing you with the basics of setting up and configuring your FastAPI environment before moving on to the intricacies of building RESTful APIs, managing data with SQL and NoSQL databases, and handling authentication and authorization.\nAfter that you\u2019ll focus on advanced topics such as custom middleware, WebSocket communication, and integration with various Python libraries.\nEach chapter is meticulously crafted with practical recipes, progressing from foundational concepts to advanced features and best practices.\nThe concluding chapters show you how to optimize performance, implement rate limiting, and execute background tasks, empowering you to become a proficient FastAPI developer.\nBy the end of this book, you\u2019ll have gained the skills you need to migrate existing apps to FastAPI and be equipped to tackle any challenge in the modern web development landscape, ensuring your apps are not only functional but also efficient, secure, and scalable.\nS\nT\nE\nD I T\nI\nO N\nwith Python&quot;,<br>        &quot;section_concepts_embedding&quot;: [0.0061342753,0.06352035,-0.16806442,-0.043760657,0.046046693,...],<br>        &quot;concepts&quot;: [&quot;API endpoints&quot;, &quot;other concept&quot;]<br>    }<br>...<br>]</pre><p>level — level of the block in the layout tree of the PDF document</p><p>text — text of the block</p><p>page — page number</p><p>parent_text — text of the parent chain of the block</p><p>parent_chain — parent chain of the block consisting of all the parents of the block until the root</p><p>section_name — name of document’s section</p><p>section_text — text of the whole document’s section. That includes text of all paragraphs included into the section</p><p>concepts —extracted knowledge concepts contained in the section. Knowledge concepts are extracted from each section’s paragraph and then combined together</p><p>section_concepts_embedding — embedding of joined into string list of concepts (I used 768-dimensional vector)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kx6ipv8SDpM90oxut1gQQA.png" /><figcaption>PDF file parsing process</figcaption></figure><p><strong>Step 2/3: Extracting concepts</strong></p><p>Next, we need to extract the core knowledge concepts from each section’s paragraphs, like a sommelier identifying notes in fine wine — except it’s ideas instead of tannins:</p><pre># services/pdf_processing.py<br>...<br>async def _extract_concepts_from_section(<br>        self, section_data: SectionData<br>    ) -&gt; SectionData:<br>        section_data = section_data.model_copy()<br>        text = section_data.section_text<br><br>        if not text:<br>            return section_data<br><br>        chunks = self._split_text_by_size(text, self.max_chunk_size)<br><br>        PROMPT = &quot;Extract main ideas described in the text below. The output should be a JSON `{&#39;concepts&#39;: [&#39;idea1&#39;, &#39;idea2&#39;]}`.&quot;<br><br>        # Function to process a single chunk<br>        async def process_chunk(chunk: str) -&gt; list[str]:<br>            try:<br>                response = await self.ollama_client.chat(<br>                    model=GENERATION_MODEL,<br>                    messages=[{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: f&quot;{PROMPT}\n\n{chunk}&quot;}],<br>                    format=ExtractedConcepts.model_json_schema(),<br>                    options={&quot;num_ctx&quot;: 8000},<br>                )<br>                extracted_result = ExtractedConcepts.model_validate_json(<br>                    response.message.content<br>                )<br>                return extracted_result.concepts<br>            except Exception as e:<br>                logging.error(f&quot;Error parsing concepts from chunk: {e}&quot;)<br>                return []<br><br>        # Run all chunk processing tasks concurrently<br>        results = await asyncio.gather(*(process_chunk(chunk) for chunk in chunks))<br>        if not results:<br>            return section_data<br><br>        flattened_results = [item for sublist in results for item in sublist]<br>        section_data.concepts = list(set(flattened_results))<br>        return section_data<br><br><br>async def _extract_concepts(<br>        self, sections_features: list[SectionData]<br>    ) -&gt; list[SectionData]:<br>        tasks = [<br>            self._extract_concepts_from_section(section)<br>            for section in sections_features<br>        ]<br><br>        # Progress of extracting concepts<br>        results = await tqdm_asyncio.gather(<br>            *tasks,<br>            desc=&quot;Extracting concepts&quot;,<br>            unit=&quot;sections&quot;,<br>            total=len(tasks),<br>            bar_format=&quot;{l_bar}{bar}| {n_fmt}/{total_fmt} sections&quot;,<br>            colour=&quot;cyan&quot;,<br>        )<br>        if not results:<br>            return sections_features<br>        return results</pre><p>I used <a href="https://ollama.com/library/llama3.2:3b">Llama3.2:3b</a> as GENERATION_MODEL and run it locally inside Ollama docker compose. Why not use a cloud vendors? Because I like my data where I can see it — in my own infrastructure. I typically establish my Docker infrastructure at project inception — this approach minimizes deployment surprises down the road. I hate surprises, unless they involve birthday cake or breakthrough discoveries.</p><p>On my local machine with modest GPU resources (4GB — yes, I know, practically stone age tech), concept extraction wasn’t exactly breaking the sound barrier:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VHqeAZZwVgzx0EAsqurelQ.png" /><figcaption>knowledge concepts extraction process</figcaption></figure><p>The complete extraction process took approximately 6.5 minutes on my GPU. Not stellar — like watching paint dry if the paint were made of algorithms — but entirely acceptable for now. Optimization is for version 2.0, after we prove the concept isn’t completely harebrained.</p><p><strong>Step 3/3: Getting embedding for sections concepts</strong></p><p>After successfully extracting knowledge concepts from all paragraphs, we concatenate them into a string and generate an embedding. Think of this as compressing the essence of knowledge into a mathematical fingerprint. These embeddings are crucial — they’re how we’ll match user questions to the precise knowledge chunks they need, faster than you can say “semantic search.”</p><p>For embeddings, I selected <a href="https://ollama.com/library/nomic-embed-text">“nomic-embed-text”</a> — a nimble model that produces 768-dimensional vectors with excellent quality. And yes, like our other components, this model runs locally within the Ollama Docker ecosystem. My current priority is minimizing reliance on vendor cloud solutions.</p><p><strong>Step 4/4: Storing extracted features into Neo4j</strong></p><p>In the final stage, we construct our knowledge graph and store it in Neo4j — think of it as building the neural pathways of our collective digital brain. Note that I also created indexes because it is very important for query performance. Without indexes, later on when we will have hundreds or thousands of books in our library, we may face a bottleneck in running complex (not only, but mostly complex multi-step) queries.</p><pre># /repository/pdf_processing.py<br>async def _neo4j_create_indexes(self, tx: AsyncManagedTransaction) -&gt; None:<br>        &quot;&quot;&quot;<br>        Create the necessary Neo4j indexes if they do not already exist.<br>        &quot;&quot;&quot;<br>        # Full-text index for Paragraph nodes on the &quot;text&quot; property<br>        await tx.run(<br>            &quot;&quot;&quot;<br>            CREATE FULLTEXT INDEX paragraphTextIndex IF NOT EXISTS<br>            FOR (n:Paragraph) ON EACH [n.text]<br>            OPTIONS {<br>                indexConfig: {<br>                    `fulltext.analyzer`: &#39;english&#39;,<br>                    `fulltext.eventually_consistent`: true<br>                }<br>            }<br>            &quot;&quot;&quot;<br>        )<br><br>        # Fulltext index for Concept nodes on the &quot;name&quot; property<br>        await tx.run(<br>            &quot;&quot;&quot;<br>            CREATE FULLTEXT INDEX conceptNameIndex IF NOT EXISTS<br>            FOR (n:Concept) ON EACH [n.name]<br>            OPTIONS {<br>                indexConfig: {<br>                    `fulltext.analyzer`: &#39;english&#39;,<br>                    `fulltext.eventually_consistent`: true<br>                }<br>            }<br>            &quot;&quot;&quot;<br>        )<br><br>        # Vector index for Section nodes on the &quot;section_concepts_embedding&quot; property<br>        await tx.run(<br>            &quot;&quot;&quot;<br>            CREATE VECTOR INDEX sectionEmbeddingIndex IF NOT EXISTS<br>            FOR (s:Section) ON (s.section_concepts_embedding)<br>            OPTIONS {<br>                indexConfig: {<br>                    `vector.dimensions`: 768,<br>                    `vector.similarity_function`: &#39;cosine&#39;<br>                }<br>            }<br>            &quot;&quot;&quot;<br>        )<br><br>        # Fulltext index for Section nodes on the &quot;section_text&quot; property<br>        await tx.run(<br>            &quot;&quot;&quot;<br>            CREATE FULLTEXT INDEX sectionTextIndex IF NOT EXISTS<br>            FOR (s:Section) ON EACH [s.section_text]<br>            OPTIONS {<br>                indexConfig: {<br>                    `fulltext.analyzer`: &#39;english&#39;,<br>                    `fulltext.eventually_consistent`: true<br>                }<br>            }<br>            &quot;&quot;&quot;<br>        )<br><br>    async def _neo4j_add_book_data(<br>        self, tx: AsyncManagedTransaction, processed_document: ProcessedBook<br>    ) -&gt; None:<br>        &quot;&quot;&quot;<br>        Merge the main Book node and related data (Sections, Paragraphs, Concepts, etc.) into Neo4j.<br>        &quot;&quot;&quot;<br>        await tx.run(<br>            &quot;&quot;&quot;<br>            MERGE (book:Book {name: $title, document_id: $document_id})<br>            SET book += $book_data<br>            <br>            WITH book<br>            CALL apoc.periodic.iterate(<br>            &quot;UNWIND $sections AS section RETURN section&quot;,<br>            &quot;<br>            MERGE (s:Section {name: section.section_name})<br>            SET s.section_text = section.section_text,<br>                s.section_concepts_embedding = section.section_concepts_embedding<br>            MERGE (book)-[:HAS_SECTION]-&gt;(s)<br><br>            WITH s, section<br>            UNWIND section.section_paragraphs_data AS paragraph<br>            CREATE (p:Paragraph)<br>            SET p = paragraph<br>            MERGE (s)-[:HAS_PARAGRAPH]-&gt;(p)<br><br>            WITH s, section<br>            UNWIND section.concepts AS section_concept<br>            MERGE (sc:Concept {name: section_concept})<br>            MERGE (s)-[:HAS_CONCEPT]-&gt;(sc)<br>            &quot;,<br>            {batchSize: 100, iterateList: true, params: {sections: $sections}, logProgress: true, logBatchProgress: true}<br>            ) YIELD batches, total, errorMessages<br><br>            WITH book<br>            UNWIND $book_concepts AS book_concept<br>            MERGE (bc:Concept {name: book_concept})<br>            MERGE (book)-[:HAS_CONCEPT]-&gt;(bc)<br>            &quot;&quot;&quot;,<br>            {<br>                &quot;document_id&quot;: processed_document.document_id,<br>                &quot;title&quot;: processed_document.title,<br>                &quot;book_data&quot;: processed_document.model_dump(<br>                    exclude={&quot;sections&quot;, &quot;status&quot;, &quot;id&quot;}<br>                ),<br>                &quot;sections&quot;: [<br>                    section.model_dump() for section in processed_document.sections<br>                ],<br>                &quot;book_concepts&quot;: processed_document.concepts,<br>            },<br>        )<br><br>    async def store_features_in_neo4j(self, processed_document: ProcessedBook) -&gt; None:<br>        &quot;&quot;&quot;<br>        High-level function that creates required indexes and stores extracted features into Neo4j.<br>        &quot;&quot;&quot;<br>        try:<br>            async with self.neo4j_async_driver.session() as session:<br>                # 1) Create indexes<br>                tx = await session.begin_transaction()<br>                await self._neo4j_create_indexes(tx)<br>                await tx.commit()<br><br>                # 2) Merge the graph data<br>                tx = await session.begin_transaction()<br>                await self._neo4j_add_book_data(tx, processed_document)<br>                await tx.commit()<br><br>            logging.info(<br>                f&quot;Stored book {processed_document.document_id} into Neo4j successfully.&quot;<br>            )<br><br>        except Exception as e:<br>            logging.error(<br>                f&quot;Error storing book {processed_document.document_id} into Neo4j: {e}&quot;<br>            )</pre><p>Observe my use of the CALL apoc.periodic.iterate procedure in Cypher code. This technique allows batch processing of data insertions:</p><pre>{batchSize: 100, iterateList: true, params: {sections: $sections}, logProgress: true, logBatchProgress: true}<br>            ) YIELD batches, total, errorMessages</pre><p>This batching strategy helps prevent memory overload during Neo4j transactions. It’s like knowing exactly how many party guests your venue can handle before things get uncomfortable.</p><p>After processing, our graph materializes with the following structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TI6PPMHvP25Loj6xIGhbgA.png" /><figcaption>Neo4j database schema visualization</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8IuESG-lvpxlQwSRCFzhrA.png" /></figure><p>These nodes represent the knowledge concepts extracted from our book. And if we expand the concept node we can see the whole useful connections:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jnAgiorzCQG7mkzwgxMqOg.png" /></figure><p>The whole processing pipeline from uploading PDF file till storing document features into graph database took 769.52 seconds:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*esvw2r3Ddvu1VR6Er0w1FA.png" /></figure><p>The entire pipeline — from PDF upload through feature extraction to graph database storage — completed in 769.52 seconds. While not performance-optimized yet, this accomplishes our functional requirements. Remember, sometimes you need a working prototype before you build the sleek production model.</p><p>That’s our PDF processing and text extraction service! In the next installment, I’ll tackle <strong>Semantic Search and RAG Implementation</strong>.</p><p>The complete source code for this series is available at <a href="https://github.com/AlekseiMikhalev/ai-library.git">GitHub</a>. Feel free to reach out with comments or suggestions — especially if you have ideas to make this system more elegant and efficient. I’m all ears… well, technically I’m all code, coffee, and questionable late-night engineering decisions, but you get the idea.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=180f4e85f478" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Local AI Library with Graph RAG and LLM: A Series Introduction]]></title>
            <link>https://medium.com/@mikhalev.aleksei1/building-a-local-ai-library-with-graph-rag-and-llm-a-series-introduction-2e3c9267e842?source=rss-7dd83f542958------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e3c9267e842</guid>
            <category><![CDATA[llm-applications]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[graphrag]]></category>
            <category><![CDATA[knowledge-management]]></category>
            <dc:creator><![CDATA[Aleksei Mikhalev]]></dc:creator>
            <pubDate>Mon, 10 Mar 2025 23:39:35 GMT</pubDate>
            <atom:updated>2025-03-18T12:20:45.261Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BCnvJbukCLj3nln4QmwT2Q.png" /></figure><h3>The Knowledge Management Challenge</h3><p>Every developer knows this frustration: “I remember reading about this concept somewhere…” Yet despite our carefully curated collection of technical resources, that critical insight remains buried in a forgotten PDF, its exact location a mystery.</p><p>This challenge intensifies when learning complex topics spanning multiple resources. Traditional note-taking often fails to capture the interconnected nature of technical knowledge, leaving us with isolated information fragments rather than a cohesive understanding.</p><h3>Transforming Static PDFs into an Interactive Knowledge Ecosystem</h3><p>This series will guide you through building an AI-powered library system that transforms static PDFs into a dynamic, queryable knowledge graph. Each post explores a specific module with detailed explanations and implementation code.</p><p>The project stems from my own struggle to efficiently manage technical resources while preserving their interconnections and context. It has evolved from personal necessity into a practical exploration of how modern AI techniques can reshape knowledge management.</p><h3>Series Roadmap</h3><p><strong>Part 1: System Architecture and Overview</strong><br> The complete system architecture, technology stack, and problem space (this post).</p><p><a href="https://medium.com/@mikhalev.aleksei1/building-a-local-ai-library-with-graph-rag-and-llm-pdf-processing-and-text-extraction-180f4e85f478"><strong>Part 2: PDF Processing and Text Extraction</strong></a><br> Building the ETL pipeline for PDF processing and reliable text extraction.</p><p><strong>Part 3: Building the Knowledge Graph</strong><br> Transforming text into a semantic knowledge graph using Neo4j, creating meaningful concept connections.</p><p><strong>Part 4: Semantic Search and RAG Implementation</strong><br> Implementing vector search and retrieval-augmented generation for contextually relevant answers.</p><p><strong>Part 5: Learning Features and Knowledge Synthesis</strong><br> Adding summarization, learning plan generation, and quiz creation features.</p><p><strong>Part 6: Building the User Interface</strong><br> Creating an intuitive interface for seamless interaction with the knowledge system.</p><h3>The Core Problem: Information Without Intelligence</h3><p>As technical professionals, we accumulate valuable resources that often become digital artifacts rather than accessible knowledge because:</p><ul><li><strong>Discovery Challenges</strong>: Finding specific information across multiple documents is prohibitively time-consuming</li><li><strong>Context Fragmentation</strong>: Connections between related concepts across different resources remain invisible</li><li><strong>Passive Storage</strong>: Static PDFs offer no interactive learning capabilities</li><li><strong>Retention Difficulties</strong>: Traditional review methods fail to reinforce complex knowledge structures</li></ul><h3>System Capabilities</h3><p>Our system transforms these challenges through:</p><ul><li><strong>PDF Ingestion</strong>: Processing uploaded technical documents</li><li><strong>Intelligent Extraction</strong>: Parsing PDFs into semantically meaningful chunks</li><li><strong>Knowledge Graph Construction</strong>: Building an interconnected representation of concepts</li><li><strong>Semantic Search</strong>: Finding relevant knowledge with links to original sources</li><li><strong>Interactive Learning</strong>: Generating summaries, learning paths, and knowledge assessments</li><li><strong>Privacy-Preserving Architecture</strong>: Running everything locally for complete data control</li></ul><h3>Technical Foundation</h3><p>The implementation combines:</p><ul><li><strong>PDF processing tools</strong> for text extraction (LLMSherpa, scikit-learn)</li><li><strong>Embedding models</strong> for semantic representation (“nomic-embed-text” open model)</li><li><strong>Neo4j </strong>for graph-based knowledge storage</li><li><strong>Local LLMs</strong> for natural language interaction (we will use Ollama to run local models in Docker containers)</li></ul><p>The result is a private knowledge ecosystem that understands how concepts connect across your entire technical library, transforming passive information into an interactive learning environment. I don’t know about you, but I’m tired of hoarding hundreds of amazing technical PDF books that just sit there, untouched — like treasure troves of knowledge I’ll never absorb unless I find a way to automate the process.</p><p>In the next part we will start implementing ETL pipeline for PDF processing!</p><p>Whether you’re a machine learning enthusiast looking to explore practical RAG applications or simply someone drowning in technical documentation, I hope you’ll join me as we build this project step by step.</p><p>Feel free to share your own document management challenges in the comments, and let me know what specific aspects of this project you’re most interested in exploring further.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e3c9267e842" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>