Skip to content

Commit c141a02

Browse files
authored
Feat: add chat complete api for no-stream and rewrite chat func for moscore (#253)
* feat: add chat complete * feat: fix chat bug
1 parent 4f49ee6 commit c141a02

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/memos/api/product_models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ class ChatRequest(BaseRequest):
8787
moscube: bool = Field(False, description="Whether to use MemOSCube")
8888

8989

90+
class ChatCompleteRequest(BaseRequest):
91+
"""Request model for chat operations."""
92+
93+
user_id: str = Field(..., description="User ID")
94+
query: str = Field(..., description="Chat query message")
95+
mem_cube_id: str | None = Field(None, description="Cube ID to use for chat")
96+
history: list[MessageDict] | None = Field(None, description="Chat history")
97+
internet_search: bool = Field(False, description="Whether to use internet search")
98+
moscube: bool = Field(False, description="Whether to use MemOSCube")
99+
base_prompt: str | None = Field(None, description="Base prompt to use for chat")
100+
101+
90102
class UserCreate(BaseRequest):
91103
user_name: str | None = Field(None, description="Name of the user")
92104
role: str = Field("USER", description="Role of the user")

src/memos/api/routers/product_router.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from memos.api.context.dependencies import G, get_g_object
1313
from memos.api.product_models import (
1414
BaseResponse,
15+
ChatCompleteRequest,
1516
ChatRequest,
1617
GetMemoryRequest,
1718
MemoryCreateRequest,
@@ -276,6 +277,33 @@ def generate_chat_response():
276277
raise HTTPException(status_code=500, detail=str(traceback.format_exc())) from err
277278

278279

280+
@router.post("/chat/complete", summary="Chat with MemOS (Complete Response)")
281+
def chat_complete(chat_req: ChatCompleteRequest):
282+
"""Chat with MemOS for a specific user. Returns complete response (non-streaming)."""
283+
try:
284+
mos_product = get_mos_product_instance()
285+
286+
# Collect all responses from the generator
287+
content = mos_product.chat(
288+
query=chat_req.query,
289+
user_id=chat_req.user_id,
290+
cube_id=chat_req.mem_cube_id,
291+
history=chat_req.history,
292+
internet_search=chat_req.internet_search,
293+
moscube=chat_req.moscube,
294+
base_prompt=chat_req.base_prompt,
295+
)
296+
297+
# Return the complete response
298+
return {"message": "Chat completed successfully", "data": {"response": content}}
299+
300+
except ValueError as err:
301+
raise HTTPException(status_code=404, detail=str(traceback.format_exc())) from err
302+
except Exception as err:
303+
logger.error(f"Failed to start chat: {traceback.format_exc()}")
304+
raise HTTPException(status_code=500, detail=str(traceback.format_exc())) from err
305+
306+
279307
@router.get("/users", summary="List all users", response_model=BaseResponse[list])
280308
def list_users():
281309
"""List all registered users."""

src/memos/mem_os/product.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,41 @@ def get_suggestion_query(
709709
response_json = json.loads(clean_response)
710710
return response_json["query"]
711711

712+
def chat(
713+
self,
714+
query: str,
715+
user_id: str,
716+
cube_id: str | None = None,
717+
history: MessageList | None = None,
718+
base_prompt: str | None = None,
719+
internet_search: bool = False,
720+
moscube: bool = False,
721+
top_k: int = 10,
722+
) -> str:
723+
"""
724+
Chat with LLM with memory references and complete response.
725+
"""
726+
self._load_user_cubes(user_id, self.default_cube_config)
727+
memories_result = super().search(
728+
query,
729+
user_id,
730+
install_cube_ids=[cube_id] if cube_id else None,
731+
top_k=top_k,
732+
mode="fine",
733+
internet_search=internet_search,
734+
moscube=moscube,
735+
)["text_mem"]
736+
if memories_result:
737+
memories_list = memories_result[0]["memories"]
738+
memories_list = self._filter_memories_by_threshold(memories_list)
739+
system_prompt = super()._build_system_prompt(memories_list, base_prompt)
740+
current_messages = [
741+
{"role": "system", "content": system_prompt},
742+
{"role": "user", "content": query},
743+
]
744+
response = self.chat_llm.generate(current_messages)
745+
return response
746+
712747
def chat_with_references(
713748
self,
714749
query: str,

0 commit comments

Comments
 (0)