### Install LiteLLM Extension Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/getting-started.mdx Installs the LiteLLM extension for PandasAI, enabling the use of various LLMs through LiteLLM. ```bash pip install pandasai-litellm ``` -------------------------------- ### Install PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/getting-started.mdx Installs the PandasAI library using either Poetry (recommended) or pip. Requires Python 3.8+ <3.12. ```bash # Using poetry (recommended) poetry add "pandasai>=3.0.0b2" # Alternative: using pip pip install "pandasai>=3.0.0b2" ``` -------------------------------- ### Install PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Installs the pandasai library using either Poetry (recommended) or pip. It also shows how to install optional dependencies for extended functionality. ```console # Using poetry (recommended) poetry add pandasai # Using pip pip install pandasai ``` ```console pip install pandasai[extra-dependency-name] ``` -------------------------------- ### Install PandasAI with Excel Support Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/examples.mdx Provides the command to install the necessary extra dependency for using PandasAI with Excel files. ```console pip install pandasai[excel] ``` -------------------------------- ### Run the PandasAI Platform Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/platform.mdx This command starts the PandasAI platform services (client and server) using Docker Compose. Once running, the platform can be accessed via web browser. ```bash docker-compose up ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/platform.mdx These commands copy example environment files and then use `sed` to update the `PANDASAI_API_KEY` in the server's environment file. This is crucial for authenticating with the PandasAI service. ```bash cp client/.env.example client/.env cp server/.env.example server/.env # Declare the API key API_KEY="YOUR_PANDASAI_API_KEY" # Update the server/.env file sed -i "" "s/^PANDASAI_API_KEY=.*/PANDASAI_API_KEY=${API_KEY}/" server/.env ``` -------------------------------- ### Install PandasAI with Modin Support Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/examples.mdx Provides the command to install the extra dependency for using PandasAI with Modin DataFrames. ```console pip install pandasai[modin] ``` -------------------------------- ### Install PandasAI with Google Sheets Support Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/examples.mdx Shows the command to install the extra dependency required for using PandasAI with Google Sheets. ```console pip install pandasai[google-sheet] ``` -------------------------------- ### Install PandasAI with Polars Support Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/examples.mdx Shows the command to install the extra dependency for using PandasAI with Polars DataFrames. ```console pip install pandasai[polars] ``` -------------------------------- ### Load Example Datasets Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/data_platform_guide.ipynb Loads two example CSV datasets ('heart.csv' and 'loans_payments.csv') using PandasAI's `read_csv` function and displays the first few rows of each. ```python # Load heart disease and loans datasets heart_df = pai.read_csv('./data/heart.csv') loans_df = pai.read_csv('./data/loans_payments.csv') # Display first few rows of each dataset print("Heart Disease Dataset:") heart_df.head() print("\nLoans Dataset:") loans_df.head() ``` -------------------------------- ### Setup PandasAI with API Key Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/data_platform_guide.ipynb Initializes PandasAI by setting the API key. This is a prerequisite for using most of the platform's features. ```python import pandasai as pai pai.api_key.set("your-key-here") ``` -------------------------------- ### Load Dataset from PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/quickstart.ipynb Loads a previously created dataset from PandasAI using its specified path. This allows for quick access to prepared data. ```python dataset = pai.load("your-organization/heart") ``` -------------------------------- ### Clone PandasAI Repository Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/platform.mdx This command clones the PandasAI repository from GitHub to your local machine. It's the first step in setting up the platform. ```bash git clone https://github.com/sinaptik-ai/pandas-ai/ cd pandas-ai ``` -------------------------------- ### Set PandasAI API Key Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/quickstart.ipynb Sets the PandasAI API key for authentication. This is a prerequisite for using most of the library's features. ```python import pandasai as pai pai.api_key.set("your-pai-api-key") ``` -------------------------------- ### Create Dataset in PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/quickstart.ipynb Creates a reusable dataset within PandasAI from a DataFrame. This involves specifying a path, name, the DataFrame itself, and a description. ```python dataset = pai.create(path="your-organization/heart", name="Heart", df = file_df, description="Heart Disease Dataset") ``` -------------------------------- ### Build Docker Images Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/platform.mdx This command builds the necessary Docker images for the PandasAI platform using Docker Compose. It prepares the application for running. ```bash docker-compose build ``` -------------------------------- ### Install and Configure LiteLLM for PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/large-language-models.mdx Installs the pandasai-litellm extension and configures it for use with PandasAI. This allows integration with various LLM providers through LiteLLM's unified interface. It shows examples for OpenAI models and other providers, requiring API keys for authentication. ```bash pip install pandasai-litellm ``` ```python import pandasai as pai from pandasai_litellm.litellm import LiteLLM # For OpenAI models llm = LiteLLM(model="gpt-4.1-mini", api_key="YOUR_OPENAI_API_KEY") # For other providers, change the model name and provide appropriate credentials # llm = LiteLLM(model="anthropic/claude-3-opus-20240229", api_key="YOUR_ANTHROPIC_API_KEY") pai.config.set({ "llm": llm }) ``` -------------------------------- ### Get Explanation for Agent's Answer Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Demonstrates how to retrieve an explanation for the answer provided by a PandasAI Agent using the `explain` method after a query. ```python response = agent.chat('What is the GDP of the United States?') explanation = agent.explain() print("The answer is", response) print("The explanation is", explanation) ``` -------------------------------- ### Get Clarification Questions from PandasAI Agent Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Shows how to use the `clarification_questions` method of a PandasAI Agent to obtain potential questions the agent might ask to gather more information for a query. ```python agent.clarification_questions('What is the GDP of the United States?') ``` -------------------------------- ### Enable PandasAI Docker Sandbox Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/agent.mdx Provides a code example for initializing and starting the Docker sandbox, then creating a PandasAI agent with the sandbox enabled for secure code execution. ```python from pandasai import Agent from pandasai_docker import DockerSandbox # Initialize and start the sandbox sandbox = DockerSandbox() sandbox.start() # Create an agent with the sandbox enabled agent = Agent("data.csv", sandbox=sandbox) # The code will now run in an isolated Docker container response = agent.chat("What is the total sales for each country?") # Don't forget to stop the sandbox when done sandbox.stop() ``` -------------------------------- ### Configure PandasAI with LiteLLM Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/getting-started.mdx Configures PandasAI to use an LLM via the LiteLLM extension, specifically using OpenAI's GPT-4.1-mini. Requires an OpenAI API key. ```python import pandasai as pai from pandasai_litellm.litellm import LiteLLM # Initialize LiteLLM with your OpenAI model llm = LiteLLM(model="gpt-4.1-mini", api_key="YOUR_OPENAI_API_KEY") # Configure PandasAI to use this LLM pai.config.set({ "llm": llm }) ``` -------------------------------- ### Configure SmartDataframe with Name and Description Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Shows how to instantiate a SmartDataframe while providing a custom name and description. This can help the underlying language model understand the context of the dataframe better. ```python df = SmartDataframe(df, name="My DataFrame", description="Brief description of what the dataframe contains") ``` -------------------------------- ### Load and Query Datasets Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/getting-started.mdx Loads existing data layers and queries them using natural language. Supports querying single datasets or comparing data across multiple datasets. ```python # Load existing datasets stocks = pai.load("organization/coca_cola_stock") companies = pai.load("organization/companies") # Query using natural language response = stocks.chat("What is the volatility of the Coca Cola stock?") response = companies.chat("What is the average revenue by region?") # Query using multiple datasets result = pai.chat("Compare the revenue between Coca Cola and Apple", stocks, companies) ``` -------------------------------- ### Install and Configure OpenAI for PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/large-language-models.mdx Installs the pandasai-openai extension and configures it for use with PandasAI. This setup requires an OpenAI API key to interact with OpenAI models. The code demonstrates how to instantiate the OpenAI class and set it as the default LLM in PandasAI. ```bash # Using poetry poetry add pandasai-openai # Using pip pip install pandasai-openai ``` ```python import pandasai as pai from pandasai_openai import OpenAI llm = OpenAI(api_token="my-openai-api-key") # Set your OpenAI API key pai.config.set({"llm": llm}) ``` -------------------------------- ### Create Data Layer (Basic) Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/getting-started.mdx Creates a data layer for a Pandas DataFrame, saving it to a specified path with a description. The schema is inferred from the DataFrame. ```python import pandasai as pai # Load your data df = pai.read_csv("data/companies.csv") # Create the data layer companies = pai.create( path="my-org/companies", df=df, description="Customer companies dataset" ) ``` -------------------------------- ### Query Data with Natural Language Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/quickstart.ipynb Asks questions about the data in a PandasAI DataFrame using natural language. The library processes the query and returns a response. ```python response = file_df.chat("What is the correlation between age and cholesterol?") print(response) ``` -------------------------------- ### Continue Conversation with PandasAI Agent Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Illustrates how to continue a conversation with a PandasAI Agent, demonstrating its ability to remember previous context and answer follow-up questions. ```python agent.chat('And which one has the most deals?') # Output: United States has the most deals ``` -------------------------------- ### Configure SmartDataframe with a config object Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Demonstrates how to instantiate SmartDataframe by passing a configuration object with specific settings. This allows for fine-grained control over PandasAI's behavior, overriding default or file-based configurations. ```python from pandasai import SmartDataframe from pandasai.llm import OpenAI # Example configuration config = { "llm": OpenAI(api_key="YOUR_API_KEY"), "verbose": True, "save_charts": True, "save_charts_path": "exports/charts", "open_charts": False, "enable_cache": False, "max_retries": 5, "security": "advanced" } df = SmartDataframe("your_dataframe.csv", config=config) # You can also override specific settings df_override = SmartDataframe("your_dataframe.csv", config={"verbose": False}) ``` -------------------------------- ### Use PandasAI with Excel File Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/examples.mdx Demonstrates how to use PandasAI with an Excel file after installing the required dependencies. The SmartDataframe is initialized with the path to the Excel file. ```python import os from pandasai import SmartDataframe # You can instantiate a SmartDataframe with a path to an Excel file sdf = SmartDataframe("data/Loan payments data.xlsx") response = sdf.chat("How many loans are from men and have been paid off?") print(response) # Output: 247 loans have been paid off by men. ``` -------------------------------- ### Instantiate and Query PandasAI Agent Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Shows how to initialize a PandasAI Agent with a DataFrame and perform an initial query. The Agent is designed for multi-turn conversations and maintains conversation state. ```python import os from pandasai import Agent import pandas as pd # Sample DataFrames sales_by_country = pd.DataFrame({ "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"], "sales": [5000, 3200, 2900, 4100, 2300, 2100, 2500, 2600, 4500, 7000], "deals_opened": [142, 80, 70, 90, 60, 50, 40, 30, 110, 120], "deals_closed": [120, 70, 60, 80, 50, 40, 30, 20, 100, 110] }) agent = Agent(sales_by_country) agent.chat('Which are the top 5 countries by sales?') # Output: China, United States, Japan, Germany, Australia ``` -------------------------------- ### Use SmartDataframe for Chat Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Demonstrates how to initialize a SmartDataframe with a pandas DataFrame and use its chat method to query data. This involves creating a sample DataFrame and then asking a question about the data. ```python import os import pandas as pd from pandasai import SmartDataframe # Sample DataFrame sales_by_country = pd.DataFrame({ "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"], "sales": [5000, 3200, 2900, 4100, 2300, 2100, 2500, 2600, 4500, 7000] }) df = SmartDataframe(sales_by_country) df.chat('Which are the top 5 countries by sales?') # Output: China, United States, Japan, Germany, Australia ``` -------------------------------- ### Install Dependencies with Poetry Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/CONTRIBUTING.md Installs project dependencies using Poetry, including all extras and development tools. This is the recommended method for setting up the development environment. ```bash poetry install --all-extras --with dev ``` -------------------------------- ### Install Dependencies with Poetry Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/contributing.mdx Installs project dependencies, including all extras and development tools, using the Poetry package manager. This is the recommended method for setting up the development environment. ```bash poetry install --all-extras --with dev ``` -------------------------------- ### Install Dependencies with Poetry Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/contributing.mdx Installs project dependencies, including all extras and development tools, using the Poetry package manager. This is the recommended method for setting up the development environment. ```bash poetry install --all-extras --with dev ``` -------------------------------- ### Install PandasAI with Pip Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/README.md Installs the PandasAI library and its dependencies using pip. Ensure you have Python 3.8+ installed. ```bash pip install "pandasai>=3.0.0b2" ``` -------------------------------- ### Configure SmartDataframe using pandasai.json Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Explains how to set up default configurations for PandasAI by creating or modifying the `pandasai.json` file in the project's root directory. Settings in this file are used unless overridden by a `config` object during instantiation. ```json { "llm": "google.palm", "save_logs": false, "verbose": true, "save_charts": true, "save_charts_path": "exports/charts", "open_charts": true, "enable_cache": true, "max_retries": 3, "security": "standard" } ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/contributing.mdx Installs the pre-commit framework, which helps in maintaining code standards by running checks before commits. This command should be run after cloning the repository. ```bash pre-commit install ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/contributing.mdx Installs the pre-commit framework, which helps maintain code quality by running checks before commits. This command should be run after cloning the repository. ```bash pre-commit install ``` -------------------------------- ### Create Data Layer (Explicit Schema) Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/getting-started.mdx Creates a data layer with an explicitly defined schema, including column names, types, and descriptions, providing more control over the dataset structure. ```python # Define a companies dataset with explicit schema companies = pai.create( path="my-org/companies", df=df, description="Customer companies dataset", columns=[ { "name": "company_name", "type": "string", "description": "The name of the company" }, { "name": "revenue", "type": "float", "description": "The revenue of the company" }, { "name": "region", "type": "string", "description": "The region of the company" } ] ) ``` -------------------------------- ### Install PandasAI with Poetry Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/README.md Installs the PandasAI library using Poetry. This is an alternative to pip for managing Python dependencies. ```bash poetry add "pandasai>=3.0.0b2" ``` -------------------------------- ### Install PandasAI Docker Sandbox Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/agent.mdx Provides the command to install the pandasai-docker package, which is required for using the sandbox environment. ```bash pip install pandasai-docker ``` -------------------------------- ### Chat with CSV Data Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/getting-started.mdx Loads a CSV file into a Pandas DataFrame and queries it using natural language. PandasAI processes the query using the configured LLM and returns a response. ```python import pandasai as pai from pandasai_litellm.litellm import LiteLLM # Initialize LiteLLM with your OpenAI model llm = LiteLLM(model="gpt-4.1-mini", api_key="YOUR_OPENAI_API_KEY") # Configure PandasAI to use this LLM pai.config.set({ "llm": llm }) # Load your data df = pai.read_csv("data/companies.csv") response = df.chat("What is the average revenue by region?") print(response) ``` -------------------------------- ### Install PandasAI Connectors Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/connectors.mdx Installs the necessary dependencies for PandasAI connectors using either poetry or pip. This is a prerequisite for using any of the data source connectors. ```console # Using poetry (recommended) poetry add pandasai[connectors] # Using pip pip install pandasai[connectors] ``` -------------------------------- ### Install PandasAI Yahoo Finance Extension Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/data-ingestion.mdx Instructions for installing the Yahoo Finance extension for PandasAI using either poetry or pip. ```bash poetry add pandasai-yfinance ``` ```bash pip install pandasai-yfinance ``` -------------------------------- ### Read CSV File with PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/quickstart.ipynb Reads a CSV file into a PandasAI DataFrame. This allows for subsequent natural language querying of the data. ```python file_df = pai.read_csv("./data/heart.csv") ``` -------------------------------- ### Use PandasAI with Google Sheets Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/examples.mdx Explains how to use PandasAI with a Google Sheet after installing the necessary dependencies. The SmartDataframe is initialized with the Google Sheet URL. ```python import os from pandasai import SmartDataframe # You can instantiate a SmartDataframe with a path to a Google Sheet sdf = SmartDataframe("https://docs.google.com/spreadsheets/d/fake/edit#gid=0") response = sdf.chat("How many loans are from men and have been paid off?") print(response) # Output: 247 loans have been paid off by men. ``` -------------------------------- ### Rephrase Query with PandasAI Agent Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Illustrates how to use the `rephrase_query` method of a PandasAI Agent to generate a more accurate or comprehensive version of an initial query. ```python rephrased_query = agent.rephrase_query('What is the GDP of the United States?') print("The rephrased query is", rephrased_query) ``` -------------------------------- ### View Docker Compose Logs Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/platform.mdx This command displays the logs from all services managed by Docker Compose. It is useful for diagnosing issues during platform operation. ```bash docker-compose logs ``` -------------------------------- ### Configuring Agent Description for Context Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/examples.mdx This example demonstrates how to provide a custom description when instantiating a PandasAI Agent. The description helps define the agent's role and provides context for the LLM, influencing its responses and interactions. ```python import os from pandasai import Agent agent = Agent( "data.csv", description="You are a data analysis agent. Your main goal is to help non-technical users to analyze data", ) ``` -------------------------------- ### Execute Code with PandasAI and Docker Sandbox Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/docker_sandbox.ipynb Demonstrates how to initialize LiteLLM, configure PandasAI, start a Docker sandbox, read a CSV file, and execute a query using the sandbox. The sandbox is then stopped. ```python import pandasai as pai from pandasai_docker import DockerSandbox from pandasai_litellm.litellm import LiteLLM # Initialize LiteLLM with your OpenAI modelllm = LiteLLM(model="gpt-4.1-mini", api_key="YOUR_OPENAI_API_KEY") # Configure PandasAI to use this LLM pai.config.set({ "llm": llm }) # initialize the sandbox sandbox = DockerSandbox() sandbox.start() # read a csv as df df = pai.read_csv("./data/heart.csv") # pass the csv and the sandbox to the agent result = pai.chat("plot total heart patients by gender", df, sandbox=sandbox) result.show() # stop the sandbox (docker container) sandbox.stop() ``` -------------------------------- ### Instantiate and Query SmartDatalake with Multiple DataFrames Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/library.mdx Demonstrates how to create a SmartDatalake instance with multiple pandas DataFrames and perform a query. The SmartDatalake automatically identifies relevant dataframes for the query. ```python import os import pandas as pd from pandasai import SmartDatalake 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) lake = SmartDatalake([employees_df, salaries_df]) lake.chat("Who gets paid the most?") # Output: Olivia gets paid the most ``` -------------------------------- ### Create and Save Dataframes with Metadata Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/examples/data_platform_guide.ipynb Demonstrates how to create and save dataframes to the PandasAI platform using the `create()` method. It includes adding metadata such as path, name, description, and detailed column information for both heart disease and loan datasets. ```python # Create heart disease dataset with semantic information heart = pai.create( path="my-team/heart", name="Heart Disease Data", df = heart_df, description="Dataset containing heart disease patient information", columns=[ {"name": "Age", "type": "integer", "description": "Age of the patient in years"}, {"name": "Sex", "type": "string", "description": "Gender of the patient (M/F)"}, {"name": "ChestPainType", "type": "string", "description": "Type of chest pain experienced"}, {"name": "RestingBP", "type": "integer", "description": "Resting blood pressure in mm Hg"}, {"name": "Cholesterol", "type": "integer", "description": "Serum cholesterol in mg/dl"}, {"name": "FastingBS", "type": "integer", "description": "Fasting blood sugar > 120 mg/dl (1: true; 0: false)"}, {"name": "MaxHR", "type": "integer", "description": "Maximum heart rate achieved"}, {"name": "Oldpeak", "type": "float", "description": "ST depression induced by exercise relative to rest"}, {"name": "HeartDisease", "type": "integer", "description": "Output class (1: heart disease; 0: normal)"} ] ) # Save loans dataset loans = pai.create( path="my-team/loans", name="Loan Payments Data", df = loans_df, description="Dataset containing loan payment information", columns=[ {"name": "loan_id", "type": "integer", "description": "Unique identifier for each loan"}, {"name": "amount", "type": "float", "description": "Loan amount in dollars"}, {"name": "term", "type": "integer", "description": "Loan term in months"}, {"name": "interest_rate", "type": "float", "description": "Annual interest rate as a percentage"}, {"name": "payment", "type": "float", "description": "Monthly payment amount"} ] ) ``` -------------------------------- ### Count OpenAI Tokens and Get Response Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/llms.mdx An example demonstrating how to use the OpenAI LLM with PandasAI, specifically focusing on counting tokens and retrieving cost information using `get_openai_callback`. It also shows how to disable conversational mode for potentially lower usage. Requires 'pandasai' and 'pandas'. ```python """Example of using PandasAI with a pandas dataframe""" from pandasai import SmartDataframe from pandasai.llm import OpenAI from pandasai.helpers.openai_info import get_openai_callback import pandas as pd llm = OpenAI() # conversational=False is supposed to display lower usage and cost 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") print(response) print(cb) # The sum of the GDP of North American countries is 19,294,482,071,552. # Tokens Used: 375 # Prompt Tokens: 210 # Completion Tokens: 165 # Total Cost (USD): $ 0.000750 ``` -------------------------------- ### Use PandasAI with Docker Sandbox Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/README.md Shows how to initialize, start, use, and stop a Docker sandbox for PandasAI. This provides a secure, isolated environment for executing data analysis tasks. ```python import pandasai as pai from pandasai_docker import DockerSandbox from pandasai_openai.openai import OpenAI # Initialize the sandbox sandbox = DockerSandbox() sandbox.start() 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] } llm = OpenAI("OPEN_AI_API_KEY") pai.config.set({ "llm": llm }) employees_df = pai.DataFrame(employees_data) salaries_df = pai.DataFrame(salaries_data) pai.chat("Who gets paid the most?", employees_df, salaries_df, sandbox=sandbox) # Don't forget to stop the sandbox when done sandbox.stop() ``` -------------------------------- ### Install Amazon Bedrock Support for PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/llms.mdx Install the package required for using Amazon Bedrock models with PandasAI. ```bash pip install pandasai[bedrock] ``` -------------------------------- ### SmartDataframe Configuration Options Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v3/smart-dataframes.mdx Explains how to configure the SmartDataframe class during initialization. It shows how to pass parameters such as the LLM instance, logging preferences, and verbosity settings. ```python smart_df = SmartDataframe(df, config={ "llm": llm, # LLM instance "save_logs": True, # Save conversation logs "verbose": False # Print detailed logs }) ``` -------------------------------- ### Install LangChain Support for PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/llms.mdx Install the necessary package to enable PandasAI's integration with LangChain models. ```bash pip install pandasai[langchain] ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/CONTRIBUTING.md Installs pre-commit Git hooks to ensure code quality and adherence to project standards before committing changes. ```bash pre-commit install ``` -------------------------------- ### Install IBM watsonx.ai Support for PandasAI Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/llms.mdx Install the necessary package to enable PandasAI's integration with IBM watsonx.ai models. ```bash pip install pandasai[ibm-watsonx-ai] ``` -------------------------------- ### Install PandasAI Docker Sandbox Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/README.md Installs the pandasai-docker package, which is required to run PandasAI within a Docker sandbox environment for enhanced security. ```bash pip install "pandasai-docker" ``` -------------------------------- ### PandasAI Agent Initialization and Query Source: https://github.com/sinaptik-ai/pandas-ai/blob/main/docs/v2/intro.mdx This snippet demonstrates how to initialize a PandasAI Agent with a DataFrame and ask a natural language question to retrieve data. It requires the pandas and pandasai libraries. ```Python import os import pandas as pd from pandasai import Agent # Sample DataFrame sales_by_country = pd.DataFrame({ "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"], "sales": [5000, 3200, 2900, 4100, 2300, 2100, 2500, 2600, 4500, 7000] }) agent = Agent(sales_by_country) agent.chat('Which are the top 5 countries by sales?') ## Output # China, United States, Japan, Germany, Australia ```