Skip to content

pratim4dasude/CustomerChat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

CustomerChat

FastAPI Next.js Pinecone GPT-4

Contextual IT Support Chatbot powered by RAG, Pinecone, and ChatGPT

Get instant, accurate IT support answers from your knowledge base


๐ŸŽฏ Overview

CustomerChat is a full-stack IT support chatbot that provides contextual answers to user queries using Retrieval-Augmented Generation (RAG). It leverages semantic search with Pinecone vector database and generates human-like responses using OpenAI's GPT-4o-mini model. The system indexes your knowledge base articles into Pinecone vectors, allowing it to find the most relevant content based on semantic similarity rather than just keyword matching. When a user asks a question, the system retrieves the top matching articles and uses them as context for GPT-4 to generate accurate, domain-specific answers. Every response includes sentiment analysis to detect user frustration levels and tracks which knowledge base articles were used, giving transparency into how answers are generated. The modern chat interface built with Next.js provides a seamless experience where users can get instant IT support without waiting for human agents.


โœจ Features

Feature Description
๐Ÿ” Semantic Search Pinecone-powered vector similarity search for finding relevant KB articles
๐Ÿค– RAG Pipeline Combines retrieval + generation for accurate, context-rich answers
๐Ÿ’ฌ Session Management Persistent chat sessions with conversation history
๐Ÿ˜Š Sentiment Analysis Real-time detection of user frustration levels (0-1 score)
๐Ÿ“š Source Tracking Shows which KB articles were used for each answer
๐Ÿ“ Markdown Support Beautiful formatted responses with lists, code blocks, and bold text
๐Ÿ”„ Knowledge Base Indexing Admin endpoint to re-index the entire knowledge base
๐Ÿ›ก๏ธ Clean UI Modern, responsive chat interface built with Next.js & Tailwind

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         Frontend (Next.js)                      โ”‚
โ”‚                                                                 โ”‚
โ”‚   Landing Page โ”€โ”€โ”€โ”€โ”€โ–บ Chat Interface โ”€โ”€โ”€โ”€โ”€โ–บ Side Panels         โ”‚
โ”‚   (Hero Section)      (Real-time Chat)     (Sentiment + Sources)โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ”‚ HTTP
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         Backend (FastAPI)                       โ”‚
โ”‚                                                                 โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚   โ”‚  Sessions   โ”‚     โ”‚   Messages  โ”‚    โ”‚      Admin      โ”‚    โ”‚
โ”‚   โ”‚   Router    โ”‚     โ”‚   Router    โ”‚    โ”‚     Router      โ”‚    โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€     โ”‚
โ”‚          โ”‚                   โ”‚                    โ”‚             โ”‚
โ”‚          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜             โ”‚
โ”‚                              โ”‚                                  โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚   โ”‚                     Services Layer                        โ”‚ โ”‚
โ”‚   โ”‚                                                           โ”‚ โ”‚
โ”‚   โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚ โ”‚
โ”‚   โ”‚   โ”‚   OpenAI    โ”‚  โ”‚   Pinecone  โ”‚  โ”‚    Sentiment    โ”‚   โ”‚ โ”‚
โ”‚   โ”‚   โ”‚   Service   โ”‚  โ”‚   Service   โ”‚  โ”‚     Service     โ”‚   โ”‚ โ”‚
โ”‚   โ”‚   โ”‚  (GPT-4 +   โ”‚  โ”‚  (Vector    โ”‚  โ”‚  (Keyword-based โ”‚   โ”‚ โ”‚
โ”‚   โ”‚   โ”‚ Embeddings) โ”‚  โ”‚   Search)   โ”‚  โ”‚  Detection)     โ”‚   โ”‚ โ”‚
โ”‚   โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚ โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                              โ”‚                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚                    โ”‚                    โ”‚
          โ–ผ                    โ–ผ                    โ–ผ
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚   SQLite    โ”‚      โ”‚  Pinecone   โ”‚      โ”‚   OpenAI    โ”‚
   โ”‚  Database   โ”‚      โ”‚  Vector DB  โ”‚      โ”‚    API      โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.10+
  • Node.js 18+
  • OpenAI API key
  • Pinecone API key

1. Clone & Setup Backend

# Navigate to backend
cd backend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
# venv\Scripts\activate   # Windows

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env
# Edit .env with your OpenAI and Pinecone API keys

2. Ingest Knowledge Base

# Run the ingestion script to index KB articles
python scripts/ingest_kb.py

3. Start Backend

# Start FastAPI server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

# API docs available at: http://localhost:8000/docs

4. Setup Frontend

# New terminal - navigate to frontend
cd frontend

# Install dependencies
npm install

# Configure environment
cp .env.local.example .env.local

# Start development server
npm run dev

5. Open in Browser


๐Ÿ“ก API Reference

Sessions

Create Session

