cmvr-visual/backend/app/adapters/provider/client.py

170 lines
5.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""推理适配公共门面:无数据库依赖,HTTP 客户端可替换。"""
from __future__ import annotations
import asyncio
import httpx
from app.domain.errors import MediaError as ProviderError
from app.domain.execution import CpuResult, current_phase_callback, enter_phase
from .contracts import capabilities, validate_arguments
from . import composition, cpu, files, gpu, qwen, runtime, speech, transport
__all__ = [
"Provider",
"capabilities",
"validate_arguments",
"ProviderError",
"CpuResult",
"current_phase_callback",
"enter_phase",
]
class Provider:
def __init__(self, settings):
self.settings = settings
self._runtime_lock = asyncio.Lock()
# 组合模块每次读取此属性,替换客户端后不会继续使用旧连接。
self.client = httpx.AsyncClient(
trust_env=False,
follow_redirects=False,
timeout=httpx.Timeout(float(settings.request_timeout_seconds)),
limits=httpx.Limits(max_connections=4, max_keepalive_connections=2),
)
async def aclose(self):
await self.client.aclose()
def _headers(self, family="inference"):
return transport._headers(self, family)
@staticmethod
def _http_error(status):
return transport._http_error(status)
async def _json(self, method, url, *, payload=None, headers=None, timeout=None):
return await transport._json(
self, method, url, payload=payload, headers=headers, timeout=timeout
)
async def health(self):
return await runtime.health(self)
@staticmethod
def _runtime_value(value):
return runtime._runtime_value(value)
async def runtime_status(self):
return await runtime.runtime_status(self)
def _mirror_runtime_policy(self, policy):
return runtime._mirror_runtime_policy(self, policy)
async def scheduler_status(self):
return await runtime.scheduler_status(self)
def _work(self, path):
return files._work(self, path)
def _asset(self, asset_id, assets):
return files._asset(self, asset_id, assets)
async def _upload_input(self, source):
return await files._upload_input(self, source)
async def _download_url(
self, url, destination, headers, *, payload=None, timeout=None, total_timeout=None
):
return await files._download_url(
self,
url,
destination,
headers,
payload=payload,
timeout=timeout,
total_timeout=total_timeout,
)
async def speech_health(self):
return await speech.health(self)
async def download(self, upstream_id, index, destination):
return await files.download(self, upstream_id, index, destination)
@staticmethod
def _json_file(work, name, value):
return files._json_file(work, name, value)
async def prepare_gpu_payload(
self,
task_type,
inputs,
parameters,
assets,
work_dir,
*,
analysis_binding=None,
dialogue_context=None,
):
return await gpu.prepare_gpu_payload(
self,
task_type,
inputs,
parameters,
assets,
work_dir,
analysis_binding=analysis_binding,
dialogue_context=dialogue_context,
)
async def submit(self, payload, idempotency_key):
return await gpu.submit(self, payload, idempotency_key)
@staticmethod
def _job_id(value):
return gpu._job_id(value)
@staticmethod
def _sanitize_job(value):
return gpu._sanitize_job(value)
async def query(self, upstream_id):
return await gpu.query(self, upstream_id)
async def upstream_capabilities(self):
return await gpu.upstream_capabilities(self)
async def control(
self,
upstream_id,
desired_state,
command_id,
expected_version=None,
expected_execution_id=None,
):
return await gpu.control(
self, upstream_id, desired_state, command_id, expected_version, expected_execution_id
)
async def cancel(self, upstream_id):
return await gpu.cancel(self, upstream_id)
async def _chat(self, messages, *, max_tokens=2048, thinking=False):
return await qwen._chat(self, messages, max_tokens=max_tokens, thinking=thinking)
async def _image_uri(self, source):
return await qwen._image_uri(self, source)
async def _structure(self, inputs, p, assets, work):
return await qwen._structure(self, inputs, p, assets, work)
async def _speech(self, inputs, p, work):
return await speech._speech(self, inputs, p, work)
async def _compose(self, inputs, p, assets, work):
return await composition._compose(self, inputs, p, assets, work)
async def run_cpu(self, task_type, inputs, parameters, assets, work_dir):
return await cpu.run_cpu(self, task_type, inputs, parameters, assets, work_dir)