File size: 12,002 Bytes
7e3820c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import httpx
import logging
from abc import ABC, abstractmethod
from typing import Optional, Dict, Any, AsyncIterator, List

class LLMAdapter(ABC):
    """Abstract base class for LLM adapters."""

    @abstractmethod
    async def generate_response(
            self,
            prompt: str,
            system_message: Optional[str] = None,
            max_new_tokens: Optional[int] = None
    ) -> str:
        """Generate a complete response from the LLM."""
        pass

    @abstractmethod
    async def generate_stream(
            self,
            prompt: str,
            system_message: Optional[str] = None,
            max_new_tokens: Optional[int] = None
    ) -> AsyncIterator[str]:
        """Generate a streaming response from the LLM."""
        pass

    @abstractmethod
    async def generate_embedding(self, text: str) -> List[float]:
        """Generate embedding vector from input text."""
        pass

    @abstractmethod
    async def check_system_status(self) -> Dict[str, Any]:
        """Check system status of the LLM Server."""
        pass

    @abstractmethod
    async def validate_system(self) -> Dict[str, Any]:
        """Validate system configuration and setup."""
        pass

    @abstractmethod
    async def initialize_model(self, model_name: Optional[str] = None) -> Dict[str, Any]:
        """Initialize specified model or default model."""
        pass

    @abstractmethod
    async def initialize_embedding_model(self, model_name: Optional[str] = None) -> Dict[str, Any]:
        """Initialize embedding model."""
        pass

    @abstractmethod
    async def download_model(self, model_name: Optional[str] = None) -> Dict[str, str]:
        """Download model files."""
        pass

    @abstractmethod
    async def cleanup(self):
        """Cleanup resources."""
        pass


class HTTPLLMAdapter(LLMAdapter):
    """HTTP adapter for connecting to LLM services over HTTP."""

    def __init__(self, config: Dict[str, Any]):
        """Initialize the HTTP LLM Adapter with configuration."""
        self.logger = logging.getLogger(__name__)
        self.logger.info("Initializing HTTP LLM Adapter")
        self.config = config
        self.llm_config = config.get('llm_server', {})

    async def _get_client(self):
        """Get or create HTTP client as needed"""
        host = self.llm_config.get('host', 'localhost')
        port = self.llm_config.get('port', 8002)

        # Construct base URL, omitting port for HF spaces
        if 'hf.space' in host:
            base_url = f"https://{host}"
        else:
            base_url = f"http://{host}:{port}"

        return httpx.AsyncClient(
            base_url=base_url,
            timeout=float(self.llm_config.get('timeout', 60.0))
        )

    def _get_endpoint(self, endpoint_name: str) -> str:
        """Get full endpoint path including prefix"""
        endpoints = self.llm_config.get('endpoints', {})
        api_prefix = self.llm_config.get('api_prefix', '')
        endpoint = endpoints.get(endpoint_name, '')
        return f"{api_prefix}{endpoint}"

    async def _make_request(
            self,
            method: str,
            endpoint: str,
            *,
            params: Optional[Dict[str, Any]] = None,
            json: Optional[Dict[str, Any]] = None,
            stream: bool = False
    ) -> Any:
        """Make an authenticated request to the LLM Server."""
        base_url = self.llm_config.get('host', 'http://localhost:8001')
        full_endpoint = f"{base_url.rstrip('/')}/{self._get_endpoint(endpoint).lstrip('/')}"

        try:
            self.logger.info(f"Making {method} request to: {full_endpoint}")
            # Create client outside the with block for streaming
            client = await self._get_client()

            if stream:
                # For streaming, return both client and response context managers
                return client, client.stream(
                    method,
                    self._get_endpoint(endpoint),
                    params=params,
                    json=json
                )
            else:
                # For non-streaming, use context manager
                async with client as c:
                    response = await c.request(
                        method,
                        self._get_endpoint(endpoint),
                        params=params,
                        json=json
                    )
                    response.raise_for_status()
                    return response

        except Exception as e:
            self.logger.error(f"Error in request to {full_endpoint}: {str(e)}")
            raise

    async def generate_response(
            self,
            prompt: str,
            system_message: Optional[str] = None,
            max_new_tokens: Optional[int] = None
    ) -> str:
        """Generate a complete response by forwarding the request to the LLM Server."""
        self.logger.debug(f"Forwarding generation request for prompt: {prompt[:50]}...")

        try:
            response = await self._make_request(
                "POST",
                "generate",
                json={
                    "prompt": prompt,
                    "system_message": system_message,
                    "max_new_tokens": max_new_tokens
                }
            )
            data = response.json()
            return data["generated_text"]

        except Exception as e:
            self.logger.error(f"Error in generate_response: {str(e)}")
            raise

    async def generate_stream(
            self,
            prompt: str,
            system_message: Optional[str] = None,
            max_new_tokens: Optional[int] = None
    ) -> AsyncIterator[str]:
        """Generate a streaming response by forwarding the request to the LLM Server."""
        self.logger.debug(f"Forwarding streaming request for prompt: {prompt[:50]}...")

        try:
            client, stream_cm = await self._make_request(
                "POST",
                "generate_stream",
                json={
                    "prompt": prompt,
                    "system_message": system_message,
                    "max_new_tokens": max_new_tokens
                },
                stream=True
            )

            async with client:
                async with stream_cm as response:
                    async for chunk in response.aiter_text():
                        yield chunk

        except Exception as e:
            self.logger.error(f"Error in generate_stream: {str(e)}")
            raise

    async def generate_embedding(self, text: str) -> List[float]:
        """Generate embedding vector from input text."""
        self.logger.debug(f"Forwarding embedding request for text: {text[:50]}...")

        try:
            response = await self._make_request(
                "POST",
                "embedding",
                json={"text": text}
            )
            data = response.json()
            return data["embedding"]

        except Exception as e:
            self.logger.error(f"Error in generate_embedding: {str(e)}")
            raise

    async def check_system_status(self) -> Dict[str, Any]:
        """Check system status of the LLM Server."""
        self.logger.debug("Checking system status...")

        try:
            response = await self._make_request(
                "GET",
                "system_status"
            )
            return response.json()

        except Exception as e:
            self.logger.error(f"Error in check_system_status: {str(e)}")
            raise

    async def validate_system(self) -> Dict[str, Any]:
        """Validate system configuration and setup."""
        self.logger.debug("Validating system configuration...")

        try:
            response = await self._make_request(
                "GET",
                "system_validate"
            )
            return response.json()

        except Exception as e:
            self.logger.error(f"Error in validate_system: {str(e)}")
            raise

    async def initialize_model(self, model_name: Optional[str] = None) -> Dict[str, Any]:
        """Initialize specified model or default model."""
        self.logger.debug(f"Initializing model: {model_name or 'default'}")

        try:
            response = await self._make_request(
                "POST",
                "model_initialize",
                params={"model_name": model_name} if model_name else None
            )
            return response.json()

        except Exception as e:
            self.logger.error(f"Error in initialize_model: {str(e)}")
            raise

    async def initialize_embedding_model(self, model_name: Optional[str] = None) -> Dict[str, Any]:
        """Initialize embedding model."""
        self.logger.debug(f"Initializing embedding model: {model_name or 'default'}")

        try:
            response = await self._make_request(
                "POST",
                "model_initialize_embedding",
                json={"model_name": model_name} if model_name else {}
            )
            return response.json()

        except Exception as e:
            self.logger.error(f"Error in initialize_embedding_model: {str(e)}")
            raise

    async def download_model(self, model_name: Optional[str] = None) -> Dict[str, str]:
        """Download model files from the LLM Server."""
        self.logger.debug(f"Forwarding model download request for: {model_name or 'default model'}")

        try:
            response = await self._make_request(
                "POST",
                "model_download",
                params={"model_name": model_name} if model_name else None
            )
            return response.json()

        except Exception as e:
            self.logger.error(f"Error in download_model: {str(e)}")
            raise

    async def cleanup(self):
        """Cleanup method - no longer needed as clients are created per-request"""
        pass


