# jannctu/hybrid-search-rag architecture

> Architecture diagram of the GitHub repository jannctu/hybrid-search-rag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jannctu/hybrid-search-rag
- Repository: https://github.com/jannctu/hybrid-search-rag

## Overview

This is an experimental trading-knowledge RAG chatbot. The user asks through a Gradio chat UI; the handler retrieves keyword and semantic matches from Elasticsearch, fuses and reranks them, builds a cited-answer prompt, generates a response, and logs the interaction. A separate ingestion script reads Markdown, chunks and embeds it, then indexes the chunks. The README also documents configurable retrieval and citation behavior. The source excerpts show the main flow; some internal details are omitted, so unsampled relationships are not inferred.

Main entry points: [`poc.py`](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py) (Chat Handler), [`ingestion.py`](https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.py) (Markdown Reader), [`knowledges`](https://github.com/jannctu/hybrid-search-rag/tree/main/knowledges) (Knowledge Markdown)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_chat["Chat and Answering"]
  node_gradio["Chat UI<br/>[poc.py]"]
  node_handler["Chat Handler<br/>[poc.py]"]
  node_prompt["Prompt Builder<br/>[poc.py]"]
  node_answer["Answer Generator<br/>[poc.py]"]
  node_citations["References and Logs<br/>[poc.py]"]
end

subgraph group_retrieval["Hybrid Retrieval"]
  node_bm25["BM25 Search<br/>[poc.py]"]
  node_query_embedding["Query Embedding<br/>[poc.py]"]
  node_vector["Vector Search<br/>[poc.py]"]
  node_fusion["Rank Fusion<br/>[poc.py]"]
  node_reranker["LLM Reranker<br/>[poc.py]"]
  node_elasticsearch[("Elasticsearch Index")]
end

subgraph group_ingestion["Knowledge Ingestion"]
  node_markdown["Knowledge Markdown"]
  node_reader["Markdown Reader<br/>[ingestion.py]"]
  node_chunker["Token Chunker<br/>[ingestion.py]"]
  node_doc_embedding["Document Embeddings<br/>[ingestion.py]"]
  node_index_writer["Index Setup and Writer<br/>[ingestion.py]"]
end

node_user(("Questioner"))
node_openai{{"OpenAI API"}}

node_user -->|"asks"| node_gradio
node_gradio -->|"dispatches"| node_handler
node_handler -->|"searches"| node_bm25
node_handler -->|"searches"| node_vector
node_vector -->|"embeds query"| node_query_embedding
node_query_embedding -->|"requests embedding"| node_openai
node_bm25 -->|"queries"| node_elasticsearch
node_vector -->|"queries"| node_elasticsearch
node_handler -->|"fuses hits"| node_fusion
node_handler -->|"reranks hits"| node_reranker
node_reranker -->|"scores relevance"| node_openai
node_handler -->|"builds context"| node_prompt
node_handler -->|"requests answer"| node_answer
node_answer -->|"generates response"| node_openai
node_handler -->|"logs and formats"| node_citations
node_markdown -->|"reads files"| node_reader
node_reader -->|"chunks text"| node_chunker
node_chunker -->|"embeds chunks"| node_doc_embedding
node_doc_embedding -->|"requests embedding"| node_openai
node_index_writer -->|"creates and indexes"| node_elasticsearch

click node_gradio "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_handler "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_prompt "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_answer "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_citations "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_bm25 "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_query_embedding "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_vector "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_fusion "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_reranker "https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py"
click node_markdown "https://github.com/jannctu/hybrid-search-rag/tree/main/knowledges"
click node_reader "https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.py"
click node_chunker "https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.py"
click node_doc_embedding "https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.py"
click node_index_writer "https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.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_gradio,node_handler,node_prompt,node_answer,node_citations toneBlue
class node_bm25,node_query_embedding,node_vector,node_fusion,node_reranker,node_elasticsearch toneAmber
class node_markdown,node_reader,node_chunker,node_doc_embedding,node_index_writer,node_openai toneMint
class node_user toneIndigo
```

## Components

### Chat and Answering

- [Chat UI](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- [Chat Handler](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- [Prompt Builder](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- [Answer Generator](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- [References and Logs](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`

### Hybrid Retrieval

- [BM25 Search](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- [Query Embedding](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- [Vector Search](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- [Rank Fusion](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- [LLM Reranker](https://github.com/jannctu/hybrid-search-rag/blob/main/poc.py): `poc.py`
- **Elasticsearch Index**

### Knowledge Ingestion

- [Knowledge Markdown](https://github.com/jannctu/hybrid-search-rag/tree/main/knowledges): `knowledges`
- [Markdown Reader](https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.py): `ingestion.py`
- [Token Chunker](https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.py): `ingestion.py`
- [Document Embeddings](https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.py): `ingestion.py`
- [Index Setup and Writer](https://github.com/jannctu/hybrid-search-rag/blob/main/ingestion.py): `ingestion.py`

### Other components

- **Questioner**
- **OpenAI API**

## Connections

- Questioner → Chat UI: asks (evidence: `README.md`)
- Chat UI → Chat Handler: dispatches (evidence: `poc.py`)
- Chat Handler → BM25 Search: searches (evidence: `poc.py`)
- Chat Handler → Vector Search: searches (evidence: `poc.py`)
- Vector Search → Query Embedding: embeds query (evidence: `poc.py`)
- Query Embedding → OpenAI API: requests embedding (evidence: `poc.py`)
- BM25 Search → Elasticsearch Index: queries (evidence: `poc.py`)
- Vector Search → Elasticsearch Index: queries (evidence: `poc.py`)
- Chat Handler → Rank Fusion: fuses hits (evidence: `poc.py`)
- Chat Handler → LLM Reranker: reranks hits (evidence: `poc.py`)
- LLM Reranker → OpenAI API: scores relevance (evidence: `poc.py`)
- Chat Handler → Prompt Builder: builds context (evidence: `poc.py`)
- Chat Handler → Answer Generator: requests answer (evidence: `poc.py`)
- Answer Generator → OpenAI API: generates response (evidence: `poc.py`)
- Chat Handler → References and Logs: logs and formats (evidence: `poc.py`)
- Knowledge Markdown → Markdown Reader: reads files (evidence: `README.md`)
- Markdown Reader → Token Chunker: chunks text (evidence: `ingestion.py`)
- Token Chunker → Document Embeddings: embeds chunks (evidence: `ingestion.py`)
- Document Embeddings → OpenAI API: requests embedding (evidence: `ingestion.py`)
- Index Setup and Writer → Elasticsearch Index: creates and indexes (evidence: `ingestion.py`)
