agi-med-common 4.1.0__py3-none-any.whl → 4.2.0__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.
- agi_med_common/__init__.py +2 -2
- agi_med_common/validators.py +59 -2
- {agi_med_common-4.1.0.dist-info → agi_med_common-4.2.0.dist-info}/METADATA +1 -1
- {agi_med_common-4.1.0.dist-info → agi_med_common-4.2.0.dist-info}/RECORD +6 -6
- {agi_med_common-4.1.0.dist-info → agi_med_common-4.2.0.dist-info}/WHEEL +0 -0
- {agi_med_common-4.1.0.dist-info → agi_med_common-4.2.0.dist-info}/top_level.txt +0 -0
agi_med_common/__init__.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
__version__ = "4.
|
1
|
+
__version__ = "4.2.0"
|
2
2
|
|
3
3
|
from .logger import LogLevelEnum, logger_init
|
4
4
|
from .models import (
|
@@ -16,6 +16,6 @@ from .models import (
|
|
16
16
|
from .file_storage import FileStorage
|
17
17
|
from .models import DiagnosticsXMLTagEnum, MTRSXMLTagEnum, DoctorChoiceXMLTagEnum
|
18
18
|
from .utils import make_session_id, read_json
|
19
|
-
from .validators import
|
19
|
+
from .validators import ExistingPath, ExistingFile, StrNotEmpty, Prompt
|
20
20
|
from .xml_parser import XMLParser
|
21
21
|
from .parallel_map import parallel_map
|
agi_med_common/validators.py
CHANGED
@@ -1,14 +1,38 @@
|
|
1
1
|
import os
|
2
2
|
import re
|
3
3
|
from pathlib import Path
|
4
|
+
from typing import Annotated, TypeVar
|
5
|
+
from pydantic import AfterValidator
|
4
6
|
|
5
7
|
|
6
|
-
|
8
|
+
PathLike = str | os.PathLike[str] | Path
|
9
|
+
|
10
|
+
|
11
|
+
def validate_existing_path(filepath: PathLike) -> PathLike:
|
7
12
|
if not os.path.exists(filepath):
|
8
|
-
raise ValueError(f"
|
13
|
+
raise ValueError(f"Path {filepath} is not exist")
|
14
|
+
return filepath
|
15
|
+
|
16
|
+
|
17
|
+
def validate_existing_file(filepath: PathLike) -> PathLike:
|
18
|
+
if not os.path.isfile(filepath):
|
19
|
+
raise ValueError(f"Path {filepath} is not file")
|
9
20
|
return filepath
|
10
21
|
|
11
22
|
|
23
|
+
ExistingPath = Annotated[Path, AfterValidator(validate_existing_path)]
|
24
|
+
ExistingFile = Annotated[Path, AfterValidator(validate_existing_file)]
|
25
|
+
|
26
|
+
|
27
|
+
def validate_not_empty(text: any) -> any:
|
28
|
+
if not text:
|
29
|
+
raise ValueError("Expected not empty")
|
30
|
+
return text
|
31
|
+
|
32
|
+
|
33
|
+
StrNotEmpty = Annotated[str, AfterValidator(validate_not_empty)]
|
34
|
+
|
35
|
+
|
12
36
|
def validate_prompt(prompt: str, prompt_required_keys: set[str]) -> str:
|
13
37
|
exist_keys: set[str] = set(re.findall(r"{(.*?)}", prompt))
|
14
38
|
if missed_keys := prompt_required_keys.difference(exist_keys):
|
@@ -16,3 +40,36 @@ def validate_prompt(prompt: str, prompt_required_keys: set[str]) -> str:
|
|
16
40
|
if extern_keys := exist_keys.difference(prompt_required_keys):
|
17
41
|
raise ValueError(f"You have more keys for prompt: {extern_keys}")
|
18
42
|
return prompt
|
43
|
+
|
44
|
+
|
45
|
+
def validate_keys(*keys):
|
46
|
+
assert keys and all(isinstance(key, str) for key in keys)
|
47
|
+
|
48
|
+
def validate(prompt: str) -> str:
|
49
|
+
prompt_required_keys: set[str] = set(keys)
|
50
|
+
exist_keys: set[str] = set(re.findall(r"{(.*?)}", prompt))
|
51
|
+
if missed_keys := prompt_required_keys.difference(exist_keys):
|
52
|
+
raise ValueError(f"Missing required key in prompt: {missed_keys}")
|
53
|
+
if extern_keys := exist_keys.difference(prompt_required_keys):
|
54
|
+
raise ValueError(f"You have more keys for prompt: {extern_keys}")
|
55
|
+
return prompt
|
56
|
+
|
57
|
+
return validate
|
58
|
+
|
59
|
+
|
60
|
+
K = TypeVar("K", bound=str)
|
61
|
+
|
62
|
+
|
63
|
+
class Prompt:
|
64
|
+
# use inside pydantic models: prompt: Prompt['base_context']
|
65
|
+
def __class_getitem__(cls, keys: K | tuple[K, ...]) -> type[str]:
|
66
|
+
if isinstance(keys, tuple):
|
67
|
+
keys_tuple = keys
|
68
|
+
else:
|
69
|
+
keys_tuple = (keys,)
|
70
|
+
|
71
|
+
# Validate that all keys are strings
|
72
|
+
if not all(isinstance(key, str) for key in keys_tuple):
|
73
|
+
raise TypeError("All keys must be strings")
|
74
|
+
|
75
|
+
return Annotated[str, AfterValidator(validate_keys(*keys_tuple))]
|
@@ -1,8 +1,8 @@
|
|
1
|
-
agi_med_common/__init__.py,sha256=
|
1
|
+
agi_med_common/__init__.py,sha256=IvElxZ-gSgSUhXTYjdzg0e5p8C74xFqsslUXktPQf5w,592
|
2
2
|
agi_med_common/file_storage.py,sha256=uyZE3ePKrbVLNVcvxw9lbgumy1UPwDGIwQMs1Bd8HFI,1502
|
3
3
|
agi_med_common/parallel_map.py,sha256=Qx6xe7DqlEUDpSucp5Hm8r9y9Iquwh9JvX7lOqHhnOw,921
|
4
4
|
agi_med_common/utils.py,sha256=5iurKl5d1zZ_cN4yqtc0_FhrwuNBcDurbWilu28_saE,325
|
5
|
-
agi_med_common/validators.py,sha256=
|
5
|
+
agi_med_common/validators.py,sha256=sWXWsJn9FBlbyUHCNBNSMiEnD9ZenXhzMBNTXjC7yRE,2413
|
6
6
|
agi_med_common/xml_parser.py,sha256=VvLIX_XCZao9i0qqpTVx8nx0vbFXSe8pEbdJdXnj97g,568
|
7
7
|
agi_med_common/logger/__init__.py,sha256=RW_0VZtbeJ4RsLqUXZUQWl5CtV9g840rU7qRlf5u49E,96
|
8
8
|
agi_med_common/logger/log_level_enum.py,sha256=lWuSMho9I0v_xf1RuwpERx5o8NJXaavjwxSdh8fxMqE,477
|
@@ -23,7 +23,7 @@ agi_med_common/models/enums/moderation_label_enum.py,sha256=lbGG4Pu7cQp57uEyQEpt
|
|
23
23
|
agi_med_common/models/enums/mtrs_label_enum.py,sha256=6emBndt3SCsQVZZFKQYCV2_iyjjmZEhwejJKJu39ZAw,257
|
24
24
|
agi_med_common/models/enums/mtrs_xml_tag_enum.py,sha256=6OxuRsrx4b2uBjfrBgm4Y789Ly337_mQXL9VPRCpLyg,273
|
25
25
|
agi_med_common/models/enums/track_id_enum.py,sha256=N3KUd97a4xDet_NKZ5URm0qWAAv3ts1l_foOjdlGY4c,532
|
26
|
-
agi_med_common-4.
|
27
|
-
agi_med_common-4.
|
28
|
-
agi_med_common-4.
|
29
|
-
agi_med_common-4.
|
26
|
+
agi_med_common-4.2.0.dist-info/METADATA,sha256=oSn4FoibCezpwEn5Qx1jUmfVAIOdnUraA3gxA_iVy0s,546
|
27
|
+
agi_med_common-4.2.0.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
|
28
|
+
agi_med_common-4.2.0.dist-info/top_level.txt,sha256=26o565jF_7wYQj7-YJfTedtT9yDxDcf8RNikOYuPq78,15
|
29
|
+
agi_med_common-4.2.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|