File size: 7,794 Bytes
a6e70b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
VentureForge LLM Client
========================
Provider-agnostic OpenAI-compatible factory.
Switch LLM provider by changing LLM_BASE_URL / LLM_API_KEY in env.

Usage:
    from src.llm.client import get_llm
    llm = get_llm(temperature=0.1)
    response = llm.invoke("Hello")
"""

from __future__ import annotations

import json
import logging
from functools import lru_cache

from langchain_core.language_models import BaseChatModel
from langchain_openai import ChatOpenAI

from src.config import settings


def get_llm(
    *,
    temperature: float | None = None,
    max_tokens: int | None = None,
    model: str | None = None,
    reasoning: bool = False,
) -> BaseChatModel:
    """
    Return a configured LLM instance.

    Args:
        temperature: Override default temperature (0.0–2.0).
        max_tokens: Override default max_tokens.
        model: Override default model name.
        reasoning: True for heavy reasoning tasks (scorer, critic) → uses
                   the large model. False for fast generative tasks.
    """
    config = settings.get_llm_config(reasoning=reasoning)
    model_name = model or config["model"]
    
    # Detect if this is a Qwen3.6 model
    is_qwen36 = "qwen3.6" in model_name.lower() or "qwen/qwen3.6" in model_name.lower()
    
    # Cap max_tokens to avoid vLLM validation errors
    # Qwen3.6-35B-A3B typically has max_model_len around 32k-128k depending on deployment
    # Use a safe default that works with most vLLM deployments
    requested_max_tokens = max_tokens or settings.max_tokens
    safe_max_tokens = min(requested_max_tokens, 32768)  # Safe limit for most deployments
    
    if requested_max_tokens > safe_max_tokens:
        logging.getLogger(__name__).warning(
            f"[llm_client] Requested max_tokens={requested_max_tokens} exceeds safe limit. "
            f"Capping to {safe_max_tokens} to avoid vLLM validation errors."
        )
    
    # Base parameters
    base_params = {
        "base_url": config["base_url"],
        "api_key": config["api_key"] or "sk-dummy",
        "model": model_name,
        "temperature": temperature if temperature is not None else settings.default_temperature,
        "max_tokens": safe_max_tokens,
        "timeout": config["timeout"],
    }
    
    # Add Qwen3.6-specific parameters if detected
    if is_qwen36:
        # Determine if this is a coding task (precise) or general task
        # Reasoning tasks (scorer, critic) use precise coding parameters
        # Non-reasoning tasks use general thinking parameters
        if reasoning:
            # Precise coding tasks: temperature=0.6, top_p=0.95, presence_penalty=0.0
            qwen_params = {
                "temperature": 0.6,
                "top_p": 0.95,
                "extra_body": {
                    "top_k": 20,
                    "repetition_penalty": 1.0,
                    "presence_penalty": 0.0,
                },
            }
        else:
            # General thinking tasks: temperature=1.0, top_p=0.95, presence_penalty=1.5
            # For structured output (JSON), disable thinking mode and presence penalty
            # ✅ FIX: presence_penalty=0.0 prevents skipping repeated tokens (quotes, commas)
            # High presence_penalty (1.5) was causing LLM to omit opening quotes in JSON strings
            qwen_params = {
                "temperature": 1.0,
                "top_p": 0.95,
                "extra_body": {
                    "top_k": 20,
                    "repetition_penalty": 1.0,
                    "presence_penalty": 0.0,  # Changed from 1.5 - critical for JSON output!
                    "chat_template_kwargs": {"enable_thinking": False},
                },
            }
        
        # Override with user-provided temperature if specified
        if temperature is not None:
            qwen_params["temperature"] = temperature
        
        # Merge Qwen parameters into base parameters
        base_params.update(qwen_params)
        
        logging.getLogger(__name__).info(
            f"[llm_client] Qwen3.6 detected, using {'thinking' if reasoning else 'instruct'} mode with "
            f"temp={qwen_params['temperature']}, top_p={qwen_params.get('top_p', 'default')}, "
            f"max_tokens={safe_max_tokens}"
        )
    
    return ChatOpenAI(**base_params)


@lru_cache(maxsize=32)
def get_structured_llm(
    output_schema: type,
    *,
    temperature: float | None = None,
    model: str | None = None,
    reasoning: bool = False,
) -> BaseChatModel:
    """
    Return an LLM configured with a Pydantic output schema for structured generation.

    Args:
        output_schema: A Pydantic v2 BaseModel subclass describing the desired output.
        temperature: Override default temperature.
        model: Override default model name.
        reasoning: True for heavy reasoning tasks.
    """
    base = get_llm(temperature=temperature, model=model, reasoning=reasoning)
    return base.with_structured_output(output_schema)


# ------------------------------------------------------------------
# JSON extraction helper — robust against LLM formatting quirks
# ------------------------------------------------------------------

def strip_thinking_tags(text: str) -> str:
    """Remove Qwen3.6 thinking tags from response text.
    
    Qwen3.6 outputs thinking process wrapped in <think>...</think> tags.
    This function strips those tags to get the actual response.
    
    Example input:
        <think>
        Here's a thinking process:
        1. Analyze...
        </think>
        
        {"result": "actual response"}
    
    Returns: '{"result": "actual response"}'
    """
    if not text:
        return text
    
    # Find and remove <think>...</think> blocks
    import re
    # Use DOTALL flag to match across newlines
    cleaned = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL | re.IGNORECASE)
    return cleaned.strip()


def extract_json(text: str) -> dict | list | None:
    """Extract the first JSON object or array from raw LLM text.

    Handles markdown fences, trailing prose, control characters, and Qwen3.6 thinking tags.
    Returns None if no valid JSON found.
    """
    if not text:
        return None
    
    # Strip Qwen3.6 thinking tags first
    text = strip_thinking_tags(text)
    
    # Pre-clean: strip whitespace
    text = text.strip()

    # Find first structural char
    start_idx = -1
    for ch in ("[", "{"):
        idx = text.find(ch)
        if idx != -1 and (start_idx == -1 or idx < start_idx):
            start_idx = idx

    # Find last matching char
    end_idx = -1
    for ch in ("]", "}"):
        idx = text.rfind(ch)
        if idx != -1 and idx > end_idx:
            end_idx = idx

    if start_idx == -1 or end_idx == -1 or end_idx <= start_idx:
        # Fallback to direct load if no markers found but text looks like JSON
        try:
            return json.loads(text, strict=False)
        except json.JSONDecodeError:
            return None

    candidate = text[start_idx : end_idx + 1]

    try:
        return json.loads(candidate, strict=False)
    except json.JSONDecodeError:
        # If widest boundary fails, maybe there's garbage between blocks?
        # For now, we stick to widest as per instructions.
        return None


def coerce_yes_no(value: str | bool) -> bool:
    """Convert 'yes'/'no' strings to bool, passing through bool values."""
    if isinstance(value, bool):
        return value
    if isinstance(value, str):
        return value.strip().lower() == "yes"
    return bool(value)


def coerce_rubric_bools(rubric_dict: dict) -> dict:
    """Convert all 'yes'/'no' string values in a rubric dict to booleans."""
    return {k: coerce_yes_no(v) for k, v in rubric_dict.items()}