File size: 16,461 Bytes
690700c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbf7898
690700c
 
 
cbf7898
690700c
 
 
 
cbf7898
 
690700c
 
 
 
52d7b60
 
 
690700c
cbf7898
 
 
 
 
 
 
 
7ed4c17
cbf7898
 
7ed4c17
 
cbf7898
 
 
 
 
 
 
 
 
 
 
52d7b60
cbf7898
52d7b60
cbf7898
52d7b60
 
cbf7898
52d7b60
 
cbf7898
52d7b60
7ed4c17
cbf7898
 
7ed4c17
690700c
 
cbf7898
 
 
 
 
 
690700c
 
 
 
cbf7898
690700c
cbf7898
 
 
 
 
 
 
 
 
 
 
690700c
 
cbf7898
690700c
 
 
cbf7898
690700c
 
 
 
 
cbf7898
 
690700c
cbf7898
690700c
 
cbf7898
690700c
 
cbf7898
690700c
 
cbf7898
690700c
cbf7898
690700c
52d7b60
 
cbf7898
690700c
 
 
cbf7898
690700c
 
 
 
 
 
cbf7898
690700c
cbf7898
 
690700c
 
 
cbf7898
690700c
 
 
 
 
cbf7898
690700c
 
cbf7898
690700c
cbf7898
 
 
690700c
cbf7898
 
 
 
 
 
 
690700c
cbf7898
690700c
cbf7898
690700c
 
 
 
cbf7898
690700c
cbf7898
690700c
cbf7898
690700c
cbf7898
690700c
52d7b60
 
 
cbf7898
690700c
 
 
cbf7898
690700c
 
 
 
cbf7898
690700c
 
 
 
 
cbf7898
690700c
 
 
 
cbf7898
690700c
 
 
cbf7898
 
 
690700c
 
cbf7898
690700c
 
cbf7898
690700c
 
cbf7898
690700c
cbf7898
690700c
 
 
 
 
cbf7898
 
 
690700c
cbf7898
52d7b60
690700c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbf7898
690700c
 
 
 
cbf7898
690700c
 
 
42fe10b
cbf7898
690700c
cbf7898
690700c
 
52d7b60
cbf7898
 
 
 
 
 
 
 
 
 
52d7b60
cbf7898
 
7ed4c17
cbf7898
 
 
 
7ed4c17
cbf7898
 
 
52d7b60
690700c
 
52d7b60
7ed4c17
cbf7898
 
 
 
 
 
 
 
 
690700c
3986a1e
690700c
cbf7898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
690700c
3986a1e
cbf7898
690700c
3986a1e
 
cbf7898
 
 
3986a1e
 
690700c
 
cbf7898
 
 
690700c
 
 
52d7b60
cbf7898
52d7b60
690700c
 
cbf7898
7ed4c17
690700c
 
 
 
 
cbf7898
690700c
52d7b60
690700c
cbf7898
690700c
 
 
 
cbf7898
690700c
 
 
 
 
cbf7898
690700c
 
 
 
cbf7898
 
 
690700c
cbf7898
690700c
 
cbf7898
690700c
cbf7898
 
690700c
 
cbf7898
 
 
 
690700c
 
 
cbf7898
 
 
 
 
690700c
 
 
 
52d7b60
690700c
cbf7898
690700c
 
 
cbf7898
690700c
 
 
 
cbf7898
690700c
 
52d7b60
 
 
 
 
 
 
690700c
 
 
cbf7898
690700c
 
 
cbf7898
690700c
cbf7898
690700c
 
cbf7898
690700c
 
 
cbf7898
690700c
cbf7898
690700c
 
cbf7898
690700c
cbf7898
 
 
 
690700c
cbf7898
 
 
 
