unique_toolkit 0.5.38__py3-none-any.whl → 0.5.40__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/content/service.py +15 -4
- unique_toolkit/language_model/service.py +90 -33
- {unique_toolkit-0.5.38.dist-info → unique_toolkit-0.5.40.dist-info}/METADATA +9 -2
- {unique_toolkit-0.5.38.dist-info → unique_toolkit-0.5.40.dist-info}/RECORD +6 -6
- {unique_toolkit-0.5.38.dist-info → unique_toolkit-0.5.40.dist-info}/LICENSE +0 -0
- {unique_toolkit-0.5.38.dist-info → unique_toolkit-0.5.40.dist-info}/WHEEL +0 -0
@@ -29,6 +29,7 @@ class ContentService(BaseService):
|
|
29
29
|
|
30
30
|
def __init__(self, event: Event, logger: Optional[logging.Logger] = None):
|
31
31
|
super().__init__(event, logger)
|
32
|
+
self.metadata_filter = event.payload.metadata_filter
|
32
33
|
|
33
34
|
DEFAULT_SEARCH_LANGUAGE = "english"
|
34
35
|
|
@@ -42,6 +43,7 @@ class ContentService(BaseService):
|
|
42
43
|
scope_ids: Optional[list[str]] = None,
|
43
44
|
chat_only: Optional[bool] = None,
|
44
45
|
metadata_filter: Optional[dict] = None,
|
46
|
+
content_ids: Optional[list[str]] = None,
|
45
47
|
) -> list[ContentChunk]:
|
46
48
|
"""
|
47
49
|
Performs a synchronous search for content chunks in the knowledge base.
|
@@ -54,14 +56,17 @@ class ContentService(BaseService):
|
|
54
56
|
reranker_config (Optional[ContentRerankerConfig]): The reranker configuration. Defaults to None.
|
55
57
|
scope_ids (Optional[list[str]]): The scope IDs. Defaults to None.
|
56
58
|
chat_only (Optional[bool]): Whether to search only in the current chat. Defaults to None.
|
57
|
-
metadata_filter (Optional[dict]): UniqueQL metadata filter. Defaults to None.
|
58
|
-
|
59
|
+
metadata_filter (Optional[dict]): UniqueQL metadata filter. If unspecified/None, it tries to use the metadata filter from the event. Defaults to None.
|
60
|
+
content_ids (Optional[list[str]]): The content IDs to search. Defaults to None.
|
59
61
|
Returns:
|
60
62
|
list[ContentChunk]: The search results.
|
61
63
|
"""
|
62
64
|
if not scope_ids:
|
63
65
|
self.logger.warning("No scope IDs provided for search.")
|
64
66
|
|
67
|
+
if metadata_filter is None:
|
68
|
+
metadata_filter = self.metadata_filter
|
69
|
+
|
65
70
|
try:
|
66
71
|
searches = unique_sdk.Search.create(
|
67
72
|
user_id=self.event.user_id,
|
@@ -79,6 +84,7 @@ class ContentService(BaseService):
|
|
79
84
|
language=search_language,
|
80
85
|
chatOnly=chat_only,
|
81
86
|
metaDataFilter=metadata_filter,
|
87
|
+
contentIds=content_ids,
|
82
88
|
)
|
83
89
|
except Exception as e:
|
84
90
|
self.logger.error(f"Error while searching content chunks: {e}")
|
@@ -101,6 +107,7 @@ class ContentService(BaseService):
|
|
101
107
|
scope_ids: Optional[list[str]] = None,
|
102
108
|
chat_only: Optional[bool] = None,
|
103
109
|
metadata_filter: Optional[dict] = None,
|
110
|
+
content_ids: Optional[list[str]] = None,
|
104
111
|
):
|
105
112
|
"""
|
106
113
|
Performs an asynchronous search for content chunks in the knowledge base.
|
@@ -113,14 +120,17 @@ class ContentService(BaseService):
|
|
113
120
|
reranker_config (Optional[ContentRerankerConfig]): The reranker configuration. Defaults to None.
|
114
121
|
scope_ids (Optional[list[str]]): The scope IDs. Defaults to None.
|
115
122
|
chat_only (Optional[bool]): Whether to search only in the current chat. Defaults to None.
|
116
|
-
metadata_filter (Optional[dict]): UniqueQL metadata filter. Defaults to None.
|
117
|
-
|
123
|
+
metadata_filter (Optional[dict]): UniqueQL metadata filter. If unspecified/None, it tries to use the metadata filter from the event. Defaults to None.
|
124
|
+
content_ids (Optional[list[str]]): The content IDs to search. Defaults to None.
|
118
125
|
Returns:
|
119
126
|
list[ContentChunk]: The search results.
|
120
127
|
"""
|
121
128
|
if not scope_ids:
|
122
129
|
self.logger.warning("No scope IDs provided for search.")
|
123
130
|
|
131
|
+
if metadata_filter is None:
|
132
|
+
metadata_filter = self.metadata_filter
|
133
|
+
|
124
134
|
try:
|
125
135
|
searches = await unique_sdk.Search.create_async(
|
126
136
|
user_id=self.event.user_id,
|
@@ -138,6 +148,7 @@ class ContentService(BaseService):
|
|
138
148
|
language=search_language,
|
139
149
|
chatOnly=chat_only,
|
140
150
|
metaDataFilter=metadata_filter,
|
151
|
+
contentIds=content_ids,
|
141
152
|
)
|
142
153
|
except Exception as e:
|
143
154
|
self.logger.error(f"Error while searching content chunks: {e}")
|
@@ -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.40
|
4
4
|
Summary:
|
5
5
|
License: Proprietary
|
6
6
|
Author: Martin Fadler
|
@@ -17,7 +17,7 @@ Requires-Dist: python-dotenv (>=1.0.1,<2.0.0)
|
|
17
17
|
Requires-Dist: regex (>=2024.5.15,<2025.0.0)
|
18
18
|
Requires-Dist: tiktoken (>=0.7.0,<0.8.0)
|
19
19
|
Requires-Dist: typing-extensions (>=4.9.0,<5.0.0)
|
20
|
-
Requires-Dist: unique-sdk (>=0.9.
|
20
|
+
Requires-Dist: unique-sdk (>=0.9.14,<0.10.0)
|
21
21
|
Description-Content-Type: text/markdown
|
22
22
|
|
23
23
|
# Unique Toolkit
|
@@ -100,6 +100,13 @@ 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.40] - 2024-12-11
|
104
|
+
- Add `other_options` to `LanguageModelService.complete`, `LanguageModelService.complete_async`, `LanguageModelService.stream_complete` and `LanguageModelService.stream_complete_async`
|
105
|
+
|
106
|
+
## [0.5.39] - 2024-12-09
|
107
|
+
- Add `contentIds` to `Search.create` and `Search.create_async`
|
108
|
+
- Use `metadata_filter` from event in `ContentService.search_content_chunks` and `ContentService.async_search_content_chunks` as default.
|
109
|
+
|
103
110
|
## [0.5.38] - 2024-11-26
|
104
111
|
- Added string representation to `LanguageModelMessage` and `LanguageModelMessages` class
|
105
112
|
|
@@ -17,7 +17,7 @@ unique_toolkit/chat/state.py,sha256=Cjgwv_2vhDFbV69xxsn7SefhaoIAEqLx3ferdVFCnOg,
|
|
17
17
|
unique_toolkit/chat/utils.py,sha256=ihm-wQykBWhB4liR3LnwPVPt_qGW6ETq21Mw4HY0THE,854
|
18
18
|
unique_toolkit/content/__init__.py,sha256=MSH2sxjQyKD2Sef92fzE5Dt9SihdzivB6yliSwJfTmQ,890
|
19
19
|
unique_toolkit/content/schemas.py,sha256=zks_Pkki2VhxICJJgHZyc-LPmRuj5dLbw3pgcUT7SW8,2362
|
20
|
-
unique_toolkit/content/service.py,sha256=
|
20
|
+
unique_toolkit/content/service.py,sha256=rznSUyOuB4nbo6bFworzFv86kzo6TVT4Nc8GMAeAJB4,17711
|
21
21
|
unique_toolkit/content/utils.py,sha256=Lake671plRsqNvO3pN_rmyVcpwbdED_KQpLcCnc4lv4,6902
|
22
22
|
unique_toolkit/embedding/__init__.py,sha256=dr8M9jvslQTxPpxgaGwzxY0FildiWf-DidN_cahPAWw,191
|
23
23
|
unique_toolkit/embedding/schemas.py,sha256=1GvKCaSk4jixzVQ2PKq8yDqwGEVY_hWclYtoAr6CC2g,96
|
@@ -38,9 +38,9 @@ unique_toolkit/evaluators/schemas.py,sha256=Jaue6Uhx75X1CyHKWj8sT3RE1JZXTqoLtfLt
|
|
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
40
|
unique_toolkit/language_model/schemas.py,sha256=wg_ly66UvLOVNdrq8xr-0uN0HPY41UIk5mVPxdG-VGs,6687
|
41
|
-
unique_toolkit/language_model/service.py,sha256=
|
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.40.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
|
44
|
+
unique_toolkit-0.5.40.dist-info/METADATA,sha256=JwqJSWAYTZJJ0Z47C7iezCZgO3BUqysc5odUtlrkWKE,14620
|
45
|
+
unique_toolkit-0.5.40.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
46
|
+
unique_toolkit-0.5.40.dist-info/RECORD,,
|
File without changes
|
File without changes
|