kweaver-dolphin 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- DolphinLanguageSDK/__init__.py +58 -0
- dolphin/__init__.py +62 -0
- dolphin/cli/__init__.py +20 -0
- dolphin/cli/args/__init__.py +9 -0
- dolphin/cli/args/parser.py +567 -0
- dolphin/cli/builtin_agents/__init__.py +22 -0
- dolphin/cli/commands/__init__.py +4 -0
- dolphin/cli/interrupt/__init__.py +8 -0
- dolphin/cli/interrupt/handler.py +205 -0
- dolphin/cli/interrupt/keyboard.py +82 -0
- dolphin/cli/main.py +49 -0
- dolphin/cli/multimodal/__init__.py +34 -0
- dolphin/cli/multimodal/clipboard.py +327 -0
- dolphin/cli/multimodal/handler.py +249 -0
- dolphin/cli/multimodal/image_processor.py +214 -0
- dolphin/cli/multimodal/input_parser.py +149 -0
- dolphin/cli/runner/__init__.py +8 -0
- dolphin/cli/runner/runner.py +989 -0
- dolphin/cli/ui/__init__.py +10 -0
- dolphin/cli/ui/console.py +2795 -0
- dolphin/cli/ui/input.py +340 -0
- dolphin/cli/ui/layout.py +425 -0
- dolphin/cli/ui/stream_renderer.py +302 -0
- dolphin/cli/utils/__init__.py +8 -0
- dolphin/cli/utils/helpers.py +135 -0
- dolphin/cli/utils/version.py +49 -0
- dolphin/core/__init__.py +107 -0
- dolphin/core/agent/__init__.py +10 -0
- dolphin/core/agent/agent_state.py +69 -0
- dolphin/core/agent/base_agent.py +970 -0
- dolphin/core/code_block/__init__.py +0 -0
- dolphin/core/code_block/agent_init_block.py +0 -0
- dolphin/core/code_block/assign_block.py +98 -0
- dolphin/core/code_block/basic_code_block.py +1865 -0
- dolphin/core/code_block/explore_block.py +1327 -0
- dolphin/core/code_block/explore_block_v2.py +712 -0
- dolphin/core/code_block/explore_strategy.py +672 -0
- dolphin/core/code_block/judge_block.py +220 -0
- dolphin/core/code_block/prompt_block.py +32 -0
- dolphin/core/code_block/skill_call_deduplicator.py +291 -0
- dolphin/core/code_block/tool_block.py +129 -0
- dolphin/core/common/__init__.py +17 -0
- dolphin/core/common/constants.py +176 -0
- dolphin/core/common/enums.py +1173 -0
- dolphin/core/common/exceptions.py +133 -0
- dolphin/core/common/multimodal.py +539 -0
- dolphin/core/common/object_type.py +165 -0
- dolphin/core/common/output_format.py +432 -0
- dolphin/core/common/types.py +36 -0
- dolphin/core/config/__init__.py +16 -0
- dolphin/core/config/global_config.py +1289 -0
- dolphin/core/config/ontology_config.py +133 -0
- dolphin/core/context/__init__.py +12 -0
- dolphin/core/context/context.py +1580 -0
- dolphin/core/context/context_manager.py +161 -0
- dolphin/core/context/var_output.py +82 -0
- dolphin/core/context/variable_pool.py +356 -0
- dolphin/core/context_engineer/__init__.py +41 -0
- dolphin/core/context_engineer/config/__init__.py +5 -0
- dolphin/core/context_engineer/config/settings.py +402 -0
- dolphin/core/context_engineer/core/__init__.py +7 -0
- dolphin/core/context_engineer/core/budget_manager.py +327 -0
- dolphin/core/context_engineer/core/context_assembler.py +583 -0
- dolphin/core/context_engineer/core/context_manager.py +637 -0
- dolphin/core/context_engineer/core/tokenizer_service.py +260 -0
- dolphin/core/context_engineer/example/incremental_example.py +267 -0
- dolphin/core/context_engineer/example/traditional_example.py +334 -0
- dolphin/core/context_engineer/services/__init__.py +5 -0
- dolphin/core/context_engineer/services/compressor.py +399 -0
- dolphin/core/context_engineer/utils/__init__.py +6 -0
- dolphin/core/context_engineer/utils/context_utils.py +441 -0
- dolphin/core/context_engineer/utils/message_formatter.py +270 -0
- dolphin/core/context_engineer/utils/token_utils.py +139 -0
- dolphin/core/coroutine/__init__.py +15 -0
- dolphin/core/coroutine/context_snapshot.py +154 -0
- dolphin/core/coroutine/context_snapshot_profile.py +922 -0
- dolphin/core/coroutine/context_snapshot_store.py +268 -0
- dolphin/core/coroutine/execution_frame.py +145 -0
- dolphin/core/coroutine/execution_state_registry.py +161 -0
- dolphin/core/coroutine/resume_handle.py +101 -0
- dolphin/core/coroutine/step_result.py +101 -0
- dolphin/core/executor/__init__.py +18 -0
- dolphin/core/executor/debug_controller.py +630 -0
- dolphin/core/executor/dolphin_executor.py +1063 -0
- dolphin/core/executor/executor.py +624 -0
- dolphin/core/flags/__init__.py +27 -0
- dolphin/core/flags/definitions.py +49 -0
- dolphin/core/flags/manager.py +113 -0
- dolphin/core/hook/__init__.py +95 -0
- dolphin/core/hook/expression_evaluator.py +499 -0
- dolphin/core/hook/hook_dispatcher.py +380 -0
- dolphin/core/hook/hook_types.py +248 -0
- dolphin/core/hook/isolated_variable_pool.py +284 -0
- dolphin/core/interfaces.py +53 -0
- dolphin/core/llm/__init__.py +0 -0
- dolphin/core/llm/llm.py +495 -0
- dolphin/core/llm/llm_call.py +100 -0
- dolphin/core/llm/llm_client.py +1285 -0
- dolphin/core/llm/message_sanitizer.py +120 -0
- dolphin/core/logging/__init__.py +20 -0
- dolphin/core/logging/logger.py +526 -0
- dolphin/core/message/__init__.py +8 -0
- dolphin/core/message/compressor.py +749 -0
- dolphin/core/parser/__init__.py +8 -0
- dolphin/core/parser/parser.py +405 -0
- dolphin/core/runtime/__init__.py +10 -0
- dolphin/core/runtime/runtime_graph.py +926 -0
- dolphin/core/runtime/runtime_instance.py +446 -0
- dolphin/core/skill/__init__.py +14 -0
- dolphin/core/skill/context_retention.py +157 -0
- dolphin/core/skill/skill_function.py +686 -0
- dolphin/core/skill/skill_matcher.py +282 -0
- dolphin/core/skill/skillkit.py +700 -0
- dolphin/core/skill/skillset.py +72 -0
- dolphin/core/trajectory/__init__.py +10 -0
- dolphin/core/trajectory/recorder.py +189 -0
- dolphin/core/trajectory/trajectory.py +522 -0
- dolphin/core/utils/__init__.py +9 -0
- dolphin/core/utils/cache_kv.py +212 -0
- dolphin/core/utils/tools.py +340 -0
- dolphin/lib/__init__.py +93 -0
- dolphin/lib/debug/__init__.py +8 -0
- dolphin/lib/debug/visualizer.py +409 -0
- dolphin/lib/memory/__init__.py +28 -0
- dolphin/lib/memory/async_processor.py +220 -0
- dolphin/lib/memory/llm_calls.py +195 -0
- dolphin/lib/memory/manager.py +78 -0
- dolphin/lib/memory/sandbox.py +46 -0
- dolphin/lib/memory/storage.py +245 -0
- dolphin/lib/memory/utils.py +51 -0
- dolphin/lib/ontology/__init__.py +12 -0
- dolphin/lib/ontology/basic/__init__.py +0 -0
- dolphin/lib/ontology/basic/base.py +102 -0
- dolphin/lib/ontology/basic/concept.py +130 -0
- dolphin/lib/ontology/basic/object.py +11 -0
- dolphin/lib/ontology/basic/relation.py +63 -0
- dolphin/lib/ontology/datasource/__init__.py +27 -0
- dolphin/lib/ontology/datasource/datasource.py +66 -0
- dolphin/lib/ontology/datasource/oracle_datasource.py +338 -0
- dolphin/lib/ontology/datasource/sql.py +845 -0
- dolphin/lib/ontology/mapping.py +177 -0
- dolphin/lib/ontology/ontology.py +733 -0
- dolphin/lib/ontology/ontology_context.py +16 -0
- dolphin/lib/ontology/ontology_manager.py +107 -0
- dolphin/lib/skill_results/__init__.py +31 -0
- dolphin/lib/skill_results/cache_backend.py +559 -0
- dolphin/lib/skill_results/result_processor.py +181 -0
- dolphin/lib/skill_results/result_reference.py +179 -0
- dolphin/lib/skill_results/skillkit_hook.py +324 -0
- dolphin/lib/skill_results/strategies.py +328 -0
- dolphin/lib/skill_results/strategy_registry.py +150 -0
- dolphin/lib/skillkits/__init__.py +44 -0
- dolphin/lib/skillkits/agent_skillkit.py +155 -0
- dolphin/lib/skillkits/cognitive_skillkit.py +82 -0
- dolphin/lib/skillkits/env_skillkit.py +250 -0
- dolphin/lib/skillkits/mcp_adapter.py +616 -0
- dolphin/lib/skillkits/mcp_skillkit.py +771 -0
- dolphin/lib/skillkits/memory_skillkit.py +650 -0
- dolphin/lib/skillkits/noop_skillkit.py +31 -0
- dolphin/lib/skillkits/ontology_skillkit.py +89 -0
- dolphin/lib/skillkits/plan_act_skillkit.py +452 -0
- dolphin/lib/skillkits/resource/__init__.py +52 -0
- dolphin/lib/skillkits/resource/models/__init__.py +6 -0
- dolphin/lib/skillkits/resource/models/skill_config.py +109 -0
- dolphin/lib/skillkits/resource/models/skill_meta.py +127 -0
- dolphin/lib/skillkits/resource/resource_skillkit.py +393 -0
- dolphin/lib/skillkits/resource/skill_cache.py +215 -0
- dolphin/lib/skillkits/resource/skill_loader.py +395 -0
- dolphin/lib/skillkits/resource/skill_validator.py +406 -0
- dolphin/lib/skillkits/resource_skillkit.py +11 -0
- dolphin/lib/skillkits/search_skillkit.py +163 -0
- dolphin/lib/skillkits/sql_skillkit.py +274 -0
- dolphin/lib/skillkits/system_skillkit.py +509 -0
- dolphin/lib/skillkits/vm_skillkit.py +65 -0
- dolphin/lib/utils/__init__.py +9 -0
- dolphin/lib/utils/data_process.py +207 -0
- dolphin/lib/utils/handle_progress.py +178 -0
- dolphin/lib/utils/security.py +139 -0
- dolphin/lib/utils/text_retrieval.py +462 -0
- dolphin/lib/vm/__init__.py +11 -0
- dolphin/lib/vm/env_executor.py +895 -0
- dolphin/lib/vm/python_session_manager.py +453 -0
- dolphin/lib/vm/vm.py +610 -0
- dolphin/sdk/__init__.py +60 -0
- dolphin/sdk/agent/__init__.py +12 -0
- dolphin/sdk/agent/agent_factory.py +236 -0
- dolphin/sdk/agent/dolphin_agent.py +1106 -0
- dolphin/sdk/api/__init__.py +4 -0
- dolphin/sdk/runtime/__init__.py +8 -0
- dolphin/sdk/runtime/env.py +363 -0
- dolphin/sdk/skill/__init__.py +10 -0
- dolphin/sdk/skill/global_skills.py +706 -0
- dolphin/sdk/skill/traditional_toolkit.py +260 -0
- kweaver_dolphin-0.1.0.dist-info/METADATA +521 -0
- kweaver_dolphin-0.1.0.dist-info/RECORD +199 -0
- kweaver_dolphin-0.1.0.dist-info/WHEEL +5 -0
- kweaver_dolphin-0.1.0.dist-info/entry_points.txt +27 -0
- kweaver_dolphin-0.1.0.dist-info/licenses/LICENSE.txt +201 -0
- kweaver_dolphin-0.1.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"""Budget manager for token allocation across context buckets."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, List, Optional, Any
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Import BucketConfig from settings
|
|
8
|
+
from ..config.settings import BucketConfig, ContextConfig
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class BudgetAllocation:
|
|
13
|
+
"""Result of budget allocation for a bucket.
|
|
14
|
+
|
|
15
|
+
Data class for bucket budget allocation results.
|
|
16
|
+
|
|
17
|
+
Attributes:
|
|
18
|
+
bucket_name (str): The name of the bucket
|
|
19
|
+
allocated_tokens (int): The number of tokens allocated to this bucket
|
|
20
|
+
priority (float): The priority (weight) of this bucket
|
|
21
|
+
content_score (float): The content score, default is 0.0
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
bucket_name: str
|
|
25
|
+
allocated_tokens: int
|
|
26
|
+
priority: float
|
|
27
|
+
content_score: float = 0.0
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class BudgetManager:
|
|
31
|
+
"""Manages token allocation across context buckets.
|
|
32
|
+
|
|
33
|
+
Manages token allocation across context buckets.
|
|
34
|
+
This class is responsible for calculating the available input budget based on the model's context limit, output budget, and system overhead,
|
|
35
|
+
and distributing tokens among buckets according to bucket configurations and content scores.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
context_config: ContextConfig,
|
|
41
|
+
):
|
|
42
|
+
"""Initialize budget manager.
|
|
43
|
+
|
|
44
|
+
Initialize the budget manager.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
tokenizer_service: Instance of TokenizerService, used for token counting
|
|
48
|
+
"""
|
|
49
|
+
self.buckets: Dict[str, BucketConfig] = {}
|
|
50
|
+
self.drop_order: List[str] = []
|
|
51
|
+
self.model_config = context_config.model
|
|
52
|
+
if context_config.buckets:
|
|
53
|
+
self.configure_buckets(context_config.buckets)
|
|
54
|
+
|
|
55
|
+
def add_bucket(self, bucket_config: BucketConfig):
|
|
56
|
+
"""Add a bucket configuration.
|
|
57
|
+
|
|
58
|
+
Add a bucket configuration.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
bucket_config (BucketConfig): The bucket configuration object
|
|
62
|
+
"""
|
|
63
|
+
self.buckets[bucket_config.name] = bucket_config
|
|
64
|
+
|
|
65
|
+
def configure_buckets(self, bucket_configs: Dict[str, Any]):
|
|
66
|
+
"""Configure multiple buckets from dictionary or BucketConfig objects.
|
|
67
|
+
|
|
68
|
+
Configure multiple buckets from a dictionary or BucketConfig objects.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
bucket_configs: A dictionary of bucket configurations (dict or BucketConfig objects)
|
|
72
|
+
"""
|
|
73
|
+
for name, config in bucket_configs.items():
|
|
74
|
+
if (
|
|
75
|
+
hasattr(config, "min_tokens")
|
|
76
|
+
and hasattr(config, "max_tokens")
|
|
77
|
+
and hasattr(config, "weight")
|
|
78
|
+
):
|
|
79
|
+
# Looks like a BucketConfig object - create compatible one with name
|
|
80
|
+
bucket_data = {
|
|
81
|
+
"min_tokens": config.min_tokens,
|
|
82
|
+
"max_tokens": config.max_tokens,
|
|
83
|
+
"weight": config.weight,
|
|
84
|
+
"compress": getattr(config, "compress", None),
|
|
85
|
+
"content_score": getattr(config, "content_score", 0.5),
|
|
86
|
+
"message_role": getattr(config, "message_role", "system"),
|
|
87
|
+
}
|
|
88
|
+
# Add optional attributes only if they exist
|
|
89
|
+
for attr in ["select", "rerank", "droppable", "placement"]:
|
|
90
|
+
if hasattr(config, attr):
|
|
91
|
+
bucket_data[attr] = getattr(config, attr)
|
|
92
|
+
bucket = BucketConfig(name=name, **bucket_data)
|
|
93
|
+
self.add_bucket(bucket)
|
|
94
|
+
elif isinstance(config, dict):
|
|
95
|
+
# Dictionary configuration
|
|
96
|
+
bucket = BucketConfig(name=name, **config)
|
|
97
|
+
self.add_bucket(bucket)
|
|
98
|
+
else:
|
|
99
|
+
raise ValueError(
|
|
100
|
+
f"Invalid bucket configuration type for {name}: {type(config)}"
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
def allocate_budget(
|
|
104
|
+
self,
|
|
105
|
+
content_scores: Optional[Dict[str, float]] = None,
|
|
106
|
+
system_overhead: int = 200,
|
|
107
|
+
) -> List[BudgetAllocation]:
|
|
108
|
+
"""
|
|
109
|
+
Allocate budget across buckets using the algorithm from context_engineer.md.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
model_context_limit: Model's context window limit
|
|
113
|
+
output_budget: Tokens reserved for output
|
|
114
|
+
content_scores: Relevance scores for each bucket's content (optional)
|
|
115
|
+
system_overhead: System overhead tokens
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
List of budget allocations
|
|
119
|
+
"""
|
|
120
|
+
available_budget = self.calculate_budget(
|
|
121
|
+
self.model_config.context_limit,
|
|
122
|
+
self.model_config.output_budget,
|
|
123
|
+
system_overhead,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
# Step 1: Initial allocation with minimum requirements
|
|
127
|
+
allocations = self._initial_allocation(available_budget)
|
|
128
|
+
remaining_budget = available_budget - sum(
|
|
129
|
+
alloc.allocated_tokens for alloc in allocations
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
# Step 2: Dynamic optimization based on content scores
|
|
133
|
+
if remaining_budget > 0:
|
|
134
|
+
# If content_scores is not provided, use the default values from BucketConfig
|
|
135
|
+
if content_scores is None:
|
|
136
|
+
content_scores = {
|
|
137
|
+
name: bucket.content_score for name, bucket in self.buckets.items()
|
|
138
|
+
}
|
|
139
|
+
allocations = self._optimize_allocation(
|
|
140
|
+
allocations, content_scores, remaining_budget
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
# Remove meaningless overflow handling logic
|
|
144
|
+
# The preceding allocation logic has already ensured that the total allocation will not exceed the available budget.
|
|
145
|
+
# Compression decisions should be based on actual content vs allocated_tokens, rather than "overflow" at the budget level
|
|
146
|
+
return allocations
|
|
147
|
+
|
|
148
|
+
def _initial_allocation(self, available_budget: int) -> List[BudgetAllocation]:
|
|
149
|
+
"""Step 1: Initial allocation with minimum requirements."""
|
|
150
|
+
allocations = []
|
|
151
|
+
min_total = sum(bucket.min_tokens for bucket in self.buckets.values())
|
|
152
|
+
|
|
153
|
+
if available_budget < min_total:
|
|
154
|
+
# Not enough budget for minimum requirements
|
|
155
|
+
# Handle edge cases: budget is negative or zero
|
|
156
|
+
if available_budget <= 0:
|
|
157
|
+
# Extreme case: each bucket is allocated at least 1 token, distributed according to the minimum required ratio
|
|
158
|
+
for bucket in self.buckets.values():
|
|
159
|
+
allocated = max(
|
|
160
|
+
1, int((bucket.min_tokens / min_total) * 1)
|
|
161
|
+
) # Allocate at least 1 proportionally
|
|
162
|
+
allocation = BudgetAllocation(
|
|
163
|
+
bucket_name=bucket.name,
|
|
164
|
+
allocated_tokens=allocated,
|
|
165
|
+
priority=bucket.weight,
|
|
166
|
+
)
|
|
167
|
+
allocations.append(allocation)
|
|
168
|
+
else:
|
|
169
|
+
# Normal scale reduction: Allocate positive budget proportionally
|
|
170
|
+
for bucket in self.buckets.values():
|
|
171
|
+
allocated = max(
|
|
172
|
+
1, int((bucket.min_tokens / min_total) * available_budget)
|
|
173
|
+
)
|
|
174
|
+
allocation = BudgetAllocation(
|
|
175
|
+
bucket_name=bucket.name,
|
|
176
|
+
allocated_tokens=allocated,
|
|
177
|
+
priority=bucket.weight,
|
|
178
|
+
)
|
|
179
|
+
allocations.append(allocation)
|
|
180
|
+
else:
|
|
181
|
+
# Normal allocation
|
|
182
|
+
remaining_budget = available_budget - min_total
|
|
183
|
+
total_weight = sum(bucket.weight for bucket in self.buckets.values())
|
|
184
|
+
|
|
185
|
+
for bucket in self.buckets.values():
|
|
186
|
+
# Calculate additional allocation based on weight
|
|
187
|
+
additional = int((bucket.weight / total_weight) * remaining_budget)
|
|
188
|
+
final_allocation = min(
|
|
189
|
+
bucket.max_tokens, bucket.min_tokens + additional
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
allocation = BudgetAllocation(
|
|
193
|
+
bucket_name=bucket.name,
|
|
194
|
+
allocated_tokens=final_allocation,
|
|
195
|
+
priority=bucket.weight,
|
|
196
|
+
)
|
|
197
|
+
allocations.append(allocation)
|
|
198
|
+
|
|
199
|
+
return allocations
|
|
200
|
+
|
|
201
|
+
def _optimize_allocation(
|
|
202
|
+
self,
|
|
203
|
+
allocations: List[BudgetAllocation],
|
|
204
|
+
content_scores: Dict[str, float],
|
|
205
|
+
remaining_budget: int,
|
|
206
|
+
) -> List[BudgetAllocation]:
|
|
207
|
+
"""Step 2: Optimize allocation based on content relevance scores."""
|
|
208
|
+
# Calculate marginal utility for each bucket
|
|
209
|
+
utilities = []
|
|
210
|
+
for allocation in allocations:
|
|
211
|
+
bucket = self.buckets[allocation.bucket_name]
|
|
212
|
+
current_score = content_scores.get(bucket.name, 0.0)
|
|
213
|
+
|
|
214
|
+
# Calculate potential improvement
|
|
215
|
+
current_tokens = allocation.allocated_tokens
|
|
216
|
+
max_tokens = bucket.max_tokens
|
|
217
|
+
|
|
218
|
+
if current_tokens < max_tokens:
|
|
219
|
+
# Marginal utility = score improvement per token
|
|
220
|
+
marginal_utility = current_score / (current_tokens + 1)
|
|
221
|
+
utilities.append(
|
|
222
|
+
(bucket.name, marginal_utility, max_tokens - current_tokens)
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# Sort by marginal utility (descending)
|
|
226
|
+
utilities.sort(key=lambda x: x[1], reverse=True)
|
|
227
|
+
|
|
228
|
+
# Allocate remaining budget based on marginal utility
|
|
229
|
+
for bucket_name, utility, available in utilities:
|
|
230
|
+
if remaining_budget <= 0:
|
|
231
|
+
break
|
|
232
|
+
|
|
233
|
+
allocation = next(a for a in allocations if a.bucket_name == bucket_name)
|
|
234
|
+
allocate_amount = min(available, remaining_budget)
|
|
235
|
+
allocation.allocated_tokens += allocate_amount
|
|
236
|
+
remaining_budget -= allocate_amount
|
|
237
|
+
|
|
238
|
+
return allocations
|
|
239
|
+
|
|
240
|
+
def calculate_budget(
|
|
241
|
+
self, model_context_limit: int, output_budget: int, system_overhead: int = 200
|
|
242
|
+
) -> int:
|
|
243
|
+
"""Calculate available input budget.
|
|
244
|
+
|
|
245
|
+
Calculate the available input budget.
|
|
246
|
+
|
|
247
|
+
Args:
|
|
248
|
+
model_context_limit: The model's context window limit
|
|
249
|
+
output_budget: Number of tokens reserved for output
|
|
250
|
+
system_overhead: Number of tokens for system overhead
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
Available input budget
|
|
254
|
+
"""
|
|
255
|
+
return model_context_limit - output_budget - system_overhead
|
|
256
|
+
|
|
257
|
+
# Removed the meaningless _handle_overflow method
|
|
258
|
+
# Compression logic is now based entirely on the comparison between actual content and allocated_tokens
|
|
259
|
+
# Instead of "overflow" detection at the budget level
|
|
260
|
+
|
|
261
|
+
def get_bucket_config(self, bucket_name: str) -> Optional[BucketConfig]:
|
|
262
|
+
"""Get configuration for a specific bucket.
|
|
263
|
+
|
|
264
|
+
Get the configuration of a specific bucket.
|
|
265
|
+
|
|
266
|
+
Args:
|
|
267
|
+
bucket_name: The name of the bucket
|
|
268
|
+
|
|
269
|
+
Returns:
|
|
270
|
+
Bucket configuration object, or None if it does not exist
|
|
271
|
+
"""
|
|
272
|
+
return self.buckets.get(bucket_name)
|
|
273
|
+
|
|
274
|
+
def set_drop_order(self, drop_order: List[str]):
|
|
275
|
+
"""Set the order for dropping content when budget is exceeded.
|
|
276
|
+
|
|
277
|
+
Set the order in which content is dropped when the budget is exceeded.
|
|
278
|
+
|
|
279
|
+
Args:
|
|
280
|
+
drop_order: List of bucket names, defining the drop order
|
|
281
|
+
"""
|
|
282
|
+
self.drop_order = drop_order
|
|
283
|
+
|
|
284
|
+
def validate_configuration(self) -> bool:
|
|
285
|
+
"""Validate the bucket configuration.
|
|
286
|
+
|
|
287
|
+
Validate the validity of the bucket configuration.
|
|
288
|
+
|
|
289
|
+
Returns:
|
|
290
|
+
True if the configuration is valid, False otherwise
|
|
291
|
+
"""
|
|
292
|
+
if not self.buckets:
|
|
293
|
+
return False
|
|
294
|
+
|
|
295
|
+
# Check that all buckets in drop order exist
|
|
296
|
+
for bucket_name in self.drop_order:
|
|
297
|
+
if bucket_name not in self.buckets:
|
|
298
|
+
return False
|
|
299
|
+
|
|
300
|
+
# Check basic constraints
|
|
301
|
+
for bucket in self.buckets.values():
|
|
302
|
+
if bucket.min_tokens > bucket.max_tokens:
|
|
303
|
+
return False
|
|
304
|
+
if bucket.weight < 0:
|
|
305
|
+
return False
|
|
306
|
+
|
|
307
|
+
return True
|
|
308
|
+
|
|
309
|
+
def get_total_min_tokens(self) -> int:
|
|
310
|
+
"""Get total minimum tokens required across all buckets.
|
|
311
|
+
|
|
312
|
+
Get the total minimum number of tokens required across all buckets.
|
|
313
|
+
|
|
314
|
+
Returns:
|
|
315
|
+
The total minimum number of tokens across all buckets
|
|
316
|
+
"""
|
|
317
|
+
return sum(bucket.min_tokens for bucket in self.buckets.values())
|
|
318
|
+
|
|
319
|
+
def get_total_max_tokens(self) -> int:
|
|
320
|
+
"""Get total maximum tokens across all buckets.
|
|
321
|
+
|
|
322
|
+
Get the total maximum number of tokens across all buckets.
|
|
323
|
+
|
|
324
|
+
Returns:
|
|
325
|
+
The total maximum number of tokens across all buckets
|
|
326
|
+
"""
|
|
327
|
+
return sum(bucket.max_tokens for bucket in self.buckets.values())
|