Skip to content

Repository files navigation

Smart FAQ Assistant MVP (Next.js + OpenAI)

A minimal web chat assistant demonstrating RAG (Retrieval-Augmented Generation) and Memory patterns using Next.js and OpenAI's GPT-4o.

🎯 Features

  • RAG Implementation: Injects relevant information from knowledge_base.txt into prompts
  • User Memory: Stores and applies user preferences (concise vs detailed responses)
  • Simple Chat UI: Clean, responsive interface built with React/Next.js
  • Serverless API: Next.js API routes for prompt construction and OpenAI calls
  • Persistent Storage: Uses lowdb for simple JSON-based user preference storage

📸 Preview

Smart FAQ Assistant UI

📋 Prerequisites

🚀 Quick Start

1. Install Dependencies

npm install

2. Configure Environment Variables

Create a .env file in the project root:

cp .env.example .env

Edit .env and add your OpenAI API key:

OPENAI_API_KEY=sk-your-actual-openai-api-key-here

3. Run Development Server

npm run dev

Open http://localhost:3000 in your browser.

📁 Project Structure

smart-faq-mvp/
├── pages/
│   ├── index.tsx              # Main chat UI
│   ├── _app.tsx               # Next.js app wrapper
│   └── api/
│       ├── chat.ts            # Main chat endpoint (RAG + Memory)
│       └── clear-preference.ts # Clear user preference endpoint
├── lib/
│   ├── memory.ts              # User preference storage (lowdb)
│   └── rag.ts                 # RAG retrieval logic
├── styles/
│   └── globals.css            # Global styles
├── knowledge_base.txt         # FAQ content (RAG source)
├── claude.md                  # Alternative Claude-style implementation
├── package.json
├── tsconfig.json
├── next.config.js
└── README.md

💡 Usage

Basic Questions

Try asking questions from the knowledge base:

  • "What is the refund policy?"
  • "What are the hardware requirements?"
  • "When are the live sessions?"
  • "Tell me about technical support"

Setting Preferences

Control response format:

  • "I want concise answers" → Sets preference to concise
  • "I prefer detailed responses" → Sets preference to detailed

The bot will remember your preference across the session!

Clearing Preferences

Click the "Clear" button in the preference bar to reset your response format preference.

🔧 Technical Details

RAG Implementation

The system uses simple keyword-based retrieval:

  1. User query is tokenized into keywords
  2. Each section in knowledge_base.txt is scored by keyword matches
  3. Top 3 most relevant sections are injected into the prompt as [RAG INFORMATION]

Memory Pattern

User preferences are stored in a JSON database (db.json):

  1. Preference phrases are detected in user messages
  2. Preferences are saved per user ID (UUID stored in localStorage)
  3. Active preferences are injected into prompts as [RESPONSE_PREFERENCE]

Prompt Construction

The final prompt sent to OpenAI follows this strict order:

  1. System Message: Defines bot role and constraints
  2. Memory Injection: User preference (if exists)
  3. RAG Injection: Relevant knowledge base fragments
  4. Chat History: Recent conversation context
  5. User Question: Current query
  6. Assistant Instruction: Final guidance

🧪 Testing

Functional Tests

  1. RAG Test: Ask "What is the refund policy?"

    • Expected: Should return refund policy from knowledge base
  2. Memory Test: Say "I want concise answers", then ask a question

    • Expected: Response should be shorter and more direct
  3. Out-of-scope Test: Ask something not in the knowledge base

    • Expected: Bot should say it cannot answer and suggest contacting support

Edge Cases

  • Long queries: RAG should still return precise matches
  • Preference false positives: Use phrases like "I want concise answers" rather than just "concise"

📝 Customization

Updating Knowledge Base

Edit knowledge_base.txt to add or modify FAQ content. Use numbered sections for best results:

1. Topic Name:
Content here...

2. Another Topic:
More content...

Changing RAG Algorithm

Modify lib/rag.ts to implement:

  • Vector similarity search (requires embeddings)
  • More sophisticated scoring
  • Section metadata filtering

Memory Options

Current implementation uses lowdb (JSON file). Alternatives:

  • SQLite: Better for production, multi-user scenarios
  • Redis: Fast, scalable, great for sessions
  • PostgreSQL: Full-featured database with complex queries

🔮 Future Enhancements

  • Vector-based RAG using OpenAI embeddings + Pinecone
  • Tool-based memory (Claude-style on-demand retrieval)
  • Multi-user authentication
  • Admin UI for knowledge base management
  • Analytics dashboard
  • File upload for dynamic knowledge base updates

📚 Alternative Implementation

See claude.md for a Claude-style design using:

  • Clean Slate memory pattern
  • Tool-based on-demand memory retrieval
  • Conditional context injection

🛠️ Build for Production

npm run build
npm start

📄 License

MIT

🤝 Contributing

This is an MVP/learning project. Feel free to fork and experiment!

📧 Support

For issues or questions about the implementation, please refer to the original specification document.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages