Skip to content

Commit 6ded745

Browse files
authored
feat(autocontext): support custom context compress prompt (#386)
1 parent dd7fdba commit 6ded745

12 files changed

Lines changed: 1396 additions & 46 deletions

File tree

agentscope-examples/advanced/src/main/java/io/agentscope/examples/advanced/AutoMemoryExample.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import io.agentscope.core.model.GenerateOptions;
3030
import io.agentscope.core.session.JsonSession;
3131
import io.agentscope.core.session.SessionManager;
32-
import io.agentscope.core.studio.StudioManager;
3332
import io.agentscope.core.tool.Toolkit;
3433
import io.agentscope.core.tool.file.ReadFileTool;
3534
import io.agentscope.core.tool.file.WriteFileTool;
@@ -44,16 +43,6 @@ public class AutoMemoryExample {
4443

4544
public static void main(String[] args) {
4645

47-
// Initialize Studio
48-
System.out.println("Connecting to Studio at http://localhost:3000...");
49-
StudioManager.init()
50-
.studioUrl("http://localhost:3000")
51-
.project("JavaExamples")
52-
.runName("studio_demo_" + System.currentTimeMillis())
53-
.initialize()
54-
.block();
55-
System.out.println("Connected to Studio\n");
56-
5746
String apiKey = ExampleUtils.getDashScopeApiKey();
5847

5948
DashScopeChatModel chatModel =

agentscope-extensions/agentscope-extensions-autocontext-memory/README.md

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ AutoContextMemory implements the `Memory` interface, providing automated context
4040
- **Progressive Compression Strategies**: Uses 6 progressive compression strategies, from lightweight to heavyweight
4141
- **Intelligent Summarization**: Uses LLM models for intelligent conversation summarization
4242
- **Content Offloading**: Offloads large content to external storage, reducing memory usage
43-
- **Tool Call Preservation**: Preserves tool call interface information (names, parameters) during compression
4443
- **Dual Storage Mechanism**: Working storage (compressed) and original storage (complete history)
4544
- **PlanNotebook Awareness**: Automatically integrates with PlanNotebook to adjust compression strategies based on current plan state, ensuring critical plan-related information is preserved during compression
45+
- **Prompt Customization**: Supports customizing compression strategy prompts for specific scenarios and domains, enabling targeted compression optimization
4646

4747
## Architecture Design
4848

@@ -115,6 +115,7 @@ All configuration parameters can be set through `AutoContextConfig`:
115115
| `offloadSinglePreview` | int | 200 | Preview length for offloaded messages (character count) |
116116
| `minConsecutiveToolMessages` | int | 6 | Minimum consecutive tool messages required for compression |
117117
| `currentRoundCompressionRatio` | double | 0.3 | Compression ratio for current round messages (0.0-1.0), default 30% |
118+
| `customPrompt` | PromptConfig | null | Custom prompt configuration (optional, uses default prompts if not set) |
118119

119120
### Configuration Example
120121

@@ -135,7 +136,7 @@ AutoContextConfig config = AutoContextConfig.builder()
135136

136137
### Basic Usage
137138

138-
**Important**: When using `AutoContextMemory` with `ReActAgent`, you **must** use `AutoContextHook` to ensure proper integration. The hook automatically handles all necessary setup.
139+
When using `AutoContextMemory` with `ReActAgent`, it's recommended to use `AutoContextHook` to automatically handle integration setup. The hook takes care of all necessary configuration.
139140

140141
```java
141142
import io.agentscope.core.ReActAgent;
@@ -154,22 +155,118 @@ AutoContextConfig config = AutoContextConfig.builder()
154155
// Create memory
155156
AutoContextMemory memory = new AutoContextMemory(config, model);
156157

157-
// Create Agent with AutoContextHook (required)
158+
// Create Agent with AutoContextHook for automatic integration
158159
ReActAgent agent = ReActAgent.builder()
159160
.name("Assistant")
160161
.model(model)
161162
.memory(memory)
162163
.toolkit(new Toolkit())
163164
.enablePlan() // Enable PlanNotebook support (optional, but recommended)
164-
.hook(new AutoContextHook()) // REQUIRED: Automatically registers ContextOffloadTool and attaches PlanNotebook
165+
.hook(new AutoContextHook()) // Automatically registers ContextOffloadTool and attaches PlanNotebook
165166
.build();
166167
```
167168

168-
The `AutoContextHook` is **required** and automatically:
169+
The `AutoContextHook` automatically:
169170
- Registers `ContextOffloadTool` to the agent's toolkit (enables context reload functionality)
170171
- Attaches the agent's `PlanNotebook` to `AutoContextMemory` for plan-aware compression (if PlanNotebook is enabled)
171172
- Ensures proper integration between `AutoContextMemory` and `ReActAgent`
172173

174+
### Custom Context Compression Prompts
175+
176+
AutoContextMemory uses default general-purpose compression prompts internally, which are carefully designed to meet most business needs. However, customizing compression prompts based on actual scenarios and business characteristics may achieve better compression results. For example, for specific domain tool invocation interfaces, you can explicitly guide the system to preserve which key information and appropriately discard which redundant content, thereby achieving higher compression rates while ensuring information integrity.
177+
178+
AutoContextMemory supports customizing compression strategy prompts, allowing optimization for specific domains and scenarios.
179+
180+
#### PromptConfig
181+
182+
The `PromptConfig` class is used to configure custom prompts. All prompts are optional. If not specified, default prompts from the `Prompts` class will be used.
183+
184+
Configurable prompts:
185+
186+
| Field | Description | Strategy |
187+
|-------|-------------|----------|
188+
| `previousRoundToolCompressPrompt` | Prompt for compressing previous round tool invocations | Strategy 1 |
189+
| `previousRoundSummaryPrompt` | Prompt for summarizing previous round conversations | Strategy 4 |
190+
| `currentRoundLargeMessagePrompt` | Prompt for summarizing current round large messages | Strategy 5 |
191+
| `currentRoundCompressPrompt` | Prompt for compressing current round messages | Strategy 6 |
192+
193+
#### Usage Examples
194+
195+
`customPrompt` is optional. You can omit it (uses default prompts), or set any subset of prompts (unset prompts will use default values).
196+
197+
```java
198+
import io.agentscope.core.memory.autocontext.PromptConfig;
199+
200+
// Option 1: Don't set customPrompt, use default prompts (backward compatible)
201+
AutoContextConfig config1 = AutoContextConfig.builder()
202+
.msgThreshold(50)
203+
.maxToken(64 * 1024)
204+
.build();
205+
206+
// Option 2: Set only some prompts, others will use default values
207+
PromptConfig customPrompt2 = PromptConfig.builder()
208+
.previousRoundToolCompressPrompt("Custom Strategy 1 prompt...")
209+
// Other prompts not set, will use default values
210+
.build();
211+
AutoContextConfig config2 = AutoContextConfig.builder()
212+
.msgThreshold(50)
213+
.customPrompt(customPrompt2)
214+
.build();
215+
216+
// Option 3: Set all prompts
217+
PromptConfig customPrompt3 = PromptConfig.builder()
218+
.previousRoundToolCompressPrompt("Custom Strategy 1 prompt...")
219+
.previousRoundSummaryPrompt("Custom Strategy 4 prompt...")
220+
.currentRoundLargeMessagePrompt("Custom Strategy 5 prompt...")
221+
.currentRoundCompressPrompt("Custom Strategy 6 prompt...")
222+
.build();
223+
AutoContextConfig config3 = AutoContextConfig.builder()
224+
.msgThreshold(50)
225+
.customPrompt(customPrompt3)
226+
.build();
227+
```
228+
229+
**Domain-Specific Prompt Examples**
230+
231+
**E-commerce Order Processing Scenario (Specific Tool Invocation Interface Example)**
232+
233+
```java
234+
// Custom prompt for e-commerce order processing scenario
235+
// Assuming the system has the following tools: get_order_info, update_order_status, calculate_price, send_notification
236+
PromptConfig ecommerceCustomPrompt = PromptConfig.builder()
237+
.previousRoundToolCompressPrompt(
238+
"You are an e-commerce order processing assistant. Please compress the following tool invocation history according to these rules:\n" +
239+
"\n" +
240+
"【Information to Preserve】\n" +
241+
"1. get_order_info tool calls: Preserve order number, order status, key product information (product ID, name, quantity, price)\n" +
242+
"2. update_order_status tool calls: Preserve order number, status changes (from X to Y), change timestamp\n" +
243+
"3. calculate_price tool calls: Preserve final calculated total price, discount amount, shipping fee\n" +
244+
"4. send_notification tool calls: Preserve notification type (SMS/email), recipient, notification content summary\n" +
245+
"\n" +
246+
"【Information to Discard】\n" +
247+
"1. Detailed product descriptions, image URLs, and other non-critical information\n" +
248+
"2. Repeated order information query results (only keep the key results from the last query)\n" +
249+
"3. Detailed steps of intermediate calculations (only keep final results)\n" +
250+
"4. Detailed logs and response content of notification sending (only keep sending status)\n" +
251+
"\n" +
252+
"Please merge multiple tool calls into a concise summary, highlighting key decision points and final status of order processing."
253+
)
254+
.currentRoundCompressPrompt(
255+
"Current round contains order processing related tool invocations. When compressing, follow these principles:\n" +
256+
"1. Preserve all key information about order status changes (order number, status transitions)\n" +
257+
"2. Preserve price calculation results (total price, discount, actual payment amount)\n" +
258+
"3. Preserve key information about notification sending (notification type, sending status)\n" +
259+
"4. You can simplify detailed parameters of tool calls, but preserve core business data\n" +
260+
"5. Merge multiple operations on the same order, only keep final status and key intermediate states"
261+
)
262+
.build();
263+
264+
AutoContextConfig config = AutoContextConfig.builder()
265+
.msgThreshold(50)
266+
.customPrompt(ecommerceCustomPrompt)
267+
.build();
268+
```
269+
173270
## API Reference
174271

175272
### AutoContextMemory
@@ -379,7 +476,7 @@ This enables saving and restoring memory state between sessions. Combined with `
379476

380477
## Best Practices
381478

382-
1. **Always Use AutoContextHook**: **Required** - Always use `AutoContextHook` when using `AutoContextMemory` with `ReActAgent`. It ensures proper integration and automatic setup.
479+
1. **Use AutoContextHook**: When using `AutoContextMemory` with `ReActAgent`, it's recommended to use `AutoContextHook` to automatically handle integration setup, ensuring `ContextOffloadTool` and `PlanNotebook` are properly configured.
383480
2. **Set Thresholds Appropriately**: Adjust `maxToken` and `tokenRatio` based on model context window size and actual usage scenarios
384481
3. **Protect Important Messages**: Use `lastKeep` to ensure recent conversations are not compressed
385482
4. **Enable PlanNotebook Integration**: When using `ReActAgent` with plans, enable `PlanNotebook` support (`.enablePlan()`) to benefit from plan-aware compression

0 commit comments

Comments
 (0)