- Cleaned up unnecessary comments in `__all__` definitions for better readability. - Adjusted indentation and formatting across modules for improved clarity (e.g., long lines, logical grouping). - Simplified conditional expressions and inline comments for context scoring and ranking. - Replaced some hard-coded values with type-safe annotations (e.g., `ClassVar`). - Removed unused imports and ensured consistent usage across test files. - Updated `test_score_not_cached_on_context` to clarify caching behavior. - Improved truncation strategy logic and marker handling.
179 lines
3.5 KiB
Python
179 lines
3.5 KiB
Python
"""
|
|
Context Management Engine
|
|
|
|
Sophisticated context assembly and optimization for LLM requests.
|
|
Provides intelligent context selection, token budget management,
|
|
and model-specific formatting.
|
|
|
|
Usage:
|
|
from app.services.context import (
|
|
ContextSettings,
|
|
get_context_settings,
|
|
SystemContext,
|
|
KnowledgeContext,
|
|
ConversationContext,
|
|
TaskContext,
|
|
ToolContext,
|
|
TokenBudget,
|
|
BudgetAllocator,
|
|
TokenCalculator,
|
|
)
|
|
|
|
# Get settings
|
|
settings = get_context_settings()
|
|
|
|
# Create budget for a model
|
|
allocator = BudgetAllocator(settings)
|
|
budget = allocator.create_budget_for_model("claude-3-sonnet")
|
|
|
|
# Create context instances
|
|
system_ctx = SystemContext.create_persona(
|
|
name="Code Assistant",
|
|
description="You are a helpful code assistant.",
|
|
capabilities=["Write code", "Debug issues"],
|
|
)
|
|
"""
|
|
|
|
# Budget Management
|
|
# Adapters
|
|
from .adapters import (
|
|
ClaudeAdapter,
|
|
DefaultAdapter,
|
|
ModelAdapter,
|
|
OpenAIAdapter,
|
|
get_adapter,
|
|
)
|
|
|
|
# Assembly
|
|
from .assembly import (
|
|
ContextPipeline,
|
|
PipelineMetrics,
|
|
)
|
|
from .budget import (
|
|
BudgetAllocator,
|
|
TokenBudget,
|
|
TokenCalculator,
|
|
)
|
|
|
|
# Cache
|
|
from .cache import ContextCache
|
|
|
|
# Compression
|
|
from .compression import (
|
|
ContextCompressor,
|
|
TruncationResult,
|
|
TruncationStrategy,
|
|
)
|
|
|
|
# Configuration
|
|
from .config import (
|
|
ContextSettings,
|
|
get_context_settings,
|
|
get_default_settings,
|
|
reset_context_settings,
|
|
)
|
|
|
|
# Engine
|
|
from .engine import ContextEngine, create_context_engine
|
|
|
|
# Exceptions
|
|
from .exceptions import (
|
|
AssemblyTimeoutError,
|
|
BudgetExceededError,
|
|
CacheError,
|
|
CompressionError,
|
|
ContextError,
|
|
ContextNotFoundError,
|
|
FormattingError,
|
|
InvalidContextError,
|
|
ScoringError,
|
|
TokenCountError,
|
|
)
|
|
|
|
# Prioritization
|
|
from .prioritization import (
|
|
ContextRanker,
|
|
RankingResult,
|
|
)
|
|
|
|
# Scoring
|
|
from .scoring import (
|
|
BaseScorer,
|
|
CompositeScorer,
|
|
PriorityScorer,
|
|
RecencyScorer,
|
|
RelevanceScorer,
|
|
ScoredContext,
|
|
)
|
|
|
|
# Types
|
|
from .types import (
|
|
AssembledContext,
|
|
BaseContext,
|
|
ContextPriority,
|
|
ContextType,
|
|
ConversationContext,
|
|
KnowledgeContext,
|
|
MessageRole,
|
|
SystemContext,
|
|
TaskComplexity,
|
|
TaskContext,
|
|
TaskStatus,
|
|
ToolContext,
|
|
ToolResultStatus,
|
|
)
|
|
|
|
__all__ = [
|
|
"AssembledContext",
|
|
"AssemblyTimeoutError",
|
|
"BaseContext",
|
|
"BaseScorer",
|
|
"BudgetAllocator",
|
|
"BudgetExceededError",
|
|
"CacheError",
|
|
"ClaudeAdapter",
|
|
"CompositeScorer",
|
|
"CompressionError",
|
|
"ContextCache",
|
|
"ContextCompressor",
|
|
"ContextEngine",
|
|
"ContextError",
|
|
"ContextNotFoundError",
|
|
"ContextPipeline",
|
|
"ContextPriority",
|
|
"ContextRanker",
|
|
"ContextSettings",
|
|
"ContextType",
|
|
"ConversationContext",
|
|
"DefaultAdapter",
|
|
"FormattingError",
|
|
"InvalidContextError",
|
|
"KnowledgeContext",
|
|
"MessageRole",
|
|
"ModelAdapter",
|
|
"OpenAIAdapter",
|
|
"PipelineMetrics",
|
|
"PriorityScorer",
|
|
"RankingResult",
|
|
"RecencyScorer",
|
|
"RelevanceScorer",
|
|
"ScoredContext",
|
|
"ScoringError",
|
|
"SystemContext",
|
|
"TaskComplexity",
|
|
"TaskContext",
|
|
"TaskStatus",
|
|
"TokenBudget",
|
|
"TokenCalculator",
|
|
"TokenCountError",
|
|
"ToolContext",
|
|
"ToolResultStatus",
|
|
"TruncationResult",
|
|
"TruncationStrategy",
|
|
"create_context_engine",
|
|
"get_adapter",
|
|
"get_context_settings",
|
|
"get_default_settings",
|
|
"reset_context_settings",
|
|
]
|