fabricatio 0.2.1.dev0__cp313-cp313-win_amd64.whl → 0.3.14.dev4__cp313-cp313-win_amd64.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.
Files changed (75) hide show
  1. fabricatio/__init__.py +12 -20
  2. fabricatio/actions/__init__.py +1 -5
  3. fabricatio/actions/article.py +319 -0
  4. fabricatio/actions/article_rag.py +416 -0
  5. fabricatio/actions/fs.py +25 -0
  6. fabricatio/actions/output.py +248 -0
  7. fabricatio/actions/rag.py +96 -0
  8. fabricatio/actions/rules.py +83 -0
  9. fabricatio/capabilities/__init__.py +1 -0
  10. fabricatio/capabilities/advanced_judge.py +20 -0
  11. fabricatio/capabilities/advanced_rag.py +61 -0
  12. fabricatio/capabilities/censor.py +105 -0
  13. fabricatio/capabilities/check.py +212 -0
  14. fabricatio/capabilities/correct.py +228 -0
  15. fabricatio/capabilities/extract.py +74 -0
  16. fabricatio/capabilities/persist.py +103 -0
  17. fabricatio/capabilities/propose.py +65 -0
  18. fabricatio/capabilities/rag.py +263 -0
  19. fabricatio/capabilities/rating.py +404 -0
  20. fabricatio/capabilities/review.py +114 -0
  21. fabricatio/capabilities/task.py +113 -0
  22. fabricatio/decorators.py +251 -179
  23. fabricatio/{core.py → emitter.py} +31 -21
  24. fabricatio/fs/__init__.py +32 -2
  25. fabricatio/fs/curd.py +32 -9
  26. fabricatio/fs/readers.py +44 -7
  27. fabricatio/journal.py +3 -19
  28. fabricatio/models/action.py +185 -61
  29. fabricatio/models/adv_kwargs_types.py +63 -0
  30. fabricatio/models/extra/__init__.py +1 -0
  31. fabricatio/models/extra/advanced_judge.py +32 -0
  32. fabricatio/models/extra/aricle_rag.py +284 -0
  33. fabricatio/models/extra/article_base.py +422 -0
  34. fabricatio/models/extra/article_essence.py +101 -0
  35. fabricatio/models/extra/article_main.py +285 -0
  36. fabricatio/models/extra/article_outline.py +46 -0
  37. fabricatio/models/extra/article_proposal.py +52 -0
  38. fabricatio/models/extra/patches.py +20 -0
  39. fabricatio/models/extra/problem.py +165 -0
  40. fabricatio/models/extra/rag.py +98 -0
  41. fabricatio/models/extra/rule.py +52 -0
  42. fabricatio/models/generic.py +704 -36
  43. fabricatio/models/kwargs_types.py +112 -17
  44. fabricatio/models/role.py +74 -27
  45. fabricatio/models/task.py +94 -60
  46. fabricatio/models/tool.py +328 -188
  47. fabricatio/models/usages.py +791 -515
  48. fabricatio/parser.py +81 -60
  49. fabricatio/rust.cp313-win_amd64.pyd +0 -0
  50. fabricatio/rust.pyi +846 -0
  51. fabricatio/toolboxes/__init__.py +1 -3
  52. fabricatio/toolboxes/fs.py +17 -1
  53. fabricatio/utils.py +156 -0
  54. fabricatio/workflows/__init__.py +1 -0
  55. fabricatio/workflows/articles.py +24 -0
  56. fabricatio/workflows/rag.py +11 -0
  57. fabricatio-0.3.14.dev4.data/scripts/tdown.exe +0 -0
  58. fabricatio-0.3.14.dev4.data/scripts/ttm.exe +0 -0
  59. fabricatio-0.3.14.dev4.dist-info/METADATA +188 -0
  60. fabricatio-0.3.14.dev4.dist-info/RECORD +64 -0
  61. {fabricatio-0.2.1.dev0.dist-info → fabricatio-0.3.14.dev4.dist-info}/WHEEL +1 -1
  62. fabricatio/_rust.cp313-win_amd64.pyd +0 -0
  63. fabricatio/_rust.pyi +0 -53
  64. fabricatio/_rust_instances.py +0 -8
  65. fabricatio/actions/communication.py +0 -15
  66. fabricatio/actions/transmission.py +0 -23
  67. fabricatio/config.py +0 -263
  68. fabricatio/models/advanced.py +0 -128
  69. fabricatio/models/events.py +0 -82
  70. fabricatio/models/utils.py +0 -78
  71. fabricatio/toolboxes/task.py +0 -6
  72. fabricatio-0.2.1.dev0.data/scripts/tdown.exe +0 -0
  73. fabricatio-0.2.1.dev0.dist-info/METADATA +0 -420
  74. fabricatio-0.2.1.dev0.dist-info/RECORD +0 -35
  75. {fabricatio-0.2.1.dev0.dist-info → fabricatio-0.3.14.dev4.dist-info}/licenses/LICENSE +0 -0
