unique_toolkit 0.5.49__py3-none-any.whl → 0.5.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.
@@ -1,7 +1,8 @@
1
1
  import logging
2
- from typing import Optional, cast
2
+ from typing import Optional, Type, cast
3
3
 
4
4
  import unique_sdk
5
+ from pydantic import BaseModel
5
6
 
6
7
  from unique_toolkit._common._base_service import BaseService
7
8
  from unique_toolkit.app.schemas import Event
@@ -37,6 +38,8 @@ class LanguageModelService(BaseService):
37
38
  temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
38
39
  timeout: int = DEFAULT_COMPLETE_TIMEOUT,
39
40
  tools: Optional[list[LanguageModelTool]] = None,
41
+ structured_output_model: Optional[Type[BaseModel]] = None,
42
+ structured_output_enforce_schema: bool = False,
40
43
  other_options: Optional[dict] = None,
41
44
  ):
42
45
  """
@@ -48,17 +51,21 @@ class LanguageModelService(BaseService):
48
51
  temperature (float): The temperature value. Defaults to 0.
49
52
  timeout (int): The timeout value in milliseconds. Defaults to 240_000.
50
53
  tools (Optional[list[LanguageModelTool]]): The tools to use. Defaults to None.
54
+ structured_output_model (Optional[Type[BaseModel]]): The structured output model. Defaults to None.
55
+ structured_output_enforce_schema (bool): Whether to enforce the schema. Defaults to False.
51
56
  other_options (Optional[dict]): The other options to use. Defaults to None.
52
57
 
53
58
  Returns:
54
59
  LanguageModelResponse: The LanguageModelResponse object.
55
60
  """
