# krupa-srinivasan/asiamotors-chatbot architecture

> Architecture diagram of the GitHub repository krupa-srinivasan/asiamotors-chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/krupa-srinivasan/asiamotors-chatbot
- Repository: https://github.com/krupa-srinivasan/asiamotors-chatbot

## Overview

#### Purpose and Entry Points
This repository implements a Retrieval-Augmented Generation (RAG) customer-support chatbot for Asia Motors Auto Garage. The API entry point is the FastAPI application in `app.py`; its primary interface is `POST /ask`. A standalone browser client is provided in `ask.html`. The knowledge source is the garage PDF at `data/business.pdf`.

#### Application Service and Request Boundary
`app.py` owns application startup, HTTP handling, retrieval, prompt construction, model invocation, and chat persistence. `/ask` accepts a `session_id` and customer question, returning a JSON answer. The API boundary allows other clients beyond the included widget, such as messaging integrations.

Grounding: `app.py`, `ask.html`

#### Startup Knowledge Ingestion
On startup, the service reads the business document, extracting page text and tables with pdfplumber. Extracted content is converted into LangChain documents, then split with recursive character chunking (about 1,000 characters with 200-character overlap). This establishes the PDF as the answer-context source.

Grounding: `app.py`, `data/business.pdf`

#### Embedding and Retrieval Layer
Chunks are embedded remotely using Jina AI’s `jina-embeddings-v3` model and indexed in a local Chroma vector store (`./chroma_data` at runtime). Each `/ask` request runs similarity search for the top three relevant chunks. Retrieval context is the factual boundary supplied to generation.

Grounding: `app.py`, `data/business.pdf`

#### Conversation and Generation Pipeline
For an incoming question, the service loads the latest ten messages for the supplied session from PostgreSQL, combines history with retrieved PDF context and a garage-front-desk system persona, and sends the prompt to Groq’s hosted `llama-3.3-70b-versatile`. It persists both the user question and generated answer in the `chat_messages` table, scoped by `session_id`.

Grounding: `app.py`

#### External Services and Runtime Dependencies
The runtime is Python 3.11 with FastAPI/Uvicorn. Required external dependencies are PostgreSQL for durable per-session history, Jina AI for embeddings, and Groq for LLM completion. Credentials and database connectivity are supplied through `JINA_API_KEY`, `GROQ_API_KEY`, and `DATABASE_URL`.

Grounding: `app.py`, `requirements.txt`

#### Deployment and Client Integration
`Dockerfile` packages the service using `python:3.11-slim` and starts Uvicorn. The vector index is local container/runtime storage, while PostgreSQL is expected to be externally provisioned. `ask.html` is a dependency-free widget that calls a configured deployed `/ask` URL; its endpoint must match the hosted API.

Grounding: `Dockerfile`, `ask.html`, `app.py`

