| """ |
| LLM Client - Handles all LLM interactions |
| """ |
| import json |
| from typing import List, Dict, Any, Optional |
| from openai import OpenAI |
| import config |
|
|
|
|
| class LLMClient: |
| """ |
| Unified LLM client interface |
| """ |
| def __init__( |
| self, |
| api_key: Optional[str] = None, |
| model: Optional[str] = None, |
| base_url: Optional[str] = None, |
| enable_thinking: Optional[bool] = None, |
| use_streaming: Optional[bool] = None |
| ): |
| self.api_key = api_key or config.OPENAI_API_KEY |
| self.model = model or config.LLM_MODEL |
| self.base_url = base_url or config.OPENAI_BASE_URL |
| self.enable_thinking = enable_thinking if enable_thinking is not None else config.ENABLE_THINKING |
| self.use_streaming = use_streaming if use_streaming is not None else config.USE_STREAMING |
|
|
| |
| client_kwargs = {"api_key": self.api_key} |
| if self.base_url: |
| client_kwargs["base_url"] = self.base_url |
| print(f"Using custom OpenAI base URL: {self.base_url}") |
|
|
| if self.enable_thinking: |
| print(f"Deep thinking mode enabled") |
|
|
| self.client = OpenAI(**client_kwargs) |
|
|
| def chat_completion( |
| self, |
| messages: List[Dict[str, str]], |
| temperature: float = 0.2, |
| response_format: Optional[Dict[str, str]] = None, |
| max_retries: int = 3 |
| ) -> str: |
| """ |
| Standard chat completion with optional thinking mode and retry mechanism |
| """ |
| kwargs = { |
| "model": self.model, |
| "messages": messages, |
| "temperature": temperature, |
| } |
|
|
| if response_format: |
| kwargs["response_format"] = response_format |
|
|
| |
| |
| is_qwen_api = self.base_url and "dashscope.aliyuncs.com" in self.base_url |
| |
| if is_qwen_api: |
| |
| |
| |
| |
| if self.use_streaming and self.enable_thinking and not response_format: |
| kwargs["extra_body"] = {"enable_thinking": True} |
| else: |
| |
| kwargs["extra_body"] = {"enable_thinking": False} |
| |
|
|
| |
| last_exception = None |
| for attempt in range(max_retries): |
| try: |
| |
| if self.use_streaming: |
| kwargs["stream"] = True |
| return self._handle_streaming_response(**kwargs) |
| else: |
| response = self.client.chat.completions.create(**kwargs) |
| return response.choices[0].message.content |
| |
| except Exception as e: |
| last_exception = e |
| if attempt < max_retries - 1: |
| import time |
| wait_time = (2 ** attempt) |
| print(f"LLM API call failed (attempt {attempt + 1}/{max_retries}): {e}") |
| print(f"Retrying in {wait_time} seconds...") |
| time.sleep(wait_time) |
| else: |
| print(f"LLM API call failed after {max_retries} attempts: {e}") |
| |
| |
| raise last_exception |
|
|
| def _handle_streaming_response(self, **kwargs) -> str: |
| """ |
| Handle streaming response and collect full content |
| """ |
| full_content = [] |
| stream = self.client.chat.completions.create(**kwargs) |
|
|
| for chunk in stream: |
| if chunk.choices[0].delta.content is not None: |
| content = chunk.choices[0].delta.content |
| full_content.append(content) |
| |
| |
|
|
| return ''.join(full_content) |
|
|
| def extract_json(self, text: str) -> Any: |
| """ |
| Extract JSON from LLM response with robust parsing |
| Supports multiple formats: |
| 1. Pure JSON |
| 2. ```json ... ``` |
| 3. ``` ... ``` (generic code block) |
| 4. JSON embedded in text with common prefixes |
| 5. Multiple JSON objects (returns first valid one) |
| """ |
| if not text or not text.strip(): |
| raise ValueError("Empty response received") |
|
|
| text = text.strip() |
|
|
| |
| common_prefixes = [ |
| "Here's the JSON:", |
| "Here is the JSON:", |
| "The JSON is:", |
| "JSON:", |
| "Result:", |
| "Output:", |
| "Answer:", |
| ] |
| for prefix in common_prefixes: |
| if text.lower().startswith(prefix.lower()): |
| text = text[len(prefix):].strip() |
|
|
| |
| try: |
| return json.loads(text) |
| except json.JSONDecodeError: |
| pass |
|
|
| |
| if "```json" in text.lower(): |
| |
| start_marker = "```json" |
| start_idx = text.lower().find(start_marker) |
| if start_idx != -1: |
| start = start_idx + len(start_marker) |
| |
| end = text.find("```", start) |
| if end != -1: |
| json_str = text[start:end].strip() |
| try: |
| return json.loads(json_str) |
| except json.JSONDecodeError as e: |
| |
| json_str = self._clean_json_string(json_str) |
| try: |
| return json.loads(json_str) |
| except json.JSONDecodeError: |
| pass |
|
|
| |
| if "```" in text: |
| start = text.find("```") + 3 |
| |
| newline = text.find("\n", start) |
| if newline != -1 and newline - start < 20: |
| start = newline + 1 |
| end = text.find("```", start) |
| if end != -1: |
| json_str = text[start:end].strip() |
| try: |
| return json.loads(json_str) |
| except json.JSONDecodeError: |
| |
| json_str = self._clean_json_string(json_str) |
| try: |
| return json.loads(json_str) |
| except json.JSONDecodeError: |
| pass |
|
|
| |
| for start_char in ['{', '[']: |
| result = self._extract_balanced_json(text, start_char) |
| if result is not None: |
| return result |
|
|
| |
| for start_char in ['{', '[']: |
| start_idx = text.find(start_char) |
| if start_idx != -1: |
| |
| chunk = text[start_idx:] |
| cleaned = self._clean_json_string(chunk) |
| try: |
| return json.loads(cleaned) |
| except json.JSONDecodeError: |
| pass |
|
|
| raise ValueError(f"Failed to extract valid JSON from response. First 300 chars: {text[:300]}...") |
|
|
| def _clean_json_string(self, json_str: str) -> str: |
| """ |
| Clean common issues in JSON strings from LLM output |
| """ |
| |
| import re |
| json_str = re.sub(r',(\s*[}\]])', r'\1', json_str) |
|
|
| |
| json_str = re.sub(r'//.*?$', '', json_str, flags=re.MULTILINE) |
| json_str = re.sub(r'/\*.*?\*/', '', json_str, flags=re.DOTALL) |
|
|
| return json_str.strip() |
|
|
| def _extract_balanced_json(self, text: str, start_char: str) -> Any: |
| """ |
| Extract a balanced JSON object or array starting with start_char |
| """ |
| end_char = '}' if start_char == '{' else ']' |
| start_idx = text.find(start_char) |
|
|
| if start_idx == -1: |
| return None |
|
|
| |
| depth = 0 |
| in_string = False |
| escape_next = False |
|
|
| for i in range(start_idx, len(text)): |
| char = text[i] |
|
|
| |
| if escape_next: |
| escape_next = False |
| continue |
|
|
| if char == '\\': |
| escape_next = True |
| continue |
|
|
| |
| if char == '"': |
| in_string = not in_string |
| continue |
|
|
| if in_string: |
| continue |
|
|
| |
| if char == start_char: |
| depth += 1 |
| elif char == end_char: |
| depth -= 1 |
| if depth == 0: |
| json_str = text[start_idx:i+1] |
| try: |
| return json.loads(json_str) |
| except json.JSONDecodeError: |
| |
| cleaned = self._clean_json_string(json_str) |
| try: |
| return json.loads(cleaned) |
| except json.JSONDecodeError: |
| |
| break |
|
|
| return None |
|
|