# 202416852-anhpv/4h2n architecture

> Architecture diagram of the GitHub repository 202416852-anhpv/4h2n, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/202416852-anhpv/4h2n
- Repository: https://github.com/202416852-anhpv/4h2n

## Overview

Điện Máy Xanh is a Vietnamese AI shopping-advice chatbot. A shopper enters a need in the chat UI; the frontend sends conversation history to FastAPI, whose LLM tool loop searches real catalog data, then selects up to three grounded product cards or asks for clarification. Store-policy questions use a separate retrieval-and-answer branch. The README documents both semantic retrieval and keyword fallback, plus hallucination guardrails. Edges reflect shown calls and explicit README behavior; unsampled UI and tool-loop details are not expanded beyond that evidence.

Main entry points: [`backend/app/llm.py`](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py) (Tool conversation loop), [`backend/app/main.py`](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/main.py) (Chat endpoint), [`backend/app/catalog.py`](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/catalog.py) (Catalog search), [`backend/app/policies.py`](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/policies.py) (Policy search)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_chat_ui["Chat experience"]
  node_app_ui["Chat application<br/>[App.tsx]"]
  node_chat_input["Chat input<br/>[ChatInput.tsx]"]
  node_message_view["Message view<br/>[MessageBubble.tsx]"]
  node_markdown["Markdown renderer<br/>[Markdown.tsx]"]
  node_product_cards["Product cards"]
  node_api_client["Chat API client<br/>[api.ts]"]
end

subgraph group_conversation["Conversation orchestration"]
  node_api_route["Chat endpoint<br/>[main.py]"]
  node_chat_schemas["Chat schemas<br/>[schemas.py]"]
  node_llm_loop["Tool conversation loop<br/>[llm.py]"]
  node_customer_clarification["Clarification response<br/>[llm.py]"]
  node_recommendation["Product recommendation<br/>[llm.py]"]
end

subgraph group_product_domain["Product retrieval"]
  node_catalog_search["Catalog search<br/>[catalog.py]"]
  node_product_catalog[("Product catalog")]
  node_product_vectors[("Product vectors")]
end

subgraph group_policy_domain["Policy retrieval"]
  node_policy_search["Policy search<br/>[policies.py]"]
  node_policy_documents["Policy documents"]
  node_policy_vectors[("Policy vectors")]
  node_policy_answer["Policy answer<br/>[llm.py]"]
end

subgraph group_shared["Shared services"]
  node_embedding_client["Embedding client<br/>[embeddings.py]"]
  node_settings["Runtime settings<br/>[config.py]"]
end

node_shopper(("Shopper"))
node_fpt_llm{{"FPT AI Factory"}}
node_fpt_embeddings{{"FPT embeddings API"}}

node_shopper -->|"enters request"| node_chat_input
node_chat_input -->|"submits text"| node_app_ui
node_app_ui -->|"sends history"| node_api_client
node_api_client -->|"POST chat"| node_api_route
node_api_route -->|"validates payload"| node_chat_schemas
node_api_route -->|"runs chat"| node_llm_loop
node_llm_loop -->|"requests tool calls"| node_fpt_llm
node_llm_loop -->|"searches products"| node_catalog_search
node_catalog_search -->|"loads records"| node_product_catalog
node_catalog_search -->|"ranks candidates"| node_product_vectors
node_catalog_search -->|"embeds query"| node_embedding_client
node_llm_loop -->|"finalizes choices"| node_recommendation
node_recommendation -->|"gets product details"| node_catalog_search
node_llm_loop -->|"asks follow-up"| node_customer_clarification
node_llm_loop -->|"searches policies"| node_policy_search
node_policy_search -->|"chunks and searches"| node_policy_documents
node_policy_search -->|"ranks chunks"| node_policy_vectors
node_policy_search -->|"embeds query"| node_embedding_client
node_llm_loop -->|"finalizes answer"| node_policy_answer
node_policy_answer -->|"grounds response"| node_policy_search
node_embedding_client -->|"requests vectors"| node_fpt_embeddings
node_embedding_client -->|"reads credentials"| node_settings
node_api_route -->|"checks configuration"| node_settings
node_api_route -->|"returns reply and cards"| node_app_ui
node_app_ui -->|"renders turns"| node_message_view
node_message_view -->|"renders text"| node_markdown
node_message_view -->|"renders products"| node_product_cards
node_product_cards -->|"requests details"| node_app_ui

