-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: Add RetrySqlQueryCreatorTool for handling failed SQL query generation #15
base: main
Are you sure you want to change the base?
feat: Add RetrySqlQueryCreatorTool for handling failed SQL query generation #15
Conversation
Reviewer's Guide by SourceryThis pull request introduces a new tool, RetrySqlQueryCreatorTool, designed to handle failed SQL query generation. Significant changes include the addition of this new tool, updates to existing methods to prioritize the retry tool, and enhancements to the SQL_QUERY_CREATOR_RETRY template to provide more detailed instructions for correcting SQL queries. File-Level Changes
Tips
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @sushantburnawal - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 7 issues found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
@@ -14,6 +14,7 @@ | |||
from langchain_core.tools import StateTool | |||
import re | |||
|
|||
ERROR = "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Consider removing the unused ERROR variable.
The variable ERROR
is defined but never used in the code. If it's not needed, it would be better to remove it to keep the code clean.
ERROR = "" | |
# Consider removing the unused ERROR variable. | |
# The variable `ERROR` is defined but never used in the code. | |
# If it's not needed, it would be better to remove it to keep the code clean. |
@@ -65,6 +66,7 @@ | |||
) | |||
executable_query = executable_query.strip('\"') | |||
executable_query = re.sub('\\n```', '',executable_query) | |||
self.db.run_no_throw(executable_query) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Duplicate call to self.db.run_no_throw(executable_query)
.
The method self.db.run_no_throw(executable_query)
is called twice consecutively. This seems redundant and could be removed.
@@ -75,14 +77,98 @@ | |||
raise NotImplementedError("QuerySparkSQLDataBaseTool does not support async") | |||
|
|||
def _extract_sql_query(self): | |||
for value in self.state: | |||
for value in reversed(self.state): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question (bug_risk): Reversing the state list might have unintended consequences.
Reversing the state list could lead to unexpected behavior if the order of states is important. Ensure that this change is intentional and won't cause issues.
) | ||
) | ||
sql_query = sql_query.replace("```","") | ||
sql_query = sql_query.replace("sql","") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Removing 'sql' from the query might cause issues.
The line sql_query = sql_query.replace("sql","")
removes all occurrences of 'sql' from the query. This might lead to incorrect SQL queries if 'sql' is part of a table or column name.
@@ -1,8 +1,20 @@ | |||
|
|||
|
|||
SQL_QUERY_CREATOR_RETRY = """ | |||
You have failed in the first attempt to generate correct sql query. Please try again to rewrite correct sql query. | |||
""" | |||
Your task is convert an incorrect query resulting from user question to a correct query which is databricks sql compatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick (typo): Typo in the prompt template.
The sentence should be 'Your task is to convert an incorrect query resulting from a user question to a correct query which is Databricks SQL compatible.'
|
||
sql_query = self._extract_sql_query() | ||
error_message = self._extract_error_message() | ||
if sql_query is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Consider logging when sql_query is None.
It might be useful to log a message when sql_query
is None to help with debugging and understanding why the tool is not meant to be run directly.
if sql_query is None: | |
if sql_query is None: | |
logging.warning("SQL query is None. This tool is not meant to be run directly.") | |
return "This tool is not meant to be run directly. Start with a SQLQueryCreatorTool" |
return input_string | ||
elif "tool='sql_db_query_creator'" in key: | ||
return input_string | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Consider raising an exception instead of returning None.
Returning None
might lead to silent failures. Consider raising an exception to make it clear that an error has occurred.
return None | |
raise ValueError("No valid key found in input string") |
if "tool='sql_db_query'" in key: | ||
if "Error" in input_string: | ||
return input_string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Merge nested if conditions (merge-nested-ifs
)
if "tool='sql_db_query'" in key: | |
if "Error" in input_string: | |
return input_string | |
if "tool='sql_db_query'" in key and "Error" in input_string: | |
return input_string | |
Explanation
Too much nesting can make code difficult to understand, and this is especiallytrue in Python, where there are no brackets to help out with the delineation of
different nesting levels.
Reading deeply nested code is confusing, since you have to keep track of which
conditions relate to which levels. We therefore strive to reduce nesting where
possible, and the situation where two if
conditions can be combined using
and
is an easy win.
if "tool='retry_sql_db_query_creator'" in key: | ||
return input_string | ||
elif "tool='sql_db_query_creator'" in key: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): We've found these issues:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Remove redundant conditional (
remove-redundant-if
)
if "tool='retry_sql_db_query_creator'" in key: | |
return input_string | |
elif "tool='sql_db_query_creator'" in key: | |
if ( | |
"tool='retry_sql_db_query_creator'" in key | |
or "tool='sql_db_query_creator'" in key | |
): |
if "tool='retry_sql_db_query_creator'" in key: | ||
return input_string | ||
elif "tool='sql_db_query_creator'" in key: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): We've found these issues:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Remove redundant conditional (
remove-redundant-if
)
if "tool='retry_sql_db_query_creator'" in key: | |
return input_string | |
elif "tool='sql_db_query_creator'" in key: | |
if ( | |
"tool='retry_sql_db_query_creator'" in key | |
or "tool='sql_db_query_creator'" in key | |
): |
Add RetrySqlQueryCreatorTool for handling failed SQL query generation
Thank you for contributing to LangChain!
If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17.
Summary by Sourcery
This pull request adds a new tool, RetrySqlQueryCreatorTool, to handle failed SQL query generation by retrying the creation process. It also updates the existing SQL query creation workflow to integrate this new tool and enhances the prompt used for retrying SQL queries.