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
4 changes: 4 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- bump: patch
changes:
fixed:
- Currency symbol display in AI explainer now shows correct symbol for each country (pound for UK, dollar for US/CA, etc)
11 changes: 5 additions & 6 deletions policyengine_api/services/tracer_analysis_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def execute_analysis(

# Get the appropriate prompt template based on country
prompt_template = self._get_prompt_template(country_id)

# Add the parsed tracer output to the prompt
prompt = prompt_template.format(
variable=variable, tracer_segment=tracer_segment
Expand Down Expand Up @@ -140,7 +140,7 @@ def _parse_tracer_output(self, tracer_output, target_variable):

def _get_prompt_template(self, country_id: str) -> str:
"""Get the appropriate prompt template with correct currency symbol based on country."""

# Determine currency instruction based on country
currency_instructions = {
"uk": "The response will be rendered as markdown, so preface £ with \\.",
Expand All @@ -149,12 +149,11 @@ def _get_prompt_template(self, country_id: str) -> str:
"il": "The response will be rendered as markdown, so preface ₪ with \\.",
"ng": "The response will be rendered as markdown, so preface ₦ with \\.",
}

currency_note = currency_instructions.get(
country_id,
"The response will be rendered as markdown."
country_id, "The response will be rendered as markdown."
)

return f"""{anthropic.HUMAN_PROMPT} You are an AI assistant explaining policy calculations.
The user has run a simulation for the variable '{{variable}}'.
Here's the tracer output:
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/services/test_tracer_analysis_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,72 @@ def test_tracer_output_for_variable_that_is_substring_of_another():
spliced_valid_tracer_output_for_variable_that_is_substring_of_another
)
assert result == expected_output


def test_get_prompt_template_for_uk():
# Given: UK country code
country_id = "uk"

# When: Getting the prompt template
template = test_service._get_prompt_template(country_id)

# Then: It should include £ currency symbol instruction
assert "preface £ with \\." in template
assert "preface $" not in template


def test_get_prompt_template_for_us():
# Given: US country code
country_id = "us"

# When: Getting the prompt template
template = test_service._get_prompt_template(country_id)

# Then: It should include $ currency symbol instruction
assert "preface $ with \\." in template
assert "preface £" not in template


def test_get_prompt_template_for_canada():
# Given: Canada country code
country_id = "ca"

# When: Getting the prompt template
template = test_service._get_prompt_template(country_id)

# Then: It should include $ currency symbol instruction
assert "preface $ with \\." in template


def test_get_prompt_template_for_israel():
# Given: Israel country code
country_id = "il"

# When: Getting the prompt template
template = test_service._get_prompt_template(country_id)

# Then: It should include ₪ currency symbol instruction
assert "preface ₪ with \\." in template


def test_get_prompt_template_for_nigeria():
# Given: Nigeria country code
country_id = "ng"

# When: Getting the prompt template
template = test_service._get_prompt_template(country_id)

# Then: It should include ₦ currency symbol instruction
assert "preface ₦ with \\." in template


def test_get_prompt_template_for_unknown_country():
# Given: Unknown country code
country_id = "xx"

# When: Getting the prompt template
template = test_service._get_prompt_template(country_id)

# Then: It should still return a valid template without currency-specific instruction
assert "The response will be rendered as markdown." in template
assert "preface" not in template or "with \\." not in template
Loading