Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 36 additions & 0 deletions continuous_eval/metrics/code/sql/sql_deterministic_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List, Union

import sqlparse

from continuous_eval.metrics.base import Metric


class SQLSyntaxMatch(Metric):
"""
This metric evaluates the syntactic similarity between the generated SQL query and a set of ground truth queries.
It uses the sqlparse library to format and compare the SQL queries.
"""

def __call__(self, answer: str, ground_truth_answers: Union[List[str], str]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if isinstance(ground_truth_answers, str):
ground_truth_answers = [ground_truth_answers]

# Format the answer and ground truth answers using sqlparse for consistent comparison
formatted_answer = sqlparse.format(answer, reindent=True, keyword_case="upper")
formatted_ground_truths = [
sqlparse.format(gt, reindent=True, keyword_case="upper")
for gt in ground_truth_answers
]

# Initialize the maximum match score
max_match_score = 0

# Compare the formatted answer with each formatted ground truth answer
for formatted_gt in formatted_ground_truths:
# Simple string comparison for now, can be improved with more sophisticated methods
match_score = float(formatted_answer == formatted_gt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider implementing a more sophisticated comparison method than simple string equality to handle cases where SQL queries might be functionally identical but differ in formatting or syntax. This could improve the robustness of the syntactic similarity evaluation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ellipsis-dev come up with a few more sophisticated ways to handle functionally identical cases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yisz, I have addressed your comments in pull request #60


You can configure Ellipsis to address comments with a direct commit or a side PR, see docs.

if match_score > max_match_score:
max_match_score = match_score

# Return the maximum match score
return {"SQL_Syntax_Match_Score": max_match_score}
Loading