Skip to content

Commit

Permalink
Calculate confidence score when a response is created
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjc712 committed Oct 6, 2023
1 parent a32f4ea commit 0c262e7
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion dataherald/api/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,33 @@ def execute_sql_query(self, query: Query) -> tuple[str, dict]:
def create_response(
self, query_request: CreateResponseRequest # noqa: ARG002
) -> Response:
evaluator = self.system.instance(Evaluator)
question_repository = QuestionRepository(self.storage)
user_question = question_repository.find_by_id(query_request.question_id)
db_connection_repository = DatabaseConnectionRepository(self.storage)
database_connection = db_connection_repository.find_by_id(
user_question.db_connection_id
)
if not database_connection:
raise HTTPException(status_code=404, detail="Database connection not found")

response = Response(
question_id=query_request.question_id, sql_query=query_request.sql_query
)
response_repository = ResponseRepository(self.storage)
response_repository.insert(response)

start_generated_answer = time.time()
try:
generates_nl_answer = GeneratesNlAnswer(self.system, self.storage)
response = generates_nl_answer.execute(response)
confidence_score = evaluator.get_confidence_score(
user_question, response, database_connection
)
response.confidence_score = confidence_score
response.exec_time = time.time() - start_generated_answer
response_repository.update(response)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e)) from e
except SQLInjectionError as e:
raise HTTPException(status_code=404, detail=str(e)) from e
return response
Expand Down

0 comments on commit 0c262e7

Please sign in to comment.