unique_toolkit 0.7.13__py3-none-any.whl → 0.7.17__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,8 +1,57 @@
1
- from unique_toolkit.language_model import LanguageModel, LanguageModelName
1
+ from typing import Annotated
2
2
 
3
+ from pydantic import BeforeValidator, PlainSerializer
3
4
 
4
- def validate_and_init_language_model(value: LanguageModelName | LanguageModel | str):
5
- if isinstance(value, LanguageModel):
6
- return value
5
+ from unique_toolkit.language_model import LanguageModelName
6
+ from unique_toolkit.language_model.infos import (
7
+ LanguageModelInfo,
8
+ LanguageModelProvider,
9
+ )
7
10
 
8
- return LanguageModel(value)
11
+ # TODO @klcd: Inform on deprecation of str as input
12
+ LMI = Annotated[
13
+ LanguageModelInfo,
14
+ BeforeValidator(
15
+ lambda v: validate_and_init_language_model_info(v),
16
+ json_schema_input_type=str | LanguageModelName | LanguageModelInfo,
17
+ ),
18
+ PlainSerializer(
19
+ lambda v: serialize_lmi(v),
20
+ when_used="json",
21
+ return_type=str | LanguageModelInfo,
22
+ ),
23
+ ]
24
+
25
+
26
+ def serialize_lmi(model: LanguageModelInfo) -> str | LanguageModelInfo:
27
+ if model.provider == LanguageModelProvider.CUSTOM:
28
+ return model
29
+
30
+ return model.name
31
+
32
+
33
+ def validate_and_init_language_model_info(
34
+ v: str | LanguageModelName | LanguageModelInfo,
35
+ ) -> LanguageModelInfo:
36
+ """Validate and initialize a LanguageModelInfo object.
37
+
38
+ Args:
39
+ v: The input value to validate and initialize.
40
+
41
+ Returns:
42
+ LanguageModelInfo: The validated and initialized LanguageModelInfo object.
43
+
44
+ """
45
+ if isinstance(v, LanguageModelName):
46
+ return LanguageModelInfo.from_name(v)
47
+ if isinstance(v, str):
48
+ if v in [name.value for name in LanguageModelName]:
49
+ return LanguageModelInfo.from_name(LanguageModelName(v))
50
+
51
+ return LanguageModelInfo(
52
+ name=v,
53
+ version="custom",
54
+ provider=LanguageModelProvider.CUSTOM,
55
+ )
56
+
57
+ return v
@@ -109,31 +109,26 @@ class ChatEventPayload(BaseModel):
109
109
  @deprecated("""Use `ChatEventPayload` instead.
110
110
  This class will be removed in the next major version.""")
111
111
  class EventPayload(ChatEventPayload):
112
- user_message: EventUserMessage
113
- assistant_message: EventAssistantMessage
114
- additional_parameters: Optional[EventAdditionalParameters] = None
115
-
116
-
117
- @deprecated(
118
- """Use the more specific `ChatEvent` instead that has the same properties. \
119
- This class will be removed in the next major version."""
120
- )
121
- class Event(BaseModel):
122
- model_config = model_config
123
-
124
- id: str
125
- event: EventName
126
- user_id: str
127
- company_id: str
128
- payload: EventPayload
129
- created_at: Optional[int] = None
130
- version: Optional[str] = None
112
+ pass
113
+ # user_message: EventUserMessage
114
+ # assistant_message: EventAssistantMessage
115
+ # additional_parameters: Optional[EventAdditionalParameters] = None
131
116
 
132
117
 
133
118
  class ChatEvent(BaseEvent):
134
119
  model_config = model_config
135
120
 
136
- event: EventName
137
121
  payload: ChatEventPayload
138
122
  created_at: Optional[int] = None
139
123
  version: Optional[str] = None
