-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathreasoning_content_replay.py
More file actions
59 lines (40 loc) · 1.82 KB
/
reasoning_content_replay.py
File metadata and controls
59 lines (40 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any, Callable
@dataclass
class ReasoningContentSource:
"""The reasoning item being considered for replay into the next request."""
item: Any
"""The raw reasoning item."""
origin_model: str | None
"""The model that originally produced the reasoning item, if known."""
provider_data: Mapping[str, Any]
"""Provider-specific metadata captured on the reasoning item."""
@dataclass
class ReasoningContentReplayContext:
"""Context passed to reasoning-content replay hooks."""
model: str
"""The model that will receive the next Chat Completions request."""
base_url: str | None
"""The request base URL, if the SDK knows the concrete endpoint."""
reasoning: ReasoningContentSource
"""The reasoning item candidate being evaluated for replay."""
ShouldReplayReasoningContent = Callable[[ReasoningContentReplayContext], bool]
def default_should_replay_reasoning_content(context: ReasoningContentReplayContext) -> bool:
"""Return whether the SDK should replay reasoning content by default."""
if "deepseek" not in context.model.lower():
return False
origin_model = context.reasoning.origin_model
# Replay only when the current request targets DeepSeek and the reasoning item either
# came from a DeepSeek model or predates provider tracking. This avoids mixing reasoning
# content from a different model family into the DeepSeek assistant message.
return (
origin_model is not None and "deepseek" in origin_model.lower()
) or context.reasoning.provider_data == {}
__all__ = [
"ReasoningContentReplayContext",
"ReasoningContentSource",
"ShouldReplayReasoningContent",
"default_should_replay_reasoning_content",
]