diff --git a/CHANGES.md b/CHANGES.md index 3edb224..a994e84 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,3 +6,5 @@ | | `chatbot/defaults.py`:`L19` | Updated the constant from 1000 to 400 | | | `chatbot/database/vector_store.py`:`L29` | Imported and used the `K` param constant introduced previously. | | Adding support for multiple files | `main.py`:`L35-69` | Replaced logic to handle multiple files instead of single one. | +| Exception Handling | `chatbot/database/mongo.py`:`L10-17` | Handled all connection related errors by single base exception provided by MongoDB. | +| | `main.py`:`L92-99` | Added a base exception handler for FastAPI so it can report any issue on logs. | \ No newline at end of file diff --git a/README.md b/README.md index 60eadfd..0cfd1f2 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,11 @@ - Head over to `http://localhost:8080/docs` ### Models Used -- sentence-transformers/all-mpnet-base-v2 +- `sentence-transformers/all-mpnet-base-v2` It is used for embedding vectors and this will run locally. Initally it will download the model into `models` directory. -- TinyLlama/TinyLlama-1.1B-Chat-v1.0 +- `TinyLlama/TinyLlama-1.1B-Chat-v1.0` It is the main LLM and being used through the HuggingFace Hub Inference API. diff --git a/chatbot/database/mongo.py b/chatbot/database/mongo.py index a0638bb..3652cf5 100644 --- a/chatbot/database/mongo.py +++ b/chatbot/database/mongo.py @@ -2,10 +2,19 @@ from pymongo import MongoClient from pymongo.collection import Collection +from pymongo.errors import ConnectionFailure from ..defaults import DB_NAME -client = MongoClient(os.environ["MONGO_URL"]) + +def create_client(): + try: + return MongoClient(os.environ["MONGO_URL"]) + except ConnectionFailure: + exit("Connection Failed to Mongodb.") + + +client = create_client() db = client[DB_NAME] diff --git a/main.py b/main.py index 359f5e6..8afaeb0 100644 --- a/main.py +++ b/main.py @@ -53,8 +53,8 @@ def post_process_files(paths: list[str], mimes: list[Optional[str]]): for path, mime in zip(paths, mimes): try: add_document(path, mime) - except Exception as e: - log.exception(e) + except Exception: + log.exception("Error while post processing files..") finally: os.remove(path) @@ -85,10 +85,18 @@ def ask_question_route(session_id: str, question: str): @app.exception_handler(ChatBotException) -async def unicorn_exception_handler(request: Request, exc: ChatBotException): +async def chatbot_exception_handler(request: Request, exc: ChatBotException): raise HTTPException(status.HTTP_406_NOT_ACCEPTABLE, str(exc)) +@app.exception_handler(Exception) +async def base_exception_handler(request: Request, exc: Exception): + log.exception("FastAPI base exception handler..") + raise HTTPException( + status.HTTP_500_INTERNAL_SERVER_ERROR, "Server Faced some issue, kindly check logs..." + ) + + if __name__ == "__main__": import uvicorn