# haramrit09k/f1rstaid architecture

> Architecture diagram of the GitHub repository haramrit09k/f1rstaid, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/haramrit09k/f1rstaid
- Repository: https://github.com/haramrit09k/f1rstaid

## Overview

##### Project Type & Purpose
- The project is a full-stack application combining a Streamlit-based web interface with backend processes that ingest documents, update the knowledge base, crawl external sources, and perform smart vector search using FAISS and OpenAI embeddings.
- The README highlights features like intelligent Q&A, multi-source knowledge integration (from official docs and Reddit), and a vector-based search engine built on FAISS.

##### Analyzing the File Structure
- Top-Level Files & Folders:
- f1rstaid.py: Serves as the main application (likely the Streamlit app).
- ingest.py & update_knowledge.py: Scripts that process documents and update the knowledge base.
- crawler/ (contains crawler.py): Implements a web crawler to fetch data.
- config/ (with reddit_config.py and sources.py): Holds configuration for third-party APIs and data sources.
- faiss_index/ (with index files): Stores the precomputed FAISS index used for vector search.
- tests/ folder: Contains various test scripts for unit and integration testing.

##### Key Architectural Components
a. Frontend:
- Streamlit-based web interface launched via f1rstaid.py.
- User interaction occurs here, making queries and displaying answers.

b. Backend Services:
- Document Ingestion (ingest.py): Handles processing and embedding of documents.
- Knowledge Updater (update_knowledge.py): Maintains and refreshes the underlying knowledge base.
- Web Crawler (crawler/crawler.py): Fetches external data (for example, from Reddit or other sources) using the configurations provided in the config/ folder.

c. Data Storage:
- FAISS index stored under faiss_index/ which contains both serialized FAISS index and supporting pickle files.

d. Configuration:
- Various API keys and source configurations are set through environment variables combined with python files in the config/ folder.

e. External Services:
- OpenAI for embeddings generation.
- Reddit API for community and guideline sourcing.
- Possibly LangChain (as mentioned in the acknowledgments) for language model framework integration.

##### Relationships and Interactions
- The frontend (Streamlit app) accepts queries and triggers backend requests.
- Ingestion and knowledge update components work together to build and refresh the FAISS index, which is then queried by the search functionality.
- The web crawler periodically collects new content based on configurations, feeding into the ingestion process.
- All modules communicate via function calls or through shared data files (such as the FAISS index), with clear data flow from external data acquisition → ingestion/embedding → FAISS index update → query handling in the Streamlit frontend.

##### Architectural Patterns & Design Principles
- Modular design: Distinct scripts separate concerns (e.g., ingestion, updating, crawling, configuration).
- Data processing pipeline: Clear stages from document ingestion, vector embedding, indexing, to query processing.
- Decoupling of configuration: API keys and source URLs are managed via separate config files and environment variables.
- Testing: A robust tests directory indicates a test-driven approach.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Frontend Layer"
        FE["Frontend (Streamlit UI)"]:::frontend
    end

    subgraph "Backend Services"
        ING["Ingestion Service"]:::backend
        KBU["Knowledge Base Updater"]:::backend
        WC["Web Crawler"]:::backend
    end

    subgraph "Data Storage"
        FAISS["FAISS Index & Storage"]:::storage
    end

    subgraph "Configuration & Environment"
        CFG["Configuration"]:::config
    end

    subgraph "External Services"
        OA["OpenAI Embeddings"]:::external
        RA["Reddit API"]:::external
        LC["LangChain Integration"]:::external
    end

    subgraph "Testing"
        TS["Testing Suite"]:::test
    end

    %% Flow Connections
    FE -->|"QueryRequest"| ING
    FE -->|"SearchQuery"| FAISS

    ING -->|"IndexUpdate"| FAISS
    KBU -->|"IndexRefresh"| FAISS
    WC -->|"NewData"| ING

    CFG -->|"APIConfig"| ING
    CFG -->|"APIConfig"| KBU
    CFG -->|"APIConfig"| WC

    ING -->|"EmbeddingCall"| OA
    ING -->|"EmbeddingCall"| RA

    WC -->|"FetchContent"| RA
    KBU -->|"UpdateCall"| OA

    %% Click Events
    click FE "https://github.com/haramrit09k/f1rstaid/blob/main/f1rstaid.py"
    click ING "https://github.com/haramrit09k/f1rstaid/blob/main/ingest.py"
    click KBU "https://github.com/haramrit09k/f1rstaid/blob/main/update_knowledge.py"
    click WC "https://github.com/haramrit09k/f1rstaid/blob/main/crawler/crawler.py"
    click FAISS "https://github.com/haramrit09k/f1rstaid/tree/main/faiss_index/"
    click CFG "https://github.com/haramrit09k/f1rstaid/tree/main/config/"
    click TS "https://github.com/haramrit09k/f1rstaid/tree/main/tests/"

    %% Styles
    classDef frontend fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef backend fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef storage fill:#A9DFBF,stroke:#239B56,stroke-width:2px;
    classDef config fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef external fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef test fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
```
