Prompts

This module includes some methods on optimally handling prompts when interacting with LLMs.

Base Prompt

A base prompt

pandasai.prompts.base

Base class to implement a new Prompt In order to better handle the instructions, this prompt module is written.

Prompt

Base class to implement a new Prompt

Source code in pandasai/prompts/base.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Prompt:
    """Base class to implement a new Prompt"""

    # pylint: disable=too-few-public-methods

    text = None
    _args = {}

    def __init__(self, **kwargs): # pylint: disable=super-init-not-called

        """
        __init__ method of Base class of Prompt Module
        Args:
            **kwargs: Inferred Keyword Arguments
        """
        if kwargs:
            self._args = kwargs

    def __str__(self):
        if self.text is None:
            raise MethodNotImplementedError

        return self.text.format(**self._args)

__init__(**kwargs)

init method of Base class of Prompt Module

Parameters:

Name Type Description Default
**kwargs

Inferred Keyword Arguments

{}
Source code in pandasai/prompts/base.py
16
17
18
19
20
21
22
23
24
def __init__(self, **kwargs): # pylint: disable=super-init-not-called

    """
    __init__ method of Base class of Prompt Module
    Args:
        **kwargs: Inferred Keyword Arguments
    """
    if kwargs:
        self._args = kwargs

Generate Python Code

A standard prompt is designed to be used when querying the LLMs to generate Python Code.

pandasai.prompts.generate_python_code

Prompt to generate Python code

Today is {today_date}.
You are provided with a pandas dataframe (df) with {num_rows} rows and {num_columns} columns.
This is the result of `print(df.head({rows_to_display}))`:
{df_head}.

When asked about the data, your response should include a python code that describes the
dataframe `df`. Using the provided dataframe, df, return the python code and make sure to prefix
the requested python code with {START_CODE_TAG} exactly and suffix the code with {END_CODE_TAG}
exactly to get the answer to the following question:

GeneratePythonCodePrompt

Bases: Prompt

Prompt to generate Python code

Source code in pandasai/prompts/generate_python_code.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class GeneratePythonCodePrompt(Prompt):
    """Prompt to generate Python code"""
    # pylint: disable=too-few-public-methods

    text: str = """
Today is {today_date}.
You are provided with a pandas dataframe (df) with {num_rows} rows and {num_columns} columns.
This is the result of `print(df.head({rows_to_display}))`:
{df_head}.

When asked about the data, your response should include a python code that describes the dataframe `df`.
Using the provided dataframe, df, return the python code and make sure to prefix the requested python code with {START_CODE_TAG} exactly and suffix the code with {END_CODE_TAG} exactly to get the answer to the following question:
"""

    def __init__(self, **kwargs):
        super().__init__(
            **kwargs,
            START_CODE_TAG=START_CODE_TAG,
            END_CODE_TAG=END_CODE_TAG,
            today_date=date.today()
        )

Generate Python Code On Error

A prompt to generate Python Code on Error

pandasai.prompts.correct_error_prompt

Prompt to correct Python Code on Error

Today is {today_date}.
You are provided with a pandas dataframe (df) with {num_rows} rows and {num_columns} columns.
This is the result of `print(df.head({rows_to_display}))`:
{df_head}.

The user asked the following question:
{question}

You generated this python code:
{code}

It fails with the following error:
{error_returned}

Correct the python code and return a new python code (do not import anything) that fixes the above
mentioned error. Do not generate the same code again. Make sure to prefix the requested python
code with {START_CODE_TAG} exactly and suffix the code with {END_CODE_TAG} exactly.

CorrectErrorPrompt

Bases: Prompt

Prompt to Correct Python code on Error

