Sinaptik-AI / pandas-ai

Chat with your database (SQL, CSV, pandas, polars, mongodb, noSQL, etc). PandasAI makes data analysis conversational using LLMs (GPT 3.5 / 4, Anthropic, VertexAI) and RAG.
https://pandas-ai.com
Other
13.61k stars 1.32k forks source link

Issue on docs #1377

Open vsneelesh opened 2 months ago

vsneelesh commented 2 months ago

I would like to know if we can connect Agents with connectors. If yes how can it be done. There is no information on how can we connect PandasAi agents with connectors. The only way it with smartdataframe or smartdatalake

dosubot[bot] commented 2 months ago

Yes, you can connect PandasAI agents with connectors. This can be done by creating an instance of the desired connector (e.g., AirtableConnector or PostgreSQLConnector) and then passing it to the Agent class. Here is an example of how it can be done:

import os
from pandasai import Agent
from pandasai.connectors import AirtableConnector

airtable_connector = AirtableConnector(
    config={
        "token": "AIRTABLE_API_TOKEN",
        "table": "AIRTABLE_TABLE_NAME",
        "base_id": "AIRTABLE_BASE_ID",
        "where": [
            # this is optional and filters the data to
            # reduce the size of the dataframe
            ["Status", "=", "In progress"]
        ],
    }
)

# By default, unless you choose a different LLM, it will use BambooLLM.
# You can get your free API key signing up at https://pandabi.ai (you can also configure it in your .env file)
os.environ["PANDASAI_API_KEY"] = "your-api-key"

agent = Agent(airtable_connector)

response = agent.chat("How many rows are there in data?")
print(response)

Additionally, you can connect multiple connectors and define relationships between tables. Here is an example using PostgreSQL connectors:

from pandasai.agent.base import Agent
from pandasai.connectors.sql import PostgreSQLConnector
from pandasai.ee.connectors.relations import ForeignKey, PrimaryKey
from pandasai.llm.openai import OpenAI

llm = OpenAI("sk-*************")

config_ = {"llm": llm, "direct_sql": True}

payment_connector = PostgreSQLConnector(
    config={
        "host": "localhost",
        "port": 5432,
        "database": "testdb",
        "username": "postgres",
        "password": "*****",
        "table": "orders",
    },
    connector_relations=[
        PrimaryKey("id"),
        ForeignKey(
            field="customer_id", foreign_table="customers", foreign_table_field="id"
        ),
    ],
)

customer_connector = PostgreSQLConnector(
    config={
        "host": "localhost",
        "port": 5432,
        "database": "mydb",
        "username": "root",
        "password": "root",
        "table": "customers",
    },
    connector_relations=[PrimaryKey("id")],
)

agent = Agent([payment_connector, customer_connector], config=config_, memory_size=10)

response = agent.chat("return orders count groupby country")
print(response)

In this example, the PostgreSQLConnector is configured with the necessary parameters and relationships between tables are defined using PrimaryKey and ForeignKey. The Agent class can accept various types of data sources, including Pandas DataFrames, connectors, and lists of these, making it versatile for different data integration needs [1][2][3][4].

To continue talking to Dosu, mention @dosu.