unique_toolkit 0.5.39__py3-none-any.whl → 0.5.41__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.
- unique_toolkit/language_model/schemas.py +21 -22
- unique_toolkit/language_model/service.py +90 -33
- {unique_toolkit-0.5.39.dist-info → unique_toolkit-0.5.41.dist-info}/METADATA +5 -1
- {unique_toolkit-0.5.39.dist-info → unique_toolkit-0.5.41.dist-info}/RECORD +6 -6
- {unique_toolkit-0.5.39.dist-info → unique_toolkit-0.5.41.dist-info}/LICENSE +0 -0
- {unique_toolkit-0.5.39.dist-info → unique_toolkit-0.5.41.dist-info}/WHEEL +0 -0
@@ -197,29 +197,28 @@ class LanguageModelTokenLimits(BaseModel):
|
|
197
197
|
token_limit_input: Optional[int] = None
|
198
198
|
token_limit_output: Optional[int] = None
|
199
199
|
|
200
|
+
fraction_input: float = Field(default=0.4, le=1, ge=0)
|
201
|
+
|
200
202
|
@model_validator(mode="after")
|
201
|
-
def
|
202
|
-
|
203
|
-
token_limit_input
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
and token_limit_output
|
219
|
-
)
|
220
|
-
self.token_limit = token_limit_input + token_limit_output
|
221
|
-
|
222
|
-
return self
|
203
|
+
def check_required_fields(self):
|
204
|
+
# Best case input and output is determined
|
205
|
+
if self.token_limit_input and self.token_limit_output:
|
206
|
+
self.token_limit = self.token_limit_input + self.token_limit_output
|
207
|
+
self.fraction_input = self.token_limit_input / self.token_limit
|
208
|
+
return self
|
209
|
+
|
210
|
+
# Deal with case where only token_limit and optional fraction_input is given
|
211
|
+
if self.token_limit:
|
212
|
+
if not self.fraction_input:
|
213
|
+
self.fraction_input = 0.4
|
214
|
+
|
215
|
+
self.token_limit_input = self.fraction_input * self.token_limit
|
216
|
+
self.token_limit_output = (1 - self.fraction_input) * self.token_limit
|
217
|
+
return self
|
218
|
+
|
219
|
+
raise ValueError(
|
220
|
+
'Either "token_limit_input" and "token_limit_output" must be provided together, or "token_limit" must be provided.'
|
221
|
+
)
|
223
222
|
|
224
223
|
|
225
224
|
class LanguageModelToolParameterProperty(BaseModel):
|
@@ -37,6 +37,7 @@ class LanguageModelService(BaseService):
|
|
37
37
|
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
38
38
|
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
39
39
|
tools: Optional[list[LanguageModelTool]] = None,
|
40
|
+
other_options: Optional[dict] = None,
|
40
41
|
):
|
41
42
|
"""
|
42
43
|
Calls the completion endpoint synchronously without streaming the response.
|
@@ -47,25 +48,26 @@ class LanguageModelService(BaseService):
|
|
47
48
|
temperature (float): The temperature value. Defaults to 0.
|
48
49
|
timeout (int): The timeout value in milliseconds. Defaults to 240_000.
|
49
50
|
tools (Optional[list[LanguageModelTool]]): The tools to use. Defaults to None.
|
51
|
+
other_options (Optional[dict]): The other options to use. Defaults to None.
|
50
52
|
|
51
53
|
Returns:
|
52
54
|
LanguageModelResponse: The LanguageModelResponse object.
|
53
55
|
"""
|
54
|
-
options = self.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
options, model, messages_dict, _ = self.prepare_completion_params_util(
|
57
|
+
messages=messages,
|
58
|
+
model_name=model_name,
|
59
|
+
temperature=temperature,
|
60
|
+
tools=tools,
|
61
|
+
other_options=other_options,
|
59
62
|
)
|
60
63
|
|
61
64
|
try:
|
62
65
|
response = unique_sdk.ChatCompletion.create(
|
63
66
|
company_id=self.event.company_id,
|
64
|
-
# TODO change or extend types in unique_sdk
|
65
67
|
model=model,
|
66
68
|
messages=cast(
|
67
69
|
list[unique_sdk.Integrated.ChatCompletionRequestMessage],
|
68
|
-
|
70
|
+
messages_dict,
|
69
71
|
),
|
70
72
|
timeout=timeout,
|
71
73
|
options=options, # type: ignore
|
@@ -84,6 +86,7 @@ class LanguageModelService(BaseService):
|
|
84
86
|
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
85
87
|
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
86
88
|
tools: Optional[list[LanguageModelTool]] = None,
|
89
|
+
other_options: Optional[dict] = None,
|
87
90
|
logger: Optional[logging.Logger] = logging.getLogger(__name__),
|
88
91
|
) -> LanguageModelResponse:
|
89
92
|
"""
|
@@ -100,6 +103,7 @@ class LanguageModelService(BaseService):
|
|
100
103
|
temperature (float): The temperature setting for the completion. Defaults to 0.
|
101
104
|
timeout (int): The timeout value in milliseconds for the request. Defaults to 240_000.
|
102
105
|
tools (Optional[list[LanguageModelTool]]): Optional list of tools to include in the request.
|
106
|
+
other_options (Optional[dict]): The other options to use. Defaults to None.
|
103
107
|
logger (Optional[logging.Logger], optional): The logger used to log errors. Defaults to the logger for the current module.
|
104
108
|
|
105
109
|
Returns:
|
@@ -108,19 +112,21 @@ class LanguageModelService(BaseService):
|
|
108
112
|
Raises:
|
109
113
|
Exception: If an error occurs during the request, an exception is raised and logged.
|
110
114
|
"""
|
111
|
-
options = cls.
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
115
|
+
options, model, messages_dict, _ = cls.prepare_completion_params_util(
|
116
|
+
messages=messages,
|
117
|
+
model_name=model_name,
|
118
|
+
temperature=temperature,
|
119
|
+
tools=tools,
|
120
|
+
other_options=other_options,
|
116
121
|
)
|
122
|
+
|
117
123
|
try:
|
118
124
|
response = await unique_sdk.ChatCompletion.create_async(
|
119
125
|
company_id=company_id,
|
120
126
|
model=model,
|
121
127
|
messages=cast(
|
122
128
|
list[unique_sdk.Integrated.ChatCompletionRequestMessage],
|
123
|
-
|
129
|
+
messages_dict,
|
124
130
|
),
|
125
131
|
timeout=timeout,
|
126
132
|
options=options, # type: ignore
|
@@ -137,6 +143,7 @@ class LanguageModelService(BaseService):
|
|
137
143
|
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
138
144
|
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
139
145
|
tools: Optional[list[LanguageModelTool]] = None,
|
146
|
+
other_options: Optional[dict] = None,
|
140
147
|
) -> LanguageModelResponse:
|
141
148
|
"""
|
142
149
|
Calls the completion endpoint asynchronously without streaming the response.
|
@@ -151,7 +158,7 @@ class LanguageModelService(BaseService):
|
|
151
158
|
temperature (float): The temperature setting for the completion. Defaults to 0.0.
|
152
159
|
timeout (int): The timeout value in milliseconds for the request. Defaults to 240,000.
|
153
160
|
tools (Optional[list[LanguageModelTool]]): Optional list of tools to include in the request.
|
154
|
-
|
161
|
+
other_options (Optional[dict]): The other options to use. Defaults to None.
|
155
162
|
Returns:
|
156
163
|
LanguageModelResponse: The response object containing the completed result.
|
157
164
|
|
@@ -165,6 +172,7 @@ class LanguageModelService(BaseService):
|
|
165
172
|
temperature=temperature,
|
166
173
|
timeout=timeout,
|
167
174
|
tools=tools,
|
175
|
+
other_options=other_options,
|
168
176
|
logger=self.logger,
|
169
177
|
)
|
170
178
|
|
@@ -178,6 +186,7 @@ class LanguageModelService(BaseService):
|
|
178
186
|
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
179
187
|
tools: Optional[list[LanguageModelTool]] = None,
|
180
188
|
start_text: Optional[str] = None,
|
189
|
+
other_options: Optional[dict] = None,
|
181
190
|
):
|
182
191
|
"""
|
183
192
|
Streams a completion in the chat session synchronously.
|
@@ -191,16 +200,19 @@ class LanguageModelService(BaseService):
|
|
191
200
|
timeout (int): The timeout value in milliseconds. Defaults to 240_000.
|
192
201
|
tools (Optional[list[LanguageModelTool]]): The tools to use. Defaults to None.
|
193
202
|
start_text (Optional[str]): The start text. Defaults to None.
|
194
|
-
|
203
|
+
other_options (Optional[dict]): The other options to use. Defaults to None.
|
195
204
|
Returns:
|
196
205
|
The LanguageModelStreamResponse object once the stream has finished.
|
197
206
|
"""
|
198
|
-
options =
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
207
|
+
options, model, messages_dict, search_context = (
|
208
|
+
self.prepare_completion_params_util(
|
209
|
+
messages=messages,
|
210
|
+
model_name=model_name,
|
211
|
+
temperature=temperature,
|
212
|
+
tools=tools,
|
213
|
+
other_options=other_options,
|
214
|
+
content_chunks=content_chunks,
|
215
|
+
)
|
204
216
|
)
|
205
217
|
|
206
218
|
try:
|
@@ -211,11 +223,10 @@ class LanguageModelService(BaseService):
|
|
211
223
|
userMessageId=self.event.payload.user_message.id,
|
212
224
|
messages=cast(
|
213
225
|
list[unique_sdk.Integrated.ChatCompletionRequestMessage],
|
214
|
-
|
226
|
+
messages_dict,
|
215
227
|
),
|
216
228
|
chatId=self.event.payload.chat_id,
|
217
229
|
searchContext=search_context,
|
218
|
-
# TODO change or extend types in unique_sdk
|
219
230
|
model=model,
|
220
231
|
timeout=timeout,
|
221
232
|
assistantId=self.event.payload.assistant_id,
|
@@ -238,6 +249,7 @@ class LanguageModelService(BaseService):
|
|
238
249
|
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
239
250
|
tools: Optional[list[LanguageModelTool]] = None,
|
240
251
|
start_text: Optional[str] = None,
|
252
|
+
other_options: Optional[dict] = None,
|
241
253
|
):
|
242
254
|
"""
|
243
255
|
Streams a completion in the chat session asynchronously.
|
@@ -251,17 +263,19 @@ class LanguageModelService(BaseService):
|
|
251
263
|
timeout (int): The timeout value in milliseconds. Defaults to 240_000.
|
252
264
|
tools (Optional[list[LanguageModelTool]]): The tools to use. Defaults to None.
|
253
265
|
start_text (Optional[str]): The start text. Defaults to None.
|
254
|
-
|
266
|
+
other_options (Optional[dict]): The other options to use. Defaults to None.
|
255
267
|
Returns:
|
256
268
|
The LanguageModelStreamResponse object once the stream has finished.
|
257
269
|
"""
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
270
|
+
options, model, messages_dict, search_context = (
|
271
|
+
self.prepare_completion_params_util(
|
272
|
+
messages=messages,
|
273
|
+
model_name=model_name,
|
274
|
+
temperature=temperature,
|
275
|
+
tools=tools,
|
276
|
+
other_options=other_options,
|
277
|
+
content_chunks=content_chunks,
|
278
|
+
)
|
265
279
|
)
|
266
280
|
|
267
281
|
try:
|
@@ -272,7 +286,7 @@ class LanguageModelService(BaseService):
|
|
272
286
|
userMessageId=self.event.payload.user_message.id,
|
273
287
|
messages=cast(
|
274
288
|
list[unique_sdk.Integrated.ChatCompletionRequestMessage],
|
275
|
-
|
289
|
+
messages_dict,
|
276
290
|
),
|
277
291
|
chatId=self.event.payload.chat_id,
|
278
292
|
searchContext=search_context,
|
@@ -280,7 +294,6 @@ class LanguageModelService(BaseService):
|
|
280
294
|
timeout=timeout,
|
281
295
|
assistantId=self.event.payload.assistant_id,
|
282
296
|
debugInfo=debug_info,
|
283
|
-
# TODO change or extend types in unique_sdk
|
284
297
|
options=options, # type: ignore
|
285
298
|
startText=start_text,
|
286
299
|
)
|
@@ -321,3 +334,47 @@ class LanguageModelService(BaseService):
|
|
321
334
|
for tool in tools
|
322
335
|
]
|
323
336
|
return options
|
337
|
+
|
338
|
+
@classmethod
|
339
|
+
def prepare_completion_params_util(
|
340
|
+
cls,
|
341
|
+
messages: LanguageModelMessages,
|
342
|
+
model_name: LanguageModelName | str,
|
343
|
+
temperature: float,
|
344
|
+
tools: Optional[list[LanguageModelTool]] = None,
|
345
|
+
other_options: Optional[dict] = None,
|
346
|
+
content_chunks: Optional[list[ContentChunk]] = None,
|
347
|
+
) -> tuple[dict, str, dict, Optional[dict]]:
|
348
|
+
"""
|
349
|
+
Prepares common parameters for completion requests.
|
350
|
+
|
351
|
+
Returns:
|
352
|
+
tuple containing:
|
353
|
+
- options (dict): Combined options including tools and temperature
|
354
|
+
- model (str): Resolved model name
|
355
|
+
- messages_dict (dict): Processed messages
|
356
|
+
- search_context (Optional[dict]): Processed content chunks if provided
|
357
|
+
"""
|
358
|
+
|
359
|
+
options = cls._add_tools_to_options({}, tools)
|
360
|
+
options["temperature"] = temperature
|
361
|
+
if other_options:
|
362
|
+
options.update(other_options)
|
363
|
+
|
364
|
+
model = (
|
365
|
+
model_name.name if isinstance(model_name, LanguageModelName) else model_name
|
366
|
+
)
|
367
|
+
|
368
|
+
# Different methods need different message dump parameters
|
369
|
+
messages_dict = messages.model_dump(
|
370
|
+
exclude_none=True,
|
371
|
+
by_alias=content_chunks is not None, # Use by_alias for streaming methods
|
372
|
+
)
|
373
|
+
|
374
|
+
search_context = (
|
375
|
+
LanguageModelService._to_search_context(content_chunks)
|
376
|
+
if content_chunks is not None
|
377
|
+
else None
|
378
|
+
)
|
379
|
+
|
380
|
+
return options, model, messages_dict, search_context
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: unique_toolkit
|
3
|
-
Version: 0.5.
|
3
|
+
Version: 0.5.41
|
4
4
|
Summary:
|
5
5
|
License: Proprietary
|
6
6
|
Author: Martin Fadler
|
@@ -100,7 +100,11 @@ All notable changes to this project will be documented in this file.
|
|
100
100
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
101
101
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
102
102
|
|
103
|
+
## [0.5.41] - 2024-12-11
|
104
|
+
- Update `LanguageModelTokenLimits` includes a fraction_input now to always have input/output token limits available.
|
103
105
|
|
106
|
+
## [0.5.40] - 2024-12-11
|
107
|
+
- Add `other_options` to `LanguageModelService.complete`, `LanguageModelService.complete_async`, `LanguageModelService.stream_complete` and `LanguageModelService.stream_complete_async`
|
104
108
|
|
105
109
|
## [0.5.39] - 2024-12-09
|
106
110
|
- Add `contentIds` to `Search.create` and `Search.create_async`
|
@@ -37,10 +37,10 @@ unique_toolkit/evaluators/output_parser.py,sha256=eI72qkzK1dZyUvnfP2SOAQCGBj_-Pw
|
|
37
37
|
unique_toolkit/evaluators/schemas.py,sha256=Jaue6Uhx75X1CyHKWj8sT3RE1JZXTqoLtfLt2xQNCX8,2507
|
38
38
|
unique_toolkit/language_model/__init__.py,sha256=YuhyczGPj6w9xX-sOVUhmozvzIFxcckHFEkeMBecr5s,1784
|
39
39
|
unique_toolkit/language_model/infos.py,sha256=kQK6F3r8xTN7oT6b39J7rxW-Y4iPXjx_Fr9bCOVQdm0,12509
|
40
|
-
unique_toolkit/language_model/schemas.py,sha256
|
41
|
-
unique_toolkit/language_model/service.py,sha256=
|
40
|
+
unique_toolkit/language_model/schemas.py,sha256=-5yTJsEUMKSc6qZ1zdBU9Eewo9qQlpqlVssKHIwUY64,6925
|
41
|
+
unique_toolkit/language_model/service.py,sha256=brNCPRA0XxgqHi2rI5i2lyFCkUiw4MNMe1VaR3UgWmY,15500
|
42
42
|
unique_toolkit/language_model/utils.py,sha256=bPQ4l6_YO71w-zaIPanUUmtbXC1_hCvLK0tAFc3VCRc,1902
|
43
|
-
unique_toolkit-0.5.
|
44
|
-
unique_toolkit-0.5.
|
45
|
-
unique_toolkit-0.5.
|
46
|
-
unique_toolkit-0.5.
|
43
|
+
unique_toolkit-0.5.41.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
|
44
|
+
unique_toolkit-0.5.41.dist-info/METADATA,sha256=im-lBhwsW2ZgUZoA6mfyogye1c6ZIFXqgaw_Ilf2yLs,14764
|
45
|
+
unique_toolkit-0.5.41.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
46
|
+
unique_toolkit-0.5.41.dist-info/RECORD,,
|
File without changes
|
File without changes
|