Source code in pandasai/prompts/correct_error_prompt.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class CorrectErrorPrompt(Prompt):
    """Prompt to Correct Python code on Error"""
    # pylint: disable=too-few-public-methods

    text: str = """
Today is {today_date}.
You are provided with a pandas dataframe (df) with {num_rows} rows and {num_columns} columns.
This is the result of `print(df.head({rows_to_display}))`:
{df_head}.

The user asked the following question:
{question}

You generated this python code:
{code}

It fails with the following error:
{error_returned}

Correct the python code and return a new python code (do not import anything) that fixes the above mentioned error. Do not generate the same code again.
Make sure to prefix the requested python code with {START_CODE_TAG} exactly and suffix the code with {END_CODE_TAG} exactly.
"""

    def __init__(self, **kwargs):
        super().__init__(
            **kwargs,
            START_CODE_TAG=START_CODE_TAG,
            END_CODE_TAG=END_CODE_TAG,
            today_date=date.today()
        )

Generate Response

A prompt to generate Conversational Response

pandasai.prompts.generate_response

Prompt to generate the response to the question in a conversational way

Question: {question}
Answer: {answer}

Rewrite the answer to the question in a conversational way.

GenerateResponsePrompt

Bases: Prompt

Prompt to generate the response to the question in a conversational way

Source code in pandasai/prompts/generate_response.py
14
15
16
17
18
19
20
21
22
23
class GenerateResponsePrompt(Prompt):
    """Prompt to generate the response to the question in a conversational way"""
    # pylint: disable=too-few-public-methods

    text: str = """
Question: {question}
Answer: {answer}

Rewrite the answer to the question in a conversational way.
"""

Generate Python Code Multiple DataFrames

A standard prompt is designed to be used when querying the LLMs to generate Python Code.

pandasai.prompts.multiple_dataframes

Prompt to generate Python code for multiple dataframes

MultipleDataframesPrompt

Bases: Prompt

Prompt to generate Python code

Source code in pandasai/prompts/multiple_dataframes.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class MultipleDataframesPrompt(Prompt):
    """Prompt to generate Python code"""
    # pylint: disable=too-few-public-methods

    text: str = """
Today is {today_date}.
You are provided with the following pandas dataframes:"""
    instruction: str = """
When asked about the data, your response should include a python code that describes the dataframes provided.
Using the provided dataframes and no other dataframes, return the python code and make sure to prefix the requested python code with {START_CODE_TAG} exactly and suffix the code with {END_CODE_TAG} exactly to get the answer to the following question:
"""

    def __init__(self, dataframes: list[pd.DataFrame]): # pylint: disable=super-init-not-called
        for i, dataframe in enumerate(dataframes, start=1):
            row, col = dataframe.shape

            self.text += f"""
Dataframe df{i}, with {row} rows and {col} columns.
This is the result of `print(df{i}.head())`:
{dataframe}"""

        self.text += self.instruction
        self.text = self.text.format(
            today_date=date.today(),
            START_CODE_TAG=START_CODE_TAG,
            END_CODE_TAG=END_CODE_TAG,
        )

    def __str__(self):
        return self.text

Generate Python Code On Error Multiple DataFrames

A prompt to generate Python Code on Error

pandasai.prompts.correct_multiples_prompt

Prompt to correct error

CorrectMultipleDataframesErrorPrompt

Bases: Prompt

Prompt to generate Python code

Source code in pandasai/prompts/correct_multiples_prompt.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class CorrectMultipleDataframesErrorPrompt(Prompt):
    """Prompt to generate Python code"""
    # pylint: disable=too-few-public-methods

    text: str = """
You are provided with the following pandas dataframes:"""

    def __init__(
        self,
        code: str,
        error_returned: Exception,
        question: str,
        df_head: list[pd.DataFrame],
    ): # pylint: disable=super-init-not-called
        for i, dataframe in enumerate(df_head, start=1):
            row, col = dataframe.shape
            self.text += f"""
Dataframe df{i}, with {row} rows and {col} columns.
This is the result of `print(df{i}.head())`:
{dataframe}"""

        instruction: str = f"""
The user asked the following question:
{question}

You generated this python code:
{code}

It fails with the following error:
{error_returned}

Correct the python code and return a new python code (do not import anything) that fixes the above mentioned error. Do not generate the same code again.
Make sure to prefix the requested python code with {START_CODE_TAG} exactly and suffix the code with {END_CODE_TAG} exactly.
"""

        self.text += instruction

    def __str__(self):
        return self.text