Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import pandas as pd
```
{% endif %}
{% include 'shared/vectordb_docs.tmpl' with context %}
{% if context.memory.count() > 1 %}
### PREVIOUS CONVERSATION
{{ context.memory.get_previous_conversation() }}
{% endif %}
{{ context.memory.get_last_message() }}

At the end, declare "result" variable as a dictionary of type and value in the following format:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from pandasai.core.prompts import GeneratePythonCodeWithSQLPrompt
from pandasai.helpers.memory import Memory


@pytest.fixture
Expand All @@ -17,6 +18,17 @@ def mock_context():
return context


@pytest.fixture
def render_context():
"""Context with real Memory for template rendering tests."""
context = Mock()
context.dfs = []
context.skills = []
context.config.direct_sql = True
context.vectorstore = None
return context


def test_to_json(mock_context):
"""Test that to_json returns the expected structure with all required fields"""
prompt = GeneratePythonCodeWithSQLPrompt(context=mock_context, output_type="code")
Expand Down Expand Up @@ -46,3 +58,36 @@ def test_to_json(mock_context):
assert result["config"]["direct_sql"] is True
assert "output_type" in result["config"]
assert result["config"]["output_type"] == "code"


def test_template_includes_previous_conversation(render_context):
"""Test that the rendered template includes previous conversation history."""
memory = Memory(memory_size=10)
memory.add("What is the average salary?", is_user=True)
memory.add('result = {"type": "number", "value": 50000}', is_user=False)
memory.add("Show me the top 5 earners", is_user=True)
render_context.memory = memory

prompt = GeneratePythonCodeWithSQLPrompt(
context=render_context, last_code_generated=None, output_type="number"
)
rendered = prompt.to_string()

assert "PREVIOUS CONVERSATION" in rendered
assert "average salary" in rendered
assert "top 5 earners" in rendered


def test_template_no_previous_conversation_for_single_message(render_context):
"""Test that single-message memory does not show PREVIOUS CONVERSATION."""
memory = Memory(memory_size=10)
memory.add("What is the average salary?", is_user=True)
render_context.memory = memory

prompt = GeneratePythonCodeWithSQLPrompt(
context=render_context, last_code_generated=None, output_type="number"
)
rendered = prompt.to_string()

assert "PREVIOUS CONVERSATION" not in rendered
assert "average salary" in rendered