Open tpanza opened 1 week ago
Hey there, @tpanza! I'm here to help you with any issues or questions you have while we wait for a human maintainer. Let's squash those bugs together! 🤖
To resolve the OperationalError: near "sql": syntax error
when using Azure OpenAI with the JSONalyze Query Engine from the Llama Index library, you need to ensure that the SQL query generated by the LLM is valid. The error typically indicates that the SQL syntax is incorrect.
Here are the steps to troubleshoot and resolve the issue:
Here is an example of how you can print the SQL query for debugging purposes:
try:
# Execute the SQL query
results = list(db.query(sql_query))
except sqlite_utils.db_exceptions.OperationalError as exc:
print_text(f"Error executing query: {sql_query}")
raise ValueError(f"Invalid query: {sql_query}") from exc
Additionally, you can adjust the prompt to provide more specific instructions to the LLM:
prompt = prompt or DEFAULT_JSONALYZE_PROMPT
response_str = await service_context.llm.apredict(
prompt=prompt,
table_name=table_name,
table_schema=table_schema,
question=query,
)
# Print the response string for debugging
print_text(f"Generated SQL Query: {response_str}")
sql_query = sql_parser.parse_response_to_sql(response_str, query_bundle)
By printing the generated SQL query, you can identify any syntax errors and adjust the prompt or the SQL parser accordingly.
The JSONalyzeQueryEngine
class in the Llama Index library provides methods to handle both synchronous and asynchronous query execution. Ensure you are using the correct method (_query
for synchronous or _aquery
for asynchronous) based on your implementation. Here is a snippet from the JSONalyzeQueryEngine
class that shows how the SQL query is generated and executed:
def _query(self, query_bundle: QueryBundle) -> Response:
query = query_bundle.query_str
if self._verbose:
print_text(f"Query: {query}\n", color="green")
# Perform the analysis
sql_query, table_schema, results = self._analyzer(
self._list_of_dict,
query_bundle,
self._service_context,
table_name=self._table_name,
prompt=self._jsonalyze_prompt,
sql_parser=self._sql_parser,
)
if self._verbose:
print_text(f"SQL Query: {sql_query}\n", color="blue")
print_text(f"Table Schema: {table_schema}\n", color="cyan")
print_text(f"SQL Response: {results}\n", color="yellow")
if self._synthesize_response:
response_str = self._service_context.llm.predict(
self._response_synthesis_prompt,
sql_query=sql_query,
table_schema=table_schema,
sql_response=results,
query_str=query_bundle.query_str,
)
if self._verbose:
print_text(f"Response: {response_str}", color="magenta")
else:
response_str = str(results)
response_metadata = {"sql_query": sql_query, "table_schema": str(table_schema)}
return Response(response=response_str, metadata=response_metadata)
By following these steps and utilizing the provided methods, you should be able to resolve the OperationalError
and ensure that the SQL queries generated by the LLM are valid and executable [1][2][3].
To continue talking to Dosu, mention @dosu.
Error executing query: sql
so the LLM wrote the query as sql
-- which is not a valid query or valid sql 😅
Error executing query: sql
so the LLM wrote the query assql
-- which is not a valid query or valid sql 😅
I'm using gpt-4-0125-Preview
, deployed in Azure, as the LLM.
Looks like the full query was:
sql
SELECT AVG(age) AS average_age FROM items;
So, somehow need to remove the wrongly inserted sql
from the beginning
Hi @logan-markewich, as the issue is happening for given GPT. I think as a solution, we can resolve the issue by modifying the given line response_synthesis_prompt: Optional[BasePromptTemplate] = None
in JSONalyzeQueryEngine
function with response_synthesis_prompt: Optional[Union[BasePromptTemplate,PromptTemplate]] = None
. As far as I can see, the given datatype will be handled properly and give correct result on modified prompts. Doing this is the best case as down the line if someone else encounters some other error on different LLM they can modify the prompt.
Or can create a separate argument to pass to the class for prompt modification as this will not require remembering to pass the text variables which are already mentioned in DEFAULT_JSONALYZE_PROMPT
Let me know if this seems logical, will create merge request
Bug Description
Tried following this example for the JSONalyze Query Engine (except for the LLM being Azure OpenAI), and
OperationalError: near "sql": syntax error
is raised.Version
0.10.38
Steps to Reproduce
Just run the code from the docs: https://docs.llamaindex.ai/en/latest/examples/query_engine/JSONalyze_query_engine/
My setup required setting the LLM to Azure OpenAI.
Python version 3.11.9.
Relevant Logs/Tracbacks