Skip to content

Commit

Permalink
Add exception handling in some places.
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh-br0 committed Aug 26, 2024
1 parent a5667b7 commit ec34dbb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion chatbot/database/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down
14 changes: 11 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ec34dbb

Please sign in to comment.