unique_toolkit 0.8.48__py3-none-any.whl → 0.8.50__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.
@@ -28,6 +28,10 @@ from unique_toolkit.language_model.schemas import (
28
28
  )
29
29
  from unique_toolkit.reference_manager.reference_manager import ReferenceManager
30
30
 
31
+ MAX_INPUT_TOKENS_SAFETY_PERCENTAGE = (
32
+ 0.1 # 10% safety margin for input tokens we need 10% less does not work.
33
+ )
34
+
31
35
 
32
36
  class SourceReductionResult(BaseModel):
33
37
  message: LanguageModelToolMessage
@@ -74,26 +78,27 @@ class LoopTokenReducer:
74
78
  ) -> LanguageModelMessages:
75
79
  """Compose the system and user messages for the plan execution step, which is evaluating if any further tool calls are required."""
76
80
 
77
- messages = await self._construct_history(
81
+ history_from_db = await self._prep_db_history(
78
82
  original_user_message,
79
83
  rendered_user_message_string,
80
84
  rendered_system_message_string,
81
- loop_history,
82
85
  remove_from_text,
83
86
  )
84
87
 
88
+ messages = self._construct_history(
89
+ history_from_db,
90
+ loop_history,
91
+ )
92
+
85
93
  token_count = self._count_message_tokens(messages)
86
94
  self._log_token_usage(token_count)
87
95
 
88
96
  while self._exceeds_token_limit(token_count):
89
97
  token_count_before_reduction = token_count
90
98
  loop_history = self._handle_token_limit_exceeded(loop_history)
91
- messages = await self._construct_history(
92
- original_user_message,
93
- rendered_user_message_string,
94
- rendered_system_message_string,
99
+ messages = self._construct_history(
100
+ history_from_db,
95
101
  loop_history,
96
- remove_from_text,
97
102
  )
98
103
  token_count = self._count_message_tokens(messages)
99
104
  self._log_token_usage(token_count)
@@ -101,6 +106,11 @@ class LoopTokenReducer:
101
106
  if token_count_after_reduction >= token_count_before_reduction:
102
107
  break
103
108
 
109
+ token_count = self._count_message_tokens(messages)
110
+ self._logger.info(
111
+ f"Final token count after reduction: {token_count} of model_capacity {self._language_model.token_limits.token_limit_input}"
112
+ )
113
+
104
114
  return messages
105
115
 
106
116
  def _exceeds_token_limit(self, token_count: int) -> bool:
@@ -110,13 +120,14 @@ class LoopTokenReducer:
110
120
  len(chunks) > 1
111
121
  for chunks in self._reference_manager.get_chunks_of_all_tools()
112
122
  )
113
-
123
+ max_tokens = int(
124
+ self._language_model.token_limits.token_limit_input
125
+ * (1 - MAX_INPUT_TOKENS_SAFETY_PERCENTAGE)
126
+ )
114
127
  # TODO: This is not fully correct at the moment as the token_count
115
128
  # include system_prompt and user question already
116
129
  # TODO: There is a problem if we exceed but only have one chunk per tool call
117
- exceeds_limit = (
118
- token_count > self._language_model.token_limits.token_limit_input
119
- )
130
+ exceeds_limit = token_count > max_tokens
120
131
 
121
132
  return has_multiple_chunks_for_a_tool_call and exceeds_limit
122
133
 
@@ -132,14 +143,13 @@ class LoopTokenReducer:
132
143
  self._logger.info(f"Token messages: {token_count}")
133
144
  # self.agent_debug_info.add("token_messages", token_count)
134
145
 
135
- async def _construct_history(
146
+ async def _prep_db_history(
136
147
  self,
137
148
  original_user_message: str,
138
149
  rendered_user_message_string: str,
139
150
  rendered_system_message_string: str,
140
- loop_history: list[LanguageModelMessage],
141
151
  remove_from_text: Callable[[str], Awaitable[str]],
142
- ) -> LanguageModelMessages:
152
+ ) -> list[LanguageModelMessage]:
143
153
  history_from_db = await self._get_history_from_db(remove_from_text)
144
154
  history_from_db = self._replace_user_message(
145
155
  history_from_db, original_user_message, rendered_user_message_string
@@ -147,9 +157,15 @@ class LoopTokenReducer:
147
157
  system_message = LanguageModelSystemMessage(
148
158
  content=rendered_system_message_string
149
159
  )
160
+ return [system_message] + history_from_db
150
161
 
162
+ def _construct_history(
163
+ self,
164
+ history_from_db: list[LanguageModelMessage],
165
+ loop_history: list[LanguageModelMessage],
166
+ ) -> LanguageModelMessages:
151
167
  constructed_history = LanguageModelMessages(
152
- [system_message] + history_from_db + loop_history,
168
+ history_from_db + loop_history,
153
169
  )
154
170
 
155
171
  return constructed_history
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: unique_toolkit
3
- Version: 0.8.48
3
+ Version: 0.8.50
4
4
  Summary:
5
5
  License: Proprietary
6
6
  Author: Cedric Klinkert
@@ -117,6 +117,14 @@ All notable changes to this project will be documented in this file.
117
117
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
118
118
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
119
119
 
120
+
121
+ ## [0.8.50] - 2025-09-08
122
+ - Minor fix in documentation
123
+ - Updated examples
124
+
125
+ ## [0.8.49] - 2025-09-05
126
+ - Fixed token reducer now has a safety margin of 10% less did not work.
127
+
120
128
  ## [0.8.48] - 2025-09-05
121
129
  - Add documentation on language models to markdown
122
130
 
@@ -71,7 +71,7 @@ unique_toolkit/framework_utilities/openai/message_builder.py,sha256=VU6mJm_upLca
71
71
  unique_toolkit/framework_utilities/utils.py,sha256=JK7g2yMfEx3eMprug26769xqNpS5WJcizf8n2zWMBng,789
72
72
  unique_toolkit/history_manager/history_construction_with_contents.py,sha256=c8Zy3erSbHGT8AdICRRlSK91T_FN6tNpTznvUzpLbWk,9023
73
73
  unique_toolkit/history_manager/history_manager.py,sha256=7mdT8li4Oo-t0d1q0pCwJksdal-Y0wLzZj-YnIlJ6xQ,8350
74
- unique_toolkit/history_manager/loop_token_reducer.py,sha256=9kqJioUehfoYs5-XMoCv-b_5JpNRrhOz62QwhC3LF3E,17899
74
+ unique_toolkit/history_manager/loop_token_reducer.py,sha256=YOWgEnodXlBeQMU3rA_wc6_qFHC_1ianuAvXN5ZZez4,18451
75
75
  unique_toolkit/history_manager/utils.py,sha256=iu4LsYOElx8HlZjcx3ZC75I-TmEYBiEP9q2J93Q63Mg,5606
76
76
  unique_toolkit/language_model/__init__.py,sha256=lRQyLlbwHbNFf4-0foBU13UGb09lwEeodbVsfsSgaCk,1971
77
77
  unique_toolkit/language_model/builder.py,sha256=4OKfwJfj3TrgO1ezc_ewIue6W7BCQ2ZYQXUckWVPPTA,3369
@@ -118,7 +118,7 @@ unique_toolkit/tools/utils/execution/execution.py,sha256=vjG2Y6awsGNtlvyQAGCTthQ
118
118
  unique_toolkit/tools/utils/source_handling/schema.py,sha256=vzAyf6ZWNexjMO0OrnB8y2glGkvAilmGGQXd6zcDaKw,870
119
119
  unique_toolkit/tools/utils/source_handling/source_formatting.py,sha256=C7uayNbdkNVJdEARA5CENnHtNY1SU6etlaqbgHNyxaQ,9152
120
120
  unique_toolkit/tools/utils/source_handling/tests/test_source_formatting.py,sha256=oM5ZxEgzROrnX1229KViCAFjRxl9wCTzWZoinYSHleM,6979
121
- unique_toolkit-0.8.48.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
122
- unique_toolkit-0.8.48.dist-info/METADATA,sha256=JJiemqChaHLXodVnd3Cj7DNXMURuyF4--7H1p8oe-gA,31005
123
- unique_toolkit-0.8.48.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
124
- unique_toolkit-0.8.48.dist-info/RECORD,,
121
+ unique_toolkit-0.8.50.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
122
+ unique_toolkit-0.8.50.dist-info/METADATA,sha256=8YBM58kjR4VR74qWcSLLCbh7avpqoUmwNF3UY8A5ZdI,31178
123
+ unique_toolkit-0.8.50.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
124
+ unique_toolkit-0.8.50.dist-info/RECORD,,