fix: implement automatic database path detection for multi-environment compatibility

- Add environment auto-detection in ChatDatabase class
- Support both local development and Docker container paths
- Local development: uses 'backend/chat_data.db' (relative path)
- Docker containers: uses '/app/backend/chat_data.db' (absolute path)
- Maintain backward compatibility with explicit path overrides
- Update RAG API server to use auto-detection

This resolves the SQLite database connection error that occurred
when running LocalGPT in local development environments while
maintaining compatibility with Docker deployments.

Fixes: Database path hardcoded to Docker container path
Tested: Local development and Docker environment detection
Breaking: No breaking changes - maintains backward compatibility
This commit is contained in:
PromptEngineer
2025-07-17 22:13:25 -07:00
parent a3402f4274
commit 35697b23a4
2 changed files with 12 additions and 3 deletions

View File

@@ -5,8 +5,16 @@ from datetime import datetime
from typing import List, Dict, Optional, Tuple
class ChatDatabase:
def __init__(self, db_path: str = "/app/backend/chat_data.db"):
self.db_path = db_path
def __init__(self, db_path: str = None):
if db_path is None:
# Auto-detect environment and set appropriate path
import os
if os.path.exists("/app"): # Docker environment
self.db_path = "/app/backend/chat_data.db"
else: # Local development environment
self.db_path = "backend/chat_data.db"
else:
self.db_path = db_path
self.init_database()
def init_database(self):

View File

@@ -17,7 +17,8 @@ from rag_system.main import get_agent
from rag_system.factory import get_indexing_pipeline
# Initialize database connection once at module level
db = ChatDatabase("backend/chat_data.db")
# Use auto-detection for environment-appropriate path
db = ChatDatabase()
# Get the desired agent mode from environment variables, defaulting to 'default'
# This allows us to easily switch between 'default', 'fast', 'react', etc.