feat: add unified strategy project workflow
This commit is contained in:
1 parent
67d12929fc
commit
a26fd32eba
28 files changed
+3382
-191
No files matched your search
+9
-5
@@ -96,9 +96,11 @@ _BUSY_PATTERNS = (
|
||||
"稍后重试",
|
||||
"请稍后重试",
|
||||
)
|
||||
_QUOTA_PATTERNS = (
|
||||
"insufficient_quota",
|
||||
"quota",
|
||||
_QUOTA_PATTERNS = (
|
||||
"insufficient_quota",
|
||||
"insufficient balance",
|
||||
"token plan limit",
|
||||
"quota",
|
||||
"billing",
|
||||
"credit",
|
||||
"payment",
|
||||
@@ -122,8 +124,10 @@ _AUTH_PATTERNS = (
|
||||
# error wrapped as `bad_response_status_code`, or a truncated/garbled inference
|
||||
# response). These are almost always transient inference-side issues and safe
|
||||
# to retry.
|
||||
_UPSTREAM_GARBAGE_PATTERNS = (
|
||||
"bad_response_status_code",
|
||||
_UPSTREAM_GARBAGE_PATTERNS = (
|
||||
"openai-compatible upstream application error",
|
||||
"null value for 'choices'",
|
||||
"bad_response_status_code",
|
||||
"bad response status",
|
||||
"expecting ',' delimiter",
|
||||
"expecting value",
|
||||
|
||||
+81
-16
@@ -25,7 +25,8 @@ message that originally carried it:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
from langchain_core.language_models import LanguageModelInput
|
||||
from langchain_core.messages import AIMessage, AIMessageChunk
|
||||
@@ -34,6 +35,65 @@ from langchain_openai import ChatOpenAI
|
||||
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages
|
||||
|
||||
|
||||
class OpenAICompatibleUpstreamError(RuntimeError):
|
||||
"""An OpenAI-compatible gateway reported an error inside a HTTP 200 body.
|
||||
|
||||
Several compatible gateways do not use an HTTP error status for quota,
|
||||
concurrency, or inference failures. Instead they return ``choices=null``
|
||||
(or an empty streamed chunk) together with a vendor ``base_resp`` object.
|
||||
LangChain raises a generic ``TypeError`` for the non-streaming shape and
|
||||
treats the streaming shape as an empty successful response. Raising one
|
||||
stable exception here lets DeerFlow's model middleware retry both paths.
|
||||
"""
|
||||
|
||||
def __init__(self, *, code: Any, message: str | None = None) -> None:
|
||||
self.code = code
|
||||
detail = _bounded_error_message(message)
|
||||
suffix = f": {detail}" if detail else ""
|
||||
super().__init__(f"OpenAI-compatible upstream application error (code={code}){suffix}")
|
||||
|
||||
|
||||
def _bounded_error_message(value: Any, *, limit: int = 500) -> str:
|
||||
"""Return a log-safe, bounded one-line provider message."""
|
||||
if not isinstance(value, str):
|
||||
return ""
|
||||
normalized = " ".join(value.split())
|
||||
return normalized[:limit]
|
||||
|
||||
|
||||
def _application_error(payload: Mapping[str, Any]) -> tuple[Any, str | None] | None:
|
||||
"""Extract a non-success ``base_resp`` error from a compatible payload."""
|
||||
raw = payload.get("base_resp") or payload.get("baseResp")
|
||||
if not isinstance(raw, Mapping):
|
||||
return None
|
||||
|
||||
code = raw.get("status_code")
|
||||
if code is None:
|
||||
code = raw.get("statusCode")
|
||||
if code is None:
|
||||
code = raw.get("code")
|
||||
|
||||
# MiniMax-compatible responses use 0 for success. Be conservative for
|
||||
# gateways that put informational metadata in ``base_resp``.
|
||||
normalized_code = str(code).strip().lower() if code is not None else ""
|
||||
if normalized_code in {"", "0", "200", "ok", "success"}:
|
||||
return None
|
||||
|
||||
message = raw.get("status_msg")
|
||||
if message is None:
|
||||
message = raw.get("statusMsg")
|
||||
if message is None:
|
||||
message = raw.get("message")
|
||||
return code, message if isinstance(message, str) else None
|
||||
|
||||
|
||||
def _raise_for_application_error(payload: Mapping[str, Any]) -> None:
|
||||
error = _application_error(payload)
|
||||
if error is not None:
|
||||
code, message = error
|
||||
raise OpenAICompatibleUpstreamError(code=code, message=message)
|
||||
|
||||
|
||||
class PatchedChatOpenAI(ChatOpenAI):
|
||||
"""ChatOpenAI with ``thought_signature`` preservation for Gemini thinking via OpenAI gateway.
|
||||
@@ -100,16 +160,20 @@ class PatchedChatOpenAI(ChatOpenAI):
|
||||
normalize_chat_payload_messages(payload)
|
||||
return payload
|
||||
|
||||
def _convert_chunk_to_generation_chunk(
|
||||
self,
|
||||
chunk: dict,
|
||||
default_chunk_class: type,
|
||||
base_generation_info: dict | None,
|
||||
) -> ChatGenerationChunk | None:
|
||||
"""Preserve streamed ``reasoning_content`` in ``additional_kwargs``."""
|
||||
generation_chunk = super()._convert_chunk_to_generation_chunk(
|
||||
chunk,
|
||||
default_chunk_class,
|
||||
def _convert_chunk_to_generation_chunk(
|
||||
self,
|
||||
chunk: dict,
|
||||
default_chunk_class: type,
|
||||
base_generation_info: dict | None,
|
||||
) -> ChatGenerationChunk | None:
|
||||
"""Preserve streamed ``reasoning_content`` in ``additional_kwargs``."""
|
||||
# Some compatible gateways return a vendor error event followed by a
|
||||
# normal-looking usage chunk, all under HTTP 200. Detect the first
|
||||
# event before LangChain converts it into an empty AIMessageChunk.
|
||||
_raise_for_application_error(chunk)
|
||||
generation_chunk = super()._convert_chunk_to_generation_chunk(
|
||||
chunk,
|
||||
default_chunk_class,
|
||||
base_generation_info,
|
||||
)
|
||||
if generation_chunk is None:
|
||||
@@ -130,11 +194,12 @@ class PatchedChatOpenAI(ChatOpenAI):
|
||||
self,
|
||||
response: dict | Any,
|
||||
generation_info: dict | None = None,
|
||||
) -> ChatResult:
|
||||
"""Preserve non-stream ``reasoning_content`` in ``additional_kwargs``."""
|
||||
result = super()._create_chat_result(response, generation_info)
|
||||
response_dict = response if isinstance(response, dict) else response.model_dump()
|
||||
choices = response_dict.get("choices", [])
|
||||
) -> ChatResult:
|
||||
"""Preserve non-stream ``reasoning_content`` in ``additional_kwargs``."""
|
||||
response_dict = response if isinstance(response, dict) else response.model_dump()
|
||||
_raise_for_application_error(response_dict)
|
||||
result = super()._create_chat_result(response, generation_info)
|
||||
choices = response_dict.get("choices", [])
|
||||
|
||||
generations: list[ChatGeneration] = []
|
||||
for index, generation in enumerate(result.generations):
|
||||
|
||||
Reference in new issue
Block a user