-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Advanced text to sql sample rows #17479
base: main
Are you sure you want to change the base?
Advanced text to sql sample rows #17479
Conversation
@@ -627,6 +627,177 @@ def sql_retriever(self) -> NLSQLRetriever: | |||
return self._sql_retriever | |||
|
|||
|
|||
class NLSQLRetrieverWithSampleRows(NLSQLRetriever): |
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.
ooc, does this actually require a brand new class? We can't just use the existing class and add an arg for the retriever?
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.
Okay, in a nutshell, we can, but the issue is that both NLSQLTableQueryEngine
and SQLTableRetrieverQueryEngine
use the NLSQLRetriever
with its defaults arguments.
For example:
self._sql_retriever = NLSQLRetriever(
sql_database,
llm=llm,
text_to_sql_prompt=text_to_sql_prompt,
context_query_kwargs=context_query_kwargs,
tables=tables,
sql_parser_mode=SQLParserMode.PGVECTOR,
context_str_prefix=context_str_prefix,
sql_only=sql_only,
callback_manager=callback_manager,
So in order to make the distinction clear, I opted for a separate class.
If we are to go with the argument option, we'll probably still need the query engine class that internally instantiate a NLSQLRetriever
with the sample rows argument used.
What do you think?
Add NLSQLRetrieverWithSampleRows and NLSQLTableQueryEngineWithSampleRows to sql_query.py
Description
This is a very minor addition to the
sql_query.py
module, adding two classesNLSQLRetrieverWithSampleRows
andNLSQLTableQueryEngineWithSampleRows
which inherit fromNLSQLRetriever
andBaseSQLTableQueryEngine
respectively. This addition is based on the great tutorial by Jerry Liu which then was updated to Workflows in this notebook. The motivation is that, in both the tutorial and the notebook, there is an implementation to add sample rows inside the context to improve the response of the LLM, by giving it a taste of the content of each retrieved table, which makes the SQL more resilient against slight variations in the values to be queried using SQL, for example, which value to use in a "where" clause under a Leave Type column, is it "Business Trip", "BUSINESS", or "TRIP" or who knows?! Giving a sample of the rows inside the context improves the response of the LLM in that regard. The implementation in the tutorial is done using Workflows or QueryPipeline, where most of it (missing only the sample rows) is already implemented in theNLSQLTableQueryEngine
class, so why not add the sample rows to it?!Fixes # Missing adding sample rows to the context in the
NLSQLTableQueryEngine
classNew Package?
Did I fill in the
tool.llamahub
section in thepyproject.toml
and provide a detailed README.md for my new integration or package?Version Bump?
Did I bump the version in the
pyproject.toml
file of the package I am updating? (Except for thellama-index-core
package)Type of Change
Please delete options that are not relevant.
How Has This Been Tested?
Your pull-request will likely not be merged unless it is covered by some form of impactful unit testing.
Suggested Checklist:
make format; make lint
to appease the lint gods