Skip to content

Commit 1b195a5

Browse files
authored
Feat: update chatbot for postprocessing memory (#267)
feat: update post processing memory for chatbot
1 parent 9e347b8 commit 1b195a5

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/memos/api/product_models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class ChatCompleteRequest(BaseRequest):
9797
internet_search: bool = Field(False, description="Whether to use internet search")
9898
moscube: bool = Field(False, description="Whether to use MemOSCube")
9999
base_prompt: str | None = Field(None, description="Base prompt to use for chat")
100+
top_k: int = Field(10, description="Number of results to return")
101+
threshold: float = Field(0.5, description="Threshold for filtering references")
100102

101103

102104
class UserCreate(BaseRequest):

src/memos/api/routers/product_router.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,23 @@ def chat_complete(chat_req: ChatCompleteRequest):
284284
mos_product = get_mos_product_instance()
285285

286286
# Collect all responses from the generator
287-
content = mos_product.chat(
287+
content, references = mos_product.chat(
288288
query=chat_req.query,
289289
user_id=chat_req.user_id,
290290
cube_id=chat_req.mem_cube_id,
291291
history=chat_req.history,
292292
internet_search=chat_req.internet_search,
293293
moscube=chat_req.moscube,
294294
base_prompt=chat_req.base_prompt,
295+
top_k=chat_req.top_k,
296+
threshold=chat_req.threshold,
295297
)
296298

297299
# Return the complete response
298-
return {"message": "Chat completed successfully", "data": {"response": content}}
300+
return {
301+
"message": "Chat completed successfully",
302+
"data": {"response": content, "references": references},
303+
}
299304

300305
except ValueError as err:
301306
raise HTTPException(status_code=404, detail=str(traceback.format_exc())) from err

src/memos/mem_os/product.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,11 +889,13 @@ def chat(
889889
internet_search: bool = False,
890890
moscube: bool = False,
891891
top_k: int = 10,
892+
threshold: float = 0.5,
892893
) -> str:
893894
"""
894895
Chat with LLM with memory references and complete response.
895896
"""
896897
self._load_user_cubes(user_id, self.default_cube_config)
898+
time_start = time.time()
897899
memories_result = super().search(
898900
query,
899901
user_id,
@@ -905,14 +907,30 @@ def chat(
905907
)["text_mem"]
906908
if memories_result:
907909
memories_list = memories_result[0]["memories"]
908-
memories_list = self._filter_memories_by_threshold(memories_list)
910+
memories_list = self._filter_memories_by_threshold(memories_list, threshold)
909911
system_prompt = super()._build_system_prompt(memories_list, base_prompt)
912+
history_info = []
913+
if history:
914+
history_info = history[-20:]
910915
current_messages = [
911916
{"role": "system", "content": system_prompt},
917+
*history_info,
912918
{"role": "user", "content": query},
913919
]
914920
response = self.chat_llm.generate(current_messages)
915-
return response
921+
time_end = time.time()
922+
self._start_post_chat_processing(
923+
user_id=user_id,
924+
cube_id=cube_id,
925+
query=query,
926+
full_response=response,
927+
system_prompt=system_prompt,
928+
time_start=time_start,
929+
time_end=time_end,
930+
speed_improvement=0.0,
931+
current_messages=current_messages,
932+
)
933+
return response, memories_list
916934

917935
def chat_with_references(
918936
self,
@@ -973,7 +991,7 @@ def chat_with_references(
973991

974992
chat_history = self.chat_history_manager[user_id]
975993
if history:
976-
chat_history.chat_history = history[-10:]
994+
chat_history.chat_history = history[-20:]
977995
current_messages = [
978996
{"role": "system", "content": system_prompt},
979997
*chat_history.chat_history,

0 commit comments

Comments
 (0)