Skip to content

feat: enhance ZAKU AI parser — better amount parsing & category mapping - #107

Merged
dhabyap merged 1 commit into
mainfrom
feature/zaku-ai-enhancement
Sep 7, 2026
Merged

feat: enhance ZAKU AI parser — better amount parsing & category mapping#107
dhabyap merged 1 commit into
mainfrom
feature/zaku-ai-enhancement

Conversation

@dhabyap

@dhabyap dhabyap commented Sep 7, 2026

Copy link
Copy Markdown
Owner

Summary

Enhance AiTransactionParserService::systemPrompt() untuk parsing transaksi Indonesia yang lebih akurat.

Changes

System Prompt Enhancement

  • Amount parsing rules: handle 13rb, 13k, 1jt, 1,5jt, typo (bayarrr, rugiii)
  • Context-aware type detection: bedain transfer ke (expense) vs transfer dari (income)
  • Priority category mapping: grab=TRANSPORTASI, pulsa=TELEKOMUNIKASI, netflix=HIBURAN, dll
  • Example outputs: 3 contoh untuk guide AI
  • Edge cases: no amount, no clear item, typo handling

Test Cases

Input Expected
Beli kopi 13rb amount: 13000
Grab 18rb category: TRANSPORTASI
Beli pulsa 10rb category: TELEKOMUNIKASI
Transfer ke ibu 200rb type: expense
Netflix 109rb category: HIBURAN

Related

Closes #106

Summary by Sourcery

Enhance the ZAKU AI transaction parser to produce more accurate structured transactions from Indonesian user messages.

New Features:

  • Improve Indonesian transaction parsing with localized amount formats, contextual income and expense detection, and prioritized category mapping.

Enhancements:

  • Add typo tolerance, missing-information handling, and representative JSON output examples to guide parser responses.

- Add amount parsing rules: rb, k, jt, juta, koma, typo handling
- Add context-aware type detection: transfer ke/dari, bayar/dapat utang
- Add priority category mapping: grab=TRANSPORTASI, pulsa=TELEKOMUNIKASI, etc.
- Add example outputs for better AI understanding
- Add edge cases handling

Closes #106
@sourcery-ai

sourcery-ai Bot commented Sep 7, 2026

Copy link
Copy Markdown

Reviewer's Guide

Enhances the AI parser’s system prompt with Indonesian-specific amount normalization, contextual transaction-type detection, prioritized category mapping, structured edge-case handling, and example-driven JSON output guidance.

Flow diagram for enhanced ZAKU AI transaction parsing

flowchart TD
    A["Indonesian user message"] --> B["ZAKU AI systemPrompt"]
    B --> C["Normalize amount\n13rb → 13000; 1,5jt → 1500000"]
    C --> D["Detect transaction type\ntransfer ke → expense\ntransfer dari → income"]
    D --> E["Apply priority category mapping\nGrab → TRANSPORTASI\nPulsa → TELEKOMUNIKASI\nNetflix → HIBURAN"]
    E --> F["Handle typos and edge cases"]
    F --> G["Return JSON transaction\nor request missing amount/item"]
Loading

File-Level Changes

Change Details Files
Replaced the minimal extraction prompt with a structured Indonesian transaction-parsing specification.
  • Added normalization rules for informal rupiah amounts, decimal million notation, and common typos.
  • Defined context-aware income and expense detection, including directional transfer phrases.
  • Added prioritized keyword-to-category mappings with a fallback category.
  • Specified strict JSON output, null-field edge-case responses, and description/amount constraints.
  • Added representative examples for coffee, transport, and transfers.
app/Services/AiTransactionParserService.php

Assessment against linked issues

Issue Objective Addressed Explanation
#106 Parse Indonesian amount formats such as 13rb, 13k, 1jt, and 1,5jt, while tolerating specified typos.
#106 Categorize transactions using Indonesian keyword mappings, including Grab as TRANSPORTASI, pulsa as TELEKOMUNIKASI, Netflix as HIBURAN, and other listed categories.
#106 Distinguish transaction types contextually, especially transfer ke as expense and transfer dari as income.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="app/Services/AiTransactionParserService.php" line_range="211" />
<code_context>
-Use "expense" if the message contains: beli, bayar, belanja, isi, sewa, tagihan, potong, hutang.
-category should be one of: {$categories}.
-If the message has no amount, set description, amount, category, and type to null.
+        return <<<'PROMPT'
+You are ZAKU AI, an Indonesian personal finance assistant.
+Your task: Parse user message into one finance transaction.
+
+## CATEGORIES AVAILABLE
+{$categories}
+
+## AMOUNT PARSING RULES
</code_context>
<issue_to_address>
**issue (bug_risk):** The nowdoc does not interpolate `{$categories}`, so every provider receives the literal placeholder instead of the database category list. The model therefore cannot reliably restrict its category output to categories that `resolveCategory()` can resolve.

