-
-
Notifications
You must be signed in to change notification settings - Fork 781
Expand file tree
/
Copy pathbasic_compaction.py
More file actions
120 lines (100 loc) · 4.06 KB
/
basic_compaction.py
File metadata and controls
120 lines (100 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
"""
Basic Context Compaction Example for PraisonAI Agents.
This example demonstrates how to use context compaction with Agent:
1. Create an Agent with context= parameter for compaction
2. Use different compaction strategies via ManagerConfig
3. Monitor compaction results
Usage:
python basic_compaction.py
"""
from praisonaiagents import Agent
from praisonaiagents.context import ManagerConfig
def main():
print("=" * 60)
print("Agent-Centric Context Compaction Demo")
print("=" * 60)
# ==========================================================================
# Available Strategies
# ==========================================================================
print("\n--- Available Compaction Strategies ---")
strategies = [
("truncate", "Remove oldest messages first"),
("sliding_window", "Keep most recent messages within limit"),
("summarize", "Replace old messages with summary"),
("smart", "Intelligently select messages to keep"),
]
for name, desc in strategies:
print(f" {name}: {desc}")
# ==========================================================================
# Agent with Context Compaction via context= param
# ==========================================================================
print("\n--- Creating Agent with Context Compaction ---")
# Use context= parameter with ManagerConfig for compaction
agent = Agent(
name="LongChat",
instructions="You are a helpful assistant for extended conversations.",
context=ManagerConfig(
auto_compact=True,
compact_threshold=0.8,
strategy="sliding_window",
)
)
print(f"Agent: {agent.name}")
print(f"Context manager: {agent.context_manager is not None}")
if agent.context_manager:
print(f"Auto-compact: {agent.context_manager.config.auto_compact}")
print(f"Threshold: {agent.context_manager.config.compact_threshold}")
print(f"Strategy: {agent.context_manager.config.strategy}")
# ==========================================================================
# Different Strategy Examples via context= param
# ==========================================================================
print("\n--- Strategy Examples ---")
# Sliding window strategy
sliding_agent = Agent(
name="SlidingAgent",
instructions="You are helpful.",
context=ManagerConfig(
auto_compact=True,
strategy="sliding_window",
)
)
print(f" Sliding: strategy={sliding_agent.context_manager.config.strategy}")
# Summarize strategy
summarize_agent = Agent(
name="SummarizeAgent",
instructions="You are helpful.",
context=ManagerConfig(
auto_compact=True,
strategy="summarize",
)
)
print(f" Summarize: strategy={summarize_agent.context_manager.config.strategy}")
# Smart strategy
smart_agent = Agent(
name="SmartAgent",
instructions="You are helpful.",
context=ManagerConfig(
auto_compact=True,
strategy="smart",
)
)
print(f" Smart: strategy={smart_agent.context_manager.config.strategy}")
# ==========================================================================
# Context Manager Stats Demo
# ==========================================================================
print("\n--- Context Manager Stats Demo ---")
# Access context manager stats
if agent.context_manager:
stats = agent.context_manager.get_stats()
print(f" Model: {stats.get('model', 'N/A')}")
print(f" Model limit: {stats.get('model_limit', 'N/A')}")
print(f" Output reserve: {stats.get('output_reserve', 'N/A')}")
print(f" Usable budget: {stats.get('usable_budget', 'N/A')}")
else:
print(" Context manager not initialized")
print("\n" + "=" * 60)
print("Demo Complete!")
print("=" * 60)
if __name__ == "__main__":
main()