----------------------
LlamaIndex provides a way to execute multiple sub-queries using the query engine. This is especially useful for compare/contrast queries across documents as well as queries pertaining to a specific document.
For example, if you have a SQL database and you want to use natural language queries that are synthesized into SQL queries, you can use the
NLSQLTableQueryEngine
. You need to specify the tables you want to use with this query engine. If you don't, the query engine will pull all the schema context, which could overflow the context window of the LLM.
Here is an example of how you can use it:
from llama_index.indices.struct_store import NLSQLTableQueryEngine
query_engine = NLSQLTableQueryEngine(
sql_database=sql_database,
tables=["city_stats"],
)
query_str = "Which city has the highest population?"
response = query_engine.query(query_str)
This query engine should be used in any case where you can specify the tables you want to query over beforehand, or the total size of all the table schema plus the rest of the prompt fits your context window.
For more complex queries, you can use the
SQLJoinQueryEngine
which can handle natural language queries that require joining multiple tables in a SQL database.
For more information, you can refer to the
LlamaIndex documentation () and
examples ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------