agi-med-common 4.1.0__py3-none-any.whl → 4.2.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,4 +1,4 @@
1
- __version__ = "4.1.0"
1
+ __version__ = "4.2.1"
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 is_file_exist, validate_prompt
19
+ from .validators import ExistingPath, ExistingFile, StrNotEmpty, SecretStrNotEmpty, Prompt
20
20
  from .xml_parser import XMLParser
21
21
  from .parallel_map import parallel_map
@@ -1,14 +1,39 @@
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, SecretStr
4
6
 
5
7
 
6
- def is_file_exist(filepath: str | os.PathLike[str] | Path) -> str | os.PathLike | Path:
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"File {filepath} is not exist. Check the path")
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
+ SecretStrNotEmpty = Annotated[SecretStr, AfterValidator(validate_not_empty)]
35
+
36
+
12
37
  def validate_prompt(prompt: str, prompt_required_keys: set[str]) -> str:
13
38
  exist_keys: set[str] = set(re.findall(r"{(.*?)}", prompt))
14
39
  if missed_keys := prompt_required_keys.difference(exist_keys):
@@ -16,3 +41,36 @@ def validate_prompt(prompt: str, prompt_required_keys: set[str]) -> str:
16
41
  if extern_keys := exist_keys.difference(prompt_required_keys):
17
42
  raise ValueError(f"You have more keys for prompt: {extern_keys}")
18
43
  return prompt
44
+
45
+
46
+ def validate_keys(*keys):
47
+ assert keys and all(isinstance(key, str) for key in keys)
48
+
49
+ def validate(prompt: str) -> str:
50
+ prompt_required_keys: set[str] = set(keys)
51
+ exist_keys: set[str] = set(re.findall(r"{(.*?)}", prompt))
52
+ if missed_keys := prompt_required_keys.difference(exist_keys):
53
+ raise ValueError(f"Missing required key in prompt: {missed_keys}")
54
+ if extern_keys := exist_keys.difference(prompt_required_keys):
55
+ raise ValueError(f"You have more keys for prompt: {extern_keys}")
56
+ return prompt
57
+
58
+ return validate
59
+
60
+
61
+ K = TypeVar("K", bound=str)
62
+
63
+
64
+ class Prompt:
65
+ # use inside pydantic models: prompt: Prompt['base_context']
66
+ def __class_getitem__(cls, keys: K | tuple[K, ...]) -> type[str]:
67
+ if isinstance(keys, tuple):
68
+ keys_tuple = keys
69
+ else:
70
+ keys_tuple = (keys,)
71
+
72
+ # Validate that all keys are strings
73
+ if not all(isinstance(key, str) for key in keys_tuple):
74
+ raise TypeError("All keys must be strings")
75
+
76
+ return Annotated[str, AfterValidator(validate_keys(*keys_tuple))]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agi_med_common
3
- Version: 4.1.0
3
+ Version: 4.2.1
4
4
  Summary: Сommon for agi-med team
5
5
  Author: AGI-MED-TEAM
6
6
  Requires-Python: >=3.11
@@ -1,8 +1,8 @@
1
- agi_med_common/__init__.py,sha256=nNut_TCYM2HS35F9wW8lZHMgqpI5MM1ZqESH5Oyp_4Y,575
1
+ agi_med_common/__init__.py,sha256=kSY51g91X68T7k39gwISEOrDa3Qi9IWCnN3wgr0VQ-E,611
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=R678gjPp-5XbnocRuEtdOQgJyCCOurxwaOe2nT04kSg,705
5
+ agi_med_common/validators.py,sha256=PGP_ztcS3pOnVCIR9LVBOaC4F3u_JePrKKUO4vp214E,2501
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.1.0.dist-info/METADATA,sha256=mScZ7Fxwcd4tOZO39IKRK_lFZz8J0_WOBjjnrd6JwRY,546
27
- agi_med_common-4.1.0.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
28
- agi_med_common-4.1.0.dist-info/top_level.txt,sha256=26o565jF_7wYQj7-YJfTedtT9yDxDcf8RNikOYuPq78,15
29
- agi_med_common-4.1.0.dist-info/RECORD,,
26
+ agi_med_common-4.2.1.dist-info/METADATA,sha256=1Gfop4JXcz0yraYXd7XXydl5DY9vGmd6Da5EnYQVANc,546
27
+ agi_med_common-4.2.1.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
28
+ agi_med_common-4.2.1.dist-info/top_level.txt,sha256=26o565jF_7wYQj7-YJfTedtT9yDxDcf8RNikOYuPq78,15
29
+ agi_med_common-4.2.1.dist-info/RECORD,,