Skip to content

Commit 35697b2

Browse files
committed
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
1 parent a3402f4 commit 35697b2

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

backend/database.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55
from typing import List, Dict, Optional, Tuple
66

77
class ChatDatabase:
8-
def __init__(self, db_path: str = "/app/backend/chat_data.db"):
9-
self.db_path = db_path
8+
def __init__(self, db_path: str = None):
9+
if db_path is None:
10+
# Auto-detect environment and set appropriate path
11+
import os
12+
if os.path.exists("/app"): # Docker environment
13+
self.db_path = "/app/backend/chat_data.db"
14+
else: # Local development environment
15+
self.db_path = "backend/chat_data.db"
16+
else:
17+
self.db_path = db_path
1018
self.init_database()
1119

1220
def init_database(self):

rag_system/api_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from rag_system.factory import get_indexing_pipeline
1818

1919
# Initialize database connection once at module level
20-
db = ChatDatabase("backend/chat_data.db")
20+
# Use auto-detection for environment-appropriate path
21+
db = ChatDatabase()
2122

2223
# Get the desired agent mode from environment variables, defaulting to 'default'
2324
# This allows us to easily switch between 'default', 'fast', 'react', etc.

0 commit comments

Comments
 (0)