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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user