run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
35.36k stars 4.98k forks source link

[Bug]: Text2SQL generates right SQL but provides wrong answer. #9523

Closed danielstankw closed 5 months ago

danielstankw commented 9 months ago

Bug Description

When using an LLM I obtain correct SQL query for my question.

query_str = "What is the total amount for segment called : 'Pharma' for the first, second and third quarter of 2022? "
response = query_engine.query(query_str)

The response I get is:

2023-12-14 12:08:02,393 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2023-12-14 12:08:02,394 INFO sqlalchemy.engine.Engine SELECT SUM(Amount) FROM finance WHERE Segment = 'Pharma' AND Year = 2022 AND Quarter IN ('Q1', 'Q2', 'Q3'); 
### Answer: 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

If I take this query and run it using sql_database.run_sql(sql_query) I do obtained a correct answer.

2023-12-14 12:08:54,797 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2023-12-14 12:08:54,798 INFO sqlalchemy.engine.Engine SELECT SUM(Amount) FROM finance WHERE Segment = 'Pharma' AND Year = 2022 AND Quarter IN ('Q1', 'Q2', 'Q3');
2023-12-14 12:08:54,799 INFO sqlalchemy.engine.Engine [generated in 0.00039s] ()
2023-12-14 12:08:54,900 INFO sqlalchemy.engine.Engine COMMIT
('[(1980417814.2799988,)]',
 {'result': [(1980417814.2799988,)], 'col_keys': ['SUM(Amount)']})

Version

0.9.13

Steps to Reproduce

None

Relevant Logs/Tracbacks

No response

dosubot[bot] commented 9 months ago

πŸ€–

Hello @danielstankw! I'm Dosu, an automated assistant here to lend a hand while we wait for a human maintainer. I'm equipped to help you troubleshoot bugs, answer any questions you might have, and guide you on becoming a contributor. Let's figure this out together!

Based on the information you've provided, it seems like the query_engine.query() method is generating the correct SQL query, but the response it returns is incorrect. This could be due to how the method processes the response from the SQL query.

The query_engine.query() method in the LlamaIndex codebase is part of the RouterQueryEngine class. It selects one out of several candidate query engines to execute a query, executes the query, and returns a response. If multiple query engines are selected, it executes the query on each of them and collects the responses. If more than one response is received, it combines them using a summarizer. If only one response is received, it is used as the final response.

Here is the relevant code:

def _query(self, query_bundle: QueryBundle) -> RESPONSE_TYPE:
    with self.callback_manager.event(
        CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str}
    ) as query_event:
        result = self._selector.select(self._metadatas, query_bundle)

        if len(result.inds) > 1:
            responses = []
            for i, engine_ind in enumerate(result.inds):
                logger.info(
                    f"Selecting query engine {engine_ind}: " f"{result.reasons[i]}."
                )
                selected_query_engine = self._query_engines[engine_ind]
                responses.append(selected_query_engine.query(query_bundle))

            if len(responses) > 1:
                final_response = combine_responses(
                    self._summarizer, responses, query_bundle
                )
            else:
                final_response = responses[0]
        else:
            try:
                selected_query_engine = self._query_engines[result.ind]
                logger.info(
                    f"Selecting query engine {result.ind}: {result.reason}."
                )
            except ValueError as e:
                raise ValueError("Failed to select query engine") from e

            final_response = selected_query_engine.query(query_bundle)

        # add selected result
        final_response.metadata = final_response.metadata or {}
        final_response.metadata["selector_result"] = result

        query_event.on_end(payload={EventPayload.RESPONSE: final_response})

    return final_response

Source: llama_index/query_engine/router_query_engine.py

It's possible that the issue lies in how the query_engine.query() method processes the SQL query response, or in how it combines multiple responses if more than one query engine is selected.

To further investigate this issue, could you please provide more information about the query_engine.query() method and the RouterQueryEngine class? Specifically, it would be helpful to know how the method processes the SQL query response, and how it combines multiple responses if more than one query engine is selected. Additionally, could you please provide the code for the combine_responses() function and the summarizer object? This information will help us understand why the query_engine.query() method is returning an incorrect response despite generating the correct SQL query.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a πŸ‘ if this is helpful and πŸ‘Ž if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.