Skip to content

Commit

Permalink
feat: save logs (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
gventuri committed Jun 28, 2023
1 parent 809d9be commit 3ffa895
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 13 additions & 0 deletions pandasai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class PandasAI(Shortcuts):
to None
_cache (Cache, optional): Cache object to store the results. Default to None
_enable_cache (bool, optional): Whether to enable cache. Default to True
_logger (logging.Logger, optional): Logger object to log the messages. Default
to None
_logs (List[dict], optional): List of logs to be stored. Default to []
_prompt_id (str, optional): Unique ID to differentiate calls. Default to None
_middlewares (List[Middleware], optional): List of middlewares to run. Default
to [ChartsMiddleware()]
Expand Down Expand Up @@ -151,6 +154,8 @@ class PandasAI(Shortcuts):
_custom_whitelisted_dependencies: List[str] = []
_start_time: float = 0
_enable_logging: bool = True
_logger: logging.Logger = None
_logs: List[str] = []
last_code_generated: Optional[str] = None
last_code_executed: Optional[str] = None
code_output: Optional[str] = None
Expand Down Expand Up @@ -223,6 +228,7 @@ def __init__(
self._save_charts = save_charts
self._save_charts_path = save_charts_path
self._process_id = str(uuid.uuid4())
self._logs = []

self._non_default_prompts = (
{} if non_default_prompts is None else non_default_prompts
Expand Down Expand Up @@ -705,11 +711,18 @@ def run_code(
def log(self, message: str):
"""Log a message"""
self._logger.info(message)
self._logs.append(message)

@property
def logs(self) -> List[str]:
"""Return the logs"""
return self._logs

def process_id(self) -> str:
"""Return the id of this PandasAI object."""
return self._process_id

@property
def last_prompt_id(self) -> str:
"""Return the id of the last prompt that was run."""
if self._prompt_id is None:
Expand Down
14 changes: 12 additions & 2 deletions tests/test_pandasai.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,12 @@ def test_process_id(self, pandasai):

def test_last_prompt_id(self, pandasai):
pandasai(pd.DataFrame(), "How many countries are in the dataframe?")
prompt_id = pandasai.last_prompt_id()
prompt_id = pandasai.last_prompt_id
assert isinstance(UUID(prompt_id, version=4), UUID)

def test_last_prompt_id_no_prompt(self, pandasai):
with pytest.raises(ValueError):
pandasai.last_prompt_id()
pandasai.last_prompt_id

def test_add_middlewares(self, pandasai, test_middleware):
middleware = test_middleware()
Expand Down Expand Up @@ -780,3 +780,13 @@ def test_replace_correct_multiple_dataframes_error_prompt(self, llm):
+ "\n\nCode:\n"
)
assert llm.last_prompt == expected_last_prompt

def test_saves_logs(self, llm):
pandas_ai = PandasAI(llm)
assert pandas_ai.logs == []
pandas_ai.log("a")
pandas_ai.log("b")
assert pandas_ai.logs == [
"a",
"b",
]

0 comments on commit 3ffa895

Please sign in to comment.