# AI Chat Implementation Findings ## Executive Summary The Biblical Guide codebase has a **fully implemented AI chat system that is currently DISABLED**. The chat functionality is present in the codebase but commented out in the main layout, preventing users from accessing it. --- ## 1. Current AI Chat Implementation Status ### Status: DISABLED (But Fully Functional) - Location of disable: `/root/biblical-guide/app/[locale]/layout.tsx` (Line 10, 133) - The FloatingChat component is imported but commented out with note "AI Chat disabled" - All API routes exist and are functional - Database models are in place - Frontend components are complete and ready --- ## 2. Frontend Components & Pages Structure ### Main Chat Component **File:** `/root/biblical-guide/components/chat/floating-chat.tsx` - **Type:** Client-side React component ('use client') - **Features:** - Floating action button (FAB) in bottom-right corner - Slide-in chat drawer with full conversation history - Supports fullscreen view with minimize/maximize - Left sidebar showing chat history with conversation management - Right side with message display and input - Multi-language support (Romanian, English, Spanish, Italian) - Real-time conversation persistence to database - Suggested questions for getting started - Conversation rename and delete functionality - Loading state with Bible-related messages **State Management:** - Local state for messages, conversations, UI modes - Uses localStorage for auth token - Integrates with global useAuth hook - Conversation history loaded from `/api/chat/conversations` ### Secondary Chat Component **File:** `/root/biblical-guide/components/chat/chat-interface.tsx` - **Type:** Simpler alternative chat interface (Client component) - **Status:** Available but not currently used in main layout - Uses Tailwind CSS instead of Material-UI - Basic message display with markdown support ### Integration Points **File:** `/root/biblical-guide/app/[locale]/page.tsx` - Chat is triggered via custom events: `floating-chat:open` - Multiple buttons dispatch these events to open chat with: - Fullscreen mode - Initial message pre-populated - Example: "Ask AI" buttons on home page and other pages --- ## 3. API Routes & Backend Implementation ### Main Chat API **File:** `/root/biblical-guide/app/api/chat/route.ts` **POST /api/chat** - Main chat endpoint - **Authentication:** Required (Bearer token) - **Request Body:** ```json { "message": "string", "conversationId": "string (optional)", "locale": "string (optional, default: 'ro')", "history": "array (optional, for anonymous users)" } ``` - **Response:** ```json { "success": boolean, "response": "AI response text", "conversationId": "string (only if authenticated)" } ``` - **Key Features:** 1. **Authentication Check:** Returns 401 if no valid Bearer token 2. **Conversation Management:** - Creates new conversations for authenticated users - Loads existing conversation history (last 15 messages) - Maintains conversation metadata 3. **Subscription Limits:** - Free tier: Limited conversations per month - Premium tier: Unlimited conversations - Uses `/lib/subscription-utils` to check limits 4. **Bible Vector Search:** - Searches for relevant Bible verses using `searchBibleHybrid()` - Supports language-specific filtering - Includes verse references in context (5 verses max) 5. **Azure OpenAI Integration:** - Calls Azure OpenAI API with formatted messages - Supports multiple languages with specific system prompts - Temperature: 0.7, Max tokens: 2000 - Handles content filtering responses 6. **Fallback System:** - Language-specific fallback responses if Azure OpenAI fails - Gracefully degrades without blocking chat ### Conversation Management API **File:** `/root/biblical-guide/app/api/chat/conversations/route.ts` **GET /api/chat/conversations** - List user conversations - **Query Parameters:** - `language` (ro|en, optional) - `limit` (1-50, default: 20) - `offset` (default: 0) - **Returns:** Paginated list of conversations with last message preview **POST /api/chat/conversations** - Create new conversation - **Request:** `{ title, language }` - **Returns:** Created conversation object ### Individual Conversation API **File:** `/root/biblical-guide/app/api/chat/conversations/[id]/route.ts` - **GET:** Load specific conversation with all messages - **PUT:** Rename conversation - **DELETE:** Soft delete conversation (sets isActive to false) ### Admin API Routes **Files:** - `/root/biblical-guide/app/api/admin/chat/conversations/route.ts` - `/root/biblical-guide/app/api/admin/chat/conversations/[id]/route.ts` - Admin dashboard: `/root/biblical-guide/app/admin/chat/page.tsx` - Monitoring component: `/root/biblical-guide/components/admin/chat/conversation-monitoring.tsx` --- ## 4. Azure OpenAI Integration ### Configuration **File:** `/root/biblical-guide/lib/ai/azure-openai.ts` **Environment Variables Required:** ``` AZURE_OPENAI_KEY= AZURE_OPENAI_ENDPOINT=https://.openai.azure.com AZURE_OPENAI_DEPLOYMENT=gpt-4o AZURE_OPENAI_API_VERSION=2025-01-01-preview AZURE_OPENAI_EMBED_DEPLOYMENT=Text-Embedding-ada-002-V2 AZURE_OPENAI_EMBED_API_VERSION=2023-05-15 EMBED_DIMS=1536 ``` **Currently Configured (from .env.local):** ``` AZURE_OPENAI_KEY=42702a67a41547919877a2ab8e4837f9 AZURE_OPENAI_ENDPOINT=https://footprints-ai.openai.azure.com AZURE_OPENAI_DEPLOYMENT=gpt-4o AZURE_OPENAI_API_VERSION=2025-01-01-preview ``` ### AI Response Generation **Function:** `generateBiblicalResponse()` in `/root/biblical-guide/app/api/chat/route.ts` **Process:** 1. Uses `searchBibleHybrid()` to find relevant Bible verses with language filtering 2. Extracts version information from database source tables 3. Creates language-specific system prompts (Romanian, English, Spanish) 4. Implements smart context building from conversation history: - Always includes last 6 messages for immediate context - Finds relevant older messages based on keyword/biblical reference matching - Applies token-based truncation (~1500 tokens max for context) - Can summarize older messages if needed 5. Calls Azure OpenAI REST API with: - System prompt with Bible context and instructions - Current user message - Conversation history (smart context) 6. Returns formatted response with Bible version citations **System Prompts:** - Language-specific (Romanian, English, Spanish supported) - Instructs AI to cite Bible versions as "[Version] Reference" - Emphasizes accuracy and empathy - Mentions biblical passages found in context --- ## 5. Database Models (Prisma) **File:** `/root/biblical-guide/prisma/schema.prisma` ### ChatConversation Model ```prisma model ChatConversation { id String @id @default(uuid()) userId String? // Optional for anonymous users title String // Auto-generated from first message language String // 'ro' or 'en' isActive Boolean @default(true) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt lastMessageAt DateTime @default(now()) user User? @relation(fields: [userId], references: [id], onDelete: Cascade) messages ChatMessage[] @@index([userId, language, lastMessageAt]) @@index([isActive, lastMessageAt]) } ``` ### ChatMessage Model ```prisma model ChatMessage { id String @id @default(uuid()) conversationId String userId String? // For backward compatibility role ChatMessageRole // USER | ASSISTANT content String @db.Text metadata Json? // Store verse references, etc. timestamp DateTime @default(now()) conversation ChatConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade) user User? @relation(fields: [userId], references: [id], onDelete: Cascade) @@index([conversationId, timestamp]) } ``` ### ChatMessageRole Enum ```prisma enum ChatMessageRole { USER ASSISTANT } ``` --- ## 6. Configuration & Feature Flags ### Current Status **DISABLED in code:** - Location: `/root/biblical-guide/app/[locale]/layout.tsx` - Lines 10 and 133 have the import and component commented out - Comment: "AI Chat disabled" ### To Enable Chat 1. **Uncomment in layout.tsx:** ```tsx import FloatingChat from '@/components/chat/floating-chat' // Line 10 // In JSX around line 133: ``` 2. **Verify environment variables are set:** ``` AZURE_OPENAI_KEY AZURE_OPENAI_ENDPOINT AZURE_OPENAI_DEPLOYMENT AZURE_OPENAI_API_VERSION ``` 3. **Ensure database tables exist:** ```bash npm run db:migrate ``` ### Translation Files **Location:** `/root/biblical-guide/messages/` - Chat translations exist in: `en.json`, `ro.json`, `es.json`, `it.json` - Key sections: - `chat.title` - "Biblical AI Chat" - `chat.subtitle` - Assistant description - `chat.placeholder` - Input placeholder text - `chat.suggestions` - Suggested questions for starting - `chat.enterToSend` - Keyboard hint --- ## 7. Authentication & Authorization ### Required for Chat - **Bearer Token:** Required in Authorization header - **Token Source:** `localStorage.getItem('authToken')` - **Verification:** Uses `verifyToken()` from `/lib/auth` ### Permission Levels 1. **Unauthenticated:** Can see chat UI but can't send messages 2. **Free Tier:** Limited conversations per month 3. **Premium Tier:** Unlimited conversations ### Conversation Limits - Tracked via `/lib/subscription-utils`: - `checkConversationLimit(userId)` - Check if user can create new conversation - `incrementConversationCount(userId)` - Track monthly conversation count --- ## 8. Related Features & Dependencies ### Vector Search Integration **File:** `/root/biblical-guide/lib/vector-search.ts` - Function: `searchBibleHybrid(message, locale, limit)` - Searches Bible verses using embeddings - Supports language filtering - Returns verse objects with reference, text, and source table ### Event System - Uses custom events for chat control: - `floating-chat:open` - Open chat with optional params - `auth:sign-in-required` - Trigger auth modal from chat ### Subscription System - Tracks free vs premium users - Enforces conversation limits for free tier - Returns upgrade URL when limits reached --- ## 9. What's Needed to Enable Chat ### Quick Checklist - [ ] Uncomment FloatingChat import in `/root/biblical-guide/app/[locale]/layout.tsx` (line 10) - [ ] Uncomment `` component in layout JSX (line 133) - [ ] Verify Azure OpenAI credentials in `.env.local` - [ ] Verify database migration has run (tables exist) - [ ] Test authentication flow - [ ] Test with sample messages - [ ] Verify Bible verse search works - [ ] Test conversation persistence ### Testing - Test unauthenticated access (should show sign-in prompt) - Test authenticated chat flow - Test conversation history loading - Test conversation rename/delete - Test fullscreen mode - Test different languages - Test free tier limits - Test long conversations with context pruning --- ## 10. File Paths Summary **Core Chat Implementation:** - `/root/biblical-guide/components/chat/floating-chat.tsx` - Main UI component - `/root/biblical-guide/components/chat/chat-interface.tsx` - Alternative UI - `/root/biblical-guide/app/api/chat/route.ts` - Main API endpoint - `/root/biblical-guide/app/api/chat/conversations/route.ts` - Conversation list/create - `/root/biblical-guide/app/api/chat/conversations/[id]/route.ts` - Individual conversation **Configuration:** - `/root/biblical-guide/.env.local` - Azure OpenAI keys - `/root/biblical-guide/lib/ai/azure-openai.ts` - Azure integration - `/root/biblical-guide/prisma/schema.prisma` - Database models - `/root/biblical-guide/messages/en.json` - English translations **Admin:** - `/root/biblical-guide/app/admin/chat/page.tsx` - Admin dashboard - `/root/biblical-guide/components/admin/chat/conversation-monitoring.tsx` - Monitoring UI - `/root/biblical-guide/app/api/admin/chat/conversations/route.ts` - Admin API **Layout/Integration:** - `/root/biblical-guide/app/[locale]/layout.tsx` - Main layout (chat disabled here) - `/root/biblical-guide/app/[locale]/page.tsx` - Home page (has chat triggers) --- ## Conclusion **The AI chat is fully implemented and ready to use.** It's only disabled by being commented out in the layout file. The system includes: 1. Complete frontend UI with Material-UI components 2. Full backend API with conversation persistence 3. Azure OpenAI integration for intelligent responses 4. Bible verse search and context injection 5. Multi-language support 6. Subscription tier enforcement 7. Admin monitoring capabilities 8. Conversation management (create, rename, delete) To enable it, simply uncomment 2 lines in the main layout file.