Spaces:
Sleeping
Sleeping
File size: 9,742 Bytes
caf8059 5ca3c16 caf8059 5ca3c16 caf8059 115b94d caf8059 5ca3c16 8f4045a caf8059 115b94d caf8059 8f4045a caf8059 8f4045a caf8059 5ca3c16 caf8059 5ca3c16 caf8059 8f4045a caf8059 5ca3c16 caf8059 5ca3c16 caf8059 5ca3c16 caf8059 5ca3c16 caf8059 5ca3c16 caf8059 8f4045a caf8059 5ca3c16 | 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 | """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
|