690700c
cbf7898
690700c
cbf7898
 
 
690700c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52d7b60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
"""
BERT-based Policy Network for FETCH/NO_FETCH decisions
Trained with Reinforcement Learning (Policy Gradient + Entropy Regularization)

This is adapted from your RL.py with:
- PolicyNetwork class (BERT-based)
- State encoding from conversation history
- Action prediction (FETCH vs NO_FETCH)
- Module-level caching (load once on startup)
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from typing import List, Dict, Optional, Tuple
from transformers import AutoTokenizer, AutoModel
from app.config import settings

# ============================================================================
# POLICY NETWORK (From RL.py)
# ============================================================================


class PolicyNetwork(nn.Module):
    """
    BERT-based Policy Network for deciding FETCH vs NO_FETCH actions.

    Architecture:
    - Base: BERT-base-uncased (pre-trained)
    - Input: Current query + conversation history + previous actions
    - Output: 2-class softmax (FETCH=0, NO_FETCH=1)
    - Special tokens: [FETCH], [NO_FETCH] for action encoding (encoded as plain text)

    Training Details:
    - Loss: Policy Gradient + Entropy Regularization
    - Optimizer: AdamW
    - Reward structure:
      * FETCH: +0.5 (always)
      * NO_FETCH + Good: +2.0
      * NO_FETCH + Bad: -0.5
    """

    def __init__(
        self,
        model_name: str = "bert-base-uncased",
        dropout_rate: float = 0.1,
        use_multilayer: bool = True,
        hidden_size: int = 128,
    ):
        super(PolicyNetwork, self).__init__()

        # Load pre-trained BERT and tokenizer
        self.bert = AutoModel.from_pretrained(model_name)
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)

        # ❗ IMPORTANT:
        # Do NOT add extra special tokens or resize embeddings here.
        # The saved checkpoint was trained with the ORIGINAL BERT vocab
        # (vocab_size=30522). Changing vocab size before loading will cause
        # the size mismatch error:
        #   saved=30522, current=30524

        self.use_multilayer = use_multilayer

        # βœ… FLEXIBLE CLASSIFIER ARCHITECTURE (with configurable hidden size)
        if use_multilayer:
            # Multi-layer classifier with specified hidden size (128 or 256)
            self.classifier = nn.Sequential(
                nn.Linear(self.bert.config.hidden_size, hidden_size),
                nn.ReLU(),
                nn.Dropout(dropout_rate),
                nn.Linear(hidden_size, 2),
            )
        else:
            # Single-layer classifier (fallback)
            self.classifier = nn.Linear(self.bert.config.hidden_size, 2)

        # Dropout for regularization
        self.dropout = nn.Dropout(dropout_rate)

    def _init_action_embeddings(self):
        """
        (Currently unused)

        In an alternative setup, this could initialize random embeddings
        for [FETCH] and [NO_FETCH] tokens if they were added as true
        special tokens. For this checkpoint we DO NOT change the vocab
        size, so we leave this unused to avoid shape mismatches.
        """
        with torch.no_grad():
            fetch_id = self.tokenizer.convert_tokens_to_ids("[FETCH]")
            no_fetch_id = self.tokenizer.convert_tokens_to_ids("[NO_FETCH]")

            embedding_dim = self.bert.config.hidden_size

            self.bert.embeddings.word_embeddings.weight[fetch_id] = (
                torch.randn(embedding_dim) * 0.02
            )
            self.bert.embeddings.word_embeddings.weight[no_fetch_id] = (
                torch.randn(embedding_dim) * 0.02
            )

    def forward(
        self, input_ids: torch.Tensor, attention_mask: torch.Tensor
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Forward pass through BERT + classifier.

        Args:
            input_ids: Tokenized input IDs (shape: [batch_size, seq_len])
            attention_mask: Attention mask (shape: [batch_size, seq_len])

        Returns:
            logits: Raw logits (shape: [batch_size, 2])
            probs: Softmax probabilities (shape: [batch_size, 2])
        """
        outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)

        # [CLS] token representation (first token)
        cls_output = outputs.last_hidden_state[:, 0, :]

        # Apply dropout
        cls_output = self.dropout(cls_output)

        # Classification
        logits = self.classifier(cls_output)

        # Softmax for probabilities
        probs = F.softmax(logits, dim=-1)

        return logits, probs

    def encode_state(
        self,
        state: Dict,
        max_length: int = None,
    ) -> Dict[str, torch.Tensor]:
        """
        Encode conversation state into BERT input format.

        State structure:
        {
            'previous_queries': [query1, query2, ...],
            'previous_actions': ['FETCH', 'NO_FETCH', ...],
            'current_query': 'user query'
        }

        Encoding format:
        "Previous query 1: {q1} [Action: [FETCH]] Previous query 2: {q2} [Action: [NO_FETCH]] Current query: <query>"

        Args:
            state: State dictionary
            max_length: Maximum sequence length (default from config)

        Returns:
            dict: Tokenized inputs (input_ids, attention_mask)
        """
        if max_length is None:
            max_length = settings.POLICY_MAX_LEN

        # Build state text from conversation history
        state_text = ""

        # Add previous queries and their actions
        prev_queries = state.get("previous_queries", [])
        prev_actions = state.get("previous_actions", [])

        if prev_queries and prev_actions:
            for i, (prev_query, prev_action) in enumerate(
                zip(prev_queries, prev_actions)
            ):
                state_text += (
                    f"Previous query {i+1}: {prev_query} [Action: [{prev_action}]] "
                )

        # Add current query
        current_query = state.get("current_query", "")
        state_text += f"Current query: {current_query}"

        # Tokenize
        encoding = self.tokenizer(
            state_text,
            truncation=True,
            padding="max_length",
            max_length=max_length,
            return_tensors="pt",
        )

        return encoding

    def predict_action(
        self,
        state: Dict,
        use_dropout: bool = False,
        num_samples: int = 10,
    ) -> Tuple[np.ndarray, Optional[np.ndarray]]:
        """
        Predict action probabilities for a given state.

        Args:
            state: Conversation state dictionary
            use_dropout: Whether to use MC Dropout for uncertainty estimation
            num_samples: Number of MC Dropout samples (if use_dropout=True)

        Returns:
            probs: Action probabilities (shape: [1, 2]) - [P(FETCH), P(NO_FETCH)]
            uncertainty: Standard deviation across samples (if use_dropout=True)
        """
        device = next(self.parameters()).device

        if use_dropout:
            # MC Dropout for uncertainty estimation
            self.train()  # Enable dropout during inference
            all_probs = []

            for _ in range(num_samples):
                with torch.no_grad():
                    encoding = self.encode_state(state)
                    input_ids = encoding["input_ids"].to(device)
                    attention_mask = encoding["attention_mask"].to(device)

                    _, probs = self.forward(input_ids, attention_mask)
                    all_probs.append(probs.cpu().numpy())

            # Average probabilities across samples
            avg_probs = np.mean(all_probs, axis=0)

            # Calculate uncertainty (standard deviation)
            uncertainty = np.std(all_probs, axis=0)

            return avg_probs, uncertainty

        else:
            # Standard inference (no uncertainty estimation)
            self.eval()
            with torch.no_grad():
                encoding = self.encode_state(state)
                input_ids = encoding["input_ids"].to(device)
                attention_mask = encoding["attention_mask"].to(device)

                _, probs = self.forward(input_ids, attention_mask)

                return probs.cpu().numpy(), None


