Spaces:
Sleeping
Sleeping
Download src/adapters/llm/chat_completion.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 9.74 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/adapters/llm/chat_completion.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/adapters/llm/chat_completion.py
-
curl -L -o chat_completion.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/adapters/llm/chat_completion.py
9.74 kB
| """Configured chat-completion protocol; no credential discovery or automatic replay.""" | |
| import json | |
| import re | |
| import time | |
| import hashlib | |
| from urllib.parse import urlsplit | |
| import httpx | |
| from src.modules.llm_gateway.service import ModelReply, ProviderFailure | |
| class ChatCompletionProvider: | |
| def __init__(self, *, name, endpoint, model, token, timeout=60, transport=None, enable_thinking=None, json_mode=True): | |
| url = urlsplit(endpoint) | |
| if (url.scheme != 'https' or not url.hostname or url.username or url.password | |
| or url.query or url.fragment or not 1 <= timeout <= 300 or not token or not model): | |
| raise ValueError('INVALID_PROVIDER_CONFIGURATION') | |
| self.name, self.endpoint, self.model = name, endpoint, model | |
| self._token, self.timeout, self.transport = token, timeout, transport | |
| self.enable_thinking = enable_thinking | |
| self.json_mode = json_mode | |
| def payload(self,messages,max_tokens): | |
| payload={'model':self.model,'messages':messages,'max_tokens':max_tokens,'stream':False} | |
| if self.json_mode:payload['response_format']={'type':'json_object'} | |
| if self.enable_thinking is not None:payload['enable_thinking']=self.enable_thinking | |
| return payload | |
| def complete(self, messages, max_tokens, cancelled): | |
| deadline = time.monotonic() + self.timeout | |
| evidence={} | |
| response_received=False | |
| received_bytes=0 | |
| try: | |
| payload = self.payload(messages,max_tokens) | |
| with httpx.Client(timeout=httpx.Timeout(self.timeout, connect=10), follow_redirects=False, | |
| trust_env=False, transport=self.transport) as client: | |
| with client.stream('POST', self.endpoint, headers={'Authorization': 'Bearer ' + self._token}, | |
| json=payload) as response: | |
| response_received=True | |
| if response.status_code in {402, 429}: | |
| raise ProviderFailure('MODEL_QUOTA_REJECTED', rejected=True, retryable=True) | |
| if response.status_code in {400, 401, 403, 404, 422}: | |
| # Inspect only bounded error text for categorical diagnostics, never expose it. | |
| error_bytes = bytearray() | |
| for part in response.iter_bytes(8192): | |
| error_bytes.extend(part) | |
| if len(error_bytes) > 16384: | |
| break | |
| error_text = bytes(error_bytes).decode('utf-8', errors='replace').lower() | |
| quota = response.status_code == 403 and any(word in error_text for word in ('quota', 'arrear', 'allocation', 'billing', 'balance')) | |
| details = {'http_status': response.status_code, 'quota_rejection': quota, | |
| 'thinking_parameter': 'enable_thinking' in error_text, | |
| 'response_format_parameter': 'response_format' in error_text, | |
| 'model_parameter': 'model' in error_text} | |
| # Retain categorical provider evidence without returning the message, | |
| # which can contain account details or credential fragments. | |
| try: | |
| envelope=json.loads(error_bytes) | |
| error=envelope.get('error',envelope) | |
| code=error.get('code') if isinstance(error,dict) else None | |
| if (isinstance(code,str) and re.fullmatch(r'[A-Za-z][A-Za-z0-9_.]{0,95}',code) | |
| and code != self._token): | |
| details['provider_error_code']=code | |
| except (ValueError,TypeError,AttributeError): | |
| pass | |
| details['response_evidence']={'response_received':True, | |
| 'received_bytes':len(error_bytes),'response_sha256':hashlib.sha256(error_bytes).hexdigest(), | |
| 'raw_retained':False} | |
| raise ProviderFailure('MODEL_QUOTA_REJECTED' if quota else 'MODEL_REQUEST_REJECTED', | |
| rejected=True, retryable=quota, details=details) | |
| if response.status_code != 200: | |
| raise ProviderFailure(details={'http_status':response.status_code,'failure_category':'http_error'}) | |
| raw = bytearray() | |
| for chunk in response.iter_bytes(chunk_size=8192): | |
| if cancelled() or time.monotonic() > deadline: | |
| raise ProviderFailure(details={'failure_category':'cancelled_or_deadline'}) | |
| raw.extend(chunk) | |
| received_bytes=len(raw) | |
| if len(raw) > 1024 * 1024: | |
| raise ProviderFailure('MODEL_RESPONSE_TOO_LARGE',details={'failure_category':'output_size', | |
| 'reason_code':'PROVIDER_RESPONSE_LIMIT','response_evidence':{'response_bytes':len(raw), | |
| 'response_sha256':hashlib.sha256(raw).hexdigest(),'response_complete':False,'raw_retained':False}}) | |
| if cancelled() or time.monotonic() > deadline: | |
| raise ProviderFailure(details={'failure_category':'cancelled_or_deadline'}) | |
| evidence={'response_bytes':len(raw),'response_sha256':hashlib.sha256(raw).hexdigest(),'raw_retained':False} | |
| try:data = json.loads(raw) | |
| except json.JSONDecodeError as error: | |
| raise ProviderFailure(details={'failure_category':'output_parse','reason_code':'PROVIDER_ENVELOPE_JSON_INVALID', | |
| 'parse_layer':'provider_envelope','parse_position':error.pos,'parse_line':error.lineno,'parse_column':error.colno, | |
| 'response_evidence':evidence}) from None | |
| choice = data['choices'][0] | |
| usage = data['usage'] | |
| input_tokens, output_tokens = usage['prompt_tokens'], usage['completion_tokens'] | |
| if (type(input_tokens) is not int or type(output_tokens) is not int | |
| or input_tokens < 0 or not 0 <= output_tokens <= max_tokens): | |
| raise ProviderFailure('MODEL_USAGE_INVALID') | |
| request_id = str(data.get('id', '')) | |
| request_id = request_id if re.fullmatch(r'[A-Za-z0-9_-]{1,128}', request_id) else '' | |
| reason = choice.get('finish_reason') | |
| reason = reason if reason in {'stop','length','tool_calls','content_filter'} else 'unsupported' | |
| value=choice.get('message',{}).get('content') | |
| evidence.update(content_type=type(value).__name__) | |
| if isinstance(value,str):evidence.update(content_bytes=len(value.encode()),content_sha256=hashlib.sha256(value.encode()).hexdigest()) | |
| details = {'finish_reason':reason,'request_id':request_id,'response_evidence':evidence} | |
| if reason != 'stop' or choice['message'].get('tool_calls'): | |
| raise ProviderFailure('MODEL_OUTPUT_TRUNCATED' if reason=='length' else 'MODEL_OUTPUT_UNSUPPORTED', | |
| details={**details,'failure_category':'output_truncated' if reason=='length' else 'output_schema', | |
| 'reason_code':'OUTPUT_LENGTH_LIMIT' if reason=='length' else 'OUTPUT_UNSUPPORTED_FINISH'},input_tokens=input_tokens,output_tokens=output_tokens) | |
| try: | |
| content=json.loads(value) | |
| except json.JSONDecodeError as error: | |
| raise ProviderFailure('MODEL_OUTPUT_INVALID',details={**details,'failure_category':'output_parse','reason_code':'COMPLETION_JSON_INVALID', | |
| 'parse_layer':'completion_content','parse_position':error.pos,'parse_line':error.lineno,'parse_column':error.colno}, | |
| input_tokens=input_tokens,output_tokens=output_tokens) from None | |
| except (ValueError,TypeError): | |
| raise ProviderFailure('MODEL_OUTPUT_INVALID',details={**details,'failure_category':'output_schema','reason_code':'COMPLETION_CONTENT_TYPE_INVALID'},input_tokens=input_tokens,output_tokens=output_tokens) from None | |
| return ModelReply(content,input_tokens,output_tokens,self.model,reason,request_id,evidence) | |
| except ProviderFailure: | |
| raise | |
| except httpx.TimeoutException as exc: | |
| reason=('CONNECT_TIMEOUT' if isinstance(exc,httpx.ConnectTimeout) else | |
| 'READ_TIMEOUT' if isinstance(exc,httpx.ReadTimeout) else | |
| 'WRITE_TIMEOUT' if isinstance(exc,httpx.WriteTimeout) else 'POOL_TIMEOUT') | |
| raise ProviderFailure(details={'failure_category':'transport_timeout','reason_code':reason, | |
| 'response_evidence':{'response_received':response_received,'received_bytes':received_bytes,'raw_retained':False}}) from None | |
| except httpx.TransportError as exc: | |
| reason='REMOTE_PROTOCOL_ERROR' if isinstance(exc,httpx.RemoteProtocolError) else 'CONNECTION_INTERRUPTED' | |
| raise ProviderFailure(details={'failure_category':'transport_error','reason_code':reason, | |
| 'response_evidence':{'response_received':response_received,'received_bytes':received_bytes,'raw_retained':False}}) from None | |
| except Exception: | |
| # Do not expose HTTP headers, response bodies, tokens or raw exception messages. | |
| raise ProviderFailure(details={'failure_category':'output_unknown','reason_code':'PROVIDER_ENVELOPE_FIELDS_UNKNOWN', | |
| 'parse_layer':'provider_envelope','response_evidence':evidence}) from None | |