class pipableai.pipable.Pipable(database_connector: DatabaseConnectorInterface, llm_api_client: LlmApiClientInterface)

Bases: object

A Python package for connecting to a remote PostgreSQL server, generating and executing natural language-based data search queries mapped to SQL queries using the pipLLM.

This module provides classes and functions for connecting to a PostgreSQL database and using a language model to generate SQL queries.

Attributes:

database_connector (DatabaseConnectorInterface): The database connector implementing the DatabaseConnectorInterface. llm_api_client (LlmApiClientInterface): The API client implementing the LlmApiClientInterface. connected (bool): A boolean indicating if the Pipable instance is connected to the database. connection: The connection object to the remote PostgreSQL server. logger: The logger object for logging messages and errors. all_table_queries (list): A list to store CREATE TABLE queries for all tables in the database.

ask(question: str, table_names: List[str] | None = None) str

Generate an SQL query.

Args:

table_names (list, optional): The list of table names for the query context. If not provided, it will be auto-generated. question (str): The query to perform in simple English.

Returns:

str: A sql query result.

Raises:

ValueError: If the language model does not generate a valid SQL query.

ask_and_execute(question: str, table_names: List[str] | None) DataFrame

Generate an SQL query and execute it on the PostgreSQL server.

Args:

table_names (list, optional): The list of table names for the query context. If not provided, it will be auto-generated. question (str): The query to perform in simple English.

Returns:

pandas.DataFrame: A DataFrame containing the query result.

Raises:

ValueError: If the language model does not generate a valid SQL query.

connect()

Establish a connection to the Database server.

This method establishes a connection to the remote PostgreSQL server using the provided database connector.

Raises:

ConnectionError: If the connection to the server cannot be established.

disconnect()

Close the connection to the Database server.

This method closes the connection to the remote PostgreSQL server.