124
+
125
+
126
+ @deprecated(
127
+ """Use the more specific `ChatEvent` instead that has the same properties. \
128
+ This class will be removed in the next major version."""
129
+ )
130
+ class Event(ChatEvent):
131
+ pass
132
+ # The below should only affect type hints
133
+ # event: EventName T
134
+ # payload: EventPayload
@@ -41,6 +41,7 @@ from unique_toolkit.language_model.infos import (
41
41
  )
42
42
  from unique_toolkit.language_model.schemas import (
43
43
  LanguageModelMessages,
44
+ LanguageModelResponse,
44
45
  LanguageModelStreamResponse,
45
46
  LanguageModelTool,
46
47
  )
@@ -81,7 +82,7 @@ class ChatService:
81
82
  @deprecated(
82
83
  "The event property is deprecated and will be removed in a future version."
83
84
  )
84
- def event(self) -> Event | ChatEvent | None:
85
+ def event(self) -> Event | ChatEvent:
85
86
  """
86
87
  Get the event object (deprecated).
87
88
 
@@ -279,14 +280,30 @@ class ChatService:
279
280
  Args:
280
281
  debug_info (dict): The new debug information.
281
282
  """
282
-
283
+ [
284
+ company_id,
285
+ user_id,
286
+ assistant_message_id,
287
+ user_message_id,
288
+ chat_id,
289
+ user_message_text,
290
+ ] = validate_required_values(
291
+ [
292
+ self._company_id,
293
+ self._user_id,
294
+ self._assistant_message_id,
295
+ self._user_message_id,
296
+ self._chat_id,
297
+ self._user_message_text,
298
+ ]
299
+ )
283
300
  return await modify_message_async(
284
- user_id=self._user_id,
285
- company_id=self._company_id,
286
- assistant_message_id=self._assistant_message_id,
287
- chat_id=self._chat_id,
288
- user_message_id=self._user_message_id,
289
- user_message_text=self._user_message_text,
301
+ user_id=user_id,
302
+ company_id=company_id,
303
+ assistant_message_id=assistant_message_id,
304
+ chat_id=chat_id,
305
+ user_message_id=user_message_id,
306
+ user_message_text=user_message_text,
290
307
  assistant=False,
291
308
  debug_info=debug_info,
292
309
  )
@@ -298,14 +315,31 @@ class ChatService:
298
315
  Args:
299
316
  debug_info (dict): The new debug information.
300
317
  """
318
+ [
319
+ company_id,
320
+ user_id,
321
+ assistant_message_id,
322
+ user_message_id,
323
+ chat_id,
324
+ user_message_text,
325
+ ] = validate_required_values(
326
+ [
327
+ self._company_id,
328
+ self._user_id,
329
+ self._assistant_message_id,
330
+ self._user_message_id,
331
+ self._chat_id,
332
+ self._user_message_text,
333
+ ]
334
+ )
301
335
 
302
336
  return modify_message(
303
- user_id=self._user_id,
304
- company_id=self._company_id,
305
- assistant_message_id=self._assistant_message_id,
306
- chat_id=self._chat_id,
307
- user_message_id=self._user_message_id,
308
- user_message_text=self._user_message_text,
337
+ user_id=user_id,
338
+ company_id=company_id,
339
+ assistant_message_id=assistant_message_id,
340
+ chat_id=chat_id,
341
+ user_message_id=user_message_id,
342
+ user_message_text=user_message_text,
309
343
  assistant=False,
310
344
  debug_info=debug_info,
311
345
  )
@@ -334,13 +368,31 @@ class ChatService:
334
368
  Raises:
335
369
  Exception: If the modification fails.
336
370
  """
