Spaces:
Runtime error
Runtime error
Commit
·
7bf5a3a
1
Parent(s):
89b5f01
fix
Browse files- db/mongoDB.py +3 -1
- dependencies.py +2 -2
- main.py +2 -4
- schemas/chat.py +1 -0
db/mongoDB.py
CHANGED
|
@@ -51,7 +51,7 @@ async def connect_to_mongo():
|
|
| 51 |
|
| 52 |
# 3. Gán các đối tượng database và collection
|
| 53 |
mongo_db.db = mongo_db.client[config.DB_NAME]
|
| 54 |
-
mongo_db.
|
| 55 |
mongo_db.token_blacklist = mongo_db.db["token_blacklist"]
|
| 56 |
mongo_db.conversations = mongo_db.db["conversations"]
|
| 57 |
|
|
@@ -63,7 +63,9 @@ async def connect_to_mongo():
|
|
| 63 |
await mongo_db.token_blacklist.create_index("expires_at", expireAfterSeconds=0)
|
| 64 |
logger.info("🔸 Successfully created TTL index for 'expires_at' in 'token_blacklist'.")
|
| 65 |
|
|
|
|
| 66 |
logger.info("✅ MongoDB connection successful and collections are ready.")
|
|
|
|
| 67 |
|
| 68 |
except ConnectionFailure as e:
|
| 69 |
logger.error(f"❌ Failed to connect to MongoDB: Connection Failure. Check your URI and network access rules in Atlas. Error: {e}", exc_info=True)
|
|
|
|
| 51 |
|
| 52 |
# 3. Gán các đối tượng database và collection
|
| 53 |
mongo_db.db = mongo_db.client[config.DB_NAME]
|
| 54 |
+
mongo_db.users = mongo_db.db["users"]
|
| 55 |
mongo_db.token_blacklist = mongo_db.db["token_blacklist"]
|
| 56 |
mongo_db.conversations = mongo_db.db["conversations"]
|
| 57 |
|
|
|
|
| 63 |
await mongo_db.token_blacklist.create_index("expires_at", expireAfterSeconds=0)
|
| 64 |
logger.info("🔸 Successfully created TTL index for 'expires_at' in 'token_blacklist'.")
|
| 65 |
|
| 66 |
+
|
| 67 |
logger.info("✅ MongoDB connection successful and collections are ready.")
|
| 68 |
+
return mongo_db
|
| 69 |
|
| 70 |
except ConnectionFailure as e:
|
| 71 |
logger.error(f"❌ Failed to connect to MongoDB: Connection Failure. Check your URI and network access rules in Atlas. Error: {e}", exc_info=True)
|
dependencies.py
CHANGED
|
@@ -70,8 +70,8 @@ async def initialize_api_components(app_state: AppState):
|
|
| 70 |
app_state.dict = load_legal_dictionary(config.LEGAL_DIC_FOLDER+ "/legal_terms.json")
|
| 71 |
app_state.weaviateDB = connect_to_weaviate(run_diagnostics=False)
|
| 72 |
# --- Kiểm tra kết nối tới MongoDB ---
|
| 73 |
-
if
|
| 74 |
-
logger.error("🔸Lỗi kết nối tới MongoDB hoặc Weaviate.")
|
| 75 |
raise HTTPException(status_code=500, detail="Lỗi kết nối tới database.")
|
| 76 |
|
| 77 |
app_state.google_api_key = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
| 70 |
app_state.dict = load_legal_dictionary(config.LEGAL_DIC_FOLDER+ "/legal_terms.json")
|
| 71 |
app_state.weaviateDB = connect_to_weaviate(run_diagnostics=False)
|
| 72 |
# --- Kiểm tra kết nối tới MongoDB ---
|
| 73 |
+
if mongo_db.users is None or app_state.weaviateDB is None:
|
| 74 |
+
logger.error("🔸Lỗi kết nối tới MongoDB hoặc Weaviate.", mongo_db.users)
|
| 75 |
raise HTTPException(status_code=500, detail="Lỗi kết nối tới database.")
|
| 76 |
|
| 77 |
app_state.google_api_key = os.environ.get("GOOGLE_API_KEY")
|
main.py
CHANGED
|
@@ -24,13 +24,11 @@ logger = logging.getLogger(__name__)
|
|
| 24 |
async def lifespan(app: FastAPI):
|
| 25 |
logger.info("✅ [Lifespan] STARTING UP...")
|
| 26 |
|
| 27 |
-
await connect_to_mongo()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
current_app_state_instance = AppState()
|
| 31 |
initialization_successful = False
|
| 32 |
try:
|
| 33 |
logger.info("✅ [Lifespan] Calling initialize_api_components...")
|
|
|
|
| 34 |
await initialize_api_components(current_app_state_instance)
|
| 35 |
app.state.app_state = current_app_state_instance
|
| 36 |
initialization_successful = True
|
|
@@ -99,6 +97,6 @@ if __name__ == "__main__":
|
|
| 99 |
host=config.API_HOST if hasattr(config, 'API_HOST') else "0.0.0.0",
|
| 100 |
port=int(config.API_PORT) if hasattr(config, 'API_PORT') else 5000,
|
| 101 |
reload=True, # reload=True chỉ nên dùng cho development
|
| 102 |
-
timeout_keep_alive=
|
| 103 |
log_level="info" # Hoặc "debug" nếu bạn muốn nhiều thông tin hơn
|
| 104 |
)
|
|
|
|
| 24 |
async def lifespan(app: FastAPI):
|
| 25 |
logger.info("✅ [Lifespan] STARTING UP...")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
current_app_state_instance = AppState()
|
| 28 |
initialization_successful = False
|
| 29 |
try:
|
| 30 |
logger.info("✅ [Lifespan] Calling initialize_api_components...")
|
| 31 |
+
await connect_to_mongo()
|
| 32 |
await initialize_api_components(current_app_state_instance)
|
| 33 |
app.state.app_state = current_app_state_instance
|
| 34 |
initialization_successful = True
|
|
|
|
| 97 |
host=config.API_HOST if hasattr(config, 'API_HOST') else "0.0.0.0",
|
| 98 |
port=int(config.API_PORT) if hasattr(config, 'API_PORT') else 5000,
|
| 99 |
reload=True, # reload=True chỉ nên dùng cho development
|
| 100 |
+
timeout_keep_alive=120, # Tăng thời gian giữ kết nối
|
| 101 |
log_level="info" # Hoặc "debug" nếu bạn muốn nhiều thông tin hơn
|
| 102 |
)
|
schemas/chat.py
CHANGED
|
@@ -14,6 +14,7 @@ class AppState(BaseModel):
|
|
| 14 |
redis: Optional[Any] = None
|
| 15 |
retriever: Optional[Any] = None
|
| 16 |
weaviateDB: Optional[Any] = None
|
|
|
|
| 17 |
reranker: Optional[Any] = None
|
| 18 |
|
| 19 |
class QueryRequest(BaseModel):
|
|
|
|
| 14 |
redis: Optional[Any] = None
|
| 15 |
retriever: Optional[Any] = None
|
| 16 |
weaviateDB: Optional[Any] = None
|
| 17 |
+
# mongoDB: Optional[Any] = None
|
| 18 |
reranker: Optional[Any] = None
|
| 19 |
|
| 20 |
class QueryRequest(BaseModel):
|