SQLTableRetrieverQueryEngine
, you need to define the LLM (Language Model) with the 'gpt-4' model and pass it to the ServiceContext
. Here is an example of how you can do it:from llama_index import ( SQLTableRetrieverQueryEngine, LLMPredictor, ServiceContext ) from llama_index.llms import OpenAI # define LLM llm = OpenAI(temperature=0, model="gpt-4") service_context = ServiceContext.from_defaults(llm=llm) # construct the SQLTableRetrieverQueryEngine query_engine = SQLTableRetrieverQueryEngine( sql_database, object_index.as_retriever(similarity_top_k=3), service_context=service_context )
OpenAI(temperature=0, model="gpt-4")
is used to define the LLM with the 'gpt-4' model. This LLM is then passed to the ServiceContext
. The ServiceContext
is then passed as an argument to the SQLTableRetrieverQueryEngine
.sql_database
and object_index
with your actual SQL database and object index.Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:from llama_index import set_global_service_context set_global_service_context(service_context)
text_to_sql_prompt
and response_synthesis_prompt
parameters when creating an instance of SQLTableRetrieverQueryEngine
. from llama_index import Prompt, SQLTableRetrieverQueryEngine # Define a custom prompt template = ( "We have provided context information below. \n" "---------------------\n" "{context_str}" "\n---------------------\n" "Given this information, please answer the question: {query_str}\n" ) custom_prompt = Prompt(template) # Construct the SQLTableRetrieverQueryEngine with the custom prompt query_engine = SQLTableRetrieverQueryEngine( sql_database, table_retriever, text_to_sql_prompt=custom_prompt, response_synthesis_prompt=custom_prompt )
Prompt(template)
is used to define a custom prompt. This custom prompt is then passed to the text_to_sql_prompt
and response_synthesis_prompt
parameters of SQLTableRetrieverQueryEngine
.sql_database
and table_retriever
with your actual SQL database and table retriever.Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:text_to_sql_prompt
and response_synthesis_prompt
are both parameters of the SQL Query Engine in LlamaIndex, but they serve different purposes:text_to_sql_prompt
: This is a prompt used to convert a natural language query into a SQL query. It's used when the SQL Query Engine needs to translate the user's question (expressed in natural language) into a SQL query that can be executed against the database.response_synthesis_prompt
: This is a prompt used to synthesize the final response from the results of the SQL query. After the SQL query has been executed and the results have been retrieved, this prompt is used to format those results into a natural language response that can be returned to the user.Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:response_synthesis_prompt
as mentiond by kapa.ai to customize my output from the SQL Query Engine?