This documentation outlines the training and deployment process of the chatbot demo project, utilizing FAISS for vector storage, OpenAI's LLM API, and intelligent intent routing with multi-level prompting.
Table of Contents
Project Overview
Dependencies
Architecture
Deployment Future Improvements
This chatbot demonstrates an advanced architecture featuring:
- Intent-based routing to classify user queries into specific categories
- Multi-level prompting with specialized templates for different use cases
- Conversation history management for contextual responses
- FAISS vector storage for semantic search over documents
- FastAPI-based inference with OpenAI's GPT-4 integration
The system intelligently routes queries to specialized handlers:
- Research: Questions about loyalty programs and travel (with document retrieval)
- Wallet: Personal points/miles management and redemption recommendations
- Unknown: General travel-related queries or redirection
Ensure the following Python packages are installed:
pip install -r requirements.txtKey dependencies:
- langchain
- langchain-openai
- langchain-community
- faiss-cpu or faiss-gpu
- openai
- fastapi
- uvicorn
- python-dotenv
- pydantic
The system uses a two-stage approach:
-
Intent Classification: A lightweight model (
gpt-4.1-nano) classifies queries into:research: Learning about loyalty programswallet: Using personal points/miles for redemptionsunknown: General travel questions or off-topic queries
-
Specialized Processing: Each intent routes to a specialized chain with tailored prompts
# Intent classification prompt
intent_template = PromptTemplate(
template=(
"You are an intent classifier. Classify the user query into one of the following categories:\n"
"- 'research': if the user is asking to learn about points/miles or loyalty programs\n"
"- 'wallet': when user is interested in the best use of miles and points in their wallets\n"
"- 'unknown': if it's unclear or doesn't fit above\n\n"
"User Query: {query}\n"
"Intent:"
),
input_variables=["query"],
)The system uses specialized prompt templates for each intent:
Research Template: For educational queries with document retrieval
- Includes context from FAISS similarity search
- Emphasizes providing source URLs
- Structured Markdown formatting
Wallet Template: For personal redemption optimization
- Accesses user's points/miles data
- Provides personalized recommendations
- Considers home airport and preferences
Unknown Template: For general or off-topic queries
- Maintains focus on travel/loyalty topics
- Politely redirects non-travel questions
class ConversationHistory:
MAX_TOKENS = 1024
TOKEN_BUFFER = 500
def add_conversation(self, user_id, human_message, ai_message)
def retrieve_conversation(self, user_id)
def truncate_conversation(self, user_id)Features:
- Per-user conversation tracking
- Token-based truncation to stay within limits
- Maintains context across multiple interactions
Prepare your dataset:
- Store articles or domain-specific documents in the data folder.
- Each document should have:
- A .txt file as the file extension.
- Metadata added at the top of the file (e.g., title, URL).
The structure may look like:
data/
├── document1.txt
Title: Sample Title
URL: https://example.com/article1
<document content>
├── document2.txt
Title: Another Title
URL: https://example.com/article2
<document content>
Use the training script in train.ipynb to generate vector embeddings and save the FAISS index.
- Load Data:
from langchain.schema import Document
from pathlib import Path
import json
def load_data():
data_path = Path("data")
documents = []
for file_path in data_path.glob("*.txt"):
with open(file_path, "r") as f:
content = f.read()
documents.append(Document(page_content=content))
return documents- Generate FAISS Index:
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
documents = load_data()
embeddings = OpenAIEmbeddings()
faiss_index = FAISS.from_documents(documents, embeddings)
faiss_index.save_local("model")The inference pipeline is implemented across multiple modules for better organization:
Chain Manager (chains.py):
class ChainManager:
def __init__(self):
self.prompt_manager = PromptManager()
self.db = FAISS.load_local("model", OpenAIEmbeddings(), allow_dangerous_deserialization=True)
self.llm = ChatOpenAI(temperature=0.0, model_name="gpt-4.1-mini")
self.llm_prep = ChatOpenAI(temperature=0.0, model_name="gpt-4.1-nano")Prompt Manager (prompt_manager.py):
class PromptManager:
def __init__(self):
self.prompts = {
"intent": intent_template,
"research": research_template,
"wallet": wallet_template,
"unknown": unknown_template
}The main handler (handler.py) implements the routing logic:
- Classify Intent: Use intent chain to determine query type
- Route to Specialized Chain: Based on intent, select appropriate processing
- Retrieve Context: For research queries, perform similarity search
- Process Query: Run through specialized prompt template
- Update History: Store conversation for future context
# Intent classification
intent = intent_chain.invoke({"query": query}).content
if intent == "research":
# Retrieve relevant documents
search_results = db.similarity_search_with_score(query=query, k=3, score_threshold=0.5)
# Process with research chain
elif intent == "wallet":
# Load user data and process with wallet chain
elif intent == "unknown":
# Process with general unknown chainThe API exposes the following endpoints:
POST /ask/
Accepts a query and returns a response based on intent routing and specialized processing.
- Input:
{
"user_id": "user_123",
"query": "What is the fastest way to achieve Marriott Bonvoy elite status?"
}- Response:
{
"statusCode": 200,
"body": {
"query": "What is the fastest way to achieve Marriott Bonvoy elite status?",
"response": "### Achieving Marriott Bonvoy Elite Status\n\n**Elite Night Credits** are the primary way to achieve status:\n- **Silver Elite**: 10 elite nights\n- **Gold Elite**: 25 elite nights\n- **Platinum Elite**: 50 elite nights\n- **Titanium Elite**: 75 elite nights\n- **Ambassador Elite**: 100+ nights + $23,000 spend\n\n**Fastest Methods:**\n1. **Credit Card Bonuses**: Marriott credit cards offer automatic elite nights\n2. **Status Challenges**: Some regions offer accelerated earning periods\n3. **Corporate Rates**: Business travelers can earn double elite nights\n\nFor more details: https://www.marriott.com/loyalty/member-benefits/elite.mi"
}
}Create a test file with conversation examples:
from handler import ask, Query
# Test different intents
# research
resp1 = ask(Query(query="How to achieve high elite status with Marriott?", user_id="test_user_123"))
# wallet
resp2 = ask(Query(query="How many United miles do I have?", user_id="test_user_123"))
# wallet (no data available in the wallet)
resp3 = ask(Query(query="How many Delta miles do I have?", user_id="test_user_123"))- Environment Variables: Create a
.envfile:
OPENAI_API_KEY=your_api_key_here- Install Dependencies:
pip install -r requirements.txt- Start the Server:
uvicorn handler:app --host 0.0.0.0 --port 8000Test with curl:
curl --location 'http://localhost:8000/ask/' \
--header 'Content-Type: application/json' \
--data '{
"user_id": "test_123",
"query": "Tell me about Delta lounges"
}'-
User Experience:
- Add streaming responses for real-time interaction
- Implement typing indicators
- Add suggested follow-up questions
-
Monitoring & Analytics:
- Track intent classification accuracy
- Monitor response quality metrics
- Implement A/B testing for prompt optimization
-
Security & Privacy:
- LLM abuse
- Implement rate limiting
- Add PII detection and redaction