-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directSqlPrompt): use connector directly if flag is set (#731)
* feat: config plot libraries (#705) * In this commit, I introduced a new configuration parameter in our application settings that allows users to define their preferred data visualization library (matplotlib, seaborn, or plotly). With this update, I've eliminated the need for the user to specify in every prompt which library to use, thereby simplifying their interaction with the application and increasing its versatility. * This commit adds a configuration parameter for users to set their preferred data visualization library (matplotlib, seaborn, or plotly), simplifying interactions and enhancing the application's versatility. * viz_library_type' in test_generate_python_code_prompt.py, resolved failing tests --------- Co-authored-by: sabatino.severino <qrxqfspfibrth6nxywai2qifza6jmskt222howzew43risnx4kva> Co-authored-by: Gabriele Venturi <[email protected]> * build: use ruff for formatting * feat: add add_message method to the agent * Release v1.4.3 * feat: workspace env (#717) * fix(chart): charts to save to save_chart_path * refactor sourcery changes * 'Refactored by Sourcery' * refactor chart save code * fix: minor leftovers * feat(workspace_env): add workspace env to store cache, temp chart and config * add error handling and comments --------- Co-authored-by: Sourcery AI <> * fix: hallucinations was plotting when not asked * Release v1.4.4 * feat(sqlConnector): add direct config run sql at runtime * feat(DirectSqlConnector): add sql test cases * fix: minor leftovers * fix(orders): check examples of different tables * 'Refactored by Sourcery' * chore(sqlprompt): add description only when we have it --------- Co-authored-by: Sab Severino <[email protected]> Co-authored-by: Gabriele Venturi <[email protected]> Co-authored-by: Sourcery AI <>
- Loading branch information
1 parent
3fa5625
commit e3c6b79
Showing
27 changed files
with
670 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Example of using PandasAI with a CSV file.""" | ||
|
||
from pandasai import SmartDatalake | ||
from pandasai.llm import OpenAI | ||
from pandasai.connectors import PostgreSQLConnector | ||
from pandasai.smart_dataframe import SmartDataframe | ||
|
||
|
||
# With a PostgreSQL database | ||
order = PostgreSQLConnector( | ||
config={ | ||
"host": "localhost", | ||
"port": 5432, | ||
"database": "testdb", | ||
"username": "postgres", | ||
"password": "123456", | ||
"table": "orders", | ||
} | ||
) | ||
|
||
order_details = PostgreSQLConnector( | ||
config={ | ||
"host": "localhost", | ||
"port": 5432, | ||
"database": "testdb", | ||
"username": "postgres", | ||
"password": "123456", | ||
"table": "order_details", | ||
} | ||
) | ||
|
||
products = PostgreSQLConnector( | ||
config={ | ||
"host": "localhost", | ||
"port": 5432, | ||
"database": "testdb", | ||
"username": "postgres", | ||
"password": "123456", | ||
"table": "products", | ||
} | ||
) | ||
|
||
|
||
llm = OpenAI("OPEN_API_KEY") | ||
|
||
|
||
order_details_smart_df = SmartDataframe( | ||
order_details, | ||
config={"llm": llm, "direct_sql": True}, | ||
description="Contain user order details", | ||
) | ||
|
||
|
||
df = SmartDatalake( | ||
[order_details_smart_df, order, products], | ||
config={"llm": llm, "direct_sql": True}, | ||
) | ||
response = df.chat("return orders with count of distinct products") | ||
print(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Analyze the data, using the provided dataframes (`dfs`). | ||
1. Prepare: Preprocessing and cleaning data if necessary | ||
2. Process: Manipulating data for analysis (grouping, filtering, aggregating, etc.) | ||
3. Analyze: Conducting the actual analysis (if the user asks to plot a chart you must save it as an image in temp_chart.png and not show the chart.) | ||
{viz_library_type} |
39 changes: 39 additions & 0 deletions
39
pandasai/assets/prompt_templates/direct_sql_connector.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
You are provided with the following samples of sql tables data: | ||
|
||
<Tables> | ||
{tables} | ||
<Tables> | ||
|
||
<conversation> | ||
{conversation} | ||
</conversation> | ||
|
||
You are provided with following function that executes the sql query, | ||
<Function> | ||
def execute_sql_query(sql_query: str) -> pd.Dataframe | ||
"""his method connect to the database executes the sql query and returns the dataframe""" | ||
</Function> | ||
|
||
This is the initial python function. Do not change the params. | ||
|
||
```python | ||
# TODO import all the dependencies required | ||
import pandas as pd | ||
|
||
def analyze_data() -> dict: | ||
""" | ||
Analyze the data, using the provided dataframes (`dfs`). | ||
1. Prepare: generate sql query to get data for analysis (grouping, filtering, aggregating, etc.) | ||
2. Process: execute the query using execute method available to you which returns dataframe | ||
3. Analyze: Conducting the actual analysis (if the user asks to plot a chart you must save it as an image in temp_chart.png and not show the chart.) | ||
{viz_library_type} | ||
At the end, return a dictionary of: | ||
{output_type_hint} | ||
""" | ||
``` | ||
|
||
Take a deep breath and reason step-by-step. Act as a senior data analyst. | ||
In the answer, you must never write the "technical" names of the tables. | ||
Based on the last message in the conversation: | ||
|
||
- return the updated analyze_data function wrapped within `python ` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
If the user requests to create a chart, utilize the Python {library} library to generate high-quality graphics that will be saved directly to a file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" Prompt to explain code generation by the LLM | ||
The previous conversation we had | ||
<Conversation> | ||
{conversation} | ||
</Conversation> | ||
Based on the last conversation you generated the following code: | ||
<Code> | ||
{code} | ||
</Code> | ||
Explain how you came up with code for non-technical people without | ||
mentioning technical details or mentioning the libraries used? | ||
""" | ||
from .file_based_prompt import FileBasedPrompt | ||
|
||
|
||
class DirectSQLPrompt(FileBasedPrompt): | ||
"""Prompt to explain code generation by the LLM""" | ||
|
||
_path_to_template = "assets/prompt_templates/direct_sql_connector.tmpl" | ||
|
||
def _prepare_tables_data(self, tables): | ||
tables_join = [] | ||
for table in tables: | ||
table_description_tag = ( | ||
f' description="{table.table_description}"' | ||
if table.table_description is not None | ||
else "" | ||
) | ||
table_head_tag = f'<table name="{table.table_name}"{table_description_tag}>' | ||
table = f"{table_head_tag}\n{table.head_csv}\n</table>" | ||
tables_join.append(table) | ||
return "\n\n".join(tables_join) | ||
|
||
def setup(self, tables) -> None: | ||
self.set_var("tables", self._prepare_tables_data(tables)) |
Oops, something went wrong.