click node_app_ui "https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/App.tsx"
click node_chat_input "https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/components/ChatInput.tsx"
click node_message_view "https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/components/MessageBubble.tsx"
click node_markdown "https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/components/Markdown.tsx"
click node_product_cards "https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/components/ProductCardView.tsx"
click node_api_client "https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/api.ts"
click node_api_route "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/main.py"
click node_chat_schemas "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/schemas.py"
click node_llm_loop "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py"
click node_customer_clarification "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py"
click node_recommendation "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py"
click node_catalog_search "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/catalog.py"
click node_product_catalog "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/data/products_detail.json"
click node_product_vectors "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/data/product_embeddings.npz"
click node_policy_search "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/policies.py"
click node_policy_documents "https://github.com/202416852-anhpv/4h2n/tree/main/backend/app/data"
click node_policy_vectors "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/data/policy_embeddings.npz"
click node_policy_answer "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py"
click node_embedding_client "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/embeddings.py"
click node_settings "https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/config.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_app_ui,node_chat_input,node_message_view,node_markdown,node_product_cards,node_api_client toneBlue
class node_api_route,node_chat_schemas,node_llm_loop,node_customer_clarification,node_recommendation toneAmber
class node_catalog_search,node_product_catalog,node_product_vectors,node_fpt_embeddings toneMint
class node_policy_search,node_policy_documents,node_policy_vectors,node_policy_answer toneRose
class node_embedding_client,node_settings,node_shopper,node_fpt_llm toneIndigo
```

## Components

### Chat experience

- [Chat application](https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Chat input](https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/components/ChatInput.tsx): `frontend/src/components/ChatInput.tsx`
- [Message view](https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/components/MessageBubble.tsx): `frontend/src/components/MessageBubble.tsx`
- [Markdown renderer](https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/components/Markdown.tsx): `frontend/src/components/Markdown.tsx`
- [Product cards](https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/components/ProductCardView.tsx): `frontend/src/components/ProductCardView.tsx`
- [Chat API client](https://github.com/202416852-anhpv/4h2n/blob/main/frontend/src/api.ts): `frontend/src/api.ts`

### Conversation orchestration

- [Chat endpoint](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/main.py): `backend/app/main.py`
- [Chat schemas](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/schemas.py): `backend/app/schemas.py`
- [Tool conversation loop](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py): `backend/app/llm.py`
- [Clarification response](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py): `backend/app/llm.py`
- [Product recommendation](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py): `backend/app/llm.py`

### Product retrieval

- [Catalog search](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/catalog.py): `backend/app/catalog.py`
- [Product catalog](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/data/products_detail.json): `backend/app/data/products_detail.json`
- [Product vectors](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/data/product_embeddings.npz): `backend/app/data/product_embeddings.npz`

### Policy retrieval

- [Policy search](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/policies.py): `backend/app/policies.py`
- [Policy documents](https://github.com/202416852-anhpv/4h2n/tree/main/backend/app/data): `backend/app/data`
- [Policy vectors](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/data/policy_embeddings.npz): `backend/app/data/policy_embeddings.npz`
- [Policy answer](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/llm.py): `backend/app/llm.py`

### Shared services

- [Embedding client](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/embeddings.py): `backend/app/embeddings.py`
- [Runtime settings](https://github.com/202416852-anhpv/4h2n/blob/main/backend/app/config.py): `backend/app/config.py`

### Other components

- **Shopper**
- **FPT AI Factory**
- **FPT embeddings API**

## Connections

- Shopper → Chat input: enters request
- Chat input → Chat application: submits text (evidence: `frontend/src/App.tsx`)
- Chat application → Chat API client: sends history (evidence: `frontend/src/App.tsx`)
- Chat API client → Chat endpoint: POST chat (evidence: `README.md`)
- Chat endpoint → Chat schemas: validates payload (evidence: `backend/app/main.py`)
- Chat endpoint → Tool conversation loop: runs chat (evidence: `backend/app/main.py`)
- Tool conversation loop → FPT AI Factory: requests tool calls (evidence: `README.md`)
- Tool conversation loop → Catalog search: searches products (evidence: `backend/app/llm.py`)
- Catalog search → Product catalog: loads records (evidence: `backend/app/catalog.py`)
- Catalog search → Product vectors: ranks candidates (evidence: `backend/app/catalog.py`)
- Catalog search → Embedding client: embeds query (evidence: `backend/app/catalog.py`)
- Tool conversation loop → Product recommendation: finalizes choices (evidence: `backend/app/llm.py`)
- Product recommendation → Catalog search: gets product details (evidence: `backend/app/llm.py`)
- Tool conversation loop → Clarification response: asks follow-up (evidence: `backend/app/llm.py`)
- Tool conversation loop → Policy search: searches policies (evidence: `backend/app/llm.py`)
- Policy search → Policy documents: chunks and searches (evidence: `backend/app/policies.py`)
- Policy search → Policy vectors: ranks chunks (evidence: `backend/app/policies.py`)
- Policy search → Embedding client: embeds query (evidence: `backend/app/policies.py`)
- Tool conversation loop → Policy answer: finalizes answer (evidence: `README.md`)
- Policy answer → Policy search: grounds response (evidence: `README.md`)
- Embedding client → FPT embeddings API: requests vectors (evidence: `backend/app/embeddings.py`)
- Embedding client → Runtime settings: reads credentials (evidence: `backend/app/embeddings.py`)
- Chat endpoint → Runtime settings: checks configuration (evidence: `backend/app/main.py`)
- Chat endpoint → Chat application: returns reply and cards (evidence: `backend/app/main.py`)
- Chat application → Message view: renders turns (evidence: `frontend/src/App.tsx`)
- Message view → Markdown renderer: renders text (evidence: `frontend/src/components/MessageBubble.tsx`)
- Message view → Product cards: renders products (evidence: `frontend/src/components/MessageBubble.tsx`)
- Product cards → Chat application: requests details (evidence: `frontend/src/App.tsx`)
