mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 15:49:41 +08:00
Merge pull request #295 from OlegSob-glitch/fix/history-reference-empty-context
fix: merge system messages and add history reference fallback
This commit is contained in:
@@ -851,14 +851,21 @@ class AgenticChatPipeline:
|
||||
system_prompt: str,
|
||||
user_content: str,
|
||||
) -> list[dict[str, Any]]:
|
||||
messages: list[dict[str, Any]] = [{"role": "system", "content": system_prompt}]
|
||||
# Merge all system content into a single message to comply with Qwen vLLM requirements
|
||||
system_parts = [system_prompt]
|
||||
if context.memory_context:
|
||||
messages.append({"role": "system", "content": context.memory_context})
|
||||
system_parts.append(context.memory_context)
|
||||
system_content = "\n\n".join(system_parts)
|
||||
|
||||
messages: list[dict[str, Any]] = [{"role": "system", "content": system_content}]
|
||||
|
||||
# Add conversation history (filter out system messages to avoid duplicates)
|
||||
for item in context.conversation_history:
|
||||
role = item.get("role")
|
||||
content = item.get("content")
|
||||
if role in {"user", "assistant", "system"} and isinstance(content, (str, list)):
|
||||
if role in {"user", "assistant"} and isinstance(content, (str, list)):
|
||||
messages.append({"role": role, "content": content})
|
||||
|
||||
messages.append({"role": "user", "content": user_content})
|
||||
return messages
|
||||
|
||||
|
||||
@@ -245,15 +245,13 @@ class ChatAgent(BaseAgent):
|
||||
"""
|
||||
messages = []
|
||||
|
||||
# System prompt
|
||||
system_prompt = self.get_prompt("system", "You are a helpful AI assistant.")
|
||||
messages.append({"role": "system", "content": system_prompt})
|
||||
|
||||
# Add context if available
|
||||
# Merge system prompt and context into single system message to comply with Qwen vLLM requirements
|
||||
system_parts = [self.get_prompt("system", "You are a helpful AI assistant.")]
|
||||
if context:
|
||||
context_template = self.get_prompt("context_template", "Reference context:\n{context}")
|
||||
context_msg = context_template.format(context=context)
|
||||
messages.append({"role": "system", "content": context_msg})
|
||||
system_parts.append(context_template.format(context=context))
|
||||
system_content = "\n\n".join(system_parts)
|
||||
messages.append({"role": "system", "content": system_content})
|
||||
|
||||
# Add conversation history
|
||||
for msg in history:
|
||||
|
||||
@@ -112,13 +112,14 @@ class ContextBuilder:
|
||||
cleaned_summary = summary.strip()
|
||||
if cleaned_summary:
|
||||
history.append({"role": "system", "content": cleaned_summary})
|
||||
# Filter out system messages from DB since summary is already added as system
|
||||
history.extend(
|
||||
{
|
||||
"role": item.get("role", "user"),
|
||||
"content": str(item.get("content", "") or ""),
|
||||
}
|
||||
for item in messages
|
||||
if item.get("role") in {"user", "assistant", "system"}
|
||||
if item.get("role") in {"user", "assistant"} # Removed "system" to avoid duplicate system messages
|
||||
and str(item.get("content", "") or "").strip()
|
||||
)
|
||||
return history
|
||||
|
||||
@@ -458,6 +458,13 @@ class TurnRuntimeManager:
|
||||
records=history_records,
|
||||
emit=lambda event: self._persist_and_publish(execution, event),
|
||||
)
|
||||
# Fallback: if analysis returns empty, use raw record content
|
||||
if not history_context.strip():
|
||||
history_context = "\n\n".join(
|
||||
f"## Session: {record.get('title', 'Untitled')}\n{record.get('output', '')}"
|
||||
for record in history_records
|
||||
if record.get("output")
|
||||
)
|
||||
|
||||
effective_user_message = raw_user_content
|
||||
context_parts: list[str] = []
|
||||
|
||||
Reference in New Issue
Block a user