babylon.rag.context_window
Context window management for the RAG system.
- class babylon.rag.context_window.ContextWindowManager(config=None, metrics_collector=None, lifecycle_manager=None)[source]
Bases:
objectManages the token usage and content prioritization in the RAG context window.
The ContextWindowManager ensures that the total token usage stays within the limits of the AI model while prioritizing the most relevant content. It implements:
Token counting and tracking
Content prioritization based on relevance
Automatic optimization when approaching limits
Integration with metrics collection
- Parameters:
config (ContextWindowConfig | None)
metrics_collector (MetricsCollector | None)
lifecycle_manager (Any)
- config
Configuration for the context window
- metrics_collector
Collector for performance metrics
- lifecycle_manager
Optional manager for object lifecycles
- __init__(config=None, metrics_collector=None, lifecycle_manager=None)[source]
Initialize the context window manager.
- Parameters:
config (
ContextWindowConfig|None) – Configuration for token limits and optimization thresholdsmetrics_collector (
MetricsCollector|None) – Collector for performance metricslifecycle_manager (
Any) – Optional manager for object lifecycles
- add_content(content_id, content, token_count, importance=0.5)[source]
Add content to the context window.
- Parameters:
- Return type:
- Returns:
True if content was added successfully, False if optimization was required
- Raises:
ContentInsertionError – If content could not be added
CapacityExceededError – If capacity is exceeded and optimization fails
- count_tokens(content)[source]
Count the number of tokens in a string.
This is a simple implementation that could be enhanced with a proper tokenizer.
- class babylon.rag.context_window.ContextWindowConfig(**data)[source]
Bases:
BaseModelConfiguration for the Context Window Management system.
- Parameters:
- max_token_limit
Maximum number of tokens allowed in the context window
- capacity_threshold
Percentage of capacity at which optimization should trigger
- prioritization_strategy
Strategy for content prioritization (relevance, recency, hybrid)
- min_content_importance
Minimum importance score for content to be kept in context
- model_config: ClassVar[ConfigDict] = {'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- babylon.rag.context_window.count_tokens(content)[source]
Count the number of tokens in content of various types.
This is a simple implementation that estimates token counts. For production, this should be replaced with a proper tokenizer for the target model.
Modules
Configuration model for the context window management system. |
|
Context window manager for token-aware content prioritization. |
|
Token counting utilities for Context Window Management. |