Skip to content

Commit

Permalink
docs: fix all docs snippets instantiations (Sinaptik-AI#825)
Browse files Browse the repository at this point in the history
Most of the snippets were either outdated or didn't work at all
  • Loading branch information
mspronesti authored Dec 16, 2023
1 parent 3e7d975 commit 57e5b4b
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 49 deletions.
4 changes: 2 additions & 2 deletions docs/LLMs/llms.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import pandas as pd
llm = OpenAI()

# conversational=False is supposed to display lower usage and cost
df = SmartDataframe("data.csv", {"llm": llm, "conversational": False})
df = SmartDataframe("data.csv", config={"llm": llm, "conversational": False})

with get_openai_callback() as cb:
response = df.chat("Calculate the sum of the gdp of north american countries")
Expand Down Expand Up @@ -175,7 +175,7 @@ from pandasai import SmartDataframe
from langchain.llms import OpenAI

langchain_llm = OpenAI(openai_api_key="my-openai-api-key")
df = SmartDataframe("data.csv", {"llm": langchain_llm})
df = SmartDataframe("data.csv", config={"llm": langchain_llm})
```

PandasAI will automatically detect that you are using a LangChain LLM and will convert it to a PandasAI LLM.
Expand Down
7 changes: 6 additions & 1 deletion docs/connectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The PostgreSQL connector allows you to connect to a PostgreSQL database. It is d
To use the PostgreSQL connector, you only need to import it into your Python code and pass it to a `SmartDataframe` or `SmartDatalake` object:

```python
from pandasai import SmartDataframe
from pandasai.connectors import PostgreSQLConnector

postgres_connector = PostgreSQLConnector(
Expand Down Expand Up @@ -67,6 +68,7 @@ Similarly to the PostgreSQL connector, the MySQL connector allows you to connect
To use the MySQL connector, you only need to import it into your Python code and pass it to a `SmartDataframe` or `SmartDatalake` object:

```python
from pandasai import SmartDataframe
from pandasai.connectors import MySQLConnector

mysql_connector = MySQLConnector(
Expand Down Expand Up @@ -96,6 +98,7 @@ Similarly to the PostgreSQL and MySQL connectors, the Sqlite connector allows yo
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 import SmartDataframe
from pandasai.connectors import SqliteConnector

connector = SqliteConnector(config={
Expand Down Expand Up @@ -146,6 +149,7 @@ The Snowflake connector allows you to connect to Snowflake. It is very similar t
To use the Snowflake connector, you only need to import it into your Python code and pass it to a `SmartDataframe` or `SmartDatalake` object:

```python
from pandasai import SmartDataframe
from pandasai.connectors import SnowFlakeConnector

snowflake_connector = SnowFlakeConnector(
Expand Down Expand Up @@ -176,7 +180,7 @@ The DataBricks connector allows you to connect to DataBricks. It is very similar
To use the DataBricks 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 DataBricksConnector
from pandasai.connectors import DatabricksConnector

databricks_connector = DatabricksConnector(
config={
Expand All @@ -202,6 +206,7 @@ The Yahoo Finance connector allows you to connect to Yahoo Finance, by simply pa
To use the Yahoo Finance connector, you only need to import it into your Python code and pass it to a `SmartDataframe` or `SmartDatalake` object:

```python
from pandasai import SmartDataframe
from pandasai.connectors.yahoo_finance import YahooFinanceConnector

yahoo_connector = YahooFinanceConnector("MSFT")
Expand Down
2 changes: 1 addition & 1 deletion docs/custom-head.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ head_df = pd.DataFrame({
"happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
})

df = SmartDataframe("data/country_gdp.csv", {
df = SmartDataframe("data/country_gdp.csv", config={
"custom_head": head_df
})
```
Expand Down
2 changes: 1 addition & 1 deletion docs/custom-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ With PandasAI, you can easily customize the instructions that are used by the li
```python
from pandasai import SmartDataframe

df = SmartDataframe("data.csv", {
df = SmartDataframe("data.csv", config={
"custom_instructions": "Custom instructions for the generation of Python code"
})
```
6 changes: 3 additions & 3 deletions docs/custom-prompts.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MyCustomPrompt(AbstractPrompt):
self.set_var("my_custom_value", kwargs["my_custom_value"])


df = SmartDataframe("data.csv", {
df = SmartDataframe("data.csv", config={
"custom_prompts": {
"generate_python_code": MyCustomPrompt(
my_custom_value="my custom value")
Expand All @@ -58,7 +58,7 @@ class MyCustomFileBasedPrompt(FileBasedPrompt):
_path_to_template = "path/to/my_prompt_template.tmpl"


df = SmartDataframe("data.csv", {
df = SmartDataframe("data.csv", config={
"custom_prompts": {
"generate_python_code": MyCustomFileBasedPrompt(
my_custom_value="my custom value")
Expand All @@ -85,7 +85,7 @@ Here's the conversation:
"""


df = SmartDataframe("data.csv", {
df = SmartDataframe("data.csv", config={
"custom_prompts": {
"generate_python_code": MyCustomPrompt()
}
Expand Down
2 changes: 1 addition & 1 deletion docs/custom-whitelisted-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ By default, PandasAI only allows to run code that uses some whitelisted modules.

```python
from pandasai import SmartDataframe
df = SmartDataframe("data.csv", {
df = SmartDataframe("data.csv", config={
"custom_whitelisted_dependencies": ["any_module"]
})
```
Expand Down
74 changes: 51 additions & 23 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Example of using PandasAI with a Pandas DataFrame
```python
from pandasai import SmartDataframe
import pandas as pd
from pandasai.llm import OpenAI

# pandas dataframe
df = pd.DataFrame({
Expand All @@ -18,10 +19,12 @@ df = pd.DataFrame({
"happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
})

llm = OpenAI(api_token="YOUR_API_TOKEN")

# convert to SmartDataframe
df = SmartDataframe(df)
df = SmartDataframe(df, config={"llm": llm})

df.chat('Calculate the sum of the gdp of north american countries')
response = df.chat('Calculate the sum of the gdp of north american countries')
print(response)
# Output: 20901884461056
```
Expand All @@ -32,11 +35,14 @@ Example of using PandasAI with a CSV file

```python
from pandasai import SmartDataframe
from pandasai.llm import OpenAI

llm = OpenAI(api_token="YOUR_API_TOKEN")

# You can instantiate a SmartDataframe with a path to a CSV file
df = Smartdataframe("data/Loan payments data.csv")
df = SmartDataframe("data/Loan payments data.csv", config={"llm": llm})

df.chat("How many loans are from men and have been paid off?")
response = df.chat("How many loans are from men and have been paid off?")
print(response)
# Output: 247 loans have been paid off by men.
```
Expand All @@ -53,11 +59,14 @@ Then, you can use PandasAI with an Excel file as follows:

```python
from pandasai import SmartDataframe
from pandasai.llm import OpenAI

llm = OpenAI(api_token="YOUR_API_TOKEN")

# You can instantiate a SmartDataframe with a path to an Excel file
df = Smartdataframe("data/Loan payments data.xlsx")
df = SmartDataframe("data/Loan payments data.xlsx", config={"llm": llm})

df.chat("How many loans are from men and have been paid off?")
response = df.chat("How many loans are from men and have been paid off?")
print(response)
# Output: 247 loans have been paid off by men.
```
Expand All @@ -74,10 +83,13 @@ Then, you can use PandasAI with a Google Sheet as follows:

```python
from pandasai import SmartDataframe
from pandasai.llm import OpenAI

llm = OpenAI(api_token="YOUR_API_TOKEN")

# You can instantiate a SmartDataframe with a path to a Google Sheet
df = Smartdataframe("https://docs.google.com/spreadsheets/d/fake/edit#gid=0")
df.chat("How many loans are from men and have been paid off?")
df = SmartDataframe("https://docs.google.com/spreadsheets/d/fake/edit#gid=0", config={"llm": llm})
response = df.chat("How many loans are from men and have been paid off?")
print(response)
# Output: 247 loans have been paid off by men.
```
Expand All @@ -97,15 +109,21 @@ Then, you can use PandasAI with a Polars DataFrame as follows:
```python
from pandasai import SmartDataframe
import polars as pl
from pandasai.llm import OpenAI

llm = OpenAI(api_token="YOUR_API_TOKEN")


# You can instantiate a SmartDataframe with a Polars DataFrame

df = pd.DataFrame([
df = pl.DataFrame({
"country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
"gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
"happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
])
df.chat("How many loans are from men and have been paid off?")
})

df = SmartDataframe(df, config={"llm": llm})
response = df.chat("How many loans are from men and have been paid off?")
print(response)
# Output: 247 loans have been paid off by men.
```
Expand All @@ -116,10 +134,13 @@ Example of using PandasAI to generate a chart from a Pandas DataFrame

```python
from pandasai import SmartDataframe
from pandasai.llm import OpenAI

df = SmartDataframe("data/Countries.csv")
llm = OpenAI(api_token="YOUR_API_TOKEN")

df = SmartDataframe("data/Countries.csv", config={"llm": llm})
response = df.chat(
"Plot the histogram of countries showing for each the gpd, using different colors for each bar"
"Plot the histogram of countries showing for each the gpd, using different colors for each bar",
)
print(response)
# Output: check out images/histogram-chart.png
Expand All @@ -131,12 +152,17 @@ You can pass a custom path to save the charts. The path must be a valid global p
Below is the example to Save Charts with user defined location.

```python
import os
from pandasai import SmartDataframe
from pandasai.llm import OpenAI

user_defined_path = os.getcwd()
df = SmartDataframe("data/Countries.csv", {
llm = OpenAI(api_token="YOUR_API_TOKEN")

df = SmartDataframe("data/Countries.csv", config={
"save_charts": True,
"save_charts_path": user_defined_path,
"llm": llm
})
response = df.chat(
"Plot the histogram of countries showing for each the gpd,"
Expand All @@ -153,6 +179,7 @@ Example of using PandasAI with multiple dataframes. In order to use multiple dat
```python
from pandasai import SmartDatalake
import pandas as pd
from pandasai.llm import OpenAI

employees_data = {
'EmployeeID': [1, 2, 3, 4, 5],
Expand All @@ -168,7 +195,9 @@ salaries_data = {
employees_df = pd.DataFrame(employees_data)
salaries_df = pd.DataFrame(salaries_data)

df = SmartDatalake([employees_df, salaries_df])
llm = OpenAI(api_token="YOUR_API_TOKEN")

df = SmartDatalake([employees_df, salaries_df], config={"llm": llm})
response = df.chat("Who gets paid the most?")
print(response)
# Output: Olivia gets paid the most.
Expand All @@ -181,8 +210,10 @@ dataframe by gender and then by loans that have been paid off.

```python
from pandasai import SmartDataframe
from pandasai.llm import OpenAI

df = SmartDataframe("data/Loan payments data.csv")
llm = OpenAI(api_token="YOUR_API_TOKEN")
df = SmartDataframe("data/Loan payments data.csv", config={"llm": llm})

# We filter by males only
from_males_df = df.chat("Filter the dataframe by women")
Expand Down Expand Up @@ -222,7 +253,7 @@ With the chat agent, you can engage in dynamic conversations where the agent ret

Feel free to initiate conversations, seek clarifications, and explore explanations to enhance your interactions with the chat agent!

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

Expand Down Expand Up @@ -265,7 +296,7 @@ print(response)

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.

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

Expand All @@ -288,12 +319,9 @@ salaries_df = pd.DataFrame(salaries_data)


@skill
def plot_salaries(merged_df: pd.DataFrame) -> str:
def plot_salaries(merged_df: pd.DataFrame):
"""
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
Displays the bar chart having name on x-axis and salaries on y-axis using streamlit
"""
import matplotlib.pyplot as plt

Expand Down
12 changes: 7 additions & 5 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Below is simple example to get started with `pandasai`.
```python
import pandas as pd
from pandasai import SmartDataframe
from pandasai.llm import OpenAI


# Sample DataFrame
df = pd.DataFrame({
Expand All @@ -53,7 +55,6 @@ df = pd.DataFrame({
})

# Instantiate a LLM
from pandasai.llm import OpenAI
llm = OpenAI(api_token="YOUR_API_TOKEN")

df = SmartDataframe(df, config={"llm": llm})
Expand Down Expand Up @@ -124,6 +125,8 @@ PandasAI also supports agents. While a `SmartDataframe` or a `SmartDatalake` can
```python
from pandasai import Agent
import pandas as pd
from pandasai.llm import OpenAI


# Sample DataFrames
df1 = pd.DataFrame({
Expand All @@ -132,7 +135,9 @@ df1 = pd.DataFrame({
"happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
})

agent = Agent([df1])
llm = OpenAI(api_token="YOUR_API_TOKEN")

agent = Agent([df1], config={"llm": llm})
```

Then, you can use the agent as follows:
Expand Down Expand Up @@ -214,6 +219,3 @@ Try out PandasAI in your browser:

You can find some examples [here](examples.md).

```
```
Loading

0 comments on commit 57e5b4b

Please sign in to comment.