90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
"""Authenticated internal model-service RPC; never forwards user-supplied endpoints."""
|
|
import contextlib
|
|
import asyncio
|
|
import hashlib
|
|
import hmac
|
|
import json
|
|
import os
|
|
import time
|
|
import uuid
|
|
from functools import lru_cache
|
|
|
|
import requests
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def _secret():
|
|
if os.environ.get('SERVER_NAME') == 'local_model':
|
|
from local_model.models.system_setting import SystemSetting
|
|
else:
|
|
from system_manage.models import SystemSetting
|
|
meta = SystemSetting.objects.filter(type=1).values_list('meta', flat=True).first()
|
|
if not meta or not meta.get('value'):
|
|
raise ValueError('模型服务认证未配置')
|
|
return hashlib.sha256(('model-runtime-v1:' + meta['value']).encode()).digest()
|
|
|
|
|
|
def signed_request(payload):
|
|
data = {**payload, 'timestamp': time.time(), 'nonce': uuid.uuid4().hex}
|
|
body = json.dumps(data, separators=(',', ':'), sort_keys=True).encode()
|
|
signature = hmac.new(_secret(), body, hashlib.sha256).hexdigest()
|
|
return body, signature
|
|
|
|
|
|
def verify_request(body, signature):
|
|
expected = hmac.new(_secret(), body, hashlib.sha256).hexdigest()
|
|
if not signature or not hmac.compare_digest(expected, signature):
|
|
raise ValueError('模型服务认证失败')
|
|
data = json.loads(body)
|
|
if abs(time.time() - float(data.get('timestamp', 0))) > 60:
|
|
raise ValueError('模型服务请求已过期')
|
|
return data
|
|
|
|
|
|
def runtime_request(operation, model_id, **extra):
|
|
from maxkb.const import CONFIG
|
|
body, signature = signed_request({'operation': operation, 'model_id': str(model_id), **extra})
|
|
host = CONFIG.get('LOCAL_MODEL_HOST')
|
|
url = f'{CONFIG.get("LOCAL_MODEL_PROTOCOL")}://{host}:{CONFIG.get("LOCAL_MODEL_PORT")}{CONFIG.get_admin_path()}/api/model_runtime'
|
|
try:
|
|
response = requests.post(url, data=body, headers={'Content-Type': 'application/json',
|
|
'X-Model-Runtime-Signature': signature},
|
|
timeout=(5, 620 if operation == 'acquire' else 15))
|
|
result = response.json()
|
|
except (requests.RequestException, ValueError) as exc:
|
|
raise ValueError('本地模型服务不可用') from exc
|
|
if response.status_code != 200 or result.get('code') != 200:
|
|
raise ValueError(result.get('message') or '模型服务请求失败')
|
|
return result['data']
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def inference_lease(model_id):
|
|
if not model_id:
|
|
# Credential validation has no saved resource and must keep its original behavior.
|
|
yield None
|
|
return
|
|
data = runtime_request('acquire', model_id)
|
|
try:
|
|
yield data
|
|
finally:
|
|
try:
|
|
runtime_request('release', model_id, lease=data['lease'])
|
|
except ValueError:
|
|
pass # Server expires abandoned leases; never hide the inference result.
|
|
|
|
|
|
@contextlib.asynccontextmanager
|
|
async def async_inference_lease(model_id):
|
|
if not model_id:
|
|
yield None
|
|
return
|
|
data = await asyncio.to_thread(runtime_request, 'acquire', model_id)
|
|
try:
|
|
yield data
|
|
finally:
|
|
try:
|
|
await asyncio.to_thread(runtime_request, 'release', model_id, lease=data['lease'])
|
|
except ValueError:
|
|
pass
|