Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dh 4953/fix five hundered error for question endpoint #238

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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