371
+ [
372
+ company_id,
373
+ user_id,
374
+ assistant_message_id,
375
+ user_message_id,
376
+ chat_id,
377
+ user_message_text,
378
+ ] = validate_required_values(
379
+ [
380
+ self._company_id,
381
+ self._user_id,
382
+ self._assistant_message_id,
383
+ self._user_message_id,
384
+ self._chat_id,
385
+ self._user_message_text,
386
+ ]
387
+ )
388
+
337
389
  return modify_message(
338
- user_id=self._user_id,
339
- company_id=self._company_id,
340
- assistant_message_id=self._assistant_message_id,
341
- chat_id=self._chat_id,
342
- user_message_id=self._user_message_id,
343
- user_message_text=self._user_message_text,
390
+ user_id=user_id,
391
+ company_id=company_id,
392
+ assistant_message_id=assistant_message_id,
393
+ chat_id=chat_id,
394
+ user_message_id=user_message_id,
395
+ user_message_text=user_message_text,
344
396
  assistant=False,
345
397
  content=content,
346
398
  references=references,
@@ -373,13 +425,32 @@ class ChatService:
373
425
  Raises:
374
426
  Exception: If the modification fails.
375
427
  """
428
+
429
+ [
430
+ company_id,
431
+ user_id,
432
+ assistant_message_id,
433
+ user_message_id,
434
+ chat_id,
435
+ user_message_text,
436
+ ] = validate_required_values(
437
+ [
438
+ self._company_id,
439
+ self._user_id,
440
+ self._assistant_message_id,
441
+ self._user_message_id,
442
+ self._chat_id,
443
+ self._user_message_text,
444
+ ]
445
+ )
446
+
376
447
  return await modify_message_async(
377
- user_id=self._user_id,
378
- company_id=self._company_id,
379
- assistant_message_id=self._assistant_message_id,
380
- chat_id=self._chat_id,
381
- user_message_id=self._user_message_id,
382
- user_message_text=self._user_message_text,
448
+ user_id=user_id,
449
+ company_id=company_id,
450
+ assistant_message_id=assistant_message_id,
451
+ chat_id=chat_id,
452
+ user_message_id=user_message_id,
453
+ user_message_text=user_message_text,
383
454
  assistant=False,
384
455
  content=content,
385
456
  references=references,
@@ -414,13 +485,31 @@ class ChatService:
414
485
  Raises:
415
486
  Exception: If the modification fails.
416
487
  """
488
+ [
489
+ company_id,
490
+ user_id,
491
+ assistant_message_id,
492
+ user_message_id,
493
+ chat_id,
494
+ user_message_text,
495
+ ] = validate_required_values(
496
+ [
497
+ self._company_id,
498
+ self._user_id,
499
+ self._assistant_message_id,
500
+ self._user_message_id,
501
+ self._chat_id,
502
+ self._user_message_text,
503
+ ]
504
+ )
505
+
417
506
  return modify_message(
418
- user_id=self._user_id,
419
- company_id=self._company_id,
420
- assistant_message_id=self._assistant_message_id,
421
- chat_id=self._chat_id,
422
- user_message_id=self._user_message_id,
423
- user_message_text=self._user_message_text,
507
+ user_id=user_id,
508
+ company_id=company_id,
509
+ assistant_message_id=assistant_message_id,
510
+ chat_id=chat_id,
511
+ user_message_id=user_message_id,
512
+ user_message_text=user_message_text,
424
513
  assistant=True,
425
514
  content=content,
426
515
  original_content=original_content,
@@ -456,14 +545,30 @@ class ChatService:
456
545
  Raises:
457
546
  Exception: If the modification fails.
458
547
  """
459
-
548
+ [
549
+ company_id,
550
+ user_id,
551
+ assistant_message_id,
552
+ user_message_id,
553
+ chat_id,
554
+ user_message_text,
555
+ ] = validate_required_values(
556
+ [
557
+ self._company_id,
558
+ self._user_id,
559
+ self._assistant_message_id,
560
+ self._user_message_id,
561
+ self._chat_id,
562
+ self._user_message_text,
563
+ ]
564
+ )
460
565
  return await modify_message_async(
461
- user_id=self._user_id,
462
- company_id=self._company_id,
463
- assistant_message_id=self._assistant_message_id,
464
- chat_id=self._chat_id,
465
- user_message_id=self._user_message_id,
466
- user_message_text=self._user_message_text,
566
+ user_id=user_id,
567
+ company_id=company_id,
568
+ assistant_message_id=assistant_message_id,
569
+ chat_id=chat_id,
570
+ user_message_id=user_message_id,
571
+ user_message_text=user_message_text,
467
572
  assistant=True,
468
573
  content=content,
469
574
  original_content=original_content,
@@ -595,11 +700,25 @@ class ChatService:
595
700
  Raises:
596
701
  Exception: If the creation fails.
597
702
  """
703
+ [
704
+ company_id,
705
+ user_id,
706
+ assistant_id,
707
+ chat_id,
708
+ ] = validate_required_values(
709
+ [
710
+ self._company_id,
711
+ self._user_id,
712
+ self._assistant_id,
713
+ self._chat_id,
714
+ ]
715
+ )
716
+
598
717
  chat_message = create_message(
599
- user_id=self._user_id,
600
- company_id=self._company_id,
601
- chat_id=self._chat_id,
602
- assistant_id=self._assistant_id,
718
+ user_id=user_id,
719
+ company_id=company_id,
720
+ chat_id=chat_id,
721
+ assistant_id=assistant_id,
603
722
  role=ChatMessageRole.ASSISTANT,
604
723
  content=content,
605
724
  original_content=original_content,
@@ -635,12 +754,24 @@ class ChatService:
635
754
  Raises:
636
755
  Exception: If the creation fails.
637
756
  """
638
-
757
+ [
758
+ company_id,
759
+ user_id,
760
+ assistant_id,
761
+ chat_id,
762
+ ] = validate_required_values(
763
+ [
764
+ self._company_id,
765
+ self._user_id,
766
+ self._assistant_id,
767
+ self._chat_id,
768
+ ]
769
+ )
639
770
  chat_message = await create_message_async(
640
- user_id=self._user_id,
641
- company_id=self._company_id,
642
- chat_id=self._chat_id,
643
- assistant_id=self._assistant_id,
771
+ user_id=user_id,
772
+ company_id=company_id,
773
+ chat_id=chat_id,
774
+ assistant_id=assistant_id,
644
775
  role=ChatMessageRole.ASSISTANT,
645
776
  content=content,
646
777
  original_content=original_content,
@@ -676,11 +807,24 @@ class ChatService:
676
807
  Raises:
677
808
  Exception: If the creation fails.
678
809
  """
810
+ [
811
+ company_id,
812
+ user_id,
813
+ assistant_id,
814
+ chat_id,
815
+ ] = validate_required_values(
816
+ [
817
+ self._company_id,
818
+ self._user_id,
819
+ self._assistant_id,
820
+ self._chat_id,
821
+ ]
822
+ )
679
823
  chat_message = create_message(
680
- user_id=self._user_id,
681
- company_id=self._company_id,
682
- chat_id=self._chat_id,
683
- assistant_id=self._assistant_id,
824
+ user_id=user_id,
825
+ company_id=company_id,
826
+ chat_id=chat_id,
827
+ assistant_id=assistant_id,
684
828
  role=ChatMessageRole.USER,
685
829
  content=content,
686
830
  original_content=original_content,
@@ -716,12 +860,24 @@ class ChatService:
716
860
  Raises:
717
861
  Exception: If the creation fails.
718
862
  """
719
-
863
+ [
864
+ company_id,
865
+ user_id,
866
+ assistant_id,
867
+ chat_id,
868
+ ] = validate_required_values(
869
+ [
870
+ self._company_id,
871
+ self._user_id,
872
+ self._assistant_id,
873
+ self._chat_id,
874
+ ]
875
+ )
720
876
  chat_message = await create_message_async(
721
- user_id=self._user_id,
722
- company_id=self._company_id,
723
- chat_id=self._chat_id,
724
- assistant_id=self._assistant_id,
877
+ user_id=user_id,
878
+ company_id=company_id,
879
+ chat_id=chat_id,
880
+ assistant_id=assistant_id,
725
881
  role=ChatMessageRole.USER,
726
882
  content=content,
727
883
  original_content=original_content,
@@ -761,9 +917,19 @@ class ChatService:
761
917
  Raises:
762
918
  Exception: If the creation fails
763
919
  """
920
+ [
921
+ company_id,
922
+ user_id,
923
+ ] = validate_required_values(
924
+ [
925
+ self._company_id,
926
+ self._user_id,
927
+ ]
928
+ )
929
+
764
930
  return create_message_assessment(
765
- user_id=self._user_id,
766
- company_id=self._company_id,
931
+ user_id=user_id,
932
+ company_id=company_id,
767
933
  assistant_message_id=assistant_message_id,
768
934
  status=status,
769
935
  type=type,
@@ -801,9 +967,19 @@ class ChatService:
801
967
  Raises:
802
968
  Exception: If the creation fails
803
969
  """
970
+ [
971
+ company_id,
972
+ user_id,
973
+ ] = validate_required_values(
974
+ [
975
+ self._company_id,
976
+ self._user_id,
977
+ ]
978
+ )
979
+
804
980
  return await create_message_assessment_async(
805
- user_id=self._user_id,
806
- company_id=self._company_id,
981
+ user_id=user_id,
982
+ company_id=company_id,
807
983
  assistant_message_id=assistant_message_id,
808
984
  status=status,
809
985
  type=type,
@@ -839,9 +1015,19 @@ class ChatService:
839
1015
  Raises:
840
1016
  Exception: If the modification fails
841
1017
  """
1018
+ [
1019
+ company_id,
1020
+ user_id,
1021
+ ] = validate_required_values(
1022
+ [
1023
+ self._company_id,
1024
+ self._user_id,
1025
+ ]
1026
+ )
1027
+
842
1028
  return modify_message_assessment(
843
- user_id=self._user_id,
844
- company_id=self._company_id,
1029
+ user_id=user_id,
1030
+ company_id=company_id,
845
1031
  assistant_message_id=assistant_message_id,
846
1032
  status=status,
847
1033
  type=type,
@@ -876,9 +1062,19 @@ class ChatService:
876
1062
  Raises:
877
1063
  Exception: If the modification fails
878
1064
  """
1065
+ [
1066
+ company_id,
1067
+ user_id,
1068
+ ] = validate_required_values(
1069
+ [
1070
+ self._company_id,
1071
+ self._user_id,
1072
+ ]
1073
+ )
1074
+
879
1075
  return await modify_message_assessment_async(
880
- user_id=self._user_id,
881
- company_id=self._company_id,
1076
+ user_id=user_id,
1077
+ company_id=company_id,
882
1078
  assistant_message_id=assistant_message_id,
883
1079
  status=status,
884
1080
  type=type,
@@ -938,6 +1134,32 @@ class ChatService:
938
1134
  other_options=other_options,
939
1135
  )
940
1136
 
1137
+ def complete(
1138
+ self,
1139
+ messages: LanguageModelMessages,
1140
+ model_name: LanguageModelName | str,
1141
+ content_chunks: list[ContentChunk] = [],
1142
+ debug_info: dict = {},
1143
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1144
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1145
+ tools: Optional[list[LanguageModelTool]] = None,
1146
+ start_text: Optional[str] = None,
1147
+ other_options: Optional[dict] = None,
1148
+ ) -> LanguageModelResponse:
1149
+ response = self.stream_complete(
1150
+ messages=messages,
1151
+ model_name=model_name,
1152
+ content_chunks=content_chunks,
1153
+ debug_info=debug_info,
1154
+ temperature=temperature,
1155
+ timeout=timeout,
1156
+ tools=tools,
1157
+ start_text=start_text,
1158
+ other_options=other_options,
1159
+ )
1160
+
1161
+ return LanguageModelResponse.from_stream_response(response)
1162
+
941
1163
  async def stream_complete_async(
942
1164
  self,
943
1165
  messages: LanguageModelMessages,
@@ -989,3 +1211,29 @@ class ChatService:
989
1211
  start_text=start_text,
990
1212
  other_options=other_options,
991
1213
  )
1214
+
1215
+ async def complete_async(
1216
+ self,
1217
+ messages: LanguageModelMessages,
1218
+ model_name: LanguageModelName | str,
1219
+ content_chunks: list[ContentChunk] = [],
1220
+ debug_info: dict = {},
1221
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1222
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1223
+ tools: Optional[list[LanguageModelTool]] = None,
1224
+ start_text: Optional[str] = None,
1225
+ other_options: Optional[dict] = None,
1226
+ ) -> LanguageModelResponse:
1227
+ response = self.stream_complete_async(
1228
+ messages=messages,
1229
+ model_name=model_name,
1230
+ content_chunks=content_chunks,
1231
+ debug_info=debug_info,
1232
+ temperature=temperature,
1233
+ timeout=timeout,
1234
+ tools=tools,
1235
+ start_text=start_text,
1236
+ other_options=other_options,
1237
+ )
1238
+
1239
+ return LanguageModelResponse.from_stream_response(await response)
@@ -213,6 +213,7 @@ def upload_content_from_bytes(
213
213
  scope_id: str | None = None,
214
214
  chat_id: str | None = None,
215
215
  skip_ingestion: bool = False,
216
+ ingestion_config: unique_sdk.Content.IngestionConfig | None = None,
216
217
  ):
217
218
  """
218
219
  Uploads content to the knowledge base.
@@ -241,6 +242,7 @@ def upload_content_from_bytes(
241
242
  scope_id=scope_id,
242
243
  chat_id=chat_id,
243
244
  skip_ingestion=skip_ingestion,
245
+ ingestion_config=ingestion_config,
244
246
  )
245
247
  except Exception as e:
246
248
  logger.error(f"Error while uploading content: {e}")
@@ -256,6 +258,7 @@ def upload_content(
256
258
  scope_id: str | None = None,
257
259
  chat_id: str | None = None,
258
260
  skip_ingestion: bool = False,
261
+ ingestion_config: unique_sdk.Content.IngestionConfig | None = None,
259
262
  ):
260
263
  """
261
264
  Uploads content to the knowledge base.
@@ -284,6 +287,7 @@ def upload_content(
284
287
  scope_id=scope_id,
285
288
  chat_id=chat_id,
286
289
  skip_ingestion=skip_ingestion,
290
+ ingestion_config=ingestion_config,
287
291
  )
288
292
  except Exception as e:
289
293
  logger.error(f"Error while uploading content: {e}")
@@ -299,6 +303,7 @@ def _trigger_upload_content(
299
303
  scope_id: str | None = None,
300
304
  chat_id: str | None = None,
301
305
  skip_ingestion: bool = False,
306
+ ingestion_config: unique_sdk.Content.IngestionConfig | None = None,
302
307
  ):
303
308
  """
304
309
  Uploads content to the knowledge base.
@@ -368,16 +373,20 @@ def _trigger_upload_content(
368
373
  logger.error(error_msg)
369
374
  raise ValueError(error_msg)
370
375
 
376
+ if ingestion_config is None:
377
+ ingestion_config = {}
378
+
379
+ if skip_ingestion:
380
+ ingestion_config["uniqueIngestionMode"] = "SKIP_INGESTION"
381
+
371
382
  input_dict = {
372
383
  "key": content_name,
373
384
  "title": content_name,
374
385
  "mimeType": mime_type,
375
386
  "byteSize": byte_size,
387
+ "ingestionConfig": ingestion_config,
376
388
  }
377
389
 
378
- if skip_ingestion:
379
- input_dict["ingestionConfig"] = {"uniqueIngestionMode": "SKIP_INGESTION"}
380
-
381
390
  if chat_id:
382
391
  _upsert_content(
383
392
  user_id=user_id,