fabricatio/parser.py CHANGED
@@ -1,93 +1,114 @@
1
- """A module to parse text using regular expressions."""
1
+ """A module for capturing patterns in text using regular expressions."""
2
2
 
3
- from typing import Any, Callable, Self, Tuple
3
+ import re
4
+ from dataclasses import dataclass, field
5
+ from functools import lru_cache
6
+ from typing import Any, Callable, Iterable, List, Optional, Self, Tuple, Type, Union
4
7
 
5
- import regex
6
- from pydantic import BaseModel, ConfigDict, Field, PositiveInt, PrivateAttr
7
- from regex import Pattern, compile
8
+ import ujson
9
+ from json_repair import repair_json
8
10
 
9
11
  from fabricatio.journal import logger
12
+ from fabricatio.rust import CONFIG
10
13
 
11
14
 
12
- class Capture(BaseModel):
15
+ @dataclass(frozen=True)
16
+ class Capture:
13
17
  """A class to capture patterns in text using regular expressions.
14
18
 
15
19
  Attributes:
16
- pattern (str): The regular expression pattern to search for.
17
- _compiled (Pattern): The compiled regular expression pattern.
20
+ target_groups (Tuple[int, ...]): The target groups to extract from the match.
21
+ pattern (str): The regex pattern to search for.
22
+ flags (int): Flags to apply when compiling the regex.
23
+ capture_type (Optional[str]): Optional hint for post-processing (e.g., 'json').
18
24
  """
19
25
 
20
- model_config = ConfigDict(use_attribute_docstrings=True)
21
- target_groups: Tuple[int, ...] = Field(default_factory=tuple)
22
- """The target groups to capture from the pattern."""
23
- pattern: str = Field(frozen=True)
26
+ pattern: str = field()
24
27
  """The regular expression pattern to search for."""
25
- flags: PositiveInt = Field(default=regex.DOTALL | regex.MULTILINE | regex.IGNORECASE, frozen=True)
26
- """The flags to use when compiling the regular expression pattern."""
27
- _compiled: Pattern = PrivateAttr()
28
-
29
- def model_post_init(self, __context: Any) -> None:
30
- """Initialize the compiled regular expression pattern after the model is initialized.
31
-
32
- Args:
33
- __context (Any): The context in which the model is initialized.
34
- """
35
- self._compiled = compile(self.pattern, self.flags)
36
-
37
- def capture(self, text: str) -> Tuple[str, ...] | str | None:
38
- """Capture the first occurrence of the pattern in the given text.
39
-
40
- Args:
41
- text (str): The text to search the pattern in.
42
-
43
- Returns:
44
- str | None: The captured text if the pattern is found, otherwise None.
45
-
46
- """
47
- match = self._compiled.search(text)
28
+ flags: int = re.DOTALL | re.MULTILINE | re.IGNORECASE
29
+ """Flags to control regex behavior (DOTALL, MULTILINE, IGNORECASE by default)."""
30
+ capture_type: Optional[str] = None
31
+ """Optional type identifier for post-processing (e.g., 'json' for JSON repair)."""
32
+ target_groups: Tuple[int, ...] = field(default_factory=tuple)
33
+ """Tuple of group indices to extract from the match (1-based indexing)."""
34
+
35
+ def fix(self, text: Union[str, Iterable[str], Any]) -> Union[str, List[str], Any]:
36
+ """Fix the text based on capture_type (e.g., JSON repair)."""
37
+ match self.capture_type:
38
+ case "json" if CONFIG.general.use_json_repair:
39
+ logger.debug("Applying JSON repair to text.")
40
+ if isinstance(text, str):
41
+ return repair_json(text, ensure_ascii=False)
42
+ return [repair_json(item, ensure_ascii=False) for item in text]
43
+ case _:
44
+ return text
45
+
46
+ def capture(self, text: str) -> Optional[Union[str, Tuple[str, ...]]]:
47
+ """Capture the first match of the pattern in the text."""
48
+ compiled = re.compile(self.pattern, self.flags)
49
+ match = compiled.match(text) or compiled.search(text)
48
50
  if match is None:
