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

@@ -170,8 +170,18 @@ export async function POST(request: Request) {
async function generateBiblicalResponse(message: string, locale: string, history: any[]): Promise<string> {
try {
// Search for relevant Bible verses using vector search with language filtering
const relevantVerses = await searchBibleHybrid(message, locale, 5)
// Temporarily bypass vector search to test Azure OpenAI
console.log('Chat API - Starting biblical response generation for:', message.substring(0, 50))
let relevantVerses: any[] = []
try {
// Search for relevant Bible verses using vector search with language filtering
relevantVerses = await searchBibleHybrid(message, locale, 5)
console.log('Chat API - Vector search successful, found', relevantVerses.length, 'verses')
} catch (vectorError) {
console.warn('Chat API - Vector search failed:', vectorError instanceof Error ? vectorError.message : String(vectorError))
// Continue without verses - test if Azure OpenAI works alone
}
// Create context from relevant verses
const versesContext = relevantVerses
@@ -221,6 +231,9 @@ Current question: ${message}`
const systemPrompt = systemPrompts[locale as keyof typeof systemPrompts] || systemPrompts.en
// Call Azure OpenAI
console.log('Chat API - Calling Azure OpenAI with endpoint:', process.env.AZURE_OPENAI_ENDPOINT)
console.log('Chat API - Using deployment:', process.env.AZURE_OPENAI_DEPLOYMENT)
const response = await fetch(
`${process.env.AZURE_OPENAI_ENDPOINT}/openai/deployments/${process.env.AZURE_OPENAI_DEPLOYMENT}/chat/completions?api-version=${process.env.AZURE_OPENAI_API_VERSION}`,
{
@@ -247,12 +260,33 @@ Current question: ${message}`
}
)
console.log('Chat API - Azure OpenAI response status:', response.status)
if (!response.ok) {
throw new Error(`Azure OpenAI API error: ${response.status}`)
}
const data = await response.json()
return data.choices[0].message.content
// Handle content filtering or empty responses
if (!data.choices || data.choices.length === 0) {
throw new Error('No response choices returned from Azure OpenAI')
}
const choice = data.choices[0]
// Check for content filtering
if (choice.finish_reason === 'content_filter') {
console.warn('Content was filtered by Azure OpenAI:', choice.content_filter_results)
throw new Error('Content was filtered by Azure OpenAI content policy')
}
// Check if message content exists
if (!choice.message || !choice.message.content) {
throw new Error('Empty response content from Azure OpenAI')
}
return choice.message.content
} catch (error) {
console.error('Error calling Azure OpenAI:', error)

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 })
}
}