Skip to content

Commit

Permalink
docs: move prompt templates to files (#593)
Browse files Browse the repository at this point in the history
* (docs): update docstrings, update custom-prompts.md
* (chore): enhance type hinting
* (fix): add missing `FileBasedPrompt` in module __init__.py
  • Loading branch information
nautics889 committed Sep 27, 2023
1 parent a228bfe commit 6a4695d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
29 changes: 28 additions & 1 deletion docs/custom-prompts.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ from pandasai.prompts import AbstractPrompt


class MyCustomPrompt(AbstractPrompt):
template = """This is your custom text for your prompt with custom {my_custom_value}"""
@property
def template(self):
return """This is your custom text for your prompt with custom {my_custom_value}"""


df = SmartDataframe("data.csv", {
Expand All @@ -31,6 +33,31 @@ df = SmartDataframe("data.csv", {
})
```

You can also use `FileBasedPrompt` in case you prefer to store prompt template in a file:

_my_prompt_template.tmpl:_
```
This is your custom text for your prompt with custom {my_custom_value}
```
_python code:_

```python
from pandasai import SmartDataframe
from pandasai.prompts import FileBasedPrompt


class MyCustomFileBasedPrompt(FileBasedPrompt):
_path_to_template = "path/to/my_prompt_template.tmpl"


df = SmartDataframe("data.csv", {
"custom_prompts": {
"generate_python_code": MyCustomFileBasedPrompt(
my_custom_value="my custom value")
}
})
```

## Using dynamic prompt values

### Variable interpolation
Expand Down
3 changes: 2 additions & 1 deletion pandasai/prompts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .base import AbstractPrompt
from .base import AbstractPrompt, FileBasedPrompt
from .correct_error_prompt import CorrectErrorPrompt
from .generate_python_code import GeneratePythonCodePrompt

__all__ = [
"AbstractPrompt",
"CorrectErrorPrompt",
"GeneratePythonCodePrompt",
"FileBasedPrompt",
]
14 changes: 11 additions & 3 deletions pandasai/prompts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@


class AbstractPrompt(ABC):
"""Base class to implement a new Prompt"""
"""Base class to implement a new Prompt.
Inheritors have to override `template` property.
"""

_args = {}

Expand Down Expand Up @@ -47,7 +50,7 @@ def _generate_dataframes(self, dfs):

@property
@abstractmethod
def template(self):
def template(self) -> str:
...

def set_var(self, var, value):
Expand All @@ -63,6 +66,11 @@ def __str__(self):


class FileBasedPrompt(AbstractPrompt):
"""Base class for prompts supposed to read template content from a file.
`_path_to_template` attribute has to be specified.
"""

_path_to_template: str

def __init__(self, **kwargs):
Expand All @@ -72,7 +80,7 @@ def __init__(self, **kwargs):
super().__init__(**kwargs)

@property
def template(self):
def template(self) -> str:
if not os.path.exists(self._path_to_template):
raise FileNotFoundError(
f"Unable to find a file with template at '{self._path_to_template}' "
Expand Down

0 comments on commit 6a4695d

Please sign in to comment.