unique_toolkit 0.6.0__py3-none-any.whl → 0.6.1__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,6 +1,6 @@
1
1
  import logging
2
2
  import re
3
- from typing import Any, Dict, List
3
+ from typing import Any, Dict, List, cast
4
4
 
5
5
  import unique_sdk
6
6
  from unique_sdk._list_object import ListObject
@@ -15,8 +15,19 @@ from unique_toolkit.chat.schemas import (
15
15
  ChatMessageAssessmentType,
16
16
  ChatMessageRole,
17
17
  )
18
- from unique_toolkit.content.schemas import ContentReference
18
+ from unique_toolkit.content.schemas import ContentChunk, ContentReference
19
19
  from unique_toolkit.content.utils import count_tokens
20
+ from unique_toolkit.language_model.constants import (
21
+ DEFAULT_COMPLETE_TEMPERATURE,
22
+ DEFAULT_COMPLETE_TIMEOUT,
23
+ )
24
+ from unique_toolkit.language_model.functions import _prepare_completion_params_util
25
+ from unique_toolkit.language_model.infos import LanguageModelName
26
+ from unique_toolkit.language_model.schemas import (
27
+ LanguageModelMessages,
28
+ LanguageModelStreamResponse,
29
+ LanguageModelTool,
30
+ )
20
31
 
21
32
  logger = logging.getLogger(__name__)
22
33
 
@@ -659,3 +670,136 @@ async def modify_message_assessment_async(
659
670
  except Exception as e:
660
671
  logger.error(f"Failed to modify message assessment: {e}")
661
672
  raise e
673
+
674
+
675
+ def stream_complete_to_chat(
676
+ company_id: str,
677
+ user_id: str,
678
+ assistant_message_id: str,
679
+ user_message_id: str,
680
+ chat_id: str,
681
+ assistant_id: str,
682
+ messages: LanguageModelMessages,
683
+ model_name: LanguageModelName | str,
684
+ content_chunks: list[ContentChunk] = [],
685
+ debug_info: dict = {},
686
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
687
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
688
+ tools: list[LanguageModelTool] | None = None,
689
+ start_text: str | None = None,
690
+ other_options: dict | None = None,
691
+ ) -> LanguageModelStreamResponse:
692
+ """
693
+ Streams a completion synchronously.
694
+
695
+ Args:
696
+ company_id (str): The company ID associated with the request.
697
+ user_id (str): The user ID for the request.
698
+ assistant_message_id (str): The assistant message ID.
699
+ user_message_id (str): The user message ID.
700
+ chat_id (str): The chat ID.
701
+ assistant_id (str): The assistant ID.
702
+ messages (LanguageModelMessages): The messages to complete.
703
+ model_name (LanguageModelName | str): The model name.
704
+ content_chunks (list[ContentChunk]): Content chunks for context.
705
+ debug_info (dict): Debug information.
706
+ temperature (float): Temperature setting.
707
+ timeout (int): Timeout in milliseconds.
708
+ tools (Optional[list[LanguageModelTool]]): Optional tools.
709
+ start_text (Optional[str]): Starting text.
710
+ other_options (Optional[dict]): Additional options.
711
+
712
+ Returns:
713
+ LanguageModelStreamResponse: The streaming response object.
714
+ """
715
+ options, model, messages_dict, search_context = _prepare_completion_params_util(
716
+ messages=messages,
717
+ model_name=model_name,
718
+ temperature=temperature,
719
+ tools=tools,
720
+ other_options=other_options,
721
+ content_chunks=content_chunks,
722
+ )
723
+
724
+ try:
725
+ response = unique_sdk.Integrated.chat_stream_completion(
726
+ user_id=user_id,
727
+ company_id=company_id,
728
+ assistantMessageId=assistant_message_id,
729
+ userMessageId=user_message_id,
730
+ messages=cast(
731
+ list[unique_sdk.Integrated.ChatCompletionRequestMessage],
732
+ messages_dict,
733
+ ),
734
+ chatId=chat_id,
735
+ searchContext=search_context,
736
+ model=model,
737
+ timeout=timeout,
738
+ assistantId=assistant_id,
739
+ debugInfo=debug_info,
740
+ options=options, # type: ignore
741
+ startText=start_text,
742
+ )
743
+ return LanguageModelStreamResponse(**response)
744
+ except Exception as e:
745
+ logger.error(f"Error streaming completion: {e}")
746
+ raise e
747
+
748
+
749
+ async def stream_complete_to_chat_async(
750
+ company_id: str,
751
+ user_id: str,
752
+ assistant_message_id: str,
753
+ user_message_id: str,
754
+ chat_id: str,
755
+ assistant_id: str,
756
+ messages: LanguageModelMessages,
757
+ model_name: LanguageModelName | str,
758
+ content_chunks: list[ContentChunk] = [],
759
+ debug_info: dict = {},
760
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
761
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
762
+ tools: list[LanguageModelTool] | None = None,
763
+ start_text: str | None = None,
764
+ other_options: dict | None = None,
765
+ ) -> LanguageModelStreamResponse:
766
+ """
767
+ Streams a completion asynchronously.
768
+
769
+ Args: [same as stream_complete]
770
+
771
+ Returns:
772
+ LanguageModelStreamResponse: The streaming response object.
773
+ """
774
+ options, model, messages_dict, search_context = _prepare_completion_params_util(
775
+ messages=messages,
776
+ model_name=model_name,
777
+ temperature=temperature,
778
+ tools=tools,
779
+ other_options=other_options,
780
+ content_chunks=content_chunks,
781
+ )
782
+
783
+ try:
784
+ response = await unique_sdk.Integrated.chat_stream_completion_async(
785
+ user_id=user_id,
786
+ company_id=company_id,
787
+ assistantMessageId=assistant_message_id,
788
+ userMessageId=user_message_id,
789
+ messages=cast(
790
+ list[unique_sdk.Integrated.ChatCompletionRequestMessage],
791
+ messages_dict,
792
+ ),
793
+ chatId=chat_id,
794
+ searchContext=search_context,
795
+ model=model,
796
+ timeout=timeout,
797
+ assistantId=assistant_id,
798
+ debugInfo=debug_info,
799
+ options=options, # type: ignore
800
+ startText=start_text,
801
+ )
802
+ return LanguageModelStreamResponse(**response)
803
+ except Exception as e:
804
+ logger.error(f"Error streaming completion: {e}")
805
+ raise e
@@ -3,6 +3,7 @@ from typing import Optional
3
3
 
4
4
  from typing_extensions import deprecated
5
5
 
6
+ from unique_toolkit._common.validate_required_values import validate_required_values
6
7
  from unique_toolkit.app.schemas import ChatEvent, Event
7
8
  from unique_toolkit.chat.constants import (
8
9
  DEFAULT_MAX_MESSAGES,
@@ -30,7 +31,24 @@ from unique_toolkit.chat.schemas import (
30
31
  ChatMessageAssessmentType,
31
32
  ChatMessageRole,
32
33
  )
33
- from unique_toolkit.content.schemas import ContentReference
34
+ from unique_toolkit.content.schemas import ContentChunk, ContentReference
35
+ from unique_toolkit.language_model.constants import (
36
+ DEFAULT_COMPLETE_TEMPERATURE,
37
+ DEFAULT_COMPLETE_TIMEOUT,
38
+ )
39
+ from unique_toolkit.language_model.infos import (
40
+ LanguageModelName,
41
+ )
42
+ from unique_toolkit.language_model.schemas import (
43
+ LanguageModelMessages,
44
+ LanguageModelStreamResponse,
45
+ LanguageModelTool,
46
+ )
47
+
48
+ from .functions import (
49
+ stream_complete_to_chat,
50
+ stream_complete_to_chat_async,
51
+ )
34
52
 
35
53
  logger = logging.getLogger(f"toolkit.{DOMAIN_NAME}.{__name__}")
36
54
 
@@ -377,8 +395,8 @@ class ChatService:
377
395
  original_content: str | None = None,
378
396
  references: list[ContentReference] = [],
379
397
  debug_info: dict = {},
380
- set_completed_at: Optional[bool] = False,
381
- ):
398
+ set_completed_at: bool | None = False,
399
+ ) -> ChatMessage:
382
400
  """
383
401
  Creates a message in the chat session synchronously.
384
402
 
@@ -395,7 +413,7 @@ class ChatService:
395
413
  Raises:
396
414
  Exception: If the creation fails.
397
415
  """
