-
Notifications
You must be signed in to change notification settings - Fork 274
feat(autocontextmemory): compress prompt optimize #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import io.agentscope.core.memory.Memory; | ||
| import io.agentscope.core.message.Msg; | ||
| import io.agentscope.core.message.MsgRole; | ||
| import io.agentscope.core.message.TextBlock; | ||
| import io.agentscope.core.plan.PlanNotebook; | ||
| import io.agentscope.core.tool.Toolkit; | ||
| import java.util.ArrayList; | ||
|
|
@@ -225,20 +226,60 @@ private Mono<PreReasoningEvent> handlePreReasoning(PreReasoningEvent event) { | |
| } | ||
|
|
||
| // Trigger compression if needed (this modifies workingMemoryStorage in place) | ||
| boolean compressed = autoContextMemory.compressIfNeeded(); | ||
|
|
||
| if (compressed) { | ||
| // Preserve system prompt and replace memory messages with compressed ones | ||
| List<Msg> originalInputMessages = event.getInputMessages(); | ||
| List<Msg> newInputMessages = new ArrayList<>(); | ||
| if (!originalInputMessages.isEmpty() | ||
| && originalInputMessages.get(0).getRole() == MsgRole.SYSTEM) { | ||
| newInputMessages.add(originalInputMessages.get(0)); | ||
| } | ||
| newInputMessages.addAll(autoContextMemory.getMessages()); | ||
| event.setInputMessages(newInputMessages); | ||
| autoContextMemory.compressIfNeeded(); | ||
|
|
||
| // Always append system prompt instruction about compressed messages | ||
| List<Msg> originalInputMessages = event.getInputMessages(); | ||
| List<Msg> newInputMessages = new ArrayList<>(); | ||
|
|
||
| if (!originalInputMessages.isEmpty() | ||
| && originalInputMessages.get(0).getRole() == MsgRole.SYSTEM) { | ||
| // Append instruction to existing system prompt | ||
| Msg originalSystemMsg = originalInputMessages.get(0); | ||
| String originalSystemText = originalSystemMsg.getTextContent(); | ||
| String appendedInstruction = | ||
| "\n\n" | ||
| + "You may see compressed messages containing <!-- CONTEXT_OFFLOAD" | ||
| + " uuid=... -->.\n" | ||
| + "- Use the UUID to call context_reload if you need full details.\n" | ||
| + "- NEVER mention, quote, or refer to UUIDs, offload tags, or internal" | ||
| + " metadata in your response."; | ||
|
|
||
| String newSystemText = | ||
| originalSystemText != null | ||
| ? originalSystemText + appendedInstruction | ||
| : appendedInstruction.trim(); | ||
|
|
||
| Msg updatedSystemMsg = | ||
| Msg.builder() | ||
| .role(MsgRole.SYSTEM) | ||
| .name(originalSystemMsg.getName()) | ||
| .content(TextBlock.builder().text(newSystemText).build()) | ||
| .metadata(originalSystemMsg.getMetadata()) | ||
| .build(); | ||
|
|
||
| newInputMessages.add(updatedSystemMsg); | ||
| } else { | ||
| // No system message exists, create a new one with the instruction | ||
| String instruction = | ||
| "You may see compressed messages containing <!-- CONTEXT_OFFLOAD uuid=..." | ||
| + " -->.\n" | ||
| + "- Use the UUID to call context_reload if you need full details.\n" | ||
| + "- NEVER mention, quote, or refer to UUIDs, offload tags, or internal" | ||
| + " metadata in your response."; | ||
|
Comment on lines
+240
to
+269
|
||
|
|
||
| newInputMessages.add( | ||
| Msg.builder() | ||
| .role(MsgRole.SYSTEM) | ||
| .name("system") | ||
| .content(TextBlock.builder().text(instruction).build()) | ||
| .build()); | ||
| } | ||
|
|
||
| // Add memory messages (compressed or not) | ||
| newInputMessages.addAll(autoContextMemory.getMessages()); | ||
| event.setInputMessages(newInputMessages); | ||
|
|
||
| return Mono.just(event); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing validation for the minCompressionTokenThreshold parameter. The method should validate that the threshold is non-negative (>= 0) to prevent invalid configurations. Negative values would cause unexpected behavior in the compression logic.