File size: 16,751 Bytes
e020674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import re
from typing import List, Optional, Dict, Any, Union, Tuple
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
from dataflow.core import LLMServingABC
from dataflow.logger import get_logger


class LiteLLMServing(LLMServingABC):
    """
    LiteLLM-based serving class that provides unified interface for multiple LLM providers.
    Supports OpenAI, Anthropic, Cohere, Azure, AWS Bedrock, Google and many more providers.
    
    This implementation avoids global state pollution by passing configuration parameters
    directly to each litellm.completion() call, ensuring thread safety and proper isolation
    between different instances. Configuration parameters are immutable after initialization.
    
    Doc: https://docs.litellm.ai/docs/providers
    """
    
    def __init__(self, 
                 model: str = "gpt-4o",
                 key_name_of_api_key: str = "OPENAI_API_KEY",
                 api_base: Optional[str] = None,
                 api_version: Optional[str] = None,
                 temperature: float = 0.7,
                 max_tokens: int = 1024,
                 top_p: float = 1.0,
                 max_workers: int = 10,
                 timeout: int = 60,
                 **kwargs: Any):
        """
        Initialize LiteLLM serving instance.
        
        Args:
            model: Model name (e.g., "gpt-4o", "claude-3-sonnet", "command-r-plus")
            key_name_of_api_key: Environment variable name for API key (default: "OPENAI_API_KEY")
            api_base: Custom API base URL
            api_version: API version for providers that support it
            temperature: Sampling temperature
            max_tokens: Maximum tokens to generate
            top_p: Top-p sampling parameter
            max_workers: Number of concurrent workers for batch processing
            timeout: Request timeout in seconds
            **kwargs: Additional parameters passed to litellm.completion()
            
        Note:
            All configuration parameters are immutable after initialization.
            If you need different settings, create a new instance.
        """
        
        # Import litellm at initialization time to support lazy importing
        try:
            import litellm
            self._litellm = litellm
        except ImportError:
            raise ImportError(
                "litellm is not installed. Please install it with: "
                "pip install open-dataflow[litellm] or pip install litellm"
            )
        
        self.model = model
        self.api_base = api_base
        self.api_version = api_version
        self.temperature = temperature
        self.max_tokens = max_tokens
        self.top_p = top_p
        self.max_workers = max_workers
        self.timeout = timeout
        self.kwargs = kwargs
        self.logger = get_logger()
        
        # Get API key from environment variable
        self.api_key = os.environ.get(key_name_of_api_key)
        if self.api_key is None:
            error_msg = f"Lack of `{key_name_of_api_key}` in environment variables. Please set `{key_name_of_api_key}` as your api-key before using LiteLLMServing."
            self.logger.error(error_msg)
            raise ValueError(error_msg)
        self.key_name_of_api_key = key_name_of_api_key
        
        # Validate model by making a test call
        self._validate_setup()
        
        self.logger.info(f"LiteLLMServing initialized with model: {model}")
    
    def switch_model(self, 
                     model: str,
                     key_name_of_api_key: Optional[str] = None,
                     api_base: Optional[str] = None,
                     api_version: Optional[str] = None,
                     **kwargs: Any):
        """
        Switch to a different model with potentially different API configuration.
        
        Args:
            model: Model name to switch to
            key_name_of_api_key: New environment variable name for API key (optional)
            api_base: New API base URL (optional)
            api_version: New API version (optional)
            **kwargs: Additional parameters for the new model
        """
        # Update model
        self.model = model
        
        # Update API key if new environment variable provided
        if key_name_of_api_key is not None:
            self.api_key = os.environ.get(key_name_of_api_key)
            if self.api_key is None:
                error_msg = f"Lack of `{key_name_of_api_key}` in environment variables. Please set `{key_name_of_api_key}` as your api-key before switching model."
                self.logger.error(error_msg)
                raise ValueError(error_msg)
            self.key_name_of_api_key = key_name_of_api_key
        
        # Update other API configuration if provided
        if api_base is not None:
            self.api_base = api_base
        if api_version is not None:
            self.api_version = api_version
        
        # Update other parameters from kwargs
        for key, value in kwargs.items():
            if hasattr(self, key):
                setattr(self, key, value)
            else:
                self.kwargs[key] = value
        
        # Validate the new configuration
        self._validate_setup()
        self.logger.success(f"Switched to model: {model}")
    
    def format_response(self, response: Dict[str, Any]) -> str:
        """
        Format LiteLLM response to include reasoning content in a structured format.
        
        This method handles the standardized LiteLLM response format and extracts
        both the main content and any reasoning_content if available.
        
        Args:
            response: The response dictionary from LiteLLM
            
        Returns:
            Formatted string with think/answer tags if reasoning is present,
            otherwise just the content
        """
        try:
            # Extract the main content
            content = response['choices'][0]['message']['content']
            
            # Check if content already has think/answer format
            if re.search(r'<think>.*</think>.*<answer>.*</answer>', content, re.DOTALL):
                return content
            
            # Try to extract reasoning_content from LiteLLM standardized format
            reasoning_content = ""
            try:
                # LiteLLM provides reasoning_content in the message object
                message = response['choices'][0]['message']
                if hasattr(message, 'reasoning_content') and message.reasoning_content:
                    reasoning_content = message.reasoning_content
                elif isinstance(message, dict) and 'reasoning_content' in message:
                    reasoning_content = message['reasoning_content']
            except (KeyError, AttributeError):
                pass
            
            # Format the response based on whether reasoning content exists
            if reasoning_content:
                return f"<think>{reasoning_content}</think>\n<answer>{content}</answer>"
            else:
                return content
                
        except (KeyError, IndexError) as e:
            self.logger.error(f"Error formatting response: {e}")
            # Return original response as string if formatting fails
            return str(response)
    
    def _validate_setup(self):
        """Validate the model and API configuration."""
        try:
            # Prepare completion parameters
            completion_params = {
                "model": self.model,
                "messages": [{"role": "user", "content": "Hi"}],
                "max_tokens": 1,
                "timeout": self.timeout
            }
            
            # Add optional parameters if provided
            if self.api_key:
                completion_params["api_key"] = self.api_key
            if self.api_base:
                completion_params["api_base"] = self.api_base
            if self.api_version:
                completion_params["api_version"] = self.api_version
                
            # Make a minimal test call to validate setup
            response = self._litellm.completion(**completion_params)
            self.logger.success("LiteLLM setup validation successful")
        except Exception as e:
            self.logger.error(f"LiteLLM setup validation failed: {e}")
            raise ValueError(f"Failed to validate LiteLLM setup: {e}")
    
    def _generate_single(self, user_input: str, system_prompt: str, retry_times: int = 3) -> str:
        """Generate response for a single input with retry logic.
        
        Args:
            user_input: User input text
            system_prompt: System prompt
            retry_times: Number of retry attempts for transient errors
            
        Returns:
            Generated response string
            
        Raises:
            Exception: If generation fails after all retries
        """
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_input}
        ]
        
        # Prepare completion parameters
        completion_params = {
            "model": self.model,
            "messages": messages,
            "temperature": self.temperature,
            "max_tokens": self.max_tokens,
            "top_p": self.top_p,
            "timeout": self.timeout,
            **self.kwargs
        }
        
        # Add optional parameters if provided
        if self.api_key:
            completion_params["api_key"] = self.api_key
        if self.api_base:
            completion_params["api_base"] = self.api_base
        if self.api_version:
            completion_params["api_version"] = self.api_version
        
        last_error = None
        for attempt in range(retry_times):
            try:
                response = self._litellm.completion(**completion_params)
                # Convert response to dict format for format_response
                response_dict = response.model_dump() if hasattr(response, 'model_dump') else response.dict()
                return self.format_response(response_dict)
            except Exception as e:
                last_error = e
                if attempt < retry_times - 1:
                    # Check if error is retryable
                    error_str = str(e).lower()
                    if any(retryable in error_str for retryable in 
                           ["rate limit", "timeout", "connection", "503", "502", "429"]):
                        wait_time = min(2 ** attempt, 10)  # Exponential backoff with max 10s
                        self.logger.warning(f"Retryable error, waiting {wait_time}s: {e}")
                        time.sleep(wait_time)
                        continue
                    
                # Non-retryable error or last attempt
                self.logger.error(f"Error generating response (attempt {attempt + 1}/{retry_times}): {e}")
                break
        
        # Raise the last error instead of returning error string
        raise last_error
    
    def generate_from_input(self, 
                          user_inputs: List[str], 
                          system_prompt: str = "You are a helpful assistant") -> List[str]:
        """
        Generate responses for a list of inputs using concurrent processing.
        
        Args:
            user_inputs: List of user input strings
            system_prompt: System prompt to use for all generations
            
        Returns:
            List of generated responses
        """
        if not user_inputs:
            return []
        
        # Single input case
        if len(user_inputs) == 1:
            try:
                return [self._generate_single(user_inputs[0], system_prompt)]
            except Exception as e:
                # For consistency with batch processing, return error message in list
                error_msg = f"Error: {str(e)}"
                self.logger.error(f"Failed to generate response: {e}")
                return [error_msg]
        
        # Batch processing with threading
        responses = [None] * len(user_inputs)
        
        def generate_with_index(idx: int, user_input: str) -> Tuple[int, str]:
            try:
                response = self._generate_single(user_input, system_prompt)
                return idx, response
            except Exception as e:
                # For batch processing, return error message to maintain list structure
                error_msg = f"Error: {str(e)}"
                self.logger.error(f"Failed to generate response for input {idx}: {e}")
                return idx, error_msg
        
        with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
            futures = [
                executor.submit(generate_with_index, idx, user_input)
                for idx, user_input in enumerate(user_inputs)
            ]
            
            for future in tqdm(as_completed(futures), total=len(futures), desc="Generating"):
                idx, response = future.result()
                responses[idx] = response
        
        return responses
    
    def generate_embedding_from_input(self, texts: List[str]) -> List[List[float]]:
        """
        Generate embeddings for a list of texts.
        
        Args:
            texts: List of text strings to embed
            
        Returns:
            List of embedding vectors
        """
        if not texts:
            return []
        
        embeddings = []
        
        # Prepare embedding parameters
        embedding_params = {
            "model": self.model,
            "timeout": self.timeout
        }
        
        # Add optional parameters if provided
        if self.api_key:
            embedding_params["api_key"] = self.api_key
        if self.api_base:
            embedding_params["api_base"] = self.api_base
        if self.api_version:
            embedding_params["api_version"] = self.api_version
        
        # Process embeddings with retry logic
        def embed_with_retry(text: str, retry_times: int = 3):
            last_error = None
            for attempt in range(retry_times):
                try:
                    response = self._litellm.embedding(
                        input=[text],
                        **embedding_params
                    )
                    return response['data'][0]['embedding']
                except Exception as e:
                    last_error = e
                    if attempt < retry_times - 1:
                        error_str = str(e).lower()
                        if any(retryable in error_str for retryable in 
                               ["rate limit", "timeout", "connection", "503", "502", "429"]):
                            wait_time = min(2 ** attempt, 10)
                            self.logger.warning(f"Retryable error in embedding, waiting {wait_time}s: {e}")
                            time.sleep(wait_time)
                            continue
                    self.logger.error(f"Error generating embedding (attempt {attempt + 1}/{retry_times}): {e}")
                    break
            raise last_error
        
        # Process in batches for better performance
        with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
            futures = [executor.submit(embed_with_retry, text) for text in texts]
            
            for future in tqdm(as_completed(futures), total=len(futures), desc="Generating embeddings"):
                try:
                    embedding = future.result()
                    embeddings.append(embedding)
                except Exception as e:
                    self.logger.error(f"Failed to generate embedding: {e}")
                    # Return empty embedding for failed cases to maintain list structure
                    embeddings.append([])
        
        return embeddings
    
    def get_supported_models(self) -> List[str]:
        """Get list of supported models for the current provider."""
        try:
            return self._litellm.model_list
        except Exception as e:
            self.logger.warning(f"Could not retrieve model list: {e}")
            return []
    

    def cleanup(self) -> None:
        """Cleanup resources."""
        self.logger.info("Cleaning up LiteLLMServing resources")
        # LiteLLM doesn't require explicit cleanup since we don't use global state
        # Instance variables will be garbage collected when the instance is destroyed
        # Clear any references to ensure proper cleanup
        self.api_key = None
        self.kwargs = None