Main entry points: [`app.py`](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py) (POST /ask), [`ask.html`](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/ask.html) (Browser widget), [`data/business.pdf`](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/data/business.pdf) (Garage knowledge PDF), [`Dockerfile`](https://github.com/krupa-srinivasan/asiamotors-chatbot/tree/main/Dockerfile) (Docker image)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_widget["Browser widget<br/>HTML client<br/>[ask.html]"]
end

subgraph group_api["RAG API"]
  node_api_app["FastAPI application<br/>Python API<br/>[app.py]"]
  node_ask_route["POST /ask<br/>request boundary<br/>[app.py]"]
  node_retrieval["Top-3 similarity retrieval<br/>retrieval stage<br/>[app.py]"]
  node_chat_history["Session history loader<br/>conversation stage<br/>[app.py]"]
  node_prompt["Grounded prompt builder<br/>generation stage<br/>[app.py]"]
end

subgraph group_knowledge["Knowledge pipeline"]
  node_pdf["Garage knowledge PDF<br/>source document<br/>[business.pdf]"]
  node_pdf_ingestion["PDF extraction and chunking<br/>startup ingestion<br/>[app.py]"]
  node_vector_store[("Chroma vector index<br/>local vector store")]
end

subgraph group_external["External services"]
  node_embedding_api{{"Jina embeddings<br/>remote embedding API"}}
  node_groq{{"Groq LLM completion<br/>hosted LLM"}}
  node_postgres[("PostgreSQL chat_messages<br/>durable chat store")]
end

subgraph group_deployment["Deployment"]
  node_container["Docker image<br/>deployment package"]
  node_requirements["Python dependencies<br/>runtime dependencies<br/>[requirements.txt]"]
end

node_widget -->|"POST question"| node_ask_route
node_container -->|"runs Uvicorn"| node_api_app
node_requirements -.->|"provides runtime libraries"| node_api_app
node_api_app -->|"startup"| node_pdf_ingestion
node_pdf -->|"read and extract"| node_pdf_ingestion
node_pdf_ingestion -->|"chunk embeddings"| node_embedding_api
node_embedding_api -->|"index vectors"| node_vector_store
node_ask_route -->|"question"| node_retrieval
node_retrieval -->|"similarity search"| node_vector_store
node_ask_route -->|"session_id"| node_chat_history
node_chat_history -->|"load last 10"| node_postgres
node_retrieval -->|"retrieved context"| node_prompt
node_chat_history -->|"conversation history"| node_prompt
node_prompt -->|"completion request"| node_groq
node_groq -->|"generated answer"| node_ask_route
node_ask_route -->|"persist question and answer"| node_postgres

click node_widget "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/ask.html"
click node_pdf "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/data/business.pdf"
click node_api_app "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py"
click node_ask_route "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py"
click node_pdf_ingestion "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py"
click node_retrieval "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py"
click node_chat_history "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py"
click node_prompt "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py"
click node_container "https://github.com/krupa-srinivasan/asiamotors-chatbot/tree/main/Dockerfile"
click node_requirements "https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/requirements.txt"

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_widget toneBlue
class node_api_app,node_ask_route,node_retrieval,node_chat_history,node_prompt toneAmber
class node_pdf,node_pdf_ingestion,node_vector_store toneMint
class node_embedding_api,node_groq,node_postgres toneRose
class node_container,node_requirements toneIndigo
```

## Components

### Client

- [Browser widget](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/ask.html): HTML client · Posts customer questions to the deployed API. · `ask.html`

### RAG API

- [FastAPI application](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py): Python API · Owns startup, /ask handling, retrieval, generation, and persistence. · `app.py`
- [POST /ask](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py): request boundary · Accepts session_id and customer question and returns a JSON answer. · `app.py`
- [Top-3 similarity retrieval](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py): retrieval stage · Selects relevant PDF chunks for each question. · `app.py`
- [Session history loader](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py): conversation stage · Loads the latest ten messages for a session. · `app.py`
- [Grounded prompt builder](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py): generation stage · Combines history, retrieved context, question, and front-desk persona. · `app.py`

### Knowledge pipeline

- [Garage knowledge PDF](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/data/business.pdf): source document · Business facts used as answer context. · `data/business.pdf`
- [PDF extraction and chunking](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/app.py): startup ingestion · Extracts page text and tables, then creates overlapping LangChain chunks. · `app.py`
- **Chroma vector index**: local vector store · Runtime-local index at ./chroma_data.

### External services

- **Jina embeddings**: remote embedding API · Uses jina-embeddings-v3 to embed document chunks.
- **Groq LLM completion**: hosted LLM · Runs llama-3.3-70b-versatile.
- **PostgreSQL chat_messages**: durable chat store · Persists user and assistant messages by session_id.

### Deployment

- [Docker image](https://github.com/krupa-srinivasan/asiamotors-chatbot/tree/main/Dockerfile): deployment package · Python 3.11-slim container starts Uvicorn. · `Dockerfile`
- [Python dependencies](https://github.com/krupa-srinivasan/asiamotors-chatbot/blob/main/requirements.txt): runtime dependencies · Defines FastAPI, Uvicorn, and integration libraries. · `requirements.txt`

## Connections

- Browser widget → POST /ask: POST question
- Docker image → FastAPI application: runs Uvicorn
- Python dependencies → FastAPI application: provides runtime libraries
- FastAPI application → PDF extraction and chunking: startup
- Garage knowledge PDF → PDF extraction and chunking: read and extract
- PDF extraction and chunking → Jina embeddings: chunk embeddings
- Jina embeddings → Chroma vector index: index vectors
- POST /ask → Top-3 similarity retrieval: question
- Top-3 similarity retrieval → Chroma vector index: similarity search
- POST /ask → Session history loader: session_id
- Session history loader → PostgreSQL chat_messages: load last 10
- Top-3 similarity retrieval → Grounded prompt builder: retrieved context
- Session history loader → Grounded prompt builder: conversation history
- Grounded prompt builder → Groq LLM completion: completion request
- Groq LLM completion → POST /ask: generated answer
- POST /ask → PostgreSQL chat_messages: persist question and answer
