summaryrefslogtreecommitdiff
path: root/src/personalization/feedback/local_llm_reward.py
blob: 9837ff022f2ed7656aab8e128172c81c8e143f76 (plain)
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
"""
Local LLM reward model using vLLM server for batch inference.

Drop-in replacement for LLMRewardClient when you want to use a local model
(e.g., Llama-3.1-8B-Instruct) instead of OpenAI API.

Uses BatchVLLMClient for efficient concurrent requests - vLLM's continuous
batching will process them together for high throughput.
"""
from __future__ import annotations

import asyncio
import hashlib
import json
from dataclasses import dataclass
from typing import Dict, List, Optional

import aiohttp

from personalization.feedback.schemas import TurnSample
from personalization.feedback.llm_reward import (
    REWARD_MAP,
    VALID_LABELS,
    JUDGE_SYSTEM_PROMPT,
    JUDGE_USER_TEMPLATE,
    RewardResult,
)


@dataclass
class LocalLLMRewardConfig:
    """Configuration for local LLM reward model."""
    vllm_url: str = "http://localhost:8005/v1"  # vLLM server URL
    model_name: Optional[str] = None  # Auto-discovered if None
    max_tokens: int = 256
    temperature: float = 0.1
    max_concurrent: int = 100  # High concurrency for vLLM batching
    timeout: Optional[int] = 60  # Per-request timeout in seconds
    confidence_threshold: float = 0.6  # tau_c: skip update if confidence < this
    enable_cache: bool = True  # Cache by hash of (q_t, a_t, q_{t+1})


