fix: accept vLLM thinking in reasoning or reasoning_content
Newer vLLM parsers put CoT in reasoning; older builds and DeepSeek still use reasoning_content. Canonicalize to the UI field and echo only the original wire field so other ChatOpenAI models stay unchanged. Point qwen3.6 at VllmChatModel. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d765afe2ac
commit
f4c7d41a60
10 files changed
+371
-123
No files matched your search
+26
-40
@@ -33,6 +33,7 @@ from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResu
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages
|
||||
from deerflow.models.reasoning_fields import apply_reasoning_from_payload, extract_reasoning_text, fill_ui_reasoning_content, restore_reasoning_fields
|
||||
|
||||
|
||||
class PatchedChatOpenAI(ChatOpenAI):
|
||||
@@ -118,12 +119,12 @@ class PatchedChatOpenAI(ChatOpenAI):
|
||||
choices = chunk.get("choices", []) or chunk.get("chunk", {}).get("choices", [])
|
||||
choice = choices[0] if choices else {}
|
||||
delta = choice.get("delta") if isinstance(choice, dict) else None
|
||||
reasoning_content = _extract_reasoning_content(delta if isinstance(delta, dict) else None)
|
||||
if reasoning_content and isinstance(generation_chunk.message, AIMessageChunk):
|
||||
generation_chunk.message = _append_reasoning_content(
|
||||
if isinstance(generation_chunk.message, AIMessageChunk):
|
||||
generation_chunk.message = apply_reasoning_from_payload(
|
||||
generation_chunk.message,
|
||||
reasoning_content,
|
||||
delta if isinstance(delta, dict) else None,
|
||||
)
|
||||
generation_chunk.message = fill_ui_reasoning_content(generation_chunk.message)
|
||||
return generation_chunk
|
||||
|
||||
def _create_chat_result(
|
||||
@@ -141,13 +142,19 @@ class PatchedChatOpenAI(ChatOpenAI):
|
||||
message = generation.message
|
||||
choice = choices[index] if index < len(choices) else {}
|
||||
choice_message = choice.get("message", {}) if isinstance(choice, dict) else {}
|
||||
reasoning_content = _extract_reasoning_content(choice_message if isinstance(choice_message, dict) else None)
|
||||
if reasoning_content and isinstance(message, AIMessage):
|
||||
message = _append_reasoning_content(message, reasoning_content)
|
||||
generation = ChatGeneration(
|
||||
message=message,
|
||||
generation_info=generation.generation_info,
|
||||
if isinstance(message, AIMessage):
|
||||
updated = apply_reasoning_from_payload(
|
||||
message,
|
||||
choice_message if isinstance(choice_message, dict) else None,
|
||||
append=False,
|
||||
)
|
||||
updated = fill_ui_reasoning_content(updated)
|
||||
if updated is not message:
|
||||
message = updated
|
||||
generation = ChatGeneration(
|
||||
message=message,
|
||||
generation_info=generation.generation_info,
|
||||
)
|
||||
generations.append(generation)
|
||||
|
||||
return ChatResult(generations=generations, llm_output=result.llm_output)
|
||||
@@ -195,43 +202,22 @@ def _restore_tool_call_signatures(payload_msg: dict, orig_msg: AIMessage) -> Non
|
||||
|
||||
|
||||
def _restore_reasoning_content(payload_msg: dict, orig_msg: AIMessage) -> None:
|
||||
"""Re-inject ``reasoning_content`` onto assistant payload message.
|
||||
"""Re-inject thinking fields onto assistant payload message.
|
||||
|
||||
Some thinking-enabled OpenAI-compatible providers require each historical
|
||||
assistant turn to include the original ``reasoning_content`` in subsequent
|
||||
requests. LangChain stores it in ``AIMessage.additional_kwargs`` but does
|
||||
not always serialize it back into ``messages``.
|
||||
assistant turn to include the original thinking field in subsequent
|
||||
requests. Newer vLLM puts that text in ``reasoning``; DeepSeek and older
|
||||
vLLM use ``reasoning_content``. Echo only the fields this turn originally
|
||||
carried so other models are not given an extra field.
|
||||
"""
|
||||
reasoning_content = orig_msg.additional_kwargs.get("reasoning_content")
|
||||
if reasoning_content is not None:
|
||||
payload_msg["reasoning_content"] = reasoning_content
|
||||
restore_reasoning_fields(payload_msg, orig_msg)
|
||||
|
||||
|
||||
def _extract_reasoning_content(payload: dict | None) -> str | None:
|
||||
"""Extract reasoning text from provider payload delta/message objects.
|
||||
|
||||
vLLM Qwen reasoning parsers use ``reasoning_content`` on streaming deltas
|
||||
and ``reasoning`` on some non-stream / older builds. Read both so the UI
|
||||
can show the first thinking token instead of waiting for answer content.
|
||||
"""
|
||||
if not isinstance(payload, dict):
|
||||
return None
|
||||
reasoning_content = payload.get("reasoning_content")
|
||||
if isinstance(reasoning_content, str) and reasoning_content:
|
||||
return reasoning_content
|
||||
reasoning = payload.get("reasoning")
|
||||
if isinstance(reasoning, str) and reasoning:
|
||||
return reasoning
|
||||
return None
|
||||
"""Extract reasoning text from provider payload delta/message objects."""
|
||||
return extract_reasoning_text(payload)
|
||||
|
||||
|
||||
def _append_reasoning_content(message: AIMessage | AIMessageChunk, reasoning_content: str) -> AIMessage | AIMessageChunk:
|
||||
"""Append streamed reasoning chunks, dedupe complete response reasoning."""
|
||||
additional_kwargs = dict(message.additional_kwargs)
|
||||
existing = additional_kwargs.get("reasoning_content")
|
||||
if isinstance(existing, str) and existing:
|
||||
if reasoning_content not in existing:
|
||||
additional_kwargs["reasoning_content"] = f"{existing}{reasoning_content}"
|
||||
else:
|
||||
additional_kwargs["reasoning_content"] = reasoning_content
|
||||
return message.model_copy(update={"additional_kwargs": additional_kwargs})
|
||||
return apply_reasoning_from_payload(message, {"reasoning_content": reasoning_content})
|
||||
Reference in new issue
Block a user