# ============================================================================
# MODULE-LEVEL CACHING (Load once on import)
# ============================================================================

# Global variables for caching
POLICY_MODEL: Optional[PolicyNetwork] = None
POLICY_TOKENIZER: Optional[AutoTokenizer] = None


def load_policy_model() -> PolicyNetwork:
    """
    Load trained policy model (called once on startup).
    Downloads from HuggingFace Hub if not present locally.
    Uses module-level caching - model stays in RAM.

    Returns:
        PolicyNetwork: Loaded policy model
    """
    global POLICY_MODEL, POLICY_TOKENIZER

    if POLICY_MODEL is None:
        # Download model from HF Hub if needed (for deployment)
        settings.download_model_if_needed(
            hf_filename="models/policy_query_only.pt",
            local_path=settings.POLICY_MODEL_PATH,
        )

        print(f"Loading policy network from {settings.POLICY_MODEL_PATH}...")
        try:
            # Load checkpoint first to detect architecture
            checkpoint = torch.load(
                settings.POLICY_MODEL_PATH, map_location=settings.DEVICE
            )

            # Unwrap if saved as {"model_state_dict": ...}
            if isinstance(checkpoint, dict) and "model_state_dict" in checkpoint:
                state_dict = checkpoint["model_state_dict"]
            else:
                state_dict = checkpoint

            # βœ… AUTO-DETECT ARCHITECTURE from checkpoint keys
            has_multilayer = "classifier.0.weight" in state_dict

            if has_multilayer:
                hidden_size = state_dict["classifier.0.weight"].shape[0]
                print(
                    f"πŸ“Š Detected: Multi-layer classifier (hidden_size={hidden_size})"
                )
            else:
                hidden_size = 768  # not really used for single-layer
                print("πŸ“Š Detected: Single-layer classifier")

            # Create model instance with correct architecture
            POLICY_MODEL = PolicyNetwork(
                model_name="bert-base-uncased",
                dropout_rate=0.1,
                use_multilayer=has_multilayer,
                hidden_size=hidden_size,
            )

            # Align vocab size / embeddings with checkpoint
            saved_vocab_size = state_dict[
                "bert.embeddings.word_embeddings.weight"
            ].shape[0]
            current_vocab_size = (
                POLICY_MODEL.bert.embeddings.word_embeddings.num_embeddings
            )

            if saved_vocab_size != current_vocab_size:
                print(
                    f"⚠️ Vocab size mismatch: saved={saved_vocab_size}, current={current_vocab_size}"
                )
                print(
                    "βœ… Resizing BERT embeddings to match saved checkpoint vocab size..."
                )
                POLICY_MODEL.bert.resize_token_embeddings(saved_vocab_size)

            # Move to device
            POLICY_MODEL = POLICY_MODEL.to(settings.DEVICE)

            # Load weights (shapes now match, so strict=False is just safety)
            POLICY_MODEL.load_state_dict(state_dict, strict=False)

            # Set to evaluation mode
            POLICY_MODEL.eval()

            # Cache tokenizer
            POLICY_TOKENIZER = POLICY_MODEL.tokenizer

            print("βœ… Policy network loaded and cached")
            print(
                f"   Model vocab size: {POLICY_MODEL.bert.embeddings.word_embeddings.num_embeddings}"
            )
            print(f"   Tokenizer vocab size: {len(POLICY_MODEL.tokenizer)}")

        except FileNotFoundError:
            print(f"❌ Policy model file not found: {settings.POLICY_MODEL_PATH}")
            print(
                f"⚠️ Make sure models are uploaded to HuggingFace Hub: {settings.HF_MODEL_REPO}"
            )
            raise
        except Exception as e:
            print(f"❌ Failed to load policy model: {e}")
            import traceback

            traceback.print_exc()
            raise

    return POLICY_MODEL