398
- return create_message(
416
+ chat_message = create_message(
399
417
  user_id=self.user_id,
400
418
  company_id=self.company_id,
401
419
  chat_id=self.chat_id,
@@ -407,6 +425,9 @@ class ChatService:
407
425
  debug_info=debug_info,
408
426
  set_completed_at=set_completed_at,
409
427
  )
428
+ # Update the assistant message id
429
+ self.assistant_message_id = chat_message.id
430
+ return chat_message
410
431
 
411
432
  async def create_assistant_message_async(
412
433
  self,
@@ -414,8 +435,8 @@ class ChatService:
414
435
  original_content: str | None = None,
415
436
  references: list[ContentReference] = [],
416
437
  debug_info: dict = {},
417
- set_completed_at: Optional[bool] = False,
418
- ):
438
+ set_completed_at: bool | None = False,
439
+ ) -> ChatMessage:
419
440
  """
420
441
  Creates a message in the chat session asynchronously.
421
442
 
@@ -433,7 +454,7 @@ class ChatService:
433
454
  Exception: If the creation fails.
434
455
  """
435
456
 
436
- return await create_message_async(
457
+ chat_message = await create_message_async(
437
458
  user_id=self.user_id,
438
459
  company_id=self.company_id,
439
460
  chat_id=self.chat_id,
@@ -445,6 +466,90 @@ class ChatService:
445
466
  debug_info=debug_info,
446
467
  set_completed_at=set_completed_at,
447
468
  )
469
+ # Update the assistant message id
470
+ self.assistant_message_id = chat_message.id
471
+ return chat_message
472
+
473
+ def create_user_message(
474
+ self,
475
+ content: str,
476
+ original_content: str | None = None,
477
+ references: list[ContentReference] = [],
478
+ debug_info: dict = {},
479
+ set_completed_at: bool | None = False,
480
+ ) -> ChatMessage:
481
+ """
482
+ Creates a user message in the chat session synchronously.
483
+
484
+ Args:
485
+ content (str): The content for the message.
486
+ original_content (str, optional): The original content for the message.
487
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
488
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
489
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
490
+
491
+ Returns:
492
+ ChatMessage: The created message.
493
+
494
+ Raises:
495
+ Exception: If the creation fails.
496
+ """
497
+ chat_message = create_message(
498
+ user_id=self.user_id,
499
+ company_id=self.company_id,
500
+ chat_id=self.chat_id,
501
+ assistant_id=self.assistant_id,
502
+ role=ChatMessageRole.USER,
503
+ content=content,
504
+ original_content=original_content,
505
+ references=references,
506
+ debug_info=debug_info,
507
+ set_completed_at=set_completed_at,
508
+ )
509
+ # Update the user message id
510
+ self.user_message_id = chat_message.id
511
+ return chat_message
512
+
513
+ async def create_user_message_async(
514
+ self,
515
+ content: str,
516
+ original_content: str | None = None,
517
+ references: list[ContentReference] = [],
518
+ debug_info: dict = {},
519
+ set_completed_at: bool | None = False,
520
+ ) -> ChatMessage:
521
+ """
522
+ Creates a user message in the chat session asynchronously.
523
+
524
+ Args:
525
+ content (str): The content for the message.
526
+ original_content (str, optional): The original content for the message.
527
+ references (list[ContentReference]): list of references. Defaults to None.
528
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
529
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
530
+
531
+ Returns:
532
+ ChatMessage: The created message.
533
+
534
+ Raises:
535
+ Exception: If the creation fails.
536
+ """
537
+
538
+ chat_message = await create_message_async(
539
+ user_id=self.user_id,
540
+ company_id=self.company_id,
541
+ chat_id=self.chat_id,
542
+ assistant_id=self.assistant_id,
543
+ role=ChatMessageRole.USER,
544
+ content=content,
545
+ original_content=original_content,
546
+ references=references,
547
+ debug_info=debug_info,
548
+ set_completed_at=set_completed_at,
549
+ )
550
+ # Update the user message id
551
+ self.user_message_id = chat_message.id
552
+ return chat_message
448
553
 
449
554
  def create_message_assessment(
450
555
  self,
@@ -599,3 +704,106 @@ class ChatService:
599
704
  explanation=explanation,
600
705
  label=label,
601
706
  )
707
+
708
+ def stream_complete(
709
+ self,
710
+ messages: LanguageModelMessages,
711
+ model_name: LanguageModelName | str,
712
+ content_chunks: list[ContentChunk] = [],
713
+ debug_info: dict = {},
714
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
715
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
716
+ tools: Optional[list[LanguageModelTool]] = None,
717
+ start_text: Optional[str] = None,
718
+ other_options: Optional[dict] = None,
719
+ ) -> LanguageModelStreamResponse:
720
+ """
721
+ Streams a completion in the chat session synchronously.
722
+ """
723
+ [
724
+ company_id,
725
+ user_id,
726
+ assistant_message_id,
727
+ user_message_id,
728
+ chat_id,
729
+ assistant_id,
730
+ ] = validate_required_values(
731
+ [
732
+ self.company_id,
733
+ self.user_id,
734
+ self.assistant_message_id,
735
+ self.user_message_id,
736
+ self.chat_id,
737
+ self.assistant_id,
738
+ ]
739
+ )
740
+
741
+ return stream_complete_to_chat(
742
+ company_id=company_id,
743
+ user_id=user_id,
744
+ assistant_message_id=assistant_message_id,
745
+ user_message_id=user_message_id,
746
+ chat_id=chat_id,
747
+ assistant_id=assistant_id,
748
+ messages=messages,
749
+ model_name=model_name,
750
+ content_chunks=content_chunks,
751
+ debug_info=debug_info,
752
+ temperature=temperature,
753
+ timeout=timeout,
754
+ tools=tools,
755
+ start_text=start_text,
756
+ other_options=other_options,
757
+ )
758
+
759
+ async def stream_complete_async(
760
+ self,
761
+ messages: LanguageModelMessages,
762
+ model_name: LanguageModelName | str,
763
+ content_chunks: list[ContentChunk] = [],
764
+ debug_info: dict = {},
765
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
766
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
767
+ tools: Optional[list[LanguageModelTool]] = None,
768
+ start_text: Optional[str] = None,
769
+ other_options: Optional[dict] = None,
770
+ ) -> LanguageModelStreamResponse:
771
+ """
772
+ Streams a completion in the chat session asynchronously.
773
+ """
774
+
775
+ [
776
+ company_id,
777
+ user_id,
778
+ assistant_message_id,
779
+ user_message_id,
780
+ chat_id,
781
+ assistant_id,
782
+ ] = validate_required_values(
783
+ [
784
+ self.company_id,
785
+ self.user_id,
786
+ self.assistant_message_id,
787
+ self.user_message_id,
788
+ self.chat_id,
789
+ self.assistant_id,
790
+ ]
791
+ )
792
+
793
+ return await stream_complete_to_chat_async(
794
+ company_id=company_id,
795
+ user_id=user_id,
796
+ assistant_message_id=assistant_message_id,
797
+ user_message_id=user_message_id,
798
+ chat_id=chat_id,
799
+ assistant_id=assistant_id,
800
+ messages=messages,
801
+ model_name=model_name,
802
+ content_chunks=content_chunks,
803
+ debug_info=debug_info,
804
+ temperature=temperature,
805
+ timeout=timeout,
806
+ tools=tools,
807
+ start_text=start_text,
808
+ other_options=other_options,
809
+ )
@@ -1,20 +1,20 @@
1
1
  import logging
2
- from typing import Optional, Type, cast
2
+ from typing import Type, cast
3
3
 
4
4
  import unique_sdk
5
5
  from pydantic import BaseModel
6
6
 
7
7
  from unique_toolkit.content.schemas import ContentChunk
8
8
  from unique_toolkit.evaluators import DOMAIN_NAME
9
- from unique_toolkit.language_model.constants import (
9
+
10
+ from .constants import (
10
11
  DEFAULT_COMPLETE_TEMPERATURE,
11
12
  DEFAULT_COMPLETE_TIMEOUT,
12
13
  )
13
- from unique_toolkit.language_model.infos import LanguageModelName
14
- from unique_toolkit.language_model.schemas import (
14
+ from .infos import LanguageModelName
15
+ from .schemas import (
15
16
  LanguageModelMessages,
16
17
  LanguageModelResponse,
17
- LanguageModelStreamResponse,
18
18
  LanguageModelTool,
19
19
  )
20
20
 
@@ -27,9 +27,9 @@ def complete(
27
27
  model_name: LanguageModelName | str,
28
28
  temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
29
29
  timeout: int = DEFAULT_COMPLETE_TIMEOUT,
30
- tools: Optional[list[LanguageModelTool]] = None,
31
- other_options: Optional[dict] = None,
32
- structured_output_model: Optional[Type[BaseModel]] = None,
30
+ tools: list[LanguageModelTool] | None = None,
31
+ other_options: dict | None = None,
32
+ structured_output_model: Type[BaseModel] | None = None,
33
33
  structured_output_enforce_schema: bool = False,
34
34
  ) -> LanguageModelResponse:
35
35
  """
@@ -80,9 +80,9 @@ async def complete_async(
80
80
  model_name: LanguageModelName | str,
81
81
  temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
82
82
  timeout: int = DEFAULT_COMPLETE_TIMEOUT,
83
- tools: Optional[list[LanguageModelTool]] = None,
84
- other_options: Optional[dict] = None,
85
- structured_output_model: Optional[Type[BaseModel]] = None,
83
+ tools: list[LanguageModelTool] | None = None,
84
+ other_options: dict | None = None,
85
+ structured_output_model: Type[BaseModel] | None = None,
86
86
  structured_output_enforce_schema: bool = False,
87
87
  ) -> LanguageModelResponse:
88
88
  """
@@ -134,142 +134,9 @@ async def complete_async(
134
134
  raise e
135
135
 
136
136
 
137
- def stream_complete_to_chat(
138
- company_id: str,
139
- user_id: str,
140
- assistant_message_id: str,
141
- user_message_id: str,
142
- chat_id: str,
143
- assistant_id: str,
144
- messages: LanguageModelMessages,
145
- model_name: LanguageModelName | str,
146
- content_chunks: list[ContentChunk] = [],
147
- debug_info: dict = {},
148
- temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
149
- timeout: int = DEFAULT_COMPLETE_TIMEOUT,
150
- tools: Optional[list[LanguageModelTool]] = None,
151
- start_text: Optional[str] = None,
152
- other_options: Optional[dict] = None,
153
- ) -> LanguageModelStreamResponse:
154
- """
155
- Streams a completion synchronously.
156
-
157
- Args:
158
- company_id (str): The company ID associated with the request.
159
- user_id (str): The user ID for the request.
160
- assistant_message_id (str): The assistant message ID.
161
- user_message_id (str): The user message ID.
162
- chat_id (str): The chat ID.
163
- assistant_id (str): The assistant ID.
164
- messages (LanguageModelMessages): The messages to complete.
165
- model_name (LanguageModelName | str): The model name.
166
- content_chunks (list[ContentChunk]): Content chunks for context.
167
- debug_info (dict): Debug information.
168
- temperature (float): Temperature setting.
169
- timeout (int): Timeout in milliseconds.
170
- tools (Optional[list[LanguageModelTool]]): Optional tools.
171
- start_text (Optional[str]): Starting text.
172
- other_options (Optional[dict]): Additional options.
173
-
174
- Returns:
175
- LanguageModelStreamResponse: The streaming response object.
176
- """
177
- options, model, messages_dict, search_context = _prepare_completion_params_util(
178
- messages=messages,
179
- model_name=model_name,
180
- temperature=temperature,
181
- tools=tools,
182
- other_options=other_options,
183
- content_chunks=content_chunks,
184
- )
185
-
186
- try:
187
- response = unique_sdk.Integrated.chat_stream_completion(
188
- user_id=user_id,
189
- company_id=company_id,
190
- assistantMessageId=assistant_message_id,
191
- userMessageId=user_message_id,
192
- messages=cast(
193
- list[unique_sdk.Integrated.ChatCompletionRequestMessage],
194
- messages_dict,
195
- ),
196
- chatId=chat_id,
197
- searchContext=search_context,
198
- model=model,
199
- timeout=timeout,
200
- assistantId=assistant_id,
201
- debugInfo=debug_info,
202
- options=options, # type: ignore
203
- startText=start_text,
204
- )
205
- return LanguageModelStreamResponse(**response)
206
- except Exception as e:
207
- logger.error(f"Error streaming completion: {e}")
208
- raise e
209
-
210
-
211
- async def stream_complete_to_chat_async(
212
- company_id: str,
213
- user_id: str,
214
- assistant_message_id: str,
215
- user_message_id: str,
216
- chat_id: str,
217
- assistant_id: str,
218
- messages: LanguageModelMessages,
219
- model_name: LanguageModelName | str,
220
- content_chunks: list[ContentChunk] = [],
221
- debug_info: dict = {},
222
- temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
223
- timeout: int = DEFAULT_COMPLETE_TIMEOUT,
224
- tools: Optional[list[LanguageModelTool]] = None,
225
- start_text: Optional[str] = None,
226
- other_options: Optional[dict] = None,
227
- ) -> LanguageModelStreamResponse:
228
- """
229
- Streams a completion asynchronously.
230
-
231
- Args: [same as stream_complete]
232
-
233
- Returns:
234
- LanguageModelStreamResponse: The streaming response object.
235
- """
236
- options, model, messages_dict, search_context = _prepare_completion_params_util(
237
- messages=messages,
238
- model_name=model_name,
239
- temperature=temperature,
240
- tools=tools,
241
- other_options=other_options,
242
- content_chunks=content_chunks,
243
- )
244
-
245
- try:
246
- response = await unique_sdk.Integrated.chat_stream_completion_async(
247
- user_id=user_id,
248
- company_id=company_id,
249
- assistantMessageId=assistant_message_id,
250
- userMessageId=user_message_id,
251
- messages=cast(
252
- list[unique_sdk.Integrated.ChatCompletionRequestMessage],
253
- messages_dict,
254
- ),
255
- chatId=chat_id,
256
- searchContext=search_context,
257
- model=model,
258
- timeout=timeout,
259
- assistantId=assistant_id,
260
- debugInfo=debug_info,
261
- options=options, # type: ignore
262
- startText=start_text,
263
- )
264
- return LanguageModelStreamResponse(**response)
265
- except Exception as e:
266
- logger.error(f"Error streaming completion: {e}")
267
- raise e
268
-
269
-
270
137
  def _add_tools_to_options(
271
138
  options: dict,
272
- tools: Optional[list[LanguageModelTool]],
139
+ tools: list[LanguageModelTool] | None,
273
140
  ) -> dict:
274
141
  if tools:
275
142
  options["tools"] = [
@@ -321,12 +188,12 @@ def _prepare_completion_params_util(
321
188
  messages: LanguageModelMessages,
322
189
  model_name: LanguageModelName | str,
323
190
  temperature: float,
324
- tools: Optional[list[LanguageModelTool]] = None,
325
- other_options: Optional[dict] = None,
326
- content_chunks: Optional[list[ContentChunk]] = None,
327
- structured_output_model: Optional[Type[BaseModel]] = None,
191
+ tools: list[LanguageModelTool] | None = None,
192
+ other_options: dict | None = None,
193
+ content_chunks: list[ContentChunk] | None = None,
194
+ structured_output_model: Type[BaseModel] | None = None,
328
195
  structured_output_enforce_schema: bool = False,
329
- ) -> tuple[dict, str, dict, Optional[dict]]:
196
+ ) -> tuple[dict, str, dict, dict | None]:
330
197
  """
331
198
  Prepares common parameters for completion requests.
332
199
 
@@ -335,7 +202,7 @@ def _prepare_completion_params_util(
335
202
  - options (dict): Combined options including tools and temperature
336
203
  - model (str): Resolved model name
337
204
  - messages_dict (dict): Processed messages
338
- - search_context (Optional[dict]): Processed content chunks if provided
205
+ - search_context (dict | None): Processed content chunks if provided
339
206
  """
340
207
 
341
208
  options = _add_tools_to_options({}, tools)
@@ -6,7 +6,6 @@ from typing_extensions import deprecated
6
6
 
7
7
  from unique_toolkit._common.validate_required_values import validate_required_values
8
8
  from unique_toolkit.app.schemas import BaseEvent, ChatEvent, Event
9
- from unique_toolkit.content.schemas import ContentChunk
10
9
  from unique_toolkit.language_model.constants import (
11
10
  DEFAULT_COMPLETE_TEMPERATURE,
12
11
  DEFAULT_COMPLETE_TIMEOUT,
@@ -15,14 +14,11 @@ from unique_toolkit.language_model.constants import (
15
14
  from unique_toolkit.language_model.functions import (
16
15
  complete,
17
16
  complete_async,
18
- stream_complete_to_chat,
19
- stream_complete_to_chat_async,
20
17
  )
21
18
  from unique_toolkit.language_model.infos import LanguageModelName
22
19
  from unique_toolkit.language_model.schemas import (
23
20
  LanguageModelMessages,
24
21
  LanguageModelResponse,
25
- LanguageModelStreamResponse,
26
22
  LanguageModelTool,
27
23
  )
28
24
 
@@ -36,10 +32,8 @@ class LanguageModelService:
36
32
  Args:
37
33
  company_id (str | None, optional): The company identifier. Defaults to None.
38
34
  user_id (str | None, optional): The user identifier. Defaults to None.
39
- assistant_message_id (str | None, optional): The assistant message identifier. Defaults to None.
40
35
  chat_id (str | None, optional): The chat identifier. Defaults to None.
41
36
  assistant_id (str | None, optional): The assistant identifier. Defaults to None.
42
- user_message_id (str | None, optional): The user message identifier. Defaults to None.
43
37
  """
44
38
 
45
39
  def __init__(
@@ -47,16 +41,12 @@ class LanguageModelService:
47
41
  event: Event | BaseEvent | None = None,
48
42
  company_id: str | None = None,
49
43
  user_id: str | None = None,
50
- assistant_message_id: str | None = None,
51
44
  chat_id: str | None = None,
52
45
  assistant_id: str | None = None,
53
- user_message_id: str | None = None,
54
46
  ):
55
47
  self._event = event
56
48
  self.company_id = company_id
57
49
  self.user_id = user_id
58
- self.assistant_message_id = assistant_message_id
59
- self.user_message_id = user_message_id
60
50
  self.chat_id = chat_id
61
51
  self.assistant_id = assistant_id
62
52
 
@@ -64,8 +54,6 @@ class LanguageModelService:
64
54
  self.company_id = event.company_id
65
55
  self.user_id = event.user_id
66
56
  if isinstance(event, (ChatEvent, Event)):
67
- self.assistant_message_id = event.payload.assistant_message.id
68
- self.user_message_id = event.payload.user_message.id
69
57
  self.chat_id = event.payload.chat_id
70
58
  self.assistant_id = event.payload.assistant_id
71
59
 
@@ -167,211 +155,3 @@ class LanguageModelService:
167
155
  structured_output_model=structured_output_model,
168
156
  structured_output_enforce_schema=structured_output_enforce_schema,
169
157
  )
170
-
171
- @deprecated("Use stream_complete_to_chat instead")
172
- def stream_complete(
173
- self,
174
- messages: LanguageModelMessages,
175
- model_name: LanguageModelName | str,
176
- content_chunks: list[ContentChunk] = [],
177
- debug_info: dict = {},
178
- temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
179
- timeout: int = DEFAULT_COMPLETE_TIMEOUT,
180
- tools: Optional[list[LanguageModelTool]] = None,
181
- start_text: Optional[str] = None,
182
- other_options: Optional[dict] = None,
183
- ) -> LanguageModelStreamResponse:
184
- """
185
- Streams a completion in the chat session synchronously.
186
- """
187
- [
188
- company_id,
189
- user_id,
190
- assistant_message_id,
191
- user_message_id,
192
- chat_id,
193
- assistant_id,
194
- ] = validate_required_values(
195
- [
196
- self.company_id,
197
- self.user_id,
198
- self.assistant_message_id,
199
- self.user_message_id,
200
- self.chat_id,
201
- self.assistant_id,
202
- ]
203
- )
204
-
205
- return stream_complete_to_chat(
206
- company_id=company_id,
207
- user_id=user_id,
208
- assistant_message_id=assistant_message_id,
209
- user_message_id=user_message_id,
210
- chat_id=chat_id,
211
- assistant_id=assistant_id,
212
- messages=messages,
213
- model_name=model_name,
214
- content_chunks=content_chunks,
215
- debug_info=debug_info,
216
- temperature=temperature,
217
- timeout=timeout,
218
- tools=tools,
219
- start_text=start_text,
220
- other_options=other_options,
221
- )
222
-
223
- def stream_complete_to_chat(
224
- self,
225
- messages: LanguageModelMessages,
226
- model_name: LanguageModelName | str,
227
- content_chunks: list[ContentChunk] = [],
228
- debug_info: dict = {},
229
- temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
230
- timeout: int = DEFAULT_COMPLETE_TIMEOUT,
231
- tools: Optional[list[LanguageModelTool]] = None,
232
- start_text: Optional[str] = None,
233
- other_options: Optional[dict] = None,
234
- ) -> LanguageModelStreamResponse:
235
- """
236
- Streams a completion in the chat session synchronously.
237
- """
238
- [
239
- company_id,
240
- user_id,
241
- assistant_message_id,
242
- user_message_id,
243
- chat_id,
244
- assistant_id,
245
- ] = validate_required_values(
246
- [
247
- self.company_id,
248
- self.user_id,
249
- self.assistant_message_id,
250
- self.user_message_id,
251
- self.chat_id,
252
- self.assistant_id,
253
- ]
254
- )
255
-
256
- return stream_complete_to_chat(
257
- company_id=company_id,
258
- user_id=user_id,
259
- assistant_message_id=assistant_message_id,
260
- user_message_id=user_message_id,
261
- chat_id=chat_id,
262
- assistant_id=assistant_id,
263
- messages=messages,
264
- model_name=model_name,
265
- content_chunks=content_chunks,
266
- debug_info=debug_info,
267
- temperature=temperature,
268
- timeout=timeout,
269
- tools=tools,
270
- start_text=start_text,
271
- other_options=other_options,
272
- )
273
-
274
- @deprecated("Use stream_complete_to_chat_async instead")
275
- async def stream_complete_async(
276
- self,
277
- messages: LanguageModelMessages,
278
- model_name: LanguageModelName | str,
279
- content_chunks: list[ContentChunk] = [],
280
- debug_info: dict = {},
281
- temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
282
- timeout: int = DEFAULT_COMPLETE_TIMEOUT,
283
- tools: Optional[list[LanguageModelTool]] = None,
284
- start_text: Optional[str] = None,
285
- other_options: Optional[dict] = None,
286
- ) -> LanguageModelStreamResponse:
287
- """
288
- Streams a completion in the chat session asynchronously.
289
- """
290
-
291
- [
292
- company_id,
293
- user_id,
294
- assistant_message_id,
295
- user_message_id,
296
- chat_id,
297
- assistant_id,
298
- ] = validate_required_values(
299
- [
300
- self.company_id,
301
- self.user_id,
302
- self.assistant_message_id,
303
- self.user_message_id,
304
- self.chat_id,
305
- self.assistant_id,
306
- ]
307
- )
308
-
309
- return await stream_complete_to_chat_async(
310
- company_id=company_id,
311
- user_id=user_id,
312
- assistant_message_id=assistant_message_id,
313
- user_message_id=user_message_id,
314
- chat_id=chat_id,
315
- assistant_id=assistant_id,
316
- messages=messages,
317
- model_name=model_name,
318
- content_chunks=content_chunks,
319
- debug_info=debug_info,
320
- temperature=temperature,
321
- timeout=timeout,
322
- tools=tools,
323
- start_text=start_text,
324
- other_options=other_options,
325
- )
326
-
327
- async def stream_complete_to_chat_async(
328
- self,
329
- messages: LanguageModelMessages,
330
- model_name: LanguageModelName | str,
331
- content_chunks: list[ContentChunk] = [],
332
- debug_info: dict = {},
333
- temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
334
- timeout: int = DEFAULT_COMPLETE_TIMEOUT,
335
- tools: Optional[list[LanguageModelTool]] = None,
336
- start_text: Optional[str] = None,
337
- other_options: Optional[dict] = None,
338
- ) -> LanguageModelStreamResponse:
339
- """
340
- Streams a completion in the chat session asynchronously.
341
- """
342
-
343
- [
344
- company_id,
345
- user_id,
346
- assistant_message_id,
347
- user_message_id,
348
- chat_id,
349
- assistant_id,
350
- ] = validate_required_values(
351
- [
352
- self.company_id,
353
- self.user_id,
354
- self.assistant_message_id,
355
- self.user_message_id,
356
- self.chat_id,
357
- self.assistant_id,
358
- ]
359
- )
360
-
361
- return await stream_complete_to_chat_async(
362
- company_id=company_id,
363
- user_id=user_id,
364
- assistant_message_id=assistant_message_id,
365
- user_message_id=user_message_id,
366
- chat_id=chat_id,
367
- assistant_id=assistant_id,
368
- messages=messages,
369
- model_name=model_name,
370
- content_chunks=content_chunks,
371
- debug_info=debug_info,
372
- temperature=temperature,
373
- timeout=timeout,
374
- tools=tools,
375
- start_text=start_text,
376
- other_options=other_options,
377
- )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: unique_toolkit
3
- Version: 0.6.0
3
+ Version: 0.6.1
4
4
  Summary:
5
5
  License: Proprietary
6
6
  Author: Martin Fadler
@@ -55,7 +55,7 @@ The `unique_toolkit.app` module encompasses functions for initializing and secur
55
55
  The `unique_toolkit.chat` module encompasses all chat related functionality.
56
56
 
57
57
  - `functions.py` comprises the functions to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message.
58
- - `service.py` comprises the ChatService and provides an interface to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message.
58
+ - `service.py` comprises the ChatService and provides an interface to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message and stream complete.
59
59
  - `schemas.py` comprises all relevant schemas, e.g., ChatMessage, used in the ChatService.
60
60
  - `utils.py` comprises utility functions to use and convert ChatMessage objects in assistants, e.g., convert_chat_history_to_injectable_string converts the chat history to a string that can be injected into a prompt.
61
61
 
@@ -83,7 +83,7 @@ Unique platform.
83
83
 
84
84
  - `infos.py` comprises the information on all language models deployed through the Unique platform. We recommend to use the LanguageModel class, initialized with the LanguageModelName, e.g., LanguageModel(LanguageModelName.AZURE_GPT_35_TURBO_0125) to get the information on the specific language model like the name, version, token limits or retirement date.
85
85
  - `functions.py` comprises the functions to complete and stream complete to chat.
86
- - `service.py` comprises the LanguageModelService and provides an interface to interact with the language models, e.g., complete or stream_complete.
86
+ - `service.py` comprises the LanguageModelService and provides an interface to interact with the language models, e.g., complete.
87
87
  - `schemas.py` comprises all relevant schemas, e.g., LanguageModelResponse, used in the LanguageModelService.
88
88
  - `utils.py` comprises utility functions to parse the output of the language model, e.g., convert_string_to_json finds and parses the last json object in a string.
89
89
 
@@ -111,6 +111,10 @@ All notable changes to this project will be documented in this file.
111
111
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
112
112
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
113
113
 
114
+ ## [0.6.1] - 2025-02-25
115
+ - [BREAKING] `LanguageModelService.stream_complete` and `LanguageModelService.stream_complete_async` are now moved to `ChatService.stream_complete` and `ChatService.stream_complete_async`. Correspondingly `assistant_message_id` and `user_message_id` are removed from `LanguageModelService`.
116
+ - Add `create_user_message` and `create_user_message_async` to `ChatService` (similar to `create_assistant_message` and `create_assistant_message_async`)
117
+
114
118
  ## [0.6.0] - 2025-02-21
115
119
  - make for each domain, its base functionality accessible from `functions.py`
116
120
  - make it possible to instantiate the domain services directly from different event types, inhereted from common `BaseEvent`
@@ -13,9 +13,9 @@ unique_toolkit/app/schemas.py,sha256=hPOh5xLNNWgWVIkdrj6ZHYaGz0cTV-5Kv7OQHOaUgV8
13
13
  unique_toolkit/app/verification.py,sha256=mffa6wm0i4hJbwzofePrkaia46xumMzECwQ0T3eKAx0,1929
14
14
  unique_toolkit/chat/__init__.py,sha256=LRs2G-JTVuci4lbtHTkVUiNcZcSR6uqqfnAyo7af6nY,619
15
15
  unique_toolkit/chat/constants.py,sha256=05kq6zjqUVB2d6_P7s-90nbljpB3ryxwCI-CAz0r2O4,83
16
- unique_toolkit/chat/functions.py,sha256=CtlvrwYWUQ7_wSQyAXf8jnARv0P-Ffh1gx2JJzISvvI,22231
16
+ unique_toolkit/chat/functions.py,sha256=J9Cmgkhj9bBxZja3ggkSp48af_LPU4Dfi9Sbc_WhhNY,27204
17
17
  unique_toolkit/chat/schemas.py,sha256=MNcGAXjK1K8zOODeMFz3FHVQL5sIBQXRwkr_2hFkG8k,2672
18
- unique_toolkit/chat/service.py,sha256=abBHpPonQYA408c_Z-WNmnROzMmYNUL55D6ek3iDubI,22048
18
+ unique_toolkit/chat/service.py,sha256=SoFeaOi0BTexhiyX6busui_7JjhlRu30YNiKdgwV3JQ,29127
19
19
  unique_toolkit/chat/state.py,sha256=Cjgwv_2vhDFbV69xxsn7SefhaoIAEqLx3ferdVFCnOg,1445
20
20
  unique_toolkit/chat/utils.py,sha256=ihm-wQykBWhB4liR3LnwPVPt_qGW6ETq21Mw4HY0THE,854
21
21
  unique_toolkit/content/__init__.py,sha256=EdJg_A_7loEtCQf4cah3QARQreJx6pdz89Rm96YbMVg,940
@@ -47,18 +47,18 @@ unique_toolkit/evaluators/schemas.py,sha256=Jaue6Uhx75X1CyHKWj8sT3RE1JZXTqoLtfLt
47
47
  unique_toolkit/language_model/__init__.py,sha256=jWko_vQj48wjnpTtlkg8iNdef0SMI3FN2kGywXRTMzg,1880
48
48
  unique_toolkit/language_model/builder.py,sha256=nsRqWO_2dgFehK5CgtqR5aqXgYUU0QL6mR0lALPrQXM,1898
49
49
  unique_toolkit/language_model/constants.py,sha256=B-topqW0r83dkC_25DeQfnPk3n53qzIHUCBS7YJ0-1U,119
50
- unique_toolkit/language_model/functions.py,sha256=Gvf7nYwfNvkER59KVGursOvvZj5uYBFgxQyG9fHRBJQ,12677
50
+ unique_toolkit/language_model/functions.py,sha256=I5jHhHsKoq7GwEQyTrM8LXB2n_6dvMAk7UklenjuHSY,7945
51
51
  unique_toolkit/language_model/infos.py,sha256=NgoV05ausVWMqrYqgH6i3s7tYG7mejupROIF_bwEGZo,13050
52
52
  unique_toolkit/language_model/prompt.py,sha256=JSawaLjQg3VR-E2fK8engFyJnNdk21zaO8pPIodzN4Q,3991
53
53
  unique_toolkit/language_model/schemas.py,sha256=87511yupvea-U6sfKWfelETevNMVPevhj7mEqX5FszU,7461
54
- unique_toolkit/language_model/service.py,sha256=IQamCLVL-k4-YifgGpwhvlPwZ81EQRpBI6eMK176ciU,12967
54
+ unique_toolkit/language_model/service.py,sha256=GupYD4uDZjy1TfVQW3jichmgQwiSgQCj350FtL4O0W4,5569
55
55
  unique_toolkit/language_model/utils.py,sha256=bPQ4l6_YO71w-zaIPanUUmtbXC1_hCvLK0tAFc3VCRc,1902
56
56
  unique_toolkit/short_term_memory/__init__.py,sha256=2mI3AUrffgH7Yt-xS57EGqnHf7jnn6xquoKEhJqk3Wg,185
57
57
  unique_toolkit/short_term_memory/constants.py,sha256=698CL6-wjup2MvU19RxSmQk3gX7aqW_OOpZB7sbz_Xg,34
58
58
  unique_toolkit/short_term_memory/functions.py,sha256=3WiK-xatY5nh4Dr5zlDUye1k3E6kr41RiscwtTplw5k,4484
59
59
  unique_toolkit/short_term_memory/schemas.py,sha256=OhfcXyF6ACdwIXW45sKzjtZX_gkcJs8FEZXcgQTNenw,1406
60
60
  unique_toolkit/short_term_memory/service.py,sha256=gdsVzoNqTXmLoBR_-p_lJlZDBo8L7Cr5EKchTNVJg1Q,5233
61
- unique_toolkit-0.6.0.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
62
- unique_toolkit-0.6.0.dist-info/METADATA,sha256=glSKhyn4h1sfvzWEDmJ1WBYYtXVwCs823B5HACP_ZJY,18600
63
- unique_toolkit-0.6.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
64
- unique_toolkit-0.6.0.dist-info/RECORD,,
61
+ unique_toolkit-0.6.1.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
62
+ unique_toolkit-0.6.1.dist-info/METADATA,sha256=HE5Tg7WWNCSsaU4O0VirvkILqvxRdHtNnfsbbu_S3Z8,19071
63
+ unique_toolkit-0.6.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
64
+ unique_toolkit-0.6.1.dist-info/RECORD,,