This commit is contained in:
zyp
2026-11-18 16:01:50 +08:00
parent 0dc48ad092
commit 0c3d0c1ce4
30 changed files with 1525 additions and 93 deletions
+12 -5
View File
@@ -197,7 +197,8 @@ def _stream_json_response(client, url, request, headers, timeout_seconds, observ
def complete_json(instruction: str, content: dict, schema: dict | None = None, *, timeout_seconds: float = 40,
max_tokens: int = 1600, images: list[tuple[int, str]] | None = None,
prose: bool = False) -> dict:
prose: bool = False, prompt_schema: dict | None = None,
minimum_execution_seconds: float = 0) -> dict:
from app.modules.work_assistant.model_dispatch import dispatch, DispatchUnavailable, reserved_turn, parallel_bid, background_review
started = time.monotonic()
reservation = reserved_turn.get()
@@ -207,7 +208,7 @@ def complete_json(instruction: str, content: dict, schema: dict | None = None, *
from app.modules.work_assistant.bid_progress import current_progress
progress = current_progress.get()
budget = progress.tokens(max_tokens) if progress is not None else max_tokens
messages = [{'role': 'system', 'content': _contract_instruction(instruction, schema)},
messages = [{'role': 'system', 'content': _contract_instruction(instruction, prompt_schema if prompt_schema is not None else schema)},
{'role': 'user', 'content': json.dumps(content, ensure_ascii=False, separators=(',', ':'))}]
tokens = FULL_CONTEXT if images else request_tokens(messages, budget, _model_client())
try:
@@ -218,9 +219,13 @@ def complete_json(instruction: str, content: dict, schema: dict | None = None, *
raise ModelUnavailable(str(exc)) from exc
service_failure = False
try:
if minimum_execution_seconds and timeout_seconds-(time.monotonic()-started)<minimum_execution_seconds:
# Admission consumed the execution window. Continue the durable job
# later without opening an upstream request or reporting an outage.
raise ModelUnavailable('review_continuation', retry_after=2)
return _complete_json(instruction, content, schema,
timeout_seconds=max(0.1, timeout_seconds - (time.monotonic() - started)),
max_tokens=max_tokens, images=images, prose=prose)
max_tokens=max_tokens, images=images, prose=prose, prompt_schema=prompt_schema)
except ModelUnavailable as exc:
service_failure = exc.retryable or exc.reason in {
'model_account_unavailable', 'direct_http_401', 'direct_http_403',
@@ -235,7 +240,7 @@ def complete_json(instruction: str, content: dict, schema: dict | None = None, *
def _complete_json(instruction: str, content: dict, schema: dict | None = None, *, timeout_seconds: float = 40,
max_tokens: int = 1600, images: list[tuple[int, str]] | None = None,
prose: bool = False) -> dict:
prose: bool = False, prompt_schema: dict | None = None) -> dict:
started = time.perf_counter()
settings = get_settings()
from app.modules.work_assistant.bid_progress import current_progress, stream_observer
@@ -255,7 +260,9 @@ def _complete_json(instruction: str, content: dict, schema: dict | None = None,
# private; only a complete, validated result advances business progress.
if observer is None:
observer = lambda raw: None
instruction = _contract_instruction(instruction, schema)
# The decoder still receives the full schema below. A caller can describe
# its shape concisely in the prompt instead of duplicating long ID branches.
instruction = _contract_instruction(instruction, prompt_schema if prompt_schema is not None else schema)
if not settings.work_assistant_model_enabled:
raise ModelUnavailable("Model routing is disabled")
gateway = (settings.work_assistant_model_control_url or "").strip().rstrip("/")