56
- options, model, messages_dict, _ = self.prepare_completion_params_util(
61
+ options, model, messages_dict, _ = self._prepare_completion_params_util(
57
62
  messages=messages,
58
63
  model_name=model_name,
59
64
  temperature=temperature,
60
65
  tools=tools,
61
66
  other_options=other_options,
67
+ structured_output_model=structured_output_model,
68
+ structured_output_enforce_schema=structured_output_enforce_schema,
62
69
  )
63
70
 
64
71
  try:
@@ -77,9 +84,8 @@ class LanguageModelService(BaseService):
77
84
  self.logger.error(f"Error completing: {e}")
78
85
  raise e
79
86
 
80
- @classmethod
81
87
  async def complete_async_util(
82
- cls,
88
+ self,
83
89
  company_id: str,
84
90
  messages: LanguageModelMessages,
85
91
  model_name: LanguageModelName | str,
@@ -87,6 +93,8 @@ class LanguageModelService(BaseService):
87
93
  timeout: int = DEFAULT_COMPLETE_TIMEOUT,
88
94
  tools: Optional[list[LanguageModelTool]] = None,
89
95
  other_options: Optional[dict] = None,
96
+ structured_output_model: Optional[Type[BaseModel]] = None,
97
+ structured_output_enforce_schema: bool = False,
90
98
  logger: Optional[logging.Logger] = logging.getLogger(__name__),
91
99
  ) -> LanguageModelResponse:
92
100
  """
@@ -104,6 +112,8 @@ class LanguageModelService(BaseService):
104
112
  timeout (int): The timeout value in milliseconds for the request. Defaults to 240_000.
105
113
  tools (Optional[list[LanguageModelTool]]): Optional list of tools to include in the request.
106
114
  other_options (Optional[dict]): The other options to use. Defaults to None.
115
+ structured_output_model (Optional[Type[BaseModel]]): The structured output model. Defaults to None.
116
+ structured_output_enforce_schema (bool): Whether to enforce the schema. Defaults to False.
107
117
  logger (Optional[logging.Logger], optional): The logger used to log errors. Defaults to the logger for the current module.
108
118
 
109
119
  Returns:
@@ -112,12 +122,14 @@ class LanguageModelService(BaseService):
112
122
  Raises:
113
123
  Exception: If an error occurs during the request, an exception is raised and logged.
114
124
  """
115
- options, model, messages_dict, _ = cls.prepare_completion_params_util(
125
+ options, model, messages_dict, _ = self._prepare_completion_params_util(
116
126
  messages=messages,
117
127
  model_name=model_name,
118
128
  temperature=temperature,
119
129
  tools=tools,
120
130
  other_options=other_options,
131
+ structured_output_model=structured_output_model,
132
+ structured_output_enforce_schema=structured_output_enforce_schema,
121
133
  )
122
134
 
123
135
  try:
@@ -143,6 +155,8 @@ class LanguageModelService(BaseService):
143
155
  temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
144
156
  timeout: int = DEFAULT_COMPLETE_TIMEOUT,
145
157
  tools: Optional[list[LanguageModelTool]] = None,
158
+ structured_output_model: Optional[Type[BaseModel]] = None,
159
+ structured_output_enforce_schema: bool = False,
146
160
  other_options: Optional[dict] = None,
147
161
  ) -> LanguageModelResponse:
148
162
  """
@@ -158,6 +172,8 @@ class LanguageModelService(BaseService):
158
172
  temperature (float): The temperature setting for the completion. Defaults to 0.0.
159
173
  timeout (int): The timeout value in milliseconds for the request. Defaults to 240,000.
160
174
  tools (Optional[list[LanguageModelTool]]): Optional list of tools to include in the request.
175
+ structured_output_model (Optional[Type[BaseModel]]): The structured output model. Defaults to None.
176
+ structured_output_enforce_schema (bool): Whether to enforce the schema. Defaults to False.
161
177
  other_options (Optional[dict]): The other options to use. Defaults to None.
162
178
  Returns:
163
179
  LanguageModelResponse: The response object containing the completed result.
@@ -174,6 +190,8 @@ class LanguageModelService(BaseService):
174
190
  tools=tools,
175
191
  other_options=other_options,
176
192
  logger=self.logger,
193
+ structured_output_model=structured_output_model,
194
+ structured_output_enforce_schema=structured_output_enforce_schema,
177
195
  )
178
196
 
179
197
  def stream_complete(
@@ -205,7 +223,7 @@ class LanguageModelService(BaseService):
205
223
  The LanguageModelStreamResponse object once the stream has finished.
206
224
  """
207
225
  options, model, messages_dict, search_context = (
208
- self.prepare_completion_params_util(
226
+ self._prepare_completion_params_util(
209
227
  messages=messages,
210
228
  model_name=model_name,
211
229
  temperature=temperature,
@@ -268,7 +286,7 @@ class LanguageModelService(BaseService):
268
286
  The LanguageModelStreamResponse object once the stream has finished.
269
287
  """
270
288
  options, model, messages_dict, search_context = (
271
- self.prepare_completion_params_util(
289
+ self._prepare_completion_params_util(
272
290
  messages=messages,
273
291
  model_name=model_name,
274
292
  temperature=temperature,
@@ -335,15 +353,32 @@ class LanguageModelService(BaseService):
335
353
  ]
336
354
  return options
337
355
 
338
- @classmethod
339
- def prepare_completion_params_util(
340
- cls,
356
+ @staticmethod
357
+ def _add_response_format_to_options(
358
+ options: dict,
359
+ structured_output_model: Type[BaseModel],
360
+ structured_output_enforce_schema: bool = False,
361
+ ) -> dict:
362
+ options["responseFormat"] = {
363
+ "type": "json_schema",
364
+ "json_schema": {
365
+ "name": structured_output_model.__name__,
366
+ "strict": structured_output_enforce_schema,
367
+ "schema": structured_output_model.model_json_schema(),
368
+ },
369
+ }
370
+ return options
371
+
372
+ def _prepare_completion_params_util(
373
+ self,
341
374
  messages: LanguageModelMessages,
342
375
  model_name: LanguageModelName | str,
343
376
  temperature: float,
344
377
  tools: Optional[list[LanguageModelTool]] = None,
345
378
  other_options: Optional[dict] = None,
346
379
  content_chunks: Optional[list[ContentChunk]] = None,
380
+ structured_output_model: Optional[Type[BaseModel]] = None,
381
+ structured_output_enforce_schema: bool = False,
347
382
  ) -> tuple[dict, str, dict, Optional[dict]]:
348
383
  """
349
384
  Prepares common parameters for completion requests.
@@ -356,8 +391,15 @@ class LanguageModelService(BaseService):
356
391
  - search_context (Optional[dict]): Processed content chunks if provided
357
392
  """
358
393
 
359
- options = cls._add_tools_to_options({}, tools)
394
+ options = self._add_tools_to_options({}, tools)
395
+
396
+ if structured_output_model:
397
+ options = self._add_response_format_to_options(
398
+ options, structured_output_model, structured_output_enforce_schema
399
+ )
400
+
360
401
  options["temperature"] = temperature
402
+
361
403
  if other_options:
362
404
  options.update(other_options)
363
405
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: unique_toolkit
3
- Version: 0.5.49
3
+ Version: 0.5.51
4
4
  Summary:
5
5
  License: Proprietary
6
6
  Author: Martin Fadler
@@ -100,6 +100,12 @@ 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.51] - 2025-01-30
104
+ - Add missing structured output arguments in complete_async
105
+
106
+ ## [0.5.50] - 2025-01-30
107
+ - Add the possibility to define completion output structure through a pydantic class
108
+
103
109
  ## [0.5.49] - 2025-01-24
104
110
  - Add `parsed` and `refusal` to `LanguageModelAssistantMessage` to support structured output
105
111
 
@@ -40,11 +40,11 @@ unique_toolkit/language_model/builder.py,sha256=nsRqWO_2dgFehK5CgtqR5aqXgYUU0QL6
40
40
  unique_toolkit/language_model/infos.py,sha256=NgoV05ausVWMqrYqgH6i3s7tYG7mejupROIF_bwEGZo,13050
41
41
  unique_toolkit/language_model/prompt.py,sha256=JSawaLjQg3VR-E2fK8engFyJnNdk21zaO8pPIodzN4Q,3991
42
42
  unique_toolkit/language_model/schemas.py,sha256=87511yupvea-U6sfKWfelETevNMVPevhj7mEqX5FszU,7461
43
- unique_toolkit/language_model/service.py,sha256=brNCPRA0XxgqHi2rI5i2lyFCkUiw4MNMe1VaR3UgWmY,15500
43
+ unique_toolkit/language_model/service.py,sha256=MS6IDB69rUeNsWRj-qB491wbmiHzgVtnvsywmjnDwSQ,17808
44
44
  unique_toolkit/language_model/utils.py,sha256=bPQ4l6_YO71w-zaIPanUUmtbXC1_hCvLK0tAFc3VCRc,1902
45
45
  unique_toolkit/short_term_memory/schemas.py,sha256=OhfcXyF6ACdwIXW45sKzjtZX_gkcJs8FEZXcgQTNenw,1406
46
46
  unique_toolkit/short_term_memory/service.py,sha256=Jd9P72-VvJy7hnqNrjmrmB5BHmsKuOpTiT0Jr-dBbsQ,1682
47
- unique_toolkit-0.5.49.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
48
- unique_toolkit-0.5.49.dist-info/METADATA,sha256=y2wgnOvbhd2PaXa5wshMxjmQ-hNrkaKhqalKAtr5K4E,15819
49
- unique_toolkit-0.5.49.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
50
- unique_toolkit-0.5.49.dist-info/RECORD,,
47
+ unique_toolkit-0.5.51.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
48
+ unique_toolkit-0.5.51.dist-info/METADATA,sha256=8mPDfyWkYAk1k8lplSFdyCK61kDOZk4sdNWBwA8MzUw,16016
49
+ unique_toolkit-0.5.51.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
50
+ unique_toolkit-0.5.51.dist-info/RECORD,,