# raahulsaxena/rag-based-patent-analysis-tool architecture

> Architecture diagram of the GitHub repository raahulsaxena/rag-based-patent-analysis-tool, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/raahulsaxena/rag-based-patent-analysis-tool
- Repository: https://github.com/raahulsaxena/rag-based-patent-analysis-tool

## Overview

1. **What this repo is centered on**
   This repository is a **patent retrieval / analysis pipeline** built around a RAG-style workflow, but the checked-in tree is mostly the **retrieval side**: patent text corpus, precomputed search artifacts, and evaluation utilities.

2. **Raw document corpus**
   The `documents/` directory is the source content layer. It contains patent-like text files such as `karafin.txt`, `lucente.txt`, `williams.txt`, plus matching `*_meta.txt` files that likely hold metadata or labels for each document. `office_action.txt` appears to be a separate document type in the same corpus.

3. **Derived retrieval artifacts**
   `chunks_array.npz` and `queries_array.npz` are offline-generated NumPy archives that look like the main indexing inputs for retrieval. These are the boundary between preprocessing and runtime: the system likely avoids re-reading and re-chunking raw text on every query.

4. **Query definitions and test fixtures**
   `queries.json` provides example or benchmark queries, while `sample_input.py` likely demonstrates the expected runtime input format. These are the external interface for the retrieval pipeline and the evaluation harness.

5. **Main runtime entry point**
   `retriever.py` is the primary application module named in the repo. Based on the tree and README, it is the component that takes a query, loads the precomputed arrays, ranks document chunks, and returns the most relevant patent content.

6. **Similarity and ranking support**
   `jaccard.py` is a dedicated similarity module. It likely provides lexical overlap scoring used either as the retrieval metric itself or as a post-retrieval comparison step for patent-to-patent matching.

7. **Evaluation / validation path**
   `retriever-test.py` is the test harness for the retrieval pipeline. It probably exercises `retriever.py` against `queries.json` and the stored NumPy artifacts to verify ranking quality or regression behavior.

8. **End-to-end data flow**
   The likely flow is: **raw patent documents → chunking / preprocessing → saved NumPy arrays → query input → retrieval scoring → ranked chunk/document results**. The comparison path likely branches through `jaccard.py` for pairwise similarity between documents or chunks.

9. **Architecture boundaries**
   There is a clear separation between:
   - **source data**: `documents/`
   - **derived index/cache data**: `*.npz`
   - **query/test data**: `queries.json`, `sample_input.py`
   - **runtime logic**: `retriever.py`, `jaccard.py`
   - **validation**: `retriever-test.py`

10. **RAG-related note**
   The README describes summarization, comparison, and classification features, but those implementation files are not present in this tree. So, for an architecture diagram of this snapshot, the safest interpretation is that the repo currently exposes a **retrieval-focused core** that would feed a downstream generator or analysis layer, rather than a complete end-to-end RAG app in the checked-in files.

11. **Technology footprint**
   The code is Python-based and the stored arrays imply heavy use of **NumPy** for persistence and fast loading. The README mentions `transformers`, `torch`, and `scikit-learn`, but those are not visible in the file tree; they may support a richer version of the pipeline, though the present repository layout is lightweight and offline-index driven.

12. **What to draw in an architecture diagram**
   A good diagram would show a **document corpus service/input**, an **offline preprocessing/indexing stage**, a **query-time retriever**, a **similarity/scoring utility**, and a **test/evaluation lane**. The key architectural concept is that most expensive work appears to be done ahead of time and stored in `.npz` artifacts, leaving the runtime path focused on fast lookup and ranking.

Main entry points: [`retriever.py`](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/retriever.py) (Retriever), [`chunks_array.npz`](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/chunks_array.npz) (Chunks index), [`documents`](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/tree/main/documents) (Documents), [`retriever-test.py`](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/retriever-test.py) (Retriever test)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_source["Source corpus"]
  node_documents[("Documents<br/>corpus")]
  node_karafin_doc["Karafin<br/>patent text<br/>[karafin.txt]"]
  node_lucente_doc["Lucente<br/>patent text<br/>[lucente.txt]"]
  node_williams_doc["Williams<br/>patent text<br/>[williams.txt]"]
  node_office_action_doc["Office action<br/>legal text<br/>[office_action.txt]"]
end

subgraph group_derived["Derived index"]
  node_chunks_array[("Chunks index<br/>npz archive<br/>[chunks_array.npz]")]
  node_queries_array[("Queries index<br/>npz archive<br/>[queries_array.npz]")]
end

subgraph group_runtime["Runtime and evaluation"]
  node_queries_json["Queries<br/>benchmark set<br/>[queries.json]"]
  node_sample_input["Sample input<br/>interface example<br/>[sample_input.py]"]
  node_retriever["Retriever<br/>ranking engine<br/>[retriever.py]"]
  node_jaccard["Jaccard<br/>similarity scorer<br/>[jaccard.py]"]
  node_retriever_test["Retriever test<br/>evaluation harness<br/>[retriever-test.py]"]
