unique_toolkit 1.13.0__py3-none-any.whl → 1.14.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.
@@ -3,6 +3,7 @@ import logging
3
3
  from pathlib import Path
4
4
  from typing import Any, overload
5
5
 
6
+ import humps
6
7
  import unique_sdk
7
8
 
8
9
  from unique_toolkit._common.validate_required_values import validate_required_values
@@ -589,6 +590,138 @@ class KnowledgeBaseService:
589
590
 
590
591
  return self._resolve_visible_file_tree(content_infos=info.content_infos)
591
592
 
593
+ def _pop_forbidden_metadata_keys(self, metadata: dict[str, Any]) -> dict[str, Any]:
594
+ forbidden_keys = [
595
+ "key",
596
+ "url",
597
+ "title",
598
+ "folderId",
599
+ "mimeType",
600
+ "companyId",
601
+ "contentId",
602
+ "folderIdPath",
603
+ "externalFileOwner",
604
+ ]
605
+ for key in forbidden_keys:
606
+ metadata.pop(key, None)
607
+ return metadata
608
+
609
+ def update_content_metadata(
610
+ self,
611
+ *,
612
+ content_info: ContentInfo,
613
+ additional_metadata: dict[str, Any],
614
+ ) -> ContentInfo:
615
+ camelized_additional_metadata = humps.camelize(additional_metadata)
616
+ camelized_additional_metadata = self._pop_forbidden_metadata_keys(
617
+ camelized_additional_metadata
618
+ )
619
+
620
+ if content_info.metadata is not None:
621
+ content_info.metadata.update(camelized_additional_metadata)
622
+ else:
623
+ content_info.metadata = camelized_additional_metadata
624
+
625
+ return update_content(
626
+ user_id=self._user_id,
627
+ company_id=self._company_id,
628
+ content_id=content_info.id,
629
+ metadata=content_info.metadata,
630
+ )
631
+
632
+ def remove_content_metadata(
633
+ self,
634
+ *,
635
+ content_info: ContentInfo,
636
+ keys_to_remove: list[str],
637
+ ) -> ContentInfo:
638
+ """
639
+ Removes the specified keys irreversibly from the content metadata.
640
+ """
641
+
642
+ if content_info.metadata is None:
643
+ _LOGGER.warning(f"Content metadata is None for content {content_info.id}")
644
+ return content_info
645
+
646
+ for key in keys_to_remove:
647
+ content_info.metadata.pop(key, None)
648
+
649
+ return update_content(
650
+ user_id=self._user_id,
651
+ company_id=self._company_id,
652
+ content_id=content_info.id,
653
+ metadata=content_info.metadata or {},
654
+ )
655
+
656
+ @overload
657
+ def update_contents_metadata(
658
+ self,
659
+ *,
660
+ additional_metadata: dict[str, Any],
661
+ content_infos: list[ContentInfo],
662
+ ) -> list[ContentInfo]: ...
663
+
664
+ @overload
665
+ def update_contents_metadata(
666
+ self, *, additional_metadata: dict[str, Any], metadata_filter: dict[str, Any]
667
+ ) -> list[ContentInfo]: ...
668
+
669
+ def update_contents_metadata(
670
+ self,
671
+ *,
672
+ additional_metadata: dict[str, Any],
673
+ metadata_filter: dict[str, Any] | None = None,
674
+ content_infos: list[ContentInfo] | None = None,
675
+ ) -> list[ContentInfo]:
676
+ additional_metadata_camelized = humps.camelize(additional_metadata)
677
+ additional_metadata_camelized = self._pop_forbidden_metadata_keys(
678
+ additional_metadata_camelized
679
+ )
680
+
681
+ if content_infos is None:
682
+ content_infos = self.get_paginated_content_infos(
683
+ metadata_filter=metadata_filter,
684
+ ).content_infos
685
+
686
+ for info in content_infos:
687
+ self.update_content_metadata(
688
+ content_info=info, additional_metadata=additional_metadata_camelized
689
+ )
690
+
691
+ return content_infos
692
+
693
+ @overload
694
+ def remove_contents_metadata(
695
+ self,
696
+ *,
697
+ keys_to_remove: list[str],
698
+ content_infos: list[ContentInfo],
699
+ ) -> list[ContentInfo]: ...
700
+
701
+ @overload
702
+ def remove_contents_metadata(
703
+ self, *, keys_to_remove: list[str], metadata_filter: dict[str, Any]
704
+ ) -> list[ContentInfo]: ...
705
+
706
+ def remove_contents_metadata(
707
+ self,
708
+ *,
709
+ keys_to_remove: list[str],
710
+ metadata_filter: dict[str, Any] | None = None,
711
+ content_infos: list[ContentInfo] | None = None,
712
+ ) -> list[ContentInfo]:
713
+ if content_infos is None:
714
+ content_infos = self.get_paginated_content_infos(
715
+ metadata_filter=metadata_filter,
716
+ ).content_infos
717
+
718
+ for info in content_infos:
719
+ self.remove_content_metadata(
720
+ content_info=info, keys_to_remove=keys_to_remove
721
+ )
722
+
723
+ return content_infos
724
+
592
725
  @overload
593
726
  def delete_content(
594
727
  self,
@@ -0,0 +1,195 @@
1
+ import random
2
+ import string
3
+ from datetime import datetime
4
+ from typing import TYPE_CHECKING
5
+
6
+ from unique_toolkit.app.schemas import (
7
+ BaseEvent,
8
+ ChatEvent,
9
+ ChatEventAdditionalParameters,
10
+ ChatEventAssistantMessage,
11
+ ChatEventPayload,
12
+ ChatEventUserMessage,
13
+ EventName,
14
+ )
15
+
16
+ if TYPE_CHECKING:
17
+ from typing import Any
18
+
19
+ from unique_toolkit.app.unique_settings import UniqueSettings
20
+
21
+
22
+ def generated_numeric_string(length: int) -> str:
23
+ return "".join(random.choices(string.digits, k=length))
24
+
25
+
26
+ def generated_alphanumeric_string(length: int) -> str:
27
+ return "".join(random.choices(string.ascii_letters + string.digits, k=length))
28
+
29
+
30
+ def generated_chat_id() -> str:
31
+ return f"chat_{generated_alphanumeric_string(16)}"
32
+
33
+
34
+ def generated_assistant_id() -> str:
35
+ return f"assistant_{generated_alphanumeric_string(16)}"
36
+
37
+
38
+ def generated_user_message_id() -> str:
39
+ return f"msg_{generated_alphanumeric_string(16)}"
40
+
41
+
42
+ class TestEventFactory:
43
+ """Factory for creating test event objects with sensible defaults.
44
+
45
+ Simplifies test setup by providing convenient methods to generate
46
+ chat events, messages, and related objects for testing.
47
+ """
48
+
49
+ def __init__(self, settings: UniqueSettings | None = None) -> None:
50
+ self._settings = settings
51
+
52
+ def _get_user_id(self) -> str:
53
+ if self._settings is None:
54
+ return generated_numeric_string(16)
55
+ else:
56
+ return self._settings.auth.user_id.get_secret_value()
57
+
58
+ def _get_company_id(self) -> str:
59
+ if self._settings is None:
60
+ return generated_numeric_string(16)
61
+ else:
62
+ return self._settings.auth.company_id.get_secret_value()
63
+
64
+ def get_chat_event_user_message(
65
+ self,
66
+ text: str,
67
+ *,
68
+ created_at: datetime | None = None,
69
+ language: str = "DE",
70
+ original_text: str | None = None,
71
+ ) -> ChatEventUserMessage:
72
+ if created_at is None:
73
+ created_at = datetime.now()
74
+
75
+ return ChatEventUserMessage(
76
+ id=generated_user_message_id(),
77
+ text=text,
78
+ original_text=original_text or text,
79
+ created_at=created_at.isoformat(),
80
+ language=language,
81
+ )
82
+
83
+ def get_chat_event_assistant_message(
84
+ self, *, created_at: datetime | None = None
85
+ ) -> ChatEventAssistantMessage:
86
+ if created_at is None:
87
+ created_at = datetime.now()
88
+
89
+ return ChatEventAssistantMessage(
90
+ id=generated_assistant_id(), created_at=created_at.isoformat()
91
+ )
92
+
93
+ def get_chat_event_additional_parameters(
94
+ self,
95
+ *,
96
+ translate_to_language: str | None = None,
97
+ content_id_to_translate: str | None = None,
98
+ ) -> ChatEventAdditionalParameters:
99
+ return ChatEventAdditionalParameters(
100
+ translate_to_language=translate_to_language,
101
+ content_id_to_translate=content_id_to_translate,
102
+ )
103
+
104
+ def get_base_event(
105
+ self,
106
+ *,
107
+ event: EventName = EventName.EXTERNAL_MODULE_CHOSEN,
108
+ user_id: str | None = None,
109
+ company_id: str | None = None,
110
+ ) -> BaseEvent:
111
+ return BaseEvent(
112
+ id=generated_alphanumeric_string(16),
113
+ event=event,
114
+ user_id=user_id or self._get_user_id(),
115
+ company_id=company_id or self._get_company_id(),
116
+ )
117
+
118
+ def get_chat_event_payload(
119
+ self,
120
+ *,
121
+ name: str,
122
+ description: str,
123
+ user_message_text: str,
124
+ user_message_created_at: datetime | None = None,
125
+ user_message_language: str = "DE",
126
+ user_message_original_text: str | None = None,
127
+ assistant_message_created_at: datetime | None = None,
128
+ configuration: dict[str, Any] | None = None,
129
+ chat_id: str | None = None,
130
+ assistant_id: str | None = None,
131
+ ) -> ChatEventPayload:
132
+ if chat_id is None:
133
+ chat_id = generated_chat_id()
134
+
135
+ if assistant_id is None:
136
+ assistant_id = generated_assistant_id()
137
+
138
+ assistant_message = self.get_chat_event_assistant_message(
139
+ created_at=assistant_message_created_at or datetime.now()
140
+ )
141
+ user_message = self.get_chat_event_user_message(
142
+ text=user_message_text,
143
+ created_at=user_message_created_at or datetime.now(),
144
+ language=user_message_language,
145
+ original_text=user_message_original_text,
146
+ )
147
+ return ChatEventPayload(
148
+ name=name,
149
+ description=description,
150
+ configuration=configuration or {},
151
+ chat_id=chat_id,
152
+ assistant_id=assistant_id,
153
+ user_message=user_message,
154
+ assistant_message=assistant_message,
155
+ )
156
+
157
+ def get_chat_event(
158
+ self,
159
+ *,
160
+ name: str,
161
+ event_name: EventName = EventName.EXTERNAL_MODULE_CHOSEN,
162
+ description: str,
163
+ user_message_text: str,
164
+ user_message_created_at: datetime = datetime.now(),
165
+ user_message_language: str = "DE",
166
+ user_message_original_text: str | None = None,
167
+ assistant_message_created_at: datetime | None = None,
168
+ configuration: dict[str, Any] | None = None,
169
+ chat_id: str = generated_chat_id(),
170
+ assistant_id: str = generated_assistant_id(),
171
+ user_id: str | None = None,
172
+ company_id: str | None = None,
173
+ version: str = "1.0",
174
+ ) -> ChatEvent:
175
+ payload = self.get_chat_event_payload(
176
+ name=name,
177
+ description=description,
178
+ user_message_text=user_message_text,
179
+ user_message_created_at=user_message_created_at,
180
+ user_message_language=user_message_language,
181
+ user_message_original_text=user_message_original_text,
182
+ assistant_message_created_at=assistant_message_created_at,
183
+ configuration=configuration or {},
184
+ chat_id=chat_id,
185
+ assistant_id=assistant_id,
186
+ )
187
+ return ChatEvent(
188
+ id=generated_alphanumeric_string(16),
189
+ event=event_name,
190
+ user_id=user_id or self._get_user_id(),
191
+ company_id=company_id or self._get_company_id(),
192
+ payload=payload,
193
+ created_at=int(datetime.now().timestamp()),
194
+ version=version,
195
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: unique_toolkit
3
- Version: 1.13.0
3
+ Version: 1.14.1
4
4
  Summary:
5
5
  License: Proprietary
6
6
  Author: Cedric Klinkert
@@ -118,6 +118,12 @@ All notable changes to this project will be documented in this file.
118
118
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
119
119
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
120
120
 
121
+ ## [1.14.1] - 2025-10-08
122
+ - Add utilities for testing
123
+
124
+ ## [1.14.0] - 2025-10-07
125
+ - Manipulate Metadata with knowledge base service
126
+
121
127
  ## [1.13.0] - 2025-10-07
122
128
  - Delete contents with knowledge base service
123
129
 
@@ -130,7 +130,7 @@ unique_toolkit/framework_utilities/openai/__init__.py,sha256=CrHYoC7lv2pBscitLer
130
130
  unique_toolkit/framework_utilities/openai/client.py,sha256=ct1cqPcIK1wPl11G9sJV39ZnLJwKr2kDUDSra0FjvtM,2007
131
131
  unique_toolkit/framework_utilities/openai/message_builder.py,sha256=RT1pZjxH42TFZlAxQ5zlqdKPvHKVTjc5t3JDUy58I7Q,6887
132
132
  unique_toolkit/framework_utilities/utils.py,sha256=JK7g2yMfEx3eMprug26769xqNpS5WJcizf8n2zWMBng,789
133
- unique_toolkit/knowledge_base.py,sha256=SdkMM8aiPeksYLjlNiqD6JgJWM5H-rpaoce-Igp0mYo,23566
133
+ unique_toolkit/knowledge_base.py,sha256=UU8S858sjrAJhOKtDb0b0RvjwzjZOvErNk4ShyLrTZY,27610
134
134
  unique_toolkit/language_model/__init__.py,sha256=lRQyLlbwHbNFf4-0foBU13UGb09lwEeodbVsfsSgaCk,1971
135
135
  unique_toolkit/language_model/builder.py,sha256=4OKfwJfj3TrgO1ezc_ewIue6W7BCQ2ZYQXUckWVPPTA,3369
136
136
  unique_toolkit/language_model/constants.py,sha256=B-topqW0r83dkC_25DeQfnPk3n53qzIHUCBS7YJ0-1U,119
@@ -149,7 +149,8 @@ unique_toolkit/short_term_memory/schemas.py,sha256=OhfcXyF6ACdwIXW45sKzjtZX_gkcJ
149
149
  unique_toolkit/short_term_memory/service.py,sha256=5PeVBu1ZCAfyDb2HLVvlmqSbyzBBuE9sI2o9Aajqjxg,8884
150
150
  unique_toolkit/smart_rules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
151
151
  unique_toolkit/smart_rules/compile.py,sha256=Ozhh70qCn2yOzRWr9d8WmJeTo7AQurwd3tStgBMPFLA,1246
152
- unique_toolkit-1.13.0.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
153
- unique_toolkit-1.13.0.dist-info/METADATA,sha256=wQc8-zijmHKsEz9v1ECKbKDJo_i7JMSWrSeppYE79CY,36164
154
- unique_toolkit-1.13.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
155
- unique_toolkit-1.13.0.dist-info/RECORD,,
152
+ unique_toolkit/test_utilities/events.py,sha256=fdekL7qsYJLqDpABH_scXgvF8TgHLdcyd0B_I0WQ7YM,6335
153
+ unique_toolkit-1.14.1.dist-info/LICENSE,sha256=GlN8wHNdh53xwOPg44URnwag6TEolCjoq3YD_KrWgss,193
154
+ unique_toolkit-1.14.1.dist-info/METADATA,sha256=AmlR3j-cMn7Lc8iRYzhPdCgbekpV1_hL0wWYKzX7FVY,36294
155
+ unique_toolkit-1.14.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
156
+ unique_toolkit-1.14.1.dist-info/RECORD,,