# kimcheolhui/rag architecture

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

- Interactive diagram: https://gitdiagram.com/kimcheolhui/rag
- Repository: https://github.com/kimcheolhui/rag

## Overview

##### Project Type and Purpose
- This is a full-stack chatbot application built for querying movie information.
- It employs a RAG architecture: it processes user queries by computing embeddings, performs vector-based search in Cosmos DB to retrieve related movie data, and then generates a natural language answer via OpenAI’s GPT model.
- The application has a UI component (with a Streamlit-based interface as the primary option) and an alternative Gradio interface (though marked as non-used in the README).

##### File Structure
- Top-Level Files: The README, main_sl.py (Streamlit UI), requirements.txt, and data files (both JSON and a ZIP with MovieLens information).
- The src Directory Contains Key Modules:
- db_setup.py: Handles Cosmos DB container setup and configuration.
- insert_data.py: Inserts the movie dataset into Cosmos DB.
- embedding.py: Interfaces with the OpenAI Embedding API to convert text into vectors.
- search.py: Executes vector-based similarity search and cache retrieval using Cosmos DB.
- completion.py: Uses OpenAI’s GPT model to generate natural language responses; also caches generated responses.
- main.py: Contains a Gradio-based UI (provided as an alternate front-end).
- There’s also a configuration file (rag_env.env) for storing essential environment variables such as API keys and endpoints.

##### Architectural Components
- Frontend Components:
- Streamlit UI: main_sl.py (primary interface for users).
- (Alternate) Gradio UI: src/main.py.
- Backend Components:
- API Clients/Modules:
- Embedding Module: Converts user queries into vector representations.
- Search Module: Uses the resulting vectors to perform similarity search in Cosmos DB.
- Completion Module: Generates natural language responses based on search results via GPT.
- Data Handling:
- Data Insertion Module: Streamlines the process of inserting and managing movie data in Cosmos DB.
- Database:
- Cosmos DB: Serves as the vector store and caching mechanism to store both movie embeddings and possibly response caches.
- External Services:
- Azure OpenAI: Used for both embeddings (text-embedding-3-small) and completions (gpt-35-turbo).
- Azure Cosmos DB: Provides vector search capabilities and caching.

## Diagram (Mermaid)

```mermaid
graph TD
    %% User Interfaces
    subgraph "User Interfaces"
        UI1["Streamlit UI"]:::ui
        UI2["Gradio UI"]:::ui
    end

    %% Backend Services
    subgraph "Backend Services"
        EMB["Embedding Module"]:::backend
        SEA["Search Module"]:::backend
        COM["Completion Module"]:::backend
        DI["Data Insertion Module"]:::backend
        DBS["Database Setup Module"]:::backend
    end

    %% External Services
    subgraph "External Services"
        EXT_EMB["Azure OpenAI (Embeddings)"]:::external
        EXT_COM["Azure OpenAI (Completions)"]:::external
    end

    %% Database
    DB["Cosmos DB"]:::database

    %% Connections
    UI1 -->|"UserQuery"| EMB
    UI2 -->|"AlternateQuery"| EMB
    EMB -->|"ComputeEmbeddings"| EXT_EMB
    EMB -->|"SendEmbedding"| SEA
    SEA -->|"VectorSearch"| DB
    DB -->|"ReturnData"| SEA
    SEA -->|"ProvideContext"| COM
    COM -->|"GenerateResponse"| EXT_COM
    COM -->|"ReturnAnswer"| UI1
    COM -->|"CacheResponse"| DB
    DI -->|"InsertData"| DB
    DBS -->|"SetupContainers"| DB

    %% Click Events
    click UI1 "https://github.com/kimcheolhui/rag/blob/main/main_sl.py"
    click UI2 "https://github.com/kimcheolhui/rag/blob/main/src/main.py"
    click EMB "https://github.com/kimcheolhui/rag/blob/main/src/embedding.py"
    click SEA "https://github.com/kimcheolhui/rag/blob/main/src/search.py"
    click COM "https://github.com/kimcheolhui/rag/blob/main/src/completion.py"
    click DI "https://github.com/kimcheolhui/rag/blob/main/src/insert_data.py"
    click DBS "https://github.com/kimcheolhui/rag/blob/main/src/db_setup.py"

    %% Styles
    classDef ui fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef backend fill:#FFE4B5,stroke:#333,stroke-width:2px;
    classDef external fill:#C1FFC1,stroke:#333,stroke-width:2px;
    classDef database fill:#FFB6C1,stroke:#333,stroke-width:2px;
```
