This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Fixing the issues with RAG #73
Open
RukshS
wants to merge
10
commits into
Dev
Choose a base branch
from
feature/rag
base: Dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1d9d1c6
fix: tweak the system prompt to be more robust
RukshS aa76166
Merge branch 'Dev' into feature/rag
RukshS 0e4a0d6
fix: chatbot issues
RukshS ffcd943
fix: enable PII filtering with Tavily and adjust the system prompt to…
RukshS 3a13223
Merge branch 'Dev' into feature/rag
RukshS f62ec50
Merge branch 'Dev' into feature/rag
RukshS 91a21ee
Merge branch 'Dev' into feature/rag
RukshS be836bc
feat: auto booking using agentic rag
RukshS 4b510b5
Merge branch 'Dev' into feature/rag
RukshS 77d516e
Merge branch 'Dev' into feature/rag
RukshS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,290 @@ | ||
| // src/app/api/ragbot/booking-suggestions/route.ts | ||
| import { NextRequest, NextResponse } from 'next/server'; | ||
| import connectDB from '@/lib/db'; | ||
| import Department from '@/lib/models/departmentSchema'; | ||
|
|
||
| interface ServiceSchema { | ||
| id: string; | ||
| name: string; | ||
| description?: string; | ||
| category?: string; | ||
| isActive?: boolean; | ||
| processingTime?: string; | ||
| fee?: number; | ||
| requirements?: string[]; | ||
| } | ||
| interface DepartmentData { | ||
| id: string; | ||
| departmentId: string; | ||
| name: string; | ||
| shortName: string; | ||
| description: string; | ||
| services?: ServiceData[]; | ||
| } | ||
|
|
||
| interface ServiceData { | ||
| id: string; | ||
| name: string; | ||
| description?: string; | ||
| category?: string; | ||
| processingTime?: string; | ||
| fee?: number; | ||
| requirements?: string[]; | ||
| departmentId?: string; | ||
| departmentName?: string; | ||
| } | ||
|
|
||
| interface BookingSuggestion { | ||
| type: 'department' | 'service'; | ||
| id: string; | ||
| name: string; | ||
| description?: string; | ||
| departmentId?: string; | ||
| departmentName?: string; | ||
| category?: string; | ||
| fee?: number; | ||
| relevanceScore: number; | ||
| } | ||
|
|
||
| /** | ||
| * Server-side booking suggestions for the RAG bot | ||
| */ | ||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| // Add proper error handling for JSON parsing | ||
| let body; | ||
| try { | ||
| body = await request.json(); | ||
| } catch (jsonError) { | ||
| console.error('❌ Invalid JSON in request body:', jsonError); | ||
| return NextResponse.json( | ||
| { | ||
| error: 'Invalid JSON in request body', | ||
| suggestions: [], | ||
| details: jsonError instanceof Error ? jsonError.message : 'Unknown JSON parsing error' | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const { query } = body; | ||
|
|
||
| if (!query) { | ||
| return NextResponse.json( | ||
| { | ||
| error: 'Query is required', | ||
| suggestions: [] | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| console.log('🔍 Booking suggestions API called with query:', query); | ||
|
|
||
| await connectDB(); | ||
|
|
||
| // Fetch departments with services | ||
| const departments = await Department.find( | ||
| { | ||
| $or: [ | ||
| { status: 'ACTIVE' }, | ||
| { status: 'active' } | ||
| ] | ||
| }, | ||
| { | ||
| _id: 1, | ||
| departmentId: 1, | ||
| name: 1, | ||
| shortName: 1, | ||
| description: 1, | ||
| services: 1 | ||
| } | ||
| ).lean(); | ||
|
|
||
| console.log('🏛️ Raw departments from DB:', departments.length); | ||
| console.log('📊 First department services:', departments[0]?.services?.length || 0); | ||
|
|
||
| // Transform data to match booking helper format | ||
| const departmentData: DepartmentData[] = departments.map(dept => ({ | ||
| id: dept._id ? String(dept._id) : '', | ||
| departmentId: dept.departmentId || '', | ||
| name: dept.name || '', | ||
| shortName: dept.shortName || '', | ||
| description: dept.description || '', | ||
| services: (dept.services || []) | ||
| .filter((service: ServiceSchema) => service.isActive) | ||
| .map((service: ServiceSchema) => ({ | ||
| id: service.id || '', | ||
| name: service.name || '', | ||
| description: service.description || '', | ||
| category: service.category || '', | ||
| processingTime: service.processingTime || '', | ||
| fee: service.fee || 0, | ||
| requirements: service.requirements || [], | ||
| departmentId: dept.departmentId || '', | ||
| departmentName: dept.name || '' | ||
| })) | ||
| })); | ||
|
|
||
| // Flatten all services | ||
| const services: ServiceData[] = departmentData.flatMap(dept => dept.services || []); | ||
|
|
||
| // Generate suggestions using server-side logic | ||
| const suggestions = generateBookingSuggestions(query, departmentData, services); | ||
|
|
||
| console.log('📊 Generated suggestions:', suggestions.length); | ||
| console.log('🎯 Suggestions:', suggestions); | ||
| console.log('🏛️ Departments found:', departmentData.length); | ||
| console.log('📋 Services found:', services.length); | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| suggestions, | ||
| departments: departmentData, | ||
| services | ||
| }); | ||
|
|
||
| } catch (error) { | ||
| console.error('Error generating booking suggestions:', error); | ||
| const errorMessage = error instanceof Error ? error.message : 'Unknown error'; | ||
|
|
||
| return NextResponse.json( | ||
| { | ||
| error: 'Internal server error', | ||
| details: errorMessage | ||
| }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Server-side suggestion logic (similar to client-side but optimized for server) | ||
| */ | ||
| function generateBookingSuggestions( | ||
| query: string, | ||
| departments: DepartmentData[], | ||
| services: ServiceData[] | ||
| ): BookingSuggestion[] { | ||
| const normalizedQuery = query.toLowerCase(); | ||
| const suggestions: BookingSuggestion[] = []; | ||
|
|
||
| // Keywords mapping for better matching | ||
| const serviceKeywords: Record<string, string[]> = { | ||
| passport: ['passport', 'travel', 'immigration', 'visa', 'abroad', 'international'], | ||
| license: ['license', 'permit', 'driving', 'vehicle', 'motorcycle', 'car'], | ||
| certificate: ['certificate', 'birth', 'death', 'marriage', 'divorce', 'citizenship'], | ||
| registration: ['register', 'registration', 'business', 'company', 'organization'], | ||
| tax: ['tax', 'income', 'vat', 'customs', 'duty', 'revenue'], | ||
| education: ['education', 'school', 'university', 'degree', 'scholarship', 'student'], | ||
| health: ['health', 'medical', 'hospital', 'medicine', 'doctor', 'treatment'], | ||
| property: ['property', 'land', 'house', 'building', 'real estate', 'title'], | ||
| insurance: ['insurance', 'social security', 'pension', 'retirement', 'benefits'], | ||
| employment: ['employment', 'job', 'work', 'labor', 'salary', 'employee'] | ||
| }; | ||
|
|
||
| // Department keywords mapping | ||
| const departmentKeywords: Record<string, string[]> = { | ||
| immigration: ['immigration', 'passport', 'visa', 'travel', 'foreign'], | ||
| transport: ['transport', 'vehicle', 'driving', 'license', 'road'], | ||
| registrar: ['registrar', 'birth', 'death', 'marriage', 'certificate'], | ||
| revenue: ['revenue', 'tax', 'customs', 'duty', 'vat'], | ||
| education: ['education', 'ministry of education', 'school', 'university'], | ||
| health: ['health', 'ministry of health', 'medical', 'hospital'], | ||
| lands: ['lands', 'property', 'title', 'survey', 'real estate'] | ||
| }; | ||
|
|
||
| // Score services based on keyword matching | ||
| services.forEach(service => { | ||
| let relevanceScore = 0; | ||
|
|
||
| // Direct name matching | ||
| if (service.name.toLowerCase().includes(normalizedQuery)) { | ||
| relevanceScore += 10; | ||
| } | ||
|
|
||
| // Description matching | ||
| if (service.description && service.description.toLowerCase().includes(normalizedQuery)) { | ||
| relevanceScore += 7; | ||
| } | ||
|
|
||
| // Category matching | ||
| if (service.category && service.category.toLowerCase().includes(normalizedQuery)) { | ||
| relevanceScore += 5; | ||
| } | ||
|
|
||
| // Keyword matching | ||
| Object.entries(serviceKeywords).forEach(([category, keywords]) => { | ||
| keywords.forEach(keyword => { | ||
| if (normalizedQuery.includes(keyword)) { | ||
| if (service.name.toLowerCase().includes(category)) { | ||
| relevanceScore += 8; | ||
| } else if (service.description?.toLowerCase().includes(category)) { | ||
| relevanceScore += 6; | ||
| } else if (service.category?.toLowerCase().includes(category)) { | ||
| relevanceScore += 4; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| if (relevanceScore > 0) { | ||
| suggestions.push({ | ||
| type: 'service', | ||
| id: service.id, | ||
| name: service.name, | ||
| description: service.description, | ||
| departmentId: service.departmentId, | ||
| departmentName: service.departmentName, | ||
| category: service.category, | ||
| fee: service.fee, | ||
| relevanceScore | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // Score departments based on keyword matching | ||
| departments.forEach(dept => { | ||
| let relevanceScore = 0; | ||
|
|
||
| // Direct name matching | ||
| if (dept.name.toLowerCase().includes(normalizedQuery) || | ||
| dept.shortName.toLowerCase().includes(normalizedQuery)) { | ||
| relevanceScore += 8; | ||
| } | ||
|
|
||
| // Description matching | ||
| if (dept.description && dept.description.toLowerCase().includes(normalizedQuery)) { | ||
| relevanceScore += 6; | ||
| } | ||
|
|
||
| // Keyword matching for departments | ||
| Object.entries(departmentKeywords).forEach(([category, keywords]) => { | ||
| keywords.forEach(keyword => { | ||
| if (normalizedQuery.includes(keyword)) { | ||
| if (dept.name.toLowerCase().includes(category) || | ||
| dept.shortName.toLowerCase().includes(category)) { | ||
| relevanceScore += 7; | ||
| } else if (dept.description?.toLowerCase().includes(category)) { | ||
| relevanceScore += 5; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| if (relevanceScore > 0) { | ||
| suggestions.push({ | ||
| type: 'department', | ||
| id: dept.departmentId, | ||
| name: dept.name, | ||
| description: dept.description, | ||
| relevanceScore | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // Sort by relevance score and return top 5 | ||
| return suggestions | ||
| .sort((a, b) => b.relevanceScore - a.relevanceScore) | ||
| .slice(0, 5); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Service IDs may be empty; include _id fallback when mapping services
Many department.service items hold only _id (not id). Mapping only service.id risks empty IDs, breaking selection and URL pre-fill.
Apply this diff to include an _id fallback:
Optionally extend ServiceSchema to reflect _id:
interface ServiceSchema { + _id?: string; id: string; name: string; description?: string; category?: string; isActive?: boolean; processingTime?: string; fee?: number; requirements?: string[]; }🤖 Prompt for AI Agents