class LocalLLMRewardClient:
    """
    Local LLM reward client using vLLM server.

    Designed for batch processing - uses async HTTP requests that vLLM
    batches together via continuous batching for high throughput.
    """

    def __init__(self, config: Optional[LocalLLMRewardConfig] = None):
        self.config = config or LocalLLMRewardConfig()
        self._model_name = self.config.model_name
        self._cache: Dict[str, RewardResult] = {}

        # Discover model name if not provided
        if self._model_name is None:
            self._discover_model_sync()

    def _discover_model_sync(self):
        """Synchronously discover model name from vLLM server."""
        import requests
        try:
            response = requests.get(
                f"{self.config.vllm_url}/models",
                timeout=10
            )
            response.raise_for_status()
            models = response.json()
            if models.get("data") and len(models["data"]) > 0:
                self._model_name = models["data"][0]["id"]
            else:
                self._model_name = "default"
        except Exception as e:
            print(f"[LocalLLMReward] Warning: Could not discover model ({e})")
            self._model_name = "default"

    def _cache_key(self, query_t: str, answer_t: str, query_t1: str) -> str:
        """Deterministic hash of the judge input triple."""
        content = f"{query_t}\x00{answer_t}\x00{query_t1}"
        return hashlib.sha256(content.encode("utf-8")).hexdigest()

    def _build_messages(self, sample: TurnSample) -> List[dict]:
        """Construct the judge prompt from (q_t, a_t, q_{t+1})."""
        user_content = JUDGE_USER_TEMPLATE.format(
            query_t=sample.query_t,
            answer_t=sample.answer_t,
            query_t1=sample.query_t1,
        )
        return [
            {"role": "system", "content": JUDGE_SYSTEM_PROMPT},
            {"role": "user", "content": user_content},
        ]

    def _parse_result(self, raw: Optional[str]) -> RewardResult:
        """Parse structured JSON output into RewardResult."""
        if raw is None:
            return RewardResult(
                label="neutral",
                confidence=0.0,
                rationale="request_failed",
                reward=0.0,
                should_update=False,
            )

        try:
            # Handle markdown code blocks
            text = raw.strip()
            if text.startswith("```"):
                lines = text.split("\n")
                text = "\n".join(
                    lines[1:-1] if lines[-1].strip() == "```" else lines[1:]
                )

            parsed = json.loads(text)
            label = parsed.get("label", "neutral")
            confidence = float(parsed.get("confidence", 0.0))
            rationale = parsed.get("rationale", "")

            if label not in VALID_LABELS:
                label = "neutral"
                confidence = 0.0

            reward = REWARD_MAP[label]

            # Confidence gating and topic_shift skip
            should_update = (
                confidence >= self.config.confidence_threshold
                and label != "topic_shift"
            )
            if not should_update:
                reward = 0.0

            return RewardResult(
                label=label,
                confidence=confidence,
                rationale=rationale,
                reward=reward,
                should_update=should_update,
            )
        except (json.JSONDecodeError, KeyError, TypeError, ValueError):
            # Try to extract JSON from text
            import re
            match = re.search(r'\{[^}]+\}', raw, re.DOTALL)
            if match:
                try:
                    parsed = json.loads(match.group())
                    label = parsed.get("label", "neutral")
                    confidence = float(parsed.get("confidence", 0.0))
                    rationale = parsed.get("rationale", "")

                    if label not in VALID_LABELS:
                        label = "neutral"
                        confidence = 0.0

                    reward = REWARD_MAP[label]
                    should_update = (
                        confidence >= self.config.confidence_threshold
                        and label != "topic_shift"
                    )
                    if not should_update:
                        reward = 0.0

                    return RewardResult(
                        label=label,
                        confidence=confidence,
                        rationale=rationale,
                        reward=reward,
                        should_update=should_update,
                    )
                except:
                    pass

            return RewardResult(
                label="neutral",
                confidence=0.0,
                rationale="parse_failure",
                reward=0.0,
                should_update=False,
            )

    async def _single_request(
        self,
        session: aiohttp.ClientSession,
        messages: List[dict],
        idx: int,
    ) -> tuple:
        """Make a single async request to vLLM server."""
        payload = {
            "model": self._model_name,
            "messages": messages,
            "max_tokens": self.config.max_tokens,
            "temperature": self.config.temperature,
            "response_format": {"type": "json_object"},
        }

        for attempt in range(3):
            try:
                timeout_config = (
                    aiohttp.ClientTimeout(total=self.config.timeout)
                    if self.config.timeout else None
                )
                async with session.post(
                    f"{self.config.vllm_url}/chat/completions",
                    json=payload,
                    timeout=timeout_config,
                ) as response:
                    if response.status == 200:
                        result = await response.json()
                        content = result["choices"][0]["message"]["content"]
                        return (idx, content, None)
                    elif response.status == 429:
                        # Rate limit - wait and retry
                        await asyncio.sleep(2 ** attempt)
                        continue
                    else:
                        error_text = await response.text()
                        return (idx, None, f"HTTP {response.status}: {error_text[:200]}")
            except asyncio.TimeoutError:
                if attempt < 2:
                    continue
                return (idx, None, "Timeout")
            except Exception as e:
                return (idx, None, str(e))

        return (idx, None, "Max retries exceeded")

    async def judge_batch_async(
        self,
        samples: List[TurnSample],
        show_progress: bool = False,
    ) -> List[RewardResult]:
        """
        Judge a batch of turns using concurrent vLLM requests.

        vLLM's continuous batching will process these together for
        high throughput.
        """
        n_samples = len(samples)
        results = [None] * n_samples

        # Check cache and build request list
        to_request = []  # (original_idx, messages)
        for i, sample in enumerate(samples):
            if self.config.enable_cache:
                key = self._cache_key(sample.query_t, sample.answer_t, sample.query_t1)
                if key in self._cache:
                    results[i] = self._cache[key]
                    continue

            messages = self._build_messages(sample)
            to_request.append((i, messages))

        if not to_request:
            return results

        # Make concurrent requests
        semaphore = asyncio.Semaphore(self.config.max_concurrent)

        async def limited_request(session, messages, idx):
            async with semaphore:
                return await self._single_request(session, messages, idx)

        connector = aiohttp.TCPConnector(limit=self.config.max_concurrent)
        headers = {"Content-Type": "application/json"}

        async with aiohttp.ClientSession(
            connector=connector, headers=headers
        ) as session:
            tasks = [
                limited_request(session, messages, orig_idx)
                for orig_idx, messages in to_request
            ]

            completed = 0
            for coro in asyncio.as_completed(tasks):
                orig_idx, content, error = await coro
                completed += 1

                if error:
                    print(f"[LocalLLMReward] Request {orig_idx} failed: {error}")

                result = self._parse_result(content)
                results[orig_idx] = result

                # Cache the result
                if self.config.enable_cache:
                    sample = samples[orig_idx]
                    key = self._cache_key(
                        sample.query_t, sample.answer_t, sample.query_t1
                    )
                    self._cache[key] = result

                if show_progress and completed % 10 == 0:
                    print(f"  [LocalLLMReward {completed}/{len(to_request)}] completed")

        return results

    async def judge_async(self, sample: TurnSample) -> RewardResult:
        """Judge a single turn (async)."""
        results = await self.judge_batch_async([sample])
        return results[0]

    def judge_batch(self, samples: List[TurnSample]) -> List[RewardResult]:
        """
        Judge a batch of turns (sync wrapper).

        This is the main entry point for batch reward estimation.
        """
        return asyncio.run(self.judge_batch_async(samples))

    def judge(self, sample: TurnSample) -> RewardResult:
        """Judge a single turn (sync wrapper)."""
        return asyncio.run(self.judge_async(sample))


# --- Convenience Functions ---

def estimate_reward_local(
    sample: TurnSample,
    config: Optional[LocalLLMRewardConfig] = None,
) -> tuple:
    """
    Synchronous single-sample reward estimation using local LLM.
    Returns (reward, should_update).
    """
    client = LocalLLMRewardClient(config)
    result = client.judge(sample)
    return result.reward, result.should_update


def estimate_rewards_batch_local(
    samples: List[TurnSample],
    config: Optional[LocalLLMRewardConfig] = None,
) -> List[tuple]:
    """
    Synchronous batch reward estimation using local LLM.
    Returns list of (reward, should_update) tuples.
    """
    client = LocalLLMRewardClient(config)
    results = client.judge_batch(samples)
    return [(r.reward, r.should_update) for r in results]