SQL Query Engine#

Default query for SQLStructStoreIndex.

class llama_index.indices.struct_store.sql_query.BaseSQLTableQueryEngine(synthesize_response: bool = True, response_synthesis_prompt: Optional[BasePromptTemplate] = None, service_context: Optional[ServiceContext] = None, verbose: bool = False, **kwargs: Any)#
as_query_component(partial: Optional[Dict[str, Any]] = None, **kwargs: Any) QueryComponent#

Get query component.

get_prompts() Dict[str, BasePromptTemplate]#

Get a prompt.

property service_context: ServiceContext#

Get service context.

abstract property sql_retriever: NLSQLRetriever#

Get SQL retriever.

update_prompts(prompts_dict: Dict[str, BasePromptTemplate]) None#

Update prompts.

Other prompts will remain in place.

llama_index.indices.struct_store.sql_query.GPTNLStructStoreQueryEngine#

alias of NLStructStoreQueryEngine

llama_index.indices.struct_store.sql_query.GPTSQLStructStoreQueryEngine#

alias of SQLStructStoreQueryEngine

class llama_index.indices.struct_store.sql_query.NLSQLTableQueryEngine(sql_database: SQLDatabase, text_to_sql_prompt: Optional[BasePromptTemplate] = None, context_query_kwargs: Optional[dict] = None, synthesize_response: bool = True, response_synthesis_prompt: Optional[BasePromptTemplate] = None, tables: Optional[Union[List[str], List[Table]]] = None, service_context: Optional[ServiceContext] = None, context_str_prefix: Optional[str] = None, sql_only: bool = False, verbose: bool = False, **kwargs: Any)#

Natural language SQL Table query engine.

Read NLStructStoreQueryEngine’s docstring for more info on NL SQL.

as_query_component(partial: Optional[Dict[str, Any]] = None, **kwargs: Any) QueryComponent#

Get query component.

get_prompts() Dict[str, BasePromptTemplate]#

Get a prompt.

property service_context: ServiceContext#

Get service context.

property sql_retriever: NLSQLRetriever#

Get SQL retriever.

update_prompts(prompts_dict: Dict[str, BasePromptTemplate]) None#

Update prompts.

Other prompts will remain in place.

class llama_index.indices.struct_store.sql_query.NLStructStoreQueryEngine(index: SQLStructStoreIndex, text_to_sql_prompt: Optional[BasePromptTemplate] = None, context_query_kwargs: Optional[dict] = None, synthesize_response: bool = True, response_synthesis_prompt: Optional[BasePromptTemplate] = None, sql_only: bool = False, **kwargs: Any)#

GPT natural language query engine over a structured database.

NOTE: deprecated in favor of SQLTableRetriever, kept for backward compatibility.

Given a natural language query, we will extract the query to SQL. Runs raw SQL over a SQLStructStoreIndex. No LLM calls are made during the SQL execution.

NOTE: this query cannot work with composed indices - if the index contains subindices, those subindices will not be queried.

Parameters
  • index (SQLStructStoreIndex) – A SQL Struct Store Index

  • text_to_sql_prompt (Optional[BasePromptTemplate]) – A Text to SQL BasePromptTemplate to use for the query. Defaults to DEFAULT_TEXT_TO_SQL_PROMPT.

  • context_query_kwargs (Optional[dict]) – Keyword arguments for the context query. Defaults to {}.

  • synthesize_response (bool) – Whether to synthesize a response from the query results. Defaults to True.

  • sql_only (bool) – Whether to get only sql and not the sql query result. Default to False.

  • response_synthesis_prompt (Optional[BasePromptTemplate]) – A Response Synthesis BasePromptTemplate to use for the query. Defaults to DEFAULT_RESPONSE_SYNTHESIS_PROMPT.

as_query_component(partial: Optional[Dict[str, Any]] = None, **kwargs: Any) QueryComponent#

Get query component.

get_prompts() Dict[str, BasePromptTemplate]#

Get a prompt.

property service_context: ServiceContext#

Get service context.

update_prompts(prompts_dict: Dict[str, BasePromptTemplate]) None#

Update prompts.

Other prompts will remain in place.

class llama_index.indices.struct_store.sql_query.PGVectorSQLQueryEngine(sql_database: SQLDatabase, text_to_sql_prompt: Optional[BasePromptTemplate] = None, context_query_kwargs: Optional[dict] = None, synthesize_response: bool = True, response_synthesis_prompt: Optional[BasePromptTemplate] = None, tables: Optional[Union[List[str], List[Table]]] = None, service_context: Optional[ServiceContext] = None, context_str_prefix: Optional[str] = None, sql_only: bool = False, **kwargs: Any)#

PGvector SQL query engine.

A modified version of the normal text-to-SQL query engine because we can infer embedding vectors in the sql query.

NOTE: this is a beta feature

as_query_component(partial: Optional[Dict[str, Any]] = None, **kwargs: Any) QueryComponent#

Get query component.

get_prompts() Dict[str, BasePromptTemplate]#

Get a prompt.

property service_context: ServiceContext#

Get service context.

property sql_retriever: NLSQLRetriever#

Get SQL retriever.

update_prompts(prompts_dict: Dict[str, BasePromptTemplate]) None#

Update prompts.

Other prompts will remain in place.

class llama_index.indices.struct_store.sql_query.SQLStructStoreQueryEngine(index: SQLStructStoreIndex, sql_context_container: Optional[SQLContextContainerBuilder] = None, sql_only: bool = False, **kwargs: Any)#

GPT SQL query engine over a structured database.

NOTE: deprecated in favor of SQLTableRetriever, kept for backward compatibility.

Runs raw SQL over a SQLStructStoreIndex. No LLM calls are made here. NOTE: this query cannot work with composed indices - if the index contains subindices, those subindices will not be queried.

as_query_component(partial: Optional[Dict[str, Any]] = None, **kwargs: Any) QueryComponent#

Get query component.

get_prompts() Dict[str, BasePromptTemplate]#

Get a prompt.

update_prompts(prompts_dict: Dict[str, BasePromptTemplate]) None#

Update prompts.

Other prompts will remain in place.

class llama_index.indices.struct_store.sql_query.SQLTableRetrieverQueryEngine(sql_database: SQLDatabase, table_retriever: ObjectRetriever[SQLTableSchema], text_to_sql_prompt: Optional[BasePromptTemplate] = None, context_query_kwargs: Optional[dict] = None, synthesize_response: bool = True, response_synthesis_prompt: Optional[BasePromptTemplate] = None, service_context: Optional[ServiceContext] = None, context_str_prefix: Optional[str] = None, sql_only: bool = False, **kwargs: Any)#

SQL Table retriever query engine.

as_query_component(partial: Optional[Dict[str, Any]] = None, **kwargs: Any) QueryComponent#

Get query component.

get_prompts() Dict[str, BasePromptTemplate]#

Get a prompt.

property service_context: ServiceContext#

Get service context.

property sql_retriever: NLSQLRetriever#

Get SQL retriever.

update_prompts(prompts_dict: Dict[str, BasePromptTemplate]) None#

Update prompts.

Other prompts will remain in place.