-
Notifications
You must be signed in to change notification settings - Fork 21
π¬ ask_db
The ask_db(...)
method serves as a convenient interface for querying the database using natural language. Users can pose questions, and the ask method, under the hood, retrieves pertinent contexts from the vector store. Subsequently, it constructs a prompt that is fed into the Large Language Model (LLM). The LLM then generates an SQL query that precisely addresses the user's question.
Furthermore, the ask_db()
method offers a comprehensive set of outputs, including:
-
Generated SQL Query(
sql
): The SQL Query generated by the LLM based on user's question. -
DataFrame Result (
sql_result
): After executing the generated SQL query, the method returns the corresponding DataFrame result. This DataFrame encapsulates the data retrieved from the database, facilitating further analysis and manipulation. -
Natural Language Response (
response
): The LLM crafts a natural language response based on the retrieved DataFrame, enhancing the user experience by providing human-readable insights derived from the database. -
Charts or Visualizations (
chart
): Additionally, the ask_db() method has the capability to generate and present charts or visualizations based on the data retrieved. This feature enhances the interpretability of the results, making it easier for users to comprehend and derive meaningful insights.
π Retrieving Data and SQL Query:
Query the system with a specific question to retrieve both the SQL query generated by the system and the resulting data.
result = ask_db(question="What is the average salary of employees?", connection=my_connection, table_names=["employees"], visualize=False)
π Visualizing Data:
Query the system for data and visualize the results using plotly charts.
result = ask_db(question="Show monthly sales trend for the past year.", connection=my_connection, visualize=True)
chart = result["chart"]
βοΈ Parameters:
- question (str): Specifies the question or query to be submitted to the system for processing.
- connection (any): The connection object to the database, required for executing SQL queries.
- table_names (list): Optional parameter specifying the list of tables relevant to the query.
- visualize (bool): Indicates whether to visualize the results using plotly charts.
π Notes:
- The
ask_db(...)
method serves as the primary interface for querying the system and obtaining insights from the underlying data. - Users must provide a valid database connection object for executing SQL queries.
- The table_names parameter is optional and can be used to specify relevant tables if needed. If table_names if provided, MindSQL will not retrieve the relevant tables from the vectorstore and use the table names provided to get relevant ddl.
- By setting the visualize parameter to True, users can request visualization of the query results using plotly charts.