Skip to content

Commit 23aeacb

Browse files
committed
refactor: define ChatRequest and related backups
1 parent 93d3aa4 commit 23aeacb

File tree

1 file changed

+159
-21
lines changed

1 file changed

+159
-21
lines changed

src/memos/api/product_models.py

Lines changed: 159 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,174 @@ class MemCubeRegister(BaseRequest):
6969

7070

7171
class ChatRequest(BaseRequest):
72-
"""Request model for chat operations."""
72+
"""Request model for chat operations.
73+
74+
This model is used as the algorithm-facing chat interface, while also
75+
remaining backward compatible with older developer-facing APIs.
76+
"""
7377

78+
# ==== Basic identifiers ====
7479
user_id: str = Field(..., description="User ID")
75-
query: str = Field(..., description="Chat query message")
76-
mem_cube_id: str | None = Field(None, description="Cube ID to use for chat")
80+
session_id: str | None = Field(
81+
None,
82+
description=(
83+
"Session ID. Used as a soft signal to give higher weight to "
84+
"recently related memories. Optional for developer API."
85+
),
86+
)
87+
88+
# ==== Query ====
89+
query: str = Field(..., description="Chat query message from user")
90+
91+
# ==== Cube scoping ====
7792
readable_cube_ids: list[str] | None = Field(
78-
None, description="List of cube IDs user can read for multi-cube chat"
93+
None,
94+
description=(
95+
"List of cube IDs user can read for multi-cube chat.\n"
96+
"- Algorithm interface: required\n"
97+
"- Developer API: optional (can fall back to mem_cube_id or defaults)"
98+
),
7999
)
100+
80101
writable_cube_ids: list[str] | None = Field(
81-
None, description="List of cube IDs user can write for multi-cube chat"
102+
None,
103+
description=(
104+
"List of cube IDs user can write for multi-cube chat.\n"
105+
"- Algorithm interface: required\n"
106+
"- Developer API: optional (can fall back to mem_cube_id or defaults)"
107+
),
82108
)
83-
history: list[MessageDict] | None = Field(None, description="Chat history")
84-
mode: SearchMode = Field(SearchMode.FAST, description="search mode: fast, fine, or mixture")
85-
internet_search: bool = Field(True, description="Whether to use internet search")
86-
system_prompt: str | None = Field(None, description="Base system prompt to use for chat")
87-
top_k: int = Field(10, description="Number of results to return")
88-
threshold: float = Field(0.5, description="Threshold for filtering references")
89-
session_id: str | None = Field(None, description="Session ID for soft-filtering memories")
90-
include_preference: bool = Field(True, description="Whether to handle preference memory")
91-
pref_top_k: int = Field(6, description="Number of preference results to return")
92-
filter: dict[str, Any] | None = Field(None, description="Filter for the memory")
93-
model_name_or_path: str | None = Field(None, description="Model name to use for chat")
94-
max_tokens: int | None = Field(None, description="Max tokens to generate")
95-
temperature: float | None = Field(None, description="Temperature for sampling")
96-
top_p: float | None = Field(None, description="Top-p (nucleus) sampling parameter")
97-
add_message_on_answer: bool = Field(True, description="Add dialogs to memory after chat")
109+
110+
# ==== History & filters ====
111+
history: MessagesType | None = Field(
112+
None,
113+
description=(
114+
"Chat history (algorithm does NOT persist it). Algorithm interface default: []."
115+
),
116+
)
117+
118+
filter: dict[str, Any] | None = Field(
119+
None,
120+
description=(
121+
"Memory filter. Developers can provide custom structured predicates "
122+
"to precisely filter memories. Algorithm interface default: {}."
123+
),
124+
)
125+
126+
# ==== Retrieval configuration ====
127+
mode: SearchMode = Field(
128+
SearchMode.FAST,
129+
description="Search mode: fast, fine, or mixture.",
130+
)
131+
132+
top_k: int = Field(
133+
10,
134+
ge=1,
135+
description="Number of memories to retrieve (top-K).",
136+
)
137+
138+
pref_top_k: int = Field(
139+
6,
140+
ge=0,
141+
description="Number of preference memories to retrieve (top-K). Default: 6.",
142+
)
143+
144+
include_preference: bool = Field(
145+
True,
146+
description=(
147+
"Whether to retrieve preference memories. If enabled, the system will "
148+
"automatically recall user preferences related to the query. Default: True."
149+
),
150+
)
151+
152+
threshold: float = Field(
153+
0.5,
154+
description="Internal similarity threshold for filtering references (algorithm internal).",
155+
)
156+
157+
# ==== LLM / generation configuration ====
158+
system_prompt: str | None = Field(
159+
None,
160+
description="Custom system prompt / instruction for this chat.",
161+
)
162+
163+
model_name_or_path: str | None = Field(
164+
None,
165+
description=("Name or path of the chat model to use.\nAlgorithm default: 'gpt-4o-mini'."),
166+
)
167+
168+
max_tokens: int | None = Field(
169+
None,
170+
description="Max tokens to generate. Algorithm default: 8192.",
171+
)
172+
173+
temperature: float | None = Field(
174+
None,
175+
description="Sampling temperature. Algorithm default: 0.7.",
176+
)
177+
178+
top_p: float | None = Field(
179+
None,
180+
description="Top-p (nucleus) sampling parameter. Algorithm default: 0.95.",
181+
)
182+
183+
add_message_on_answer: bool = Field(
184+
True,
185+
description="Whether to append dialog messages to memory after chat. Default: True.",
186+
)
187+
188+
# ==== Search / pipeline toggles ====
189+
internet_search: bool = Field(
190+
True,
191+
description=(
192+
"Whether to use internet search (internal usage; external API may ignore this flag)."
193+
),
194+
)
195+
196+
# ==== Backward compatibility ====
98197
moscube: bool = Field(
99-
False, description="(Deprecated) Whether to use legacy MemOSCube pipeline"
198+
False,
199+
description="(Deprecated) Whether to use legacy MemOSCube pipeline.",
200+
)
201+
202+
mem_cube_id: str | None = Field(
203+
None,
204+
description=(
205+
"(Deprecated) Single cube ID to use for chat. "
206+
"Prefer `readable_cube_ids` / `writable_cube_ids` for multi-cube chat."
207+
),
100208
)
101209

210+
@model_validator(mode="after")
211+
def _normalize_for_algorithm(self):
212+
"""
213+
Normalize fields for algorithm interface while preserving backward compatibility.
214+
215+
Rules:
216+
- mem_cube_id → readable_cube_ids / writable_cube_ids if they are missing
217+
- moscube: log warning when True (deprecated)
218+
"""
219+
220+
# ---- mem_cube_id backward compatibility ----
221+
if self.mem_cube_id is not None:
222+
logger.warning(
223+
"ChatRequest.mem_cube_id is deprecated and will be removed in a future version. "
224+
"Please migrate to `readable_cube_ids` / `writable_cube_ids`."
225+
)
226+
if not self.readable_cube_ids:
227+
self.readable_cube_ids = [self.mem_cube_id]
228+
if not self.writable_cube_ids:
229+
self.writable_cube_ids = [self.mem_cube_id]
230+
231+
# ---- Deprecated moscube flag ----
232+
if self.moscube:
233+
logger.warning(
234+
"ChatRequest.moscube is deprecated. Legacy MemOSCube pipeline "
235+
"will be removed in a future version."
236+
)
237+
238+
return self
239+
102240

103241
class ChatCompleteRequest(BaseRequest):
104242
"""Request model for chat operations."""

0 commit comments

Comments
 (0)