# ============================================================================
# PREDICTION FUNCTIONS
# ============================================================================


def create_state_from_history(
    current_query: str,
    conversation_history: List[Dict],
    max_history: int = 2,
) -> Dict:
    """
    Create state dictionary from conversation history.
    Extracts last N query-action pairs.

    Args:
        current_query: Current user query
        conversation_history: List of conversation turns
            Each turn: {'role': 'user'/'assistant', 'content': '...', 'metadata': {...}}
        max_history: Maximum number of previous turns to include (default: 2)

    Returns:
        dict: State dictionary for policy network
    """
    state = {
        "current_query": current_query,
        "previous_queries": [],
        "previous_actions": [],
    }

    if not conversation_history:
        return state

    # Extract last N conversation turns (user + assistant pairs)
    relevant_history = conversation_history[-(max_history * 2) :]

    for i, turn in enumerate(relevant_history):
        # User turns
        if turn.get("role") == "user":
            query = turn.get("content", "")
            state["previous_queries"].append(query)

            # Look for corresponding assistant turn
            if i + 1 < len(relevant_history):
                bot_turn = relevant_history[i + 1]
                if bot_turn.get("role") == "assistant":
                    metadata = bot_turn.get("metadata", {})
                    action = metadata.get("policy_action", "FETCH")
                    state["previous_actions"].append(action)

    return state


def predict_policy_action(
    query: str,
    history: List[Dict] = None,
    return_probs: bool = False,
) -> Dict:
    """
    Predict FETCH/NO_FETCH action for a query.

    Args:
        query: User query text
        history: Conversation history (optional)
        return_probs: Whether to return full probability distribution

    Returns:
        dict: Prediction results
        {
            'action': 'FETCH' or 'NO_FETCH',
            'confidence': float (0-1),
            'fetch_prob': float,
            'no_fetch_prob': float,
            'should_retrieve': bool
        }
    """
    # Load model (cached after first call)
    model = load_policy_model()

    # Create state from history
    if history is None:
        history = []

    state = create_state_from_history(query, history)

    # Predict action
    probs, _ = model.predict_action(state, use_dropout=False)

    # Extract probabilities
    fetch_prob = float(probs[0][0])
    no_fetch_prob = float(probs[0][1])

    # Determine action (argmax)
    action_idx = int(np.argmax(probs[0]))
    action = "FETCH" if action_idx == 0 else "NO_FETCH"
    confidence = float(probs[0][action_idx])

    # Check confidence threshold
    should_retrieve = (action == "FETCH") or (
        action == "NO_FETCH" and confidence < settings.CONFIDENCE_THRESHOLD
    )

    result = {
        "action": action,
        "confidence": confidence,
        "should_retrieve": should_retrieve,
        "policy_decision": action,
    }

    if return_probs:
        result["fetch_prob"] = fetch_prob
        result["no_fetch_prob"] = no_fetch_prob

    return result


# ============================================================================
# USAGE EXAMPLE (for reference)
# ============================================================================
"""
# In your service file:
from app.ml.policy_network import predict_policy_action

# Predict action
history = [
    {'role': 'user', 'content': 'What is my balance?'},
    {'role': 'assistant', 'content': '$1000', 'metadata': {'policy_action': 'FETCH'}}
]

result = predict_policy_action(
    query="Thank you!",
    history=history,
    return_probs=True
)

print(result)
# {
#     'action': 'NO_FETCH',
#     'confidence': 0.95,
#     'should_retrieve': False,
#     'fetch_prob': 0.05,
#     'no_fetch_prob': 0.95
# }
"""