POST /sessions

Creates a new chat session.

Response:

{
  "session_id": "550e8400-e29b-41d4-a716-446655440000",
  "user_id": null,
  "created_at": "2024-01-15T10:30:00"
}

Get Messages

GET /sessions/{session_id}/messages

Retrieves all messages for a session.

Send Message

POST /sessions/{session_id}/messages

Body:

{
  "content": "How do I reset my VPN password?"
}

Response:

{
  "reply": "To reset your VPN password, follow these steps...",
  "sentiment": "neutral",
  "frustration_score": 0.5,
  "sources": [
    {
      "ki_id": "123",
      "title": "VPN Password Reset Guide",
      "category": "vpn",
      "snippet": "To reset your VPN password..."
    }
  ]
}

Admin

Reindex Knowledge Base

POST /admin/kb/reindex

Re-ingests all KB articles into Pinecone.

Get KB Stats

GET /admin/kb/stats

Returns database and Pinecone vector counts.

Health Check

GET /health

Response:

{
  "status": "healthy",
  "service": "CustomerChat API",
  "version": "1.0.0"
}

๐Ÿ—„๏ธ Database Schema

Sessions

Column Type Description
id Integer Primary key
session_id String Unique UUID
user_id String Optional user identifier
created_at DateTime Creation timestamp

Messages

Column Type Description
id Integer Primary key
session_id Integer FK to sessions
role String "user" or "assistant"
content Text Message content
sentiment String positive/neutral/negative
frustration_score Float 0-1 scale
sources Text JSON of used KB articles
timestamp DateTime Message timestamp

KB Items

Column Type Description
id Integer Primary key
ki_id String Unique KB identifier
title String Article title
content Text Full article text
category String Topic category
tags String Optional tags

๐Ÿ› ๏ธ Tech Stack

Backend

Technology Purpose
FastAPI REST API framework
SQLAlchemy ORM for SQLite
Pinecone Vector database for embeddings
OpenAI GPT-4o-mini + text-embeddings-3-small
Uvicorn ASGI server

Frontend

Technology Purpose
Next.js 14 React framework with App Router
TypeScript Type safety
Tailwind CSS Styling
React Markdown Markdown rendering
Lucide Icons Icon library

Infrastructure

Technology Purpose
Pinecone Vector similarity search
OpenAI API LLM + embeddings
SQLite Local database

๐Ÿ“ Project Structure

CustomerChat/
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ main.py           # FastAPI entry point
โ”‚   โ”‚   โ”œโ”€โ”€ config.py         # Environment config
โ”‚   โ”‚   โ”œโ”€โ”€ database.py       # SQLite setup
โ”‚   โ”‚   โ”œโ”€โ”€ models.py         # SQLAlchemy models
โ”‚   โ”‚   โ”œโ”€โ”€ schemas.py        # Pydantic schemas
โ”‚   โ”‚   โ”œโ”€โ”€ routers/          # API endpoints
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ sessions.py   # Chat sessions
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ admin.py      # Admin operations
โ”‚   โ”‚   โ””โ”€โ”€ services/         # Business logic
โ”‚   โ”‚       โ”œโ”€โ”€ openai_service.py
โ”‚   โ”‚       โ”œโ”€โ”€ pinecone_service.py
โ”‚   โ”‚       โ””โ”€โ”€ sentiment_service.py
โ”‚   โ”œโ”€โ”€ scripts/
โ”‚   โ”‚   โ””โ”€โ”€ ingest_kb.py     # KB ingestion script
โ”‚   โ”œโ”€โ”€ requirements.txt
โ”‚   โ””โ”€โ”€ .env.example
โ”œโ”€โ”€ frontend/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx     # Landing page
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ chat/page.tsx # Chat interface
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ layout.tsx
โ”‚   โ”‚   โ”œโ”€โ”€ components/       # UI components
โ”‚   โ”‚   โ”œโ”€โ”€ lib/             # API client
โ”‚   โ”‚   โ””โ”€โ”€ types/           # TypeScript types
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ tailwind.config.ts
โ””โ”€โ”€ Dataset/
    โ””โ”€โ”€ synthetic_knowledge_items.csv

๐Ÿ”ฎ How RAG Works

  1. Ingestion: KB articles are embedded using text-embedding-3-small and stored in Pinecone
  2. Query: User message is embedded with the same model
  3. Retrieval: Pinecone finds top-5 similar articles using cosine similarity
  4. Generation: GPT-4o-mini generates answer using:
    • System prompt (role: IT support assistant)
    • Retrieved context (KB articles)
    • Conversation history
    • User message
  5. Response: Formatted answer returned with sentiment + sources

Built with โค๏ธ using FastAPI, Next.js, Pinecone & OpenAI

About

RAG-powered IT chatbot using Pinecone + GPT-4 with semantic search, session management, and sentiment analysis.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors