Skip to content

Commit

Permalink
Merge pull request #679 from gventuri/release/v1.4
Browse files Browse the repository at this point in the history
Release/v1.4
  • Loading branch information
gventuri authored Oct 25, 2023
2 parents 76a159f + 43efd40 commit aac1e3e
Show file tree
Hide file tree
Showing 37 changed files with 2,363 additions and 102 deletions.
22 changes: 22 additions & 0 deletions docs/connectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ df = SmartDataframe(mysql_connector)
df.chat('What is the total amount of loans in the last year?')
```

### Sqlite connector

Similarly to the PostgreSQL and MySQL connectors, the Sqlite connector allows you to connect to a local Sqlite database file. It is designed to be easy to use, even if you are not familiar with Sqlite or with PandasAI.

To use the Sqlite connector, you only need to import it into your Python code and pass it to a `SmartDataframe` or `SmartDatalake` object:

```python
from pandasai.connectors import SqliteConnector

connector = SqliteConnector(config={
"database" : "PATH_TO_DB",
"table" : "actor",
"where" :[
["first_name","=","PENELOPE"]
]
})

df = SmartDataframe(connector)
df.chat('How many records are there ?')
```


### Generic SQL connector

The generic SQL connector allows you to connect to any SQL database that is supported by SQLAlchemy.
Expand Down
54 changes: 54 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,57 @@ for question in questions:
response = agent.explain()
print(response)
```

## Add Skills to the Agent

You can add customs functions for the agent to use, allowing the agent to expand its capabilities. These custom functions can be seamlessly integrated with the agent's skills, enabling a wide range of user-defined operations.

```
import pandas as pd
from pandasai import Agent
from pandasai.llm.openai import OpenAI
from pandasai.skills import skill
employees_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Name": ["John", "Emma", "Liam", "Olivia", "William"],
"Department": ["HR", "Sales", "IT", "Marketing", "Finance"],
}
salaries_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Salary": [5000, 6000, 4500, 7000, 5500],
}
employees_df = pd.DataFrame(employees_data)
salaries_df = pd.DataFrame(salaries_data)
@skill(
name="Display employee salary",
description="Plots the employee salaries against names",
usage="Displays the plot having name on x axis and salaries on y axis",
)
def plot_salaries(merged_df: pd.DataFrame) -> str:
import matplotlib.pyplot as plt
plt.bar(merged_df["Name"], merged_df["Salary"])
plt.xlabel("Employee Name")
plt.ylabel("Salary")
plt.title("Employee Salaries")
plt.xticks(rotation=45)
plt.savefig("temp_chart.png")
plt.close()
llm = OpenAI("YOUR_API_KEY")
agent = Agent([employees_df, salaries_df], config={"llm": llm}, memory_size=10)
agent.add_skills(plot_salaries)
# Chat with the agent
response = agent.chat("Plot the employee salaries against names")
print(response)
```
1 change: 1 addition & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Settings:
- `save_charts_path`: the path where to save the charts. Defaults to `exports/charts/`. You can use this setting to override the default path.
- `enable_cache`: whether to enable caching. Defaults to `True`. If set to `True`, PandasAI will cache the results of the LLM to improve the response time. If set to `False`, PandasAI will always call the LLM.
- `use_error_correction_framework`: whether to use the error correction framework. Defaults to `True`. If set to `True`, PandasAI will try to correct the errors in the code generated by the LLM with further calls to the LLM. If set to `False`, PandasAI will not try to correct the errors in the code generated by the LLM.
- `use_advanced_reasoning_framework`: whether to use the advanced reasoning framework. Defaults to `False`. If set to `True`, PandasAI will try to use advanced reasoning to improve the results of the LLM and provide an explanation for the results.
- `max_retries`: the maximum number of retries to use when using the error correction framework. Defaults to `3`. You can use this setting to override the default number of retries.
- `custom_prompts`: the custom prompts to use. Defaults to `{}`. You can use this setting to override the default custom prompts. You can find more information about custom prompts [here](custom-prompts.md).
- `custom_whitelisted_dependencies`: the custom whitelisted dependencies to use. Defaults to `{}`. You can use this setting to override the default custom whitelisted dependencies. You can find more information about custom whitelisted dependencies [here](custom-whitelisted-dependencies.md).
Expand Down
113 changes: 113 additions & 0 deletions docs/skills.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Skills

You can add customs functions for the agent to use, allowing the agent to expand its capabilities. These custom functions can be seamlessly integrated with the agent's skills, enabling a wide range of user-defined operations.

## Example Usage

```python

import pandas as pd
from pandasai import Agent

from pandasai.llm.openai import OpenAI
from pandasai.skills import skill

employees_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Name": ["John", "Emma", "Liam", "Olivia", "William"],
"Department": ["HR", "Sales", "IT", "Marketing", "Finance"],
}

salaries_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Salary": [5000, 6000, 4500, 7000, 5500],
}

employees_df = pd.DataFrame(employees_data)
salaries_df = pd.DataFrame(salaries_data)

# Function doc string to give more context to the model for use this skill
@skill
def plot_salaries(name: list[str], salaries: list[int]):
"""
Displays the bar chart having name on x axis and salaries on y axis
Args:
name (list[str]): Employee name
salaries (list[int]): Salaries
"""
# plot bars
import matplotlib.pyplot as plt

plt.bar(name, salaries)
plt.xlabel("Employee Name")
plt.ylabel("Salary")
plt.title("Employee Salaries")
plt.xticks(rotation=45)



llm = OpenAI("YOUR_API_KEY")
agent = Agent([employees_df, salaries_df], config={"llm": llm}, memory_size=10)

agent.add_skills(plot_salaries)

# Chat with the agent
response = agent.chat("Plot the employee salaries against names")


```

## Add Streamlit Skill

```python
import pandas as pd
from pandasai import Agent

from pandasai.llm.openai import OpenAI
from pandasai.skills import skill
import streamlit as st

employees_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Name": ["John", "Emma", "Liam", "Olivia", "William"],
"Department": ["HR", "Sales", "IT", "Marketing", "Finance"],
}

salaries_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Salary": [5000, 6000, 4500, 7000, 5500],
}

employees_df = pd.DataFrame(employees_data)
salaries_df = pd.DataFrame(salaries_data)

# Function doc string to give more context to the model for use this skill
@skill
def plot_salaries(name: list[str], salary: list[int]):
"""
Displays the bar chart having name on x axis and salaries on y axis using streamlit
Args:
name (list[str]): Employee name
salaries (list[int]): Salaries
"""
import matplotlib.pyplot as plt

plt.bar(name, salary)
plt.xlabel("Employee Name")
plt.ylabel("Salary")
plt.title("Employee Salaries")
plt.xticks(rotation=45)
plt.savefig("temp_chart.png")
fig = plt.gcf()
st.pyplot(fig)


llm = OpenAI("YOUR_API_KEY")
agent = Agent([employees_df, salaries_df], config={"llm": llm}, memory_size=10)

agent.add_skills(plot_salaries_using_streamlit)

# Chat with the agent
response = agent.chat("Plot the employee salaries against names")
print(response)
```
15 changes: 13 additions & 2 deletions examples/from_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pandasai import SmartDatalake
from pandasai.llm import OpenAI
from pandasai.connectors import MySQLConnector, PostgreSQLConnector
from pandasai.connectors import MySQLConnector, PostgreSQLConnector, SqliteConnector

# With a MySQL database
loan_connector = MySQLConnector(
Expand Down Expand Up @@ -38,8 +38,19 @@
}
)

# With a Sqlite databse

invoice_connector = SqliteConnector(
config={
"database": "local_path_to_db",
"table": "invoices",
"where": [["status", "=", "pending"]],
}
)
llm = OpenAI()
df = SmartDatalake([loan_connector, payment_connector], config={"llm": llm})
df = SmartDatalake(
[loan_connector, payment_connector, invoice_connector], config={"llm": llm}
)
response = df.chat("How many people from the United states?")
print(response)
# Output: 247 loans have been paid off by men.
47 changes: 47 additions & 0 deletions examples/skills_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pandas as pd
from pandasai import Agent

from pandasai.llm.openai import OpenAI
from pandasai.skills import skill

employees_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Name": ["John", "Emma", "Liam", "Olivia", "William"],
"Department": ["HR", "Sales", "IT", "Marketing", "Finance"],
}

salaries_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Salary": [5000, 6000, 4500, 7000, 5500],
}

employees_df = pd.DataFrame(employees_data)
salaries_df = pd.DataFrame(salaries_data)


# Add function docstring to give more context to model
@skill
def plot_salaries(name: list[str], salary: list[int]) -> str:
"""
Displays the bar chart having name on x axis and salaries on y axis using streamlit
Args:
name (list[str]): Employee name
salaries (list[int]): Salaries
"""
import matplotlib.pyplot as plt

plt.bar(name, salary)
plt.xlabel("Employee Name")
plt.ylabel("Salary")
plt.title("Employee Salaries")
plt.xticks(rotation=45)


llm = OpenAI("YOUR-API-KEY")
agent = Agent([employees_df, salaries_df], config={"llm": llm}, memory_size=10)

agent.add_skills(plot_salaries)

# Chat with the agent
response = agent.chat("Plot the employee salaries against names")
print(response)
59 changes: 59 additions & 0 deletions examples/using_pandasai_log_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import pandas as pd
from pandasai import Agent

from pandasai.llm.openai import OpenAI

employees_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Name": ["John", "Emma", "Liam", "Olivia", "William"],
"Department": ["HR", "Sales", "IT", "Marketing", "Finance"],
}

salaries_data = {
"EmployeeID": [1, 2, 3, 4, 5],
"Salary": [5000, 6000, 4500, 7000, 5500],
}

employees_df = pd.DataFrame(employees_data)
salaries_df = pd.DataFrame(salaries_data)

# Example 1: Using Environment Variables
os.environ["LOGGING_SERVER_URL"] = "SERVER_URL"
os.environ["LOGGING_SERVER_API_KEY"] = "YOUR_API_KEY"


llm = OpenAI("YOUR_API_KEY")
agent = Agent(
[employees_df, salaries_df],
config={
"llm": llm,
"enable_cache": True,
},
memory_size=10,
)

# Chat with the agent
response = agent.chat("Plot salary against department?")
print(response)


# Example 2: Using Config
llm = OpenAI("YOUR_API_KEY")
agent = Agent(
[employees_df, salaries_df],
config={
"llm": llm,
"enable_cache": True,
"log_server": {
"server_url": "SERVER_URL",
"api_key": "YOUR_API_KEY",
},
},
memory_size=10,
)

# Chat with the agent
response = agent.chat("Plot salary against department?")

print(response)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ nav:
- callbacks.md
- custom-instructions.md
- custom-prompts.md
- skills.md
- custom-whitelisted-dependencies.md
- Examples:
- examples.md
Expand Down
10 changes: 9 additions & 1 deletion pandasai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from .schemas.df_config import Config
from .helpers.cache import Cache
from .agent import Agent
from .skills import skill

__version__ = importlib.metadata.version(__package__ or __name__)

Expand Down Expand Up @@ -257,4 +258,11 @@ def clear_cache(filename: str = None):
cache.clear()


__all__ = ["PandasAI", "SmartDataframe", "SmartDatalake", "Agent", "clear_cache"]
__all__ = [
"PandasAI",
"SmartDataframe",
"SmartDatalake",
"Agent",
"clear_cache",
"skill",
]
Loading

0 comments on commit aac1e3e

Please sign in to comment.