|
@@ -273,7 +273,7 @@ def save_conversation(speaker_id: str, speaker_name: str, user_query: str, reply
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_conversation_context(speaker_id: str | None, speaker_name: str | None, limit: int = 5) -> str:
|
|
def load_conversation_context(speaker_id: str | None, speaker_name: str | None, limit: int = 5) -> str:
|
|
|
- """Retrieve recent conversation history for a speaker."""
|
|
|
|
|
|
|
+ """Retrieve recent conversation history as formatted text for system prompt."""
|
|
|
if not speaker_id:
|
|
if not speaker_id:
|
|
|
return ""
|
|
return ""
|
|
|
with _MEM_LOCK:
|
|
with _MEM_LOCK:
|
|
@@ -290,3 +290,18 @@ def load_conversation_context(speaker_id: str | None, speaker_name: str | None,
|
|
|
if history:
|
|
if history:
|
|
|
return "以下是之前和该用户的对话记录,请参考上下文回应:\n" + "\n".join(history)
|
|
return "以下是之前和该用户的对话记录,请参考上下文回应:\n" + "\n".join(history)
|
|
|
return ""
|
|
return ""
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def load_conversation_hist(speaker_id: str, limit: int = 6) -> list[dict]:
|
|
|
|
|
+ """Return conversation history as OpenAI message list for self.hist."""
|
|
|
|
|
+ with _MEM_LOCK:
|
|
|
|
|
+ if not _MEMORY_LOADED:
|
|
|
|
|
+ _load_memories()
|
|
|
|
|
+ entries = _MEMORY.get(speaker_id, [])
|
|
|
|
|
+ hist = []
|
|
|
|
|
+ for e in entries[-limit:]:
|
|
|
|
|
+ q, r = e.get("query", ""), e.get("reply", "")
|
|
|
|
|
+ if q and r:
|
|
|
|
|
+ hist.append({"role": "user", "content": q})
|
|
|
|
|
+ hist.append({"role": "assistant", "content": r})
|
|
|
|
|
+ return hist
|