51
+ logger.debug(f"Capture Failed: {text}")
49
52
  return None
50
53
 
54
+ groups = self.fix(match.groups())
51
55
  if self.target_groups:
52
- cap = tuple(match.group(g) for g in self.target_groups)
53
- logger.debug(f"Captured text: {'\n\n'.join(cap)}")
56
+ cap = tuple(groups[g - 1] for g in self.target_groups)
57
+ logger.debug(f"Captured texts: {'\n==\n'.join(cap)}")
54
58
  return cap
55
- cap = match.group(1)
59
+ cap = groups[0]
56
60
  logger.debug(f"Captured text: \n{cap}")
57
61
  return cap
58
62
 
59
- def convert_with[T](self, text: str, convertor: Callable[[Tuple[str, ...]], T] | Callable[[str], T]) -> T | None:
60
- """Convert the given text using the pattern.
61
-
62
- Args:
63
- text (str): The text to search the pattern in.
64
- convertor (Callable[[Tuple[str, ...]], T] | Callable[[str], T]): The function to convert the captured text.
65
-
66
- Returns:
67
- str | None: The converted text if the pattern is found, otherwise None.
68
- """
63
+ def convert_with(
64
+ self,
65
+ text: str,
66
+ convertor: Callable[[Union[str, Tuple[str, ...]]], Any],
67
+ ) -> Optional[Any]:
68
+ """Convert captured text using a provided function."""
69
69
  if (cap := self.capture(text)) is None:
70
70
  return None
71
71
  try:
72
72
  return convertor(cap)
73
- except (ValueError, SyntaxError) as e:
74
- logger.error(f"Failed to convert text using {convertor.__name__} to convert.\nerror: {e}\n {cap}")
73
+ except Exception as e: # noqa: BLE001
74
+ logger.error(f"Failed to convert text using {convertor.__name__}: {e}\n{cap}")
75
75
  return None
76
76
 
77
+ def validate_with[T, K, E](
78
+ self,
79
+ text: str,
80
+ target_type: Type[T],
81
+ elements_type: Optional[Type[E]] = None,
82
+ length: Optional[int] = None,
83
+ deserializer: Callable[[Union[str, Tuple[str, ...]]], K] = lambda x: ujson.loads(x) if isinstance(x, str) else ujson.loads(x[0]),
84
+ ) -> Optional[T]:
85
+ """Deserialize and validate the captured text against expected types."""
86
+ judges = [lambda obj: isinstance(obj, target_type)]
87
+ if elements_type:
88
+ judges.append(lambda obj: all(isinstance(e, elements_type) for e in obj))
89
+ if length:
90
+ judges.append(lambda obj: len(obj) == length)
91
+
92
+ if (out := self.convert_with(text, deserializer)) and all(j(out) for j in judges):
93
+ return out # type: ignore
94
+ return None
95
+
77
96
  @classmethod
97
+ @lru_cache(32)
78
98
  def capture_code_block(cls, language: str) -> Self:
79
- """Capture the first occurrence of a code block in the given text.
99
+ """Capture a code block of the given language."""
100
+ return cls(pattern=f"```{language}(.*?)```", capture_type=language)
80
101
 
81
- Args:
82
- language (str): The text containing the code block.
83
-
84
- Returns:
85
- Self: The instance of the class with the captured code block.
86
- """
87
- return cls(pattern=f"```{language}\n(.*?)\n```")
102
+ @classmethod
103
+ @lru_cache(32)
104
+ def capture_generic_block(cls, language: str) -> Self:
105
+ """Capture a generic block of the given language."""
106
+ return cls(
107
+ pattern=f"--- Start of {language} ---(.*?)--- End of {language} ---",
108
+ capture_type=language,
109
+ )
88
110
 
89
111
 
90
112
  JsonCapture = Capture.capture_code_block("json")
91
113
  PythonCapture = Capture.capture_code_block("python")
92
- MarkdownCapture = Capture.capture_code_block("markdown")
93
- CodeBlockCapture = Capture(pattern="```.*?\n(.*?)\n```")
114
+ GenericCapture = Capture.capture_generic_block("String")
Binary file