Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prompt_id): attribute missing error #1509

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandasai/core/code_generation/code_cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def _handle_charts(self, code: str) -> str:
return add_save_chart(
code,
logger=self.context.logger,
file_name=str(self.context.prompt_id),
file_name=str(self.context.last_prompt_id),
save_charts_path_str=self.context.config.save_charts_path,
)
return add_save_chart(
Expand Down
8 changes: 4 additions & 4 deletions pandasai/core/response/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .parser import ResponseParser
from .base import BaseResponse
from .string import StringResponse
from .number import NumberResponse
from .dataframe import DataFrameResponse
from .chart import ChartResponse
from .dataframe import DataFrameResponse
from .number import NumberResponse
from .parser import ResponseParser
from .string import StringResponse

__all__ = [
"ResponseParser",
Expand Down
2 changes: 1 addition & 1 deletion pandasai/core/response/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any
import json
from typing import Any


class BaseResponse:
Expand Down
5 changes: 3 additions & 2 deletions pandasai/core/response/chart.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any
from PIL import Image
import base64
import io
from typing import Any

from PIL import Image

from .base import BaseResponse

Expand Down
1 change: 1 addition & 0 deletions pandasai/core/response/dataframe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any

import pandas as pd

from .base import BaseResponse
Expand Down
4 changes: 2 additions & 2 deletions pandasai/core/response/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from pandasai.exceptions import InvalidOutputValueMismatch

from .base import BaseResponse
from .chart import ChartResponse
from .dataframe import DataFrameResponse
from .number import NumberResponse
from .string import StringResponse
from .dataframe import DataFrameResponse
from .chart import ChartResponse


class ResponseParser:
Expand Down
2 changes: 1 addition & 1 deletion pandasai/dataframe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

import pandasai as pai
from pandasai.config import Config
from pandasai.core.response import BaseResponse
from pandasai.exceptions import DatasetNotFound, PandasAIApiKeyError
from pandasai.helpers.dataframe_serializer import (
DataframeSerializer,
DataframeSerializerType,
)
from pandasai.helpers.path import find_project_root
from pandasai.helpers.request import get_pandaai_session
from pandasai.core.response import BaseResponse

if TYPE_CHECKING:
from pandasai.agent.base import Agent
Expand Down
73 changes: 72 additions & 1 deletion tests/unit_tests/core/code_generation/test_code_cleaning.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ast
import unittest
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

from pandasai.agent.state import AgentState
from pandasai.core.code_generation.code_cleaning import CodeCleaner
Expand Down Expand Up @@ -174,6 +174,77 @@ def test_extract_fix_dataframe_redeclarations(self):
)
self.assertIsInstance(updated_node, ast.AST)

@patch(
"pandasai.core.code_generation.code_cleaning.add_save_chart"
) # Replace with actual module name
def test_handle_charts_save_charts_true(self, mock_add_save_chart):
handler = self.cleaner
handler.context = MagicMock()
handler.context.config.save_charts = True
handler.context.logger = MagicMock() # Mock logger
handler.context.last_prompt_id = 123
handler.context.config.save_charts_path = "/custom/path"

code = 'some text "temp_chart.png" more text'

handler._handle_charts(code)

mock_add_save_chart.assert_called_once_with(
code,
logger=handler.context.logger,
file_name="123",
save_charts_path_str="/custom/path",
)

@patch("pandasai.core.code_generation.code_cleaning.add_save_chart")
@patch(
"pandasai.core.code_generation.code_cleaning.find_project_root",
return_value="/root/project",
) # Mock project root
def test_handle_charts_save_charts_false(
self, mock_find_project_root, mock_add_save_chart
):
handler = self.cleaner
handler.context = MagicMock()
handler.context.config.save_charts = False
handler.context.logger = MagicMock()
handler.context.last_prompt_id = 123

code = 'some text "temp_chart.png" more text'

handler._handle_charts(code)

mock_add_save_chart.assert_called_once_with(
code,
logger=handler.context.logger,
file_name="temp_chart",
save_charts_path_str="/root/project/exports/charts",
)

def test_handle_charts_empty_code(self):
handler = self.cleaner

code = ""
expected_code = "" # It should remain empty, as no substitution is made

result = handler._handle_charts(code)

self.assertEqual(
result, expected_code, f"Expected '{expected_code}', but got '{result}'"
)

def test_handle_charts_no_png(self):
handler = self.cleaner

code = "some text without png"
expected_code = "some text without png" # No change should occur

result = handler._handle_charts(code)

self.assertEqual(
result, expected_code, f"Expected '{expected_code}', but got '{result}'"
)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion tests/unit_tests/helpers/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import pandas as pd

from pandasai.core.response.parser import ResponseParser
from pandasai.core.response import (
ChartResponse,
DataFrameResponse,
NumberResponse,
StringResponse,
)
from pandasai.core.response.parser import ResponseParser
from pandasai.exceptions import InvalidOutputValueMismatch


Expand Down
6 changes: 4 additions & 2 deletions tests/unit_tests/response/test_chart_response.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest
from PIL import Image
import base64
import io

import pytest
from PIL import Image

from pandasai.core.response.chart import ChartResponse


Expand Down
3 changes: 2 additions & 1 deletion tests/unit_tests/response/test_dataframe_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import pandas as pd
import pytest

from pandasai.core.response.dataframe import DataFrameResponse


Expand Down
Loading