unique_toolkit 0.8.49__py3-none-any.whl → 0.8.51__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.
@@ -4,11 +4,11 @@
4
4
  import json
5
5
  from typing import Any, Callable
6
6
 
7
- from pydantic import BaseModel
8
-
9
- from unique_toolkit._common.token.image_token_counting import (
7
+ from _common.utils.token.image_token_counting import (
10
8
  calculate_image_tokens_from_base64,
11
9
  )
10
+ from pydantic import BaseModel
11
+
12
12
  from unique_toolkit.language_model import (
13
13
  LanguageModelMessage,
14
14
  LanguageModelMessages,
@@ -71,11 +71,25 @@ def num_tokens_per_messages(
71
71
  for message in messages:
72
72
  num_tokens = 3 # extra_tokens_per_message
73
73
  for key, value in message.items():
74
- if isinstance(value, str):
74
+ if key == "content":
75
+ if message.get("role") == "tool":
76
+ """
77
+ We have observed a general difference in the way tool response messages are handled.
78
+ Specifically, if we take a list of tool responses and artificially transform them into user messages (content field stays the same)
79
+ the token consumption goes does drastically, this seems to scale with the number of tokens in the tool responses.
80
+
81
+ the json.dumps() method was found by trial and error. It will give a conservative estimate, but seems to be close to the token count
82
+ returned from the openai call.
83
+ """
84
+ num_tokens += len(encode(json.dumps(value)))
85
+ elif isinstance(value, list):
86
+ # NOTE: The result returned by the function below is not 100% accurate.
87
+ num_tokens += handle_message_with_images(value, encode)
88
+ else:
89
+ num_tokens += len(encode(value))
90
+ elif isinstance(value, str):
75
91
  num_tokens += len(encode(value))
76
- elif isinstance(value, list):
77
- # NOTE: The result returned by the function below is not 100% accurate.
78
- num_tokens += handle_message_with_images(value, encode)
92
+
79
93
  if key == "name":
80
94
  num_tokens += 1 # extra_tokens_per_name
81
95
 
@@ -163,8 +177,14 @@ def messages_to_openai_messages(
163
177
  messages = LanguageModelMessages(messages)
164
178
 
165
179
  return [
166
- {k: v for k, v in m.items() if (k in ["content", "role"] and v is not None)}
167
- for m in json.loads(messages.model_dump_json())
180
+ {
181
+ k: v
182
+ for k, v in m.items()
183
+ if (
184
+ k in ["content", "role", "name"] and v is not None
185
+ ) # Ignore tool_calls for now
186
+ }
187
+ for m in messages.model_dump(mode="json")
168
188
  ]
169
189
 
170
190
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: unique_toolkit
3
- Version: 0.8.49
3
+ Version: 0.8.51
4
4
  Summary:
5
5
  License: Proprietary
6
6
  Author: Cedric Klinkert
@@ -117,6 +117,13 @@ 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
+ ## [0.8.51] - 2025-09-06
121
+ - Update token counter to latest version of monorepo.
122
+
123
+ ## [0.8.50] - 2025-09-08
124
+ - Minor fix in documentation
125
+ - Updated examples
126
+
120
127
  ## [0.8.49] - 2025-09-05
121
128
  - Fixed token reducer now has a safety margin of 10% less did not work.
122
129
 
@@ -14,7 +14,7 @@ unique_toolkit/_common/exception.py,sha256=caQIE1btsQnpKCHqL2cgWUSbHup06enQu_Pt7
14
14
  unique_toolkit/_common/feature_flags/schema.py,sha256=3JpTuld8kK-UQ5B0sbYTu0yqhyFPnChXG2Iv4BNqHdg,539
15
15
  unique_toolkit/_common/pydantic_helpers.py,sha256=4a8LPey31k4dCztYag1OBhYnGHREN08-l3NEjbFD1ok,743
16
16
  unique_toolkit/_common/token/image_token_counting.py,sha256=VpFfZyY0GIH27q_Wy4YNjk2algqvbCtJyzuuROoFQPw,2189
17
- unique_toolkit/_common/token/token_counting.py,sha256=Jo5B11GLlnaZDi4u7xCzIboLl8zn5cY_dmrogHixVdk,6132
17
+ unique_toolkit/_common/token/token_counting.py,sha256=cg7RPmmotZWofrGMrphaRSr2ch9mT7tMKl9AJXyYGWo,7112
18
18
  unique_toolkit/_common/utils/structured_output/schema.py,sha256=Tp7kDYcmKtnUhcuRkH86TSYhylRff0ZZJYb2dLkISts,131
19
19
  unique_toolkit/_common/validate_required_values.py,sha256=Y_M1ub9gIKP9qZ45F6Zq3ZHtuIqhmOjl8Z2Vd3avg8w,588
20
20
  unique_toolkit/_common/validators.py,sha256=aZwbMho7XszN7lT5RtemaiXgC0WJ4u40oeVgsNGhF4U,2803
@@ -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.49.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
122
- unique_toolkit-0.8.49.dist-info/METADATA,sha256=4EVSBrsj4dhQW8rwBS-jWGhqXZZDe0nR30DoGFsZ1Do,31103
123
- unique_toolkit-0.8.49.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
124
- unique_toolkit-0.8.49.dist-info/RECORD,,
121
+ unique_toolkit-0.8.51.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
122
+ unique_toolkit-0.8.51.dist-info/METADATA,sha256=4tMl3Lf8D4sKZKgXRTZf8fPes-6qSK75P0dY5HzURgA,31257
123
+ unique_toolkit-0.8.51.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
124
+ unique_toolkit-0.8.51.dist-info/RECORD,,