end

node_documents -->|"contains"| node_karafin_doc
node_documents -->|"contains"| node_lucente_doc
node_documents -->|"contains"| node_williams_doc
node_documents -->|"contains"| node_office_action_doc
node_karafin_doc -->|"chunked into"| node_chunks_array
node_lucente_doc -->|"chunked into"| node_chunks_array
node_williams_doc -->|"chunked into"| node_chunks_array
node_office_action_doc -->|"chunked into"| node_chunks_array
node_chunks_array -->|"loads"| node_retriever
node_queries_array -->|"loads"| node_retriever
node_queries_json -->|"drives"| node_retriever
node_sample_input -->|"formats input"| node_retriever
node_retriever -->|"scores with"| node_jaccard
node_jaccard -->|"verified by"| node_retriever_test
node_retriever -->|"tested by"| node_retriever_test
node_queries_json -->|"fixtures"| node_retriever_test

click node_documents "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/tree/main/documents"
click node_karafin_doc "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/documents/karafin.txt"
click node_lucente_doc "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/documents/lucente.txt"
click node_williams_doc "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/documents/williams.txt"
click node_office_action_doc "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/documents/office_action.txt"
click node_chunks_array "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/chunks_array.npz"
click node_queries_array "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/queries_array.npz"
click node_queries_json "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/queries.json"
click node_sample_input "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/sample_input.py"
click node_retriever "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/retriever.py"
click node_jaccard "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/jaccard.py"
click node_retriever_test "https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/retriever-test.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_documents,node_karafin_doc,node_lucente_doc,node_williams_doc,node_office_action_doc toneBlue
class node_chunks_array,node_queries_array toneAmber
class node_queries_json,node_sample_input,node_retriever,node_jaccard,node_retriever_test toneMint
```

## Components

### Source corpus

Raw patent and related text inputs.

- [Documents](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/tree/main/documents): corpus · Raw patent text and metadata files. · `documents`
- [Karafin](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/documents/karafin.txt): patent text · Representative patent document. · `documents/karafin.txt`
- [Lucente](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/documents/lucente.txt): patent text · Representative patent document. · `documents/lucente.txt`
- [Williams](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/documents/williams.txt): patent text · Representative patent document. · `documents/williams.txt`
- [Office action](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/documents/office_action.txt): legal text · Additional document type in the corpus. · `documents/office_action.txt`

### Derived index

Offline-generated retrieval artifacts.

- [Chunks index](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/chunks_array.npz): npz archive · Precomputed chunk representations for retrieval. · `chunks_array.npz`
- [Queries index](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/queries_array.npz): npz archive · Precomputed query-related representations. · `queries_array.npz`

### Runtime and evaluation

Query-time retrieval, scoring, and validation.

- [Queries](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/queries.json): benchmark set · Example or evaluation queries. · `queries.json`
- [Sample input](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/sample_input.py): interface example · Expected runtime input format. · `sample_input.py`
- [Retriever](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/retriever.py): ranking engine · Loads cached arrays and ranks relevant chunks. · `retriever.py`
- [Jaccard](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/jaccard.py): similarity scorer · Lexical overlap scoring for comparison or ranking. · `jaccard.py`
- [Retriever test](https://github.com/raahulsaxena/rag-based-patent-analysis-tool/blob/main/retriever-test.py): evaluation harness · Regression and quality checks for retrieval behavior. · `retriever-test.py`

## Connections

- Documents → Karafin: contains · Corpus includes patent text files.
- Documents → Lucente: contains · Corpus includes patent text files.
- Documents → Williams: contains · Corpus includes patent text files.
- Documents → Office action: contains · Corpus includes additional legal text.
- Karafin → Chunks index: chunked into · Raw text is transformed into retrieval chunks.
- Lucente → Chunks index: chunked into · Raw text is transformed into retrieval chunks.
- Williams → Chunks index: chunked into · Raw text is transformed into retrieval chunks.
- Office action → Chunks index: chunked into · Raw text is transformed into retrieval chunks.
- Chunks index → Retriever: loads · Retriever reads cached chunk representations.
- Queries index → Retriever: loads · Retriever may use cached query representations.
- Queries → Retriever: drives · Benchmark queries are passed into retrieval.
- Sample input → Retriever: formats input · Example input structure for runtime use.
- Retriever → Jaccard: scores with · Retriever can use lexical similarity for ranking.
- Jaccard → Retriever test: verified by · Similarity behavior is exercised through tests.
- Retriever → Retriever test: tested by · Retrieval output is validated by the test harness.
- Queries → Retriever test: fixtures · Tests likely use benchmark queries as input.
