Skip to content

Commit

Permalink
Dh 4953/fix five hundered error for question endpoint (#238)
Browse files Browse the repository at this point in the history
* DH-4953/fixing the question 500 error

* DH-4953/raising 400 when API key is invalid
  • Loading branch information
MohammadrezaPourreza authored Nov 6, 2023
1 parent 24a0ef1 commit 45e77c7
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions dataherald/api/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
from typing import List

import openai
from bson import json_util
from bson.objectid import InvalidId, ObjectId
from fastapi import BackgroundTasks, HTTPException
Expand Down Expand Up @@ -146,9 +147,9 @@ def answer_question(
"error_message": "Connections doesn't exist",
},
)
context = context_store.retrieve_context_for_question(user_question)
start_generated_answer = time.time()
try:
context = context_store.retrieve_context_for_question(user_question)
start_generated_answer = time.time()
generated_answer = sql_generation.generate_response(
user_question, database_connection, context[0]
)
Expand Down Expand Up @@ -368,6 +369,8 @@ def update_response(self, response_id: str) -> Response:
response = generates_nl_answer.execute(response)
response.exec_time = time.time() - start_generated_answer
response_repository.update(response)
except openai.error.AuthenticationError as e:
raise HTTPException(status_code=400, detail=str(e)) from e
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e)) from e
except SQLInjectionError as e:
Expand Down Expand Up @@ -454,6 +457,8 @@ def create_response(
response.confidence_score = confidence_score
response.exec_time = time.time() - start_generated_answer
response_repository.update(response)
except openai.error.AuthenticationError as e:
raise HTTPException(status_code=400, detail=str(e)) from e
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e)) from e
except SQLInjectionError as e:
Expand Down

0 comments on commit 45e77c7

Please sign in to comment.