class OpenAIAdapter(LLMAdapter):
    """Adapter for OpenAI-compatible services (OpenAI, Azure OpenAI, local services with OpenAI API)."""

    def __init__(self, config: Dict[str, Any]):
        self.logger = logging.getLogger(__name__)
        self.logger.info("Initializing OpenAI Adapter")
        self.config = config
        self.openai_config = config.get('openai', {})
        # Additional OpenAI-specific setup would go here

    async def generate_response(self, prompt: str, system_message: Optional[str] = None, max_new_tokens: Optional[int] = None) -> str:
        """OpenAI implementation - would use openai Python client"""
        # Implementation would go here
        pass

    async def generate_stream(self, prompt: str, system_message: Optional[str] = None, max_new_tokens: Optional[int] = None) -> AsyncIterator[str]:
        """OpenAI streaming implementation"""
        # Implementation would go here
        async def placeholder_stream():
            yield "Not implemented yet"
        return placeholder_stream()

    # ... implementations for other methods


class vLLMAdapter(LLMAdapter):
    """Adapter for vLLM services."""

    def __init__(self, config: Dict[str, Any]):
        self.logger = logging.getLogger(__name__)
        self.logger.info("Initializing vLLM Adapter")
        self.config = config
        self.vllm_config = config.get('vllm', {})
        # Additional vLLM-specific setup would go here

    # ... implementations for all methods


# Factory function to create the appropriate adapter
def create_adapter(config: Dict[str, Any]) -> LLMAdapter:
    """Create an adapter instance based on configuration."""
    adapter_type = config.get('adapter', {}).get('type', 'http')

    if adapter_type == 'http':
        return HTTPLLMAdapter(config)
    elif adapter_type == 'openai':
        return OpenAIAdapter(config)
    elif adapter_type == 'vllm':
        return vLLMAdapter(config)
    else:
        raise ValueError(f"Unknown adapter type: {adapter_type}")