**Suggested fix:** Use an interpolating heredoc (`<<<PROMPT`) or concatenate the category list into the nowdoc.

```suggestion
        return <<<PROMPT
```
</issue_to_address>

### Comment 2
<location path="app/Services/AiTransactionParserService.php" line_range="239-248" />
<code_context>
+- "dapat utang dari [orang]" → INCOME
+
+## CATEGORY MAPPING (priority order)
+1. TELEKOMUNIKASI: pulsa, axis, telkomsel, xl, indosat, paket data
+2. TRANSPORTASI: grab, gojek, bjrt, taxi, parkir, tol, bensin, ojol
+3. HIBURAN: netflix, spotify, youtube, film, nonton, game
+4. KESEHATAN: obat, apotek, dokter, rs, vitamin
+5. KECANTIKAN: shampoo, sabun, parfum, skincare, salon
+6. PENDIDIKAN: buku, les, kursus, sekolah
+7. MAKANAN: makan, minum, kopi, teh, roti, gorengan, jajan, warteg
+8. TAGIHAN: listrik, air, internet, bpjs
+9. GAJI: salary, gajian, paycheck
+10. LAINNYA: default (fallback)
</code_context>
<issue_to_address>
**issue (bug_risk):** The prompt instructs the model to return categories such as `TELEKOMUNIKASI`, `KESEHATAN`, `KECANTIKAN`, and `PENDIDIKAN`, but the seeded categories are `TAGIHAN`, `HIBURAN`, `MAKANAN`, `TRANSPORT`, `GAJI`, and `LAINNYA`. For example, `Beli pulsa 10rb` produces `TELEKOMUNIKASI`, which `resolveCategory()` cannot find and therefore stores as `LAINNYA` instead of the intended category.

**Triggers:** When the AI returns one of the newly introduced category names that is not present in the database.

**Suggested fix:** Align the prompt mappings with the actual category names, or add and migrate the new categories before asking the model to return them.

```suggestion
1. TAGIHAN: pulsa, axis, telkomsel, xl, indosat, paket data, listrik, air, internet, bpjs
2. TRANSPORT: grab, gojek, bjrt, taxi, parkir, tol, bensin, ojol
3. HIBURAN: netflix, spotify, youtube, film, nonton, game
4. MAKANAN: makan, minum, kopi, teh, roti, gorengan, jajan, warteg
5. GAJI: salary, gajian, paycheck
6. LAINNYA: obat, apotek, dokter, rs, vitamin, shampoo, sabun, parfum, skincare, salon, buku, les, kursus, sekolah, default (fallback)
```
</issue_to_address>

### Comment 3
<location path="app/Services/AiTransactionParserService.php" line_range="234" />
<code_context>
+
+Context rules:
+- "transfer ke [orang]" → EXPENSE
+- "transfer dari [orang]" → INCOME
+- "bayar utang ke [orang]" → EXPENSE
+- "dapat utang dari [orang]" → INCOME
</code_context>
<issue_to_address>
**issue (broader_impact):** The prompt declares every `transfer dari [orang]` message to be income, but `aiChat()` applies a later keyword override that does not recognize `transfer dari` and can classify the same message as expense when it also contains the existing `untuk` keyword. Thus an input such as `transfer dari ibu untuk beli laptop 200rb` is persisted with the wrong type.

**Triggers:** When a `transfer dari` message also contains an expense override keyword such as `untuk`.

**Suggested fix:** Apply the context-aware transfer rules in the controller override as well, or remove the conflicting post-processing and centralize type detection.
</issue_to_address>

### Comment 4
<location path="app/Services/AiTransactionParserService.php" line_range="261" />
<code_context>
+}
+
+## EDGE CASES
+- No amount → {"description":null,"amount":null,"category":null,"type":null,"response":" berapa jumlahnya?"}
+- No clear item → {"description":null,"amount":null,"category":null,"type":null,"response":" beli/bayar apa?"}
+- Typo → still parse (don't fail)
</code_context>
<issue_to_address>
**nitpick:** The no-amount response begins with a leading space (`" berapa jumlahnya?"`), so the API returns a visibly malformed response whenever this edge case is selected.

**Triggers:** When the model follows the no-amount edge-case example.

**Suggested fix:** Remove the leading space from the response string.

```suggestion
- No amount → {"description":null,"amount":null,"category":null,"type":null,"response":"berapa jumlahnya?"}
```
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 3 findings to address first, and the prompt can cause incorrect amounts, transaction types, or categories to be written to users’ financial records; changing the heredoc to a nowdoc also leaves {$categories} uninterpolated, so category validation may fail or produce mismatches. Reverting stops future bad parses but does not repair records already created, which must be identified and corrected or rerun.

Blocking findings: app/Services/AiTransactionParserService.php:211, app/Services/AiTransactionParserService.php:248, app/Services/AiTransactionParserService.php:234


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Use "expense" if the message contains: beli, bayar, belanja, isi, sewa, tagihan, potong, hutang.
category should be one of: {$categories}.
If the message has no amount, set description, amount, category, and type to null.
return <<<'PROMPT'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The nowdoc does not interpolate {$categories}, so every provider receives the literal placeholder instead of the database category list. The model therefore cannot reliably restrict its category output to categories that resolveCategory() can resolve.

Suggested fix: Use an interpolating heredoc (<<<PROMPT) or concatenate the category list into the nowdoc.

Suggested change
return <<<'PROMPT'
return <<<PROMPT

Comment on lines +239 to +248
1. TELEKOMUNIKASI: pulsa, axis, telkomsel, xl, indosat, paket data
2. TRANSPORTASI: grab, gojek, bjrt, taxi, parkir, tol, bensin, ojol
3. HIBURAN: netflix, spotify, youtube, film, nonton, game
4. KESEHATAN: obat, apotek, dokter, rs, vitamin
5. KECANTIKAN: shampoo, sabun, parfum, skincare, salon
6. PENDIDIKAN: buku, les, kursus, sekolah
7. MAKANAN: makan, minum, kopi, teh, roti, gorengan, jajan, warteg
8. TAGIHAN: listrik, air, internet, bpjs
9. GAJI: salary, gajian, paycheck
10. LAINNYA: default (fallback)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The prompt instructs the model to return categories such as TELEKOMUNIKASI, KESEHATAN, KECANTIKAN, and PENDIDIKAN, but the seeded categories are TAGIHAN, HIBURAN, MAKANAN, TRANSPORT, GAJI, and LAINNYA. For example, Beli pulsa 10rb produces TELEKOMUNIKASI, which resolveCategory() cannot find and therefore stores as LAINNYA instead of the intended category.

Triggers: When the AI returns one of the newly introduced category names that is not present in the database.

Suggested fix: Align the prompt mappings with the actual category names, or add and migrate the new categories before asking the model to return them.

Suggested change
1. TELEKOMUNIKASI: pulsa, axis, telkomsel, xl, indosat, paket data
2. TRANSPORTASI: grab, gojek, bjrt, taxi, parkir, tol, bensin, ojol
3. HIBURAN: netflix, spotify, youtube, film, nonton, game
4. KESEHATAN: obat, apotek, dokter, rs, vitamin
5. KECANTIKAN: shampoo, sabun, parfum, skincare, salon
6. PENDIDIKAN: buku, les, kursus, sekolah
7. MAKANAN: makan, minum, kopi, teh, roti, gorengan, jajan, warteg
8. TAGIHAN: listrik, air, internet, bpjs
9. GAJI: salary, gajian, paycheck
10. LAINNYA: default (fallback)
1. TAGIHAN: pulsa, axis, telkomsel, xl, indosat, paket data, listrik, air, internet, bpjs
2. TRANSPORT: grab, gojek, bjrt, taxi, parkir, tol, bensin, ojol
3. HIBURAN: netflix, spotify, youtube, film, nonton, game
4. MAKANAN: makan, minum, kopi, teh, roti, gorengan, jajan, warteg
5. GAJI: salary, gajian, paycheck
6. LAINNYA: obat, apotek, dokter, rs, vitamin, shampoo, sabun, parfum, skincare, salon, buku, les, kursus, sekolah, default (fallback)


Context rules:
- "transfer ke [orang]" → EXPENSE
- "transfer dari [orang]" → INCOME

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (broader_impact): The prompt declares every transfer dari [orang] message to be income, but aiChat() applies a later keyword override that does not recognize transfer dari and can classify the same message as expense when it also contains the existing untuk keyword. Thus an input such as transfer dari ibu untuk beli laptop 200rb is persisted with the wrong type.

Triggers: When a transfer dari message also contains an expense override keyword such as untuk.

Suggested fix: Apply the context-aware transfer rules in the controller override as well, or remove the conflicting post-processing and centralize type detection.

}

## EDGE CASES
- No amount → {"description":null,"amount":null,"category":null,"type":null,"response":" berapa jumlahnya?"}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: The no-amount response begins with a leading space (" berapa jumlahnya?"), so the API returns a visibly malformed response whenever this edge case is selected.

Triggers: When the model follows the no-amount edge-case example.

Suggested fix: Remove the leading space from the response string.

Suggested change
- No amount → {"description":null,"amount":null,"category":null,"type":null,"response":" berapa jumlahnya?"}
- No amount → {"description":null,"amount":null,"category":null,"type":null,"response":"berapa jumlahnya?"}

@dhabyap
dhabyap merged commit be8a0ba into main Sep 7, 2026
1 of 2 checks passed
@dhabyap
dhabyap deleted the feature/zaku-ai-enhancement branch September 7, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P1] Enhance ZAKU AI Parser — Better Transaction Parsing

1 participant