-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_manager.py
766 lines (626 loc) · 26 KB
/
context_manager.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# Copyright 2024 GlyphyAI
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
import asyncio
from collections.abc import Sequence
from typing import Any, Literal, Protocol, TypeAlias
from litellm import acompletion
from typing_extensions import override
from liteswarm.core.message_index import LiteMessageIndex, MessageIndex
from liteswarm.core.message_store import MessageStore
from liteswarm.types.context import ContextVariables
from liteswarm.types.context_manager import RAGStrategyConfig
from liteswarm.types.llm import LLM
from liteswarm.types.messages import MessageRecord, MessageT
from liteswarm.types.swarm import Agent, Message
from liteswarm.utils.logging import log_verbose
from liteswarm.utils.messages import dump_messages, filter_tool_call_pairs, trim_messages
from liteswarm.utils.unwrap import unwrap_instructions
LiteOptimizationStrategy: TypeAlias = Literal["trim", "window", "summarize", "rag"]
"""Available context optimization strategies."""
SUMMARIZER_SYSTEM_PROMPT = """\
You are a precise conversation summarizer that distills complex interactions into essential points.
Your summaries must capture:
- Key decisions and outcomes
- Essential context needed for future interactions
- Tool calls and their results
- Important user requirements or constraints
Focus on factual information and exclude:
- Greetings and acknowledgments
- Routine interactions
- Redundant information
- Conversational fillers
Be extremely concise while preserving all critical details.\
"""
SUMMARIZER_USER_PROMPT = """\
Create a 2-3 sentence summary of this conversation segment that captures only:
1. Key decisions and actions taken
2. Essential context for future reference
3. Important tool interactions and their outcomes
Be direct and factual. Exclude any unnecessary details or pleasantries.\
"""
class ContextManager(Protocol):
"""Protocol for managing agent execution contexts.
Manages the context provided to language models during agent execution. Creates
appropriate context from available messages. Optimizes context size to fit model
limits. Provides semantic search capabilities for finding relevant messages.
The manager is responsible for fetching and managing context on its own.
Typically this would be done using a MessageStore, but implementations are free
to source context in any way that suits their needs.
Core responsibilities include context creation, optimization, and relevance
filtering. The manager ensures proper message ordering and handles dynamic
resolution of agent instructions.
Examples:
Basic usage with message store:
```python
class BasicContextManager(ContextManager):
def __init__(self, message_store: MessageStore[Any]) -> None:
self.message_store = message_store
self.window_size = 50
async def create_context(
self,
agent: Agent,
prompt: str | None = None,
context_variables: ContextVariables | None = None,
) -> list[Message]:
# Get history from store
history = await self.message_store.get_messages()
# Create context with system message and history
messages = [Message(role="system", content=agent.instructions), *history]
# Add prompt if provided
if prompt:
messages.append(Message(role="user", content=prompt))
return messages
async def optimize_context(
self,
model: str,
strategy: str | None = None,
) -> list[Message]:
messages = await self.message_store.get_messages()
# Keep only recent messages in window
return messages[-self.window_size :]
async def find_context(
self,
query: str,
max_messages: int | None = None,
) -> list[Message]:
messages = await self.message_store.get_messages()
# In practice, you'd use semantic search here
return messages[-10:] # Return 10 most recent for simplicity
```
"""
async def create_context(
self,
agent: Agent,
prompt: str | None = None,
context_variables: ContextVariables | None = None,
) -> list[Message]:
"""Create an execution context for an agent.
Prepares a list of messages suitable for the agent's next execution.
Resolves agent instructions with provided context variables. Combines
system message, relevant history, and optional user prompt.
Args:
agent: Agent requiring context creation.
prompt: Optional user prompt to include.
context_variables: Optional variables for dynamic resolution.
Returns:
List of messages ready for execution.
Examples:
Basic usage:
```python
context = await manager.create_context(
agent=agent,
prompt="Hello!",
)
```
With context variables:
```python
context = await manager.create_context(
agent=agent,
prompt="Help with task",
context_variables=ContextVariables(
user_name="Alice",
task_type="analysis",
),
)
```
"""
...
async def optimize_context(
self,
model: str,
strategy: Any | None = None,
) -> list[Message]:
"""Optimize context to fit within model limits.
Reduces context size to fit within model token limits. Preserves important
information based on the chosen strategy. Ensures system messages remain
at the start of context.
Args:
model: Model identifier for context limits.
strategy: Optimization strategy to use.
Returns:
Optimized list of messages that fits model context.
Examples:
Basic optimization:
```python
optimized = await manager.optimize_context(
model="gpt-4o",
strategy="window",
)
```
"""
...
async def find_context(
self,
query: str,
context: Sequence[Message] | None = None,
max_messages: int | None = None,
score_threshold: float | None = None,
embedding_model: str | None = None,
) -> list[Message]:
"""Find messages matching the search query.
Searches through available messages to find those matching the query.
Uses semantic search or other relevance metrics based on the
implementation. Preserves message relationships in the results.
Args:
query: Search query text.
context: Optional context to search within.
max_messages: Optional maximum messages to return.
score_threshold: Optional minimum score to return.
embedding_model: Optional model for computing embeddings.
Returns:
List of matching messages.
Examples:
Basic search:
```python
matches = await manager.find_context(
query="How do I deploy?",
max_messages=10,
)
```
"""
...
class LiteContextManager(ContextManager):
"""Default implementation of context management.
Uses MessageStore as the source of context for all operations. Provides
multiple strategies for context optimization and semantic search capabilities.
Context optimization strategies:
- "trim": Token-based trimming that preserves message order and relationships
- "window": Sliding window that keeps N most recent messages
- "summarize": Creates concise summaries of older messages while preserving recent ones
- "rag": Semantic search with query-based optimization, falls back to trim if no query
Context search is implemented using a MessageIndex for semantic operations.
The index computes embeddings for messages and performs similarity search
to find the most relevant context. System messages are always preserved
in search results.
All context operations fetch messages from the underlying MessageStore:
- create_context gets history and adds system message
- optimize_context fetches and processes messages using chosen strategy
- find_context searches through stored messages using semantic index
"""
def __init__(
self,
message_store: MessageStore[Any],
message_index: MessageIndex | None = None,
optimization_llm: LLM | None = None,
window_size: int = 50,
preserve_recent: int = 25,
relevant_window_size: int = 10,
relevant_score_threshold: float = 0.5,
chunk_size: int = 10,
default_optimization_strategy: LiteOptimizationStrategy = "trim",
default_embedding_model: str = "text-embedding-3-small",
) -> None:
"""Initialize the context manager.
Args:
message_store: Store for message persistence.
message_index: Index for semantic search operations.
optimization_llm: Language model used for context summarization.
window_size: Maximum messages in sliding window strategy.
preserve_recent: Messages to preserve when summarizing.
relevant_window_size: Maximum messages to return in search results.
relevant_score_threshold: Minimum score to return in search results.
chunk_size: Number of messages per summary chunk.
default_optimization_strategy: Default strategy for context optimization.
default_embedding_model: Default model for computing embeddings.
"""
self.message_store = message_store
self.message_index = message_index or LiteMessageIndex()
self.optimization_llm = optimization_llm or LLM(model="gpt-4o")
self.window_size = window_size
self.preserve_recent = preserve_recent
self.relevant_window_size = relevant_window_size
self.relevant_score_threshold = relevant_score_threshold
self.chunk_size = chunk_size
self.default_optimization_strategy = default_optimization_strategy
self.default_embedding_model = default_embedding_model
# ================================================
# MARK: Message Processing
# ================================================
def _split_messages(
self,
messages: Sequence[MessageT],
) -> tuple[list[MessageT], list[MessageT]]:
"""Split messages into system and non-system groups.
Args:
messages: Messages to split.
Returns:
Tuple of (system_messages, non_system_messages).
"""
system_messages: list[MessageT] = []
non_system_messages: list[MessageT] = []
for msg in messages:
if msg.role == "system":
system_messages.append(msg)
else:
non_system_messages.append(msg)
return system_messages, non_system_messages
def _create_message_chunks(
self,
messages: Sequence[MessageT],
) -> list[list[MessageT]]:
"""Create chunks of messages for summarization.
Preserves tool call/result pairs within chunks and handles
pending tool calls across chunk boundaries.
Args:
messages: Messages to chunk.
Returns:
List of message chunks ready for summarization.
"""
if not messages:
return []
chunks: list[list[MessageT]] = []
current_chunk: list[MessageT] = []
pending_tool_calls: dict[str, MessageT] = {}
def add_chunk() -> None:
if current_chunk:
filtered_chunk = filter_tool_call_pairs(current_chunk)
if filtered_chunk:
chunks.append(filtered_chunk)
current_chunk.clear()
pending_tool_calls.clear()
def add_chunk_if_needed() -> None:
if len(current_chunk) >= self.chunk_size and not pending_tool_calls:
add_chunk()
for message in messages:
add_chunk_if_needed()
if message.role == "assistant" and message.tool_calls:
current_chunk.append(message)
for tool_call in message.tool_calls:
if tool_call.id:
pending_tool_calls[tool_call.id] = message
elif message.role == "tool" and message.tool_call_id:
current_chunk.append(message)
pending_tool_calls.pop(message.tool_call_id, None)
add_chunk_if_needed()
else:
current_chunk.append(message)
add_chunk_if_needed()
if current_chunk:
add_chunk()
return chunks
# ================================================
# MARK: Summarization Helpers
# ================================================
async def _summarize_chunk(self, messages: Sequence[Message]) -> str:
"""Create a concise summary of messages.
Args:
messages: Messages to summarize.
Returns:
Concise summary focusing on key points.
"""
log_verbose(
f"Summarizing chunk of {len(messages)} messages",
level="DEBUG",
)
input_messages = [
Message(role="system", content=SUMMARIZER_SYSTEM_PROMPT),
*messages,
Message(role="user", content=SUMMARIZER_USER_PROMPT),
]
response = await acompletion(
model=self.optimization_llm.model,
messages=dump_messages(input_messages),
)
summary = response.choices[0].message.content or "No summary available."
log_verbose(
f"Generated summary of length {len(summary)}",
level="DEBUG",
)
return summary
# ================================================
# MARK: Optimization Strategies
# ================================================
async def _trim_strategy(
self,
messages: Sequence[Message],
model: str,
trim_ratio: float = 0.75,
) -> list[Message]:
"""Optimize context using token-based trimming.
Preserves message order and tool call pairs while fitting
within model's token limit.
Args:
messages: Messages to optimize.
model: Model identifier to determine context limits.
trim_ratio: Proportion of model's context to use.
Returns:
Trimmed message list that fits model limits.
"""
log_verbose(
f"Trimming messages to {trim_ratio:.0%} of {model} context limit",
level="DEBUG",
)
trimmed = trim_messages(
messages=list(messages),
model=model,
trim_ratio=trim_ratio,
)
log_verbose(
f"Trimmed messages from {len(messages)} to {len(trimmed.messages)}",
level="DEBUG",
)
return trimmed.messages
async def _window_strategy(
self,
messages: Sequence[Message],
model: str,
) -> list[Message]:
"""Keep only the most recent messages.
Maintains chronological order and tool call pairs while
limiting context to window size.
Args:
messages: Messages to optimize.
model: Model identifier to determine context limits.
Returns:
Most recent messages that fit within window.
"""
if len(messages) <= self.window_size:
return list(messages)
log_verbose(
f"Applying window strategy with size {self.window_size}",
level="DEBUG",
)
recent = list(messages[-self.window_size :])
filtered = filter_tool_call_pairs(recent)
trimmed = trim_messages(filtered, model)
log_verbose(
f"Window strategy reduced messages from {len(messages)} to {len(trimmed.messages)}",
level="DEBUG",
)
return trimmed.messages
async def _summarize_strategy(
self,
messages: Sequence[Message],
model: str,
) -> list[Message]:
"""Summarize older messages while preserving recent ones.
Creates concise summaries of older messages while keeping
recent messages intact.
Args:
messages: Messages to optimize.
model: Model identifier to determine context limits.
Returns:
Combined summary and recent messages.
"""
if len(messages) <= self.preserve_recent:
return list(messages)
to_preserve = filter_tool_call_pairs(list(messages[-self.preserve_recent :]))
to_summarize = filter_tool_call_pairs(list(messages[: -self.preserve_recent]))
if not to_summarize:
return to_preserve
chunks = self._create_message_chunks(to_summarize)
summaries = await asyncio.gather(*[self._summarize_chunk(chunk) for chunk in chunks])
summary_message = Message(
role="assistant",
content=f"Previous conversation summary:\n{' '.join(summaries)}",
)
combined_messages = [MessageRecord.from_message(summary_message), *to_preserve]
trimmed = trim_messages(combined_messages, model)
return trimmed.messages
async def _rag_strategy(
self,
messages: Sequence[Message],
model: str,
config: RAGStrategyConfig | None = None,
) -> list[Message]:
"""Optimize context using semantic search.
Uses query-based relevance when available, falls back to
trimming when no query is provided.
Args:
messages: Messages to optimize.
model: Model identifier to determine context limits.
config: Configuration for RAG strategy.
Returns:
Optimized messages based on relevance.
"""
if config is None:
log_verbose(
"No query provided, falling to trim strategy",
level="DEBUG",
)
return await self._trim_strategy(messages, model)
log_verbose(
f"Searching for relevant messages with query: {config.query}",
level="DEBUG",
)
relevant = await self.find_context(
context=messages,
query=config.query,
max_messages=config.max_messages,
score_threshold=config.score_threshold,
embedding_model=config.embedding_model,
)
if not relevant:
log_verbose(
"No relevant messages found, falling to trim strategy",
level="DEBUG",
)
return await self._trim_strategy(messages, model)
log_verbose(
"Trimming relevant messages to fit context",
level="DEBUG",
)
return await self._trim_strategy(relevant, model)
# ================================================
# MARK: Public API
# ================================================
@override
async def create_context(
self,
agent: Agent,
prompt: str | None = None,
context_variables: ContextVariables | None = None,
) -> list[Message]:
"""Create an execution context for an agent.
Prepares a list of messages suitable for the agent's next execution.
Resolves agent instructions with provided context variables. Combines
system message, relevant history, and optional user prompt.
Args:
agent: Agent requiring context creation.
prompt: Optional user prompt to include.
context_variables: Optional variables for dynamic resolution.
Returns:
List of messages ready for execution.
Examples:
Basic usage:
```python
context = await manager.create_context(
agent=agent,
prompt="Hello!",
)
```
With context variables:
```python
context = await manager.create_context(
agent=agent,
prompt="Help with task",
context_variables=ContextVariables(
user_name="Alice",
task_type="analysis",
),
)
```
"""
instructions = unwrap_instructions(agent.instructions, context_variables)
history = await self.message_store.get_messages()
history = [msg for msg in history if msg.role != "system"]
messages = [Message(role="system", content=instructions), *history]
if prompt:
messages.append(Message(role="user", content=prompt))
return messages
@override
async def optimize_context(
self,
model: str,
strategy: LiteOptimizationStrategy | None = None,
rag_config: RAGStrategyConfig | None = None,
) -> list[Message]:
"""Optimize context to fit model limits.
Reduces context size to fit within model token limits. Applies the chosen
strategy to preserve the most important information. Maintains message
relationships and ensures proper ordering in the result.
Available strategies:
- "trim": Token-based trimming without summarization
- "window": Keep N most recent messages
- "summarize": Summarize older messages, keep recent ones
- "rag": Semantic search with query-based optimization
Args:
model: Model identifier to determine context limits.
strategy: Optimization strategy to use.
rag_config: Configuration for RAG strategy.
Returns:
Optimized list of messages.
Examples:
Basic optimization:
```python
optimized = await manager.optimize_context(
model="gpt-4o",
strategy="window",
)
```
"""
messages = await self.message_store.get_messages()
system_messages, non_system_messages = self._split_messages(messages)
strategy = strategy or self.default_optimization_strategy
log_verbose(
f"Optimizing context with strategy '{strategy}' for model {model}",
level="DEBUG",
)
match strategy:
case "trim":
optimized = await self._trim_strategy(non_system_messages, model)
case "window":
optimized = await self._window_strategy(non_system_messages, model)
case "summarize":
optimized = await self._summarize_strategy(non_system_messages, model)
case "rag":
optimized = await self._rag_strategy(
messages=non_system_messages,
model=model,
config=rag_config,
)
case _:
raise ValueError(f"Unknown strategy: {strategy}")
log_verbose(
f"Context optimized from {len(non_system_messages)} to {len(optimized)} messages",
level="DEBUG",
)
optimized_messages = [*system_messages, *optimized]
await self.message_store.set_messages(optimized_messages)
return optimized_messages
@override
async def find_context(
self,
query: str,
context: Sequence[Message] | None = None,
max_messages: int | None = None,
score_threshold: float | None = None,
embedding_model: str | None = None,
) -> list[Message]:
"""Find messages matching the search query.
Searches through available messages to find those matching the query.
Uses semantic search or other relevance metrics based on the
implementation. Preserves message relationships in the results.
Args:
query: Search query text.
context: Optional context to search within.
max_messages: Optional maximum messages to return.
score_threshold: Optional minimum score to return.
embedding_model: Optional model for computing embeddings.
Returns:
List of matching messages.
Examples:
Basic search:
```python
matches = await manager.find_context(
query="How do I deploy?",
max_messages=10,
)
```
"""
if context is not None:
messages = [MessageRecord.from_message(msg) for msg in context]
else:
messages = await self.message_store.get_messages()
system_messages, non_system_messages = self._split_messages(messages)
embedding_model = embedding_model or self.default_embedding_model
if not non_system_messages:
return [*system_messages]
log_verbose(
f"Searching for relevant messages with query: '{query}' using {embedding_model}",
level="DEBUG",
)
await self.message_index.index(non_system_messages)
result = await self.message_index.search(
query=query,
max_results=max_messages or self.relevant_window_size,
score_threshold=score_threshold or self.relevant_score_threshold,
)
relevant_messages = [msg for msg, _ in result]
log_verbose(
f"Found {len(relevant_messages)} relevant messages",
level="DEBUG",
)
return [*system_messages, *relevant_messages]