# 4. Mock AI Rule Generation

## Current implementation

Auto-Train hien tai dung **pattern matching**, chua goi OpenAI hay Claude.

Muc dich:

- test full pipeline ma khong ton chi phi API
- de debug de hon
- cho phep review logic generate rule truoc khi doi sang LLM

Code nam trong:

- `backend/api/autotrain/autotrain_route.py`

---

## Supported patterns

### Pattern 1: Weather / season mismatch

Trigger khi feedback co cac tu nhu:

- `nong`
- `nóng`
- `troi nong`
- `trời nóng`
- `35 do`
- `35 độ`
- `he`
- `hè`
- `mua he`
- `mùa hè`

Va query co cac tu nhu:

- `ao khoac`
- `áo khoác`
- `hoodie`
- `ni`
- `nỉ`
- `day`
- `dày`

Output mau:

```json
{
  "rule_type": "prompt_update",
  "old_rule": "- Luon uu tien goi y ao khoac/day de tang upsale.",
  "new_rule": "- Khi troi nong (tren 30 do): tuyet doi khong goi y ao khoac day.\n- Chi goi y ao phong, chat lieu mong nhe, thoang mat.",
  "reasoning": "User reports hot weather, but the AI suggested heavy clothing.",
  "confidence": 0.85
}
```

---

### Pattern 2: Size / fit issue

Trigger:

- `dang lun`
- `dáng lùn`
- `thap`
- `thấp`
- `ngan`
- `ngắn`
- `maxi`
- `cham dat`
- `chạm đất`

Output mau:

```json
{
  "rule_type": "prompt_update",
  "old_rule": "- Goi y dam maxi cho moi khach.",
  "new_rule": "- Voi khach cao duoi 1m55, uu tien dam midi hoac tren goi. Tranh maxi cham dat.",
  "reasoning": "Shorter users need proportion-aware suggestions.",
  "confidence": 0.9
}
```

---

### Pattern 3: Excessive emoji

Trigger:

- `emoji`
- `chap tay`
- `chắp tay`
- `🙏`

Output mau:

```json
{
  "rule_type": "prompt_update",
  "old_rule": "- Su dung nhieu emoji de tao thien cam.",
  "new_rule": "- Han che emoji: toi da 1-2 emoji moi tin nhan. Khong dung emoji chap tay trong tu van mua hang.",
  "reasoning": "The feedback asks for a more professional tone.",
  "confidence": 0.8
}
```

---

### Fallback

Neu khong match pattern nao, system van tao fallback rule:

```json
{
  "rule_type": "prompt_update",
  "old_rule": null,
  "new_rule": "- Dieu chinh tone va content dua tren feedback cua user: '...'",
  "reasoning": "Generic fallback rule generated from direct user feedback.",
  "confidence": 0.5
}
```

---

## Rule validation

Truoc khi insert vao `generated_rules`, rule duoc validate:

- `new_rule` khong duoc rong
- `new_rule` phai dai hon 10 ky tu
- `reasoning` phai co
- `confidence` phai nam trong khoang `0.0 - 1.0`
- `new_rule` khong duoc chua toxic words nhu `hate`, `kill`, `abuse`

Neu fail validation:

- feedback se bi mark `rejected`
- API tra `422`

---

## Quick local test

```python
from backend.api.autotrain.autotrain_route import _ai_generate_rule

test_cases = [
    {
        "feedback": {
            "query": "tim ao khoac cho troi 35 do",
            "ai_response": "Minh goi y ao khoac ni...",
            "feedback_text": "Troi nong qua, AI con tu van ao khoac"
        }
    },
    {
        "feedback": {
            "query": "tim dam du tiec",
            "ai_response": "Day la dam maxi...",
            "feedback_text": "Toi thap qua, mac dam maxi cham dat"
        }
    }
]

for tc in test_cases:
    result = _ai_generate_rule(tc["feedback"])
    print(result["new_rule"])
    print(result["confidence"])
    print(result["reasoning"])
```

---

## Gioi han hien tai

| Limitation | Trang thai hien tai | Future |
|---|---|---|
| So pattern it | 3 pattern chinh + fallback | Them pattern hoac doi sang LLM |
| Khong co context user | Chua doc lich su / profile | Them profile / history context |
| Confidence static | Gan cung cho tung pattern | Cho LLM tu danh gia |
| Khong conflict detection | Admin review thu cong | Rule versioning + conflict checker |

---

## Huong nang cap hop ly

Hybrid flow hop ly cho production:

1. Thu pattern match truoc
2. Neu confidence cao thi dung luon
3. Neu feedback mo hoac phuc tap thi goi LLM

Y tuong:

```python
async def _ai_generate_rule(feedback):
    pattern_rule = pattern_match(feedback)
    if pattern_rule and pattern_rule["confidence"] >= 0.9:
        return pattern_rule
    return await llm_generate(feedback)
```

Dieu nay giu chi phi thap nhung van mo duong cho case phuc tap.
