Skip to content

# RAG Chatbot - Complete Implementation Build a production-ready **RAG (Retrieval-Augmented Generation) chatbot** with a ChatGPT-like frontend and Python FastAPI backend. ## Frontend Requirements ### Technology Stack - **Framework**: React with Type...#1

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-172838121-1125883684-564b80f3-b1cd-4699-bc55-98542bc65e04

Conversation

Copy link
Contributor

Copilot AI commented Dec 31, 2025

RAG Chatbot - Complete Implementation

  • Project Restructuring

    • Move existing frontend files to frontend/ directory
    • Create backend/ directory structure
  • Frontend Implementation (React + TypeScript)

    • Install required dependencies (react-markdown, react-syntax-highlighter, remark-math, rehype-katex, katex)
    • Create TypeScript types (types/index.ts)
    • Create API service (services/api.ts)
    • Create components:
      • ChatMessage component with CSS
      • ChatInput component with CSS
      • ChatWindow component with CSS
      • CodeBlock component with CSS
      • LoadingIndicator component with CSS
    • Update App.tsx and App.css for chat interface
    • Update index.css and index.html
    • Create frontend README.md
  • Backend Implementation (Python FastAPI)

    • Create main.py (FastAPI entry point)
    • Create config.py (environment variables)
    • Create models.py (Pydantic models)
    • Create RAG modules:
      • vectorstore.py (Chroma DB)
      • ingestion.py (document loading)
      • retrieval.py (RAG query logic)
    • Create LLM modules:
      • longcat_client.py (LongCat API wrapper)
    • Create utils:
      • logger.py (logging utilities)
    • Create requirements.txt
    • Create .env.example
    • Create details.txt (setup instructions)
    • Create backend README.md
    • Create data/ folder with README and .gitkeep
    • Create chroma_db/ folder with .gitignore
  • Configuration Files

    • Update root .gitignore
    • Create root README.md with full documentation
  • Testing & Validation

    • Test frontend build
    • Test backend startup
    • Run code review
    • Run CodeQL security check
Original prompt

RAG Chatbot - Complete Implementation

Build a production-ready RAG (Retrieval-Augmented Generation) chatbot with a ChatGPT-like frontend and Python FastAPI backend.

Frontend Requirements

Technology Stack

  • Framework: React with TypeScript
  • Styling: Pure CSS (NO Tailwind) - each component has its own CSS file
  • Build Tool: Vite or Create React App

Core Features

  1. Chat Interface

    • Clean, ChatGPT-inspired UI
    • Message input box at bottom with send button
    • Scrollable chat history with auto-scroll to latest message
    • User messages aligned right, bot messages aligned left
    • Avatar/icons for user and bot
    • Loading indicator (typing animation) while waiting for responses
    • Timestamp for each message
    • Clear chat button
  2. Rendering Capabilities (CRITICAL - Must Support)

    • Markdown: Full markdown support (headers, lists, links, bold, italic, blockquotes, tables)
    • Code Blocks: Syntax-highlighted code with language detection, line numbers, and copy-to-clipboard button
    • LaTeX: Render mathematical formulas (inline with $.. .$ and display mode with $$...$$)
  3. Required Libraries

    • react-markdown - Markdown rendering
    • react-syntax-highlighter - Code syntax highlighting
    • remark-math and rehype-katex - Math support in markdown
    • katex - LaTeX rendering
    • Standard React hooks for state management

File Structure

frontend/
β”œβ”€β”€ public/
β”‚   └── index. html
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ ChatMessage/
β”‚   β”‚   β”‚   β”œβ”€β”€ ChatMessage.tsx
β”‚   β”‚   β”‚   └── ChatMessage.css
β”‚   β”‚   β”œβ”€β”€ ChatInput/
β”‚   β”‚   β”‚   β”œβ”€β”€ ChatInput.tsx
β”‚   β”‚   β”‚   └── ChatInput.css
β”‚   β”‚   β”œβ”€β”€ ChatWindow/
β”‚   β”‚   β”‚   β”œβ”€β”€ ChatWindow.tsx
β”‚   β”‚   β”‚   └── ChatWindow.css
β”‚   β”‚   β”œβ”€β”€ CodeBlock/
β”‚   β”‚   β”‚   β”œβ”€β”€ CodeBlock.tsx
β”‚   β”‚   β”‚   └── CodeBlock.css
β”‚   β”‚   └── LoadingIndicator/
β”‚   β”‚       β”œβ”€β”€ LoadingIndicator.tsx
β”‚   β”‚       └── LoadingIndicator.css
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── api.ts
β”‚   β”œβ”€β”€ types/
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ App.tsx
β”‚   β”œβ”€β”€ App.css
β”‚   β”œβ”€β”€ index.tsx
β”‚   └── index.css
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
└── README.md

Backend Requirements

Technology Stack

  • Framework: Python FastAPI with CORS middleware
  • LLM Provider: LongCat API (using OpenAI Python client)
  • RAG Framework: LangChain
  • Vector Database: Chroma (free, embedded, no external setup)
  • Embeddings: OpenAI text-embedding-3-small (via OpenAI API)
  • Document Processing: PyPDF for PDFs, LangChain text loaders for Markdown

LongCat API Integration

Use the OpenAI-compatible client (best option):

from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("LONGCAT_API_KEY"),
    base_url="https://api.longcat.chat/openai"
)

response = client.chat.completions. create(
    model="LongCat-Flash-Chat",
    messages=[... ],
    max_tokens=2000,
    temperature=0.7
)

Core Features

  1. Document Ingestion System

    • Automatically scan backend/data/ folder on startup
    • Load all PDF and Markdown (. md) files
    • Chunk documents: 1000 characters per chunk, 200 character overlap
    • Generate embeddings using OpenAI's text-embedding-3-small
    • Store in Chroma vector database (persistent storage in backend/chroma_db/)
    • Provide /reindex endpoint to manually re-index documents
  2. RAG Pipeline

    • User sends query β†’ Retrieve k=5 most similar document chunks
    • Build context from retrieved chunks
    • Construct final prompt:
      System: You are a helpful AI assistant. Answer questions based on the provided context documents.  If the answer isn't in the context, say so clearly.  Be concise and accurate. 
      
      Context:  {retrieved_chunks}
      
      User Question: {user_input}
      
    • Send to LongCat LLM
    • Return response with sources/metadata
  3. Comprehensive Server Logging (CRITICAL)
    Display ALL of the following in console logs:

    ============================================
    [2025-12-31 10:30:45] NEW QUERY RECEIVED
    ============================================
    USER INPUT: "What is the capital of France?"
    
    --------------------------------------------
    RAG RETRIEVAL PROCESS
    --------------------------------------------
    Similarity Search: k=5
    Retrieved Chunks:  5
    
    Similarity Scores:
      1. Score: 0.8734 | Source: documents/geography. pdf (page 12)
      2. Score: 0.8521 | Source: documents/europe.md
      3. Score: 0.7892 | Source: documents/geography.pdf (page 45)
      4. Score: 0.7654 | Source: documents/cities.md
      5. Score: 0.7341 | Source: documents/world_facts.pdf (page 3)
    
    Chunk Previews:
      [1] "France is a country in Western Europe.  Its capital and largest city is Paris..."
      [2] "European capitals include:  Paris (France), Berlin (Germany)..."
      [3] "Major European cities by population: Paris - 2.1M..."
      [4] "Paris, the capital of France, is known for...
    
    

πŸ’¬ We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: H0NEYP0T-628 <232321695+H0NEYP0T-628@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants