sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -2,8 +2,11 @@ import logging
from langchain.chat_models import BaseChatModel
from deerflow.config import get_app_config
from deerflow.config.app_config import AppConfig
from deerflow.config import get_app_config
from deerflow.config.app_config import AppConfig
from deerflow.config.model_config import WriteFileTransport
from deerflow.config.system_settings import get_write_file_compatibility_settings
from deerflow.models.chat_stream_state import needs_qwen_flash_next_integrity
from deerflow.reflection import resolve_class
from deerflow.tracing import build_tracing_callbacks
@@ -31,6 +34,92 @@ def _vllm_disable_chat_template_kwargs(chat_template_kwargs: dict) -> dict:
return disable_kwargs
# Qwen3.8 chat templates only accept these three effort names.
_QWEN_REASONING_EFFORT = {
"minimal": "low",
"low": "low",
"medium": "medium",
"high": "xhigh",
"xhigh": "xhigh",
}
_OPENAI_THINKING_OFF_EXTRA_BODY = {
"enable_thinking": False,
"chat_template_kwargs": {"enable_thinking": False},
"thinking": {"type": "disabled"},
}
def _is_vllm_provider(model_use: str) -> bool:
lowered = (model_use or "").lower()
return "vllm_provider" in lowered or lowered.endswith(":vllmchatmodel")
def _is_qwen38_model(model_config) -> bool:
blob = f"{getattr(model_config, 'name', '')} {getattr(model_config, 'model', '')}".lower()
return "qwen3.8" in blob
def _should_control_openai_thinking(model_config) -> bool:
"""Whether this OpenAI-compatible model needs explicit thinking on/off flags.
Qwen3.8 (and current vLLM reasoning parsers) default to thinking at ``xhigh``.
If DeerFlow does not send ``enable_thinking: false``, the first visible answer
token waits until that hidden reasoning finishes — third-party chat UIs that
disable thinking or stream the reasoning field therefore feel much faster.
"""
if getattr(model_config, "supports_thinking", False):
return True
if _is_vllm_provider(getattr(model_config, "use", "")):
return True
return _is_qwen38_model(model_config)
def _map_qwen_reasoning_effort(effort: object) -> str | None:
if effort is None:
return None
return _QWEN_REASONING_EFFORT.get(str(effort).strip().lower())
def _apply_openai_thinking_control(
model_config,
model_class,
model_settings_from_config: dict,
*,
thinking_enabled: bool,
) -> None:
"""Inject vLLM/Qwen thinking flags when the UI toggle is on or off."""
if "extra_body" not in getattr(model_class, "model_fields", {}):
return
qwen_vllm = _is_vllm_provider(getattr(model_config, "use", "")) or _is_qwen38_model(model_config)
if thinking_enabled:
if not qwen_vllm:
return
enable_body: dict = {
"enable_thinking": True,
"chat_template_kwargs": {"enable_thinking": True},
}
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
enable_body,
)
return
if not _should_control_openai_thinking(model_config):
return
disable_body = (
{
"enable_thinking": False,
"chat_template_kwargs": {"enable_thinking": False},
}
if qwen_vllm
else _OPENAI_THINKING_OFF_EXTRA_BODY
)
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
disable_body,
)
def _enable_stream_usage_by_default(model_use_path: str, model_settings_from_config: dict) -> None:
"""Enable stream usage for OpenAI-compatible models unless explicitly configured.
@@ -92,9 +181,50 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
"when_thinking_enabled",
"when_thinking_disabled",
"thinking",
"supports_vision",
},
)
"supports_vision",
"write_file_transport",
},
)
configured_write_file_transport = model_config.write_file_transport
runtime_write_file_compatibility = get_write_file_compatibility_settings()
effective_write_file_transport = (
WriteFileTransport.BUFFERED_GATEWAY
if runtime_write_file_compatibility.enabled
else configured_write_file_transport
)
if effective_write_file_transport in {
WriteFileTransport.BUFFERED_GATEWAY,
WriteFileTransport.ARTIFACT_RENDERER,
WriteFileTransport.TEXT_BRIDGE,
}:
from deerflow.models.patched_openai import PatchedChatOpenAI
if not issubclass(model_class, PatchedChatOpenAI):
raise ValueError(
"write_file_transport=buffered_gateway/artifact_renderer/text_bridge is supported only by "
"langchain_openai:ChatOpenAI or PatchedChatOpenAI models"
)
if model_config.use_responses_api:
raise ValueError(
"write_file_transport=buffered_gateway/artifact_renderer/text_bridge requires the "
"OpenAI-compatible chat-completions API, not use_responses_api"
)
# The admin runtime switch overrides every compatible model on its next
# construction. With the switch off, retain the legacy per-model value.
model_settings_from_config["write_file_transport"] = effective_write_file_transport.value
# ``max_input_tokens`` is a DeerFlow-side safety limit, not an OpenAI API
# request parameter. ModelConfig intentionally allows provider-specific
# extras, so without removing it here ChatOpenAI would forward the unknown
# key to some compatible gateways and turn the protection itself into a
# HTTP 400. Mirror it into LangChain's local-only ``profile`` metadata
# when the selected model supports that field; the final context guard
# reads the original model config and therefore also works for adapters
# without profiles.
configured_max_input_tokens = model_settings_from_config.pop("max_input_tokens", None)
if configured_max_input_tokens is not None and "profile" in getattr(model_class, "model_fields", {}):
profile = dict(model_settings_from_config.get("profile") or {})
profile["max_input_tokens"] = configured_max_input_tokens
model_settings_from_config["profile"] = profile
# Compute effective when_thinking_enabled by merging in the `thinking` shortcut field.
# The `thinking` shortcut is equivalent to setting when_thinking_enabled["thinking"].
has_thinking_settings = (model_config.when_thinking_enabled is not None) or (model_config.thinking is not None)
@@ -107,6 +237,13 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
raise ValueError(f"Model {name} does not support thinking. Set `supports_thinking` to true in the `config.yaml` to enable thinking.") from None
if effective_wte:
model_settings_from_config.update(effective_wte)
elif thinking_enabled and model_config.supports_thinking and model_config.use == "langchain_openai:ChatOpenAI":
# vLLM/Qwen OpenAI-compatible endpoints commonly require this explicit
# request flag before they emit delta.reasoning.
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
{"enable_thinking": True},
)
if not thinking_enabled:
if model_config.when_thinking_disabled is not None:
# User-provided disable settings take full precedence
@@ -127,9 +264,25 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
elif has_thinking_settings and effective_wte.get("thinking", {}).get("type"):
# Native langchain_anthropic: thinking is a direct constructor parameter
model_settings_from_config["thinking"] = {"type": "disabled"}
if not model_config.supports_reasoning_effort:
kwargs.pop("reasoning_effort", None)
# The Gateway validates the database-backed capability before constructing
# the graph. Honor that server-validated runtime value without consulting a
# legacy config.yaml flag. Qwen/vLLM accepts low/medium/xhigh; normalize the
# old UI aliases defensively for internal callers that do not traverse the
# public run boundary.
requested_reasoning_effort = kwargs.get("reasoning_effort")
if requested_reasoning_effort is not None:
model_settings_from_config.pop("reasoning_effort", None)
if _is_vllm_provider(model_config.use) or _is_qwen38_model(model_config):
mapped_effort = _map_qwen_reasoning_effort(requested_reasoning_effort)
if mapped_effort is not None:
kwargs["reasoning_effort"] = mapped_effort
_apply_openai_thinking_control(
model_config,
model_class,
model_settings_from_config,
thinking_enabled=thinking_enabled,
)
# Hard override: force-disable thinking regardless of the per-model config shape.
# Injected only for OpenAI-compatible classes (those declaring `extra_body`);
@@ -139,15 +292,10 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
# (DeepSeek-V3.1+/Qwen3/GLM hybrid models on api.siliconflow.cn — the knob the
# nested ``chat_template_kwargs`` shape does NOT reach), vLLM/Qwen native
# ``chat_template_kwargs.enable_thinking``, and Zhipu GLM ``thinking.type``.
# Only models that explicitly opt into thinking should receive vendor-specific
# disable payloads. Generic OpenAI-compatible gateways (including LiteLLM)
# may reject these otherwise-unknown fields instead of forwarding them.
if (
force_disable_thinking
and not thinking_enabled
and model_config.supports_thinking
and "extra_body" in getattr(model_class, "model_fields", {})
):
# Only models that explicitly opt into thinking should receive vendor-specific
# disable payloads. Generic OpenAI-compatible gateways (including LiteLLM)
# may reject these otherwise-unknown fields instead of forwarding them.
if force_disable_thinking and not thinking_enabled and model_config.supports_thinking and "extra_body" in getattr(model_class, "model_fields", {}):
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
{"enable_thinking": False, "chat_template_kwargs": {"enable_thinking": False}, "thinking": {"type": "disabled"}},
@@ -186,11 +334,35 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
if "stream_usage" in getattr(model_class, "model_fields", {}):
model_settings_from_config["stream_usage"] = True
model_instance = model_class(**kwargs, **model_settings_from_config)
# The deployment may expose the affected model under a generic provider id.
# Enable its local stream checks by either id or configured name, while
# honoring an explicit override and keeping other models on their old path.
if "strict_stream_integrity" in getattr(model_class, "model_fields", {}):
if "strict_stream_integrity" in kwargs:
model_settings_from_config["strict_stream_integrity"] = kwargs.pop("strict_stream_integrity")
elif needs_qwen_flash_next_integrity(model_config.name, model_config.model):
model_settings_from_config.setdefault("strict_stream_integrity", True)
callbacks = build_tracing_callbacks()
if callbacks:
existing_callbacks = model_instance.callbacks or []
model_instance = model_class(**kwargs, **model_settings_from_config)
# Always emit a compact model lifecycle trace. In particular, the first
# streamed callback proves that the upstream endpoint accepted the request
# and started returning data, including reasoning-only chunks whose normal
# content string is empty.
from deerflow.models.timing_callback import ModelTimingCallback
existing_callbacks = model_instance.callbacks or []
model_instance.callbacks = [
*existing_callbacks,
ModelTimingCallback(
model_name=name,
base_url=str(model_config.base_url) if model_config.base_url else None,
),
]
callbacks = build_tracing_callbacks()
if callbacks:
existing_callbacks = model_instance.callbacks or []
model_instance.callbacks = [*existing_callbacks, *callbacks]
logger.debug(f"Tracing attached to model '{name}' with providers={len(callbacks)}")
return model_instance