# yashnayi234/retrieval-augmented-generation-custom-chatbot---genai architecture

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

- Interactive diagram: https://gitdiagram.com/yashnayi234/retrieval-augmented-generation-custom-chatbot---genai
- Repository: https://github.com/yashnayi234/retrieval-augmented-generation-custom-chatbot---genai

## Overview

##### Project Overview
- This project is a domain-specific chatbot built using a Retrieval-Augmented Generation (RAG) approach for financial queries.
- It leverages financial data (CSV dataset) and integrates OpenAI’s API for generating context-aware responses.
- The implementation is organized primarily around a Jupyter Notebook (for interactive use), which orchestrates data loading, embedding computation, retrieval via cosine similarity, prompt construction, and final answer generation.

##### Main Components
a. Data Source:
- Financial Dataset (“finanical_rag_embedding_dataset.csv”) that contains domain-specific data (text entries, and possibly precomputed embeddings).

b. Data Preparation & Preprocessing:
- A module or code segment for loading and cleaning the CSV data.
- Preprocessing routines for embedding computation (if not already computed).

c. Embedding & Retrieval Module:
- Implements the embedding generation (using pre-trained models) and cosine similarity computation to retrieve relevant context.
- Responsible for efficiently filtering down the dataset entries based on relevance.

d. Prompt Construction Module:
- Dynamically builds the prompt, integrating the retrieved context with the user query.
- Takes into account customizations like token limits (`max_prompt_tokens`, `max_answer_tokens`) and a flexible prompt template.

e. OpenAI API Integration:
- A component that sends the constructed prompt to the OpenAI API.
- Manages receiving and processing the generated response.

f. User Query / Interactive Interface:
- The Jupyter Notebook acts as an interactive front end where queries are inputted and results are displayed.

##### Relationships and Data Flow
- The user inputs a financial query into the notebook interface.
- Data Preprocessing:
- The query may trigger the data preparation routines that load or preprocess the CSV dataset.
- Embedding & Retrieval:
- The project computes or uses precomputed embeddings.
- It runs a cosine similarity search to find the most relevant financial contexts.
- Prompt Construction:
- The retrieved context is combined with the query (using the custom prompt template) to form a final prompt.
- API Call:
- The prompt is sent to the OpenAI API, and the response is received.
- Response Return:
- The response is then relayed back to the user, all orchestrated within the notebook.

##### Architectural Patterns and Design Principles
- Modular Design: Each stage (data ingestion, processing, retrieval, prompt construction, API communication) is conceptually separated, making the architecture clean and extendable.
- Reusability and Customization: The prompt template and token limit parameters allow for significant flexibility.
- Integration Centric: Strong reliance on external services (OpenAI API) and established libraries (Pandas, SciPy) for robust performance.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Financial RAG Chatbot"
        A["User Query (Jupyter Notebook)"]:::internal
        B["Data Loader & Preprocessor"]:::internal
        C["Embedding & Retrieval Engine"]:::internal
        D["Prompt Constructor"]:::internal
        E["OpenAI API Interface"]:::external
        F["Financial Dataset"]:::data
    end

    A -->|"calls"| B
    F -->|"provides_data"| B
    B -->|"preprocesses"| C
    C -->|"retrieves_context"| D
    D -->|"builds_prompt"| E
    E -->|"returns_response"| A

    click A "https://github.com/yashnayi234/retrieval-augmented-generation-custom-chatbot---genai/blob/main/Retrieval Augmented Generation (Custom Chatbot)- GenAI.ipynb"
    click B "https://github.com/yashnayi234/retrieval-augmented-generation-custom-chatbot---genai/blob/main/Retrieval Augmented Generation (Custom Chatbot)- GenAI.ipynb"
    click C "https://github.com/yashnayi234/retrieval-augmented-generation-custom-chatbot---genai/blob/main/Retrieval Augmented Generation (Custom Chatbot)- GenAI.ipynb"
    click D "https://github.com/yashnayi234/retrieval-augmented-generation-custom-chatbot---genai/blob/main/Retrieval Augmented Generation (Custom Chatbot)- GenAI.ipynb"
    click E "https://github.com/yashnayi234/retrieval-augmented-generation-custom-chatbot---genai/blob/main/Retrieval Augmented Generation (Custom Chatbot)- GenAI.ipynb"
    click F "https://github.com/yashnayi234/retrieval-augmented-generation-custom-chatbot---genai/blob/main/finanical_rag_embedding_dataset.csv"
    
    classDef internal fill:#C6E2FF,stroke:#333,stroke-width:2px;
    classDef external fill:#FFD700,stroke:#333,stroke-width:2px,stroke-dasharray:5,5;
    classDef data fill:#90EE90,stroke:#333,stroke-width:2px;
```
