Enhance RAG system to support multiple vector databases and improve AI chat functionality

- Update vector-search.ts to query all available vector tables per language instead of single table
- Add getAllVectorTables() function to discover all language-specific vector tables
- Enhance searchBibleHybrid() to query multiple tables and merge results by relevance score
- Enhance searchBibleSemantic() to combine results from all available vector databases
- Add comprehensive error handling and logging for vector search operations
- Improve Azure OpenAI content filtering detection and error handling
- Add test-vector API endpoint for database diagnostics and debugging
- Fix environment configuration with complete Azure OpenAI settings
- Enable multi-translation biblical context from diverse Bible versions simultaneously

Tested: Romanian chat works excellently with rich biblical context and verse citations
Issue: English requires vector table creation - 47 English Bible versions exist but no vector tables

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-25 07:21:59 +00:00
parent 3ae9733805
commit 2d27eae756
3 changed files with 224 additions and 85 deletions

View File

@@ -0,0 +1,81 @@
import { NextResponse } from 'next/server'
import { Pool } from 'pg'
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
})
export async function GET() {
try {
console.log('Test Vector - Starting database connection test')
const client = await pool.connect()
try {
// Test basic connection
const testQuery = await client.query('SELECT NOW() as current_time')
console.log('Test Vector - Database connection successful:', testQuery.rows[0])
// Check if ai_bible schema exists
const schemaCheck = await client.query(`
SELECT EXISTS (
SELECT 1 FROM information_schema.schemata
WHERE schema_name = 'ai_bible'
) AS exists
`)
console.log('Test Vector - ai_bible schema exists:', schemaCheck.rows[0].exists)
// List all vector tables
const VECTOR_SCHEMA = process.env.VECTOR_SCHEMA || 'ai_bible'
const tables = await client.query(`
SELECT table_name FROM information_schema.tables
WHERE table_schema = $1 AND table_name LIKE 'bv_%'
ORDER BY table_name
`, [VECTOR_SCHEMA])
console.log('Test Vector - Found vector tables:', tables.rows.length)
tables.rows.forEach(row => console.log('- ' + row.table_name))
// Check for English tables specifically
const englishTables = await client.query(`
SELECT table_name FROM information_schema.tables
WHERE table_schema = $1 AND table_name LIKE 'bv_en_%'
ORDER BY table_name
`, [VECTOR_SCHEMA])
console.log('Test Vector - English tables found:', englishTables.rows.length)
// Check BibleVersion table for available versions
const versions = await client.query(`
SELECT language, abbreviation, "isDefault", name
FROM "BibleVersion"
WHERE language IN ('en', 'ro')
ORDER BY language, "isDefault" DESC, "createdAt" ASC
`)
console.log('Test Vector - Bible versions available:')
versions.rows.forEach(v => console.log(`- ${v.language}: ${v.abbreviation} (${v.name}) - default: ${v.isDefault}`))
return NextResponse.json({
success: true,
database_connected: true,
ai_bible_schema_exists: schemaCheck.rows[0].exists,
total_vector_tables: tables.rows.length,
english_vector_tables: englishTables.rows.length,
vector_tables: tables.rows.map(r => r.table_name),
english_tables: englishTables.rows.map(r => r.table_name),
bible_versions: versions.rows,
current_time: testQuery.rows[0].current_time
})
} finally {
client.release()
}
} catch (error) {
console.error('Test Vector - Database connection failed:', error)
return NextResponse.json({
success: false,
error: error instanceof Error ? error.message : String(error),
database_connected: false
}, { status: 500 })
}
}