fabricatio 0.2.7.dev4__cp312-cp312-win_amd64.whl → 0.2.8__cp312-cp312-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 (49) hide show
  1. fabricatio/__init__.py +4 -11
  2. fabricatio/actions/article.py +226 -92
  3. fabricatio/actions/article_rag.py +86 -21
  4. fabricatio/actions/output.py +71 -3
  5. fabricatio/actions/rag.py +3 -3
  6. fabricatio/actions/rules.py +39 -0
  7. fabricatio/capabilities/advanced_judge.py +23 -0
  8. fabricatio/capabilities/censor.py +90 -0
  9. fabricatio/capabilities/check.py +195 -0
  10. fabricatio/capabilities/correct.py +160 -96
  11. fabricatio/capabilities/propose.py +20 -4
  12. fabricatio/capabilities/rag.py +5 -4
  13. fabricatio/capabilities/rating.py +68 -23
  14. fabricatio/capabilities/review.py +21 -190
  15. fabricatio/capabilities/task.py +9 -10
  16. fabricatio/config.py +11 -3
  17. fabricatio/fs/curd.py +4 -0
  18. fabricatio/models/action.py +24 -10
  19. fabricatio/models/adv_kwargs_types.py +25 -0
  20. fabricatio/models/extra/__init__.py +1 -0
  21. fabricatio/models/extra/advanced_judge.py +32 -0
  22. fabricatio/models/extra/article_base.py +324 -89
  23. fabricatio/models/extra/article_essence.py +49 -176
  24. fabricatio/models/extra/article_main.py +48 -127
  25. fabricatio/models/extra/article_outline.py +12 -152
  26. fabricatio/models/extra/article_proposal.py +29 -13
  27. fabricatio/models/extra/patches.py +7 -0
  28. fabricatio/models/extra/problem.py +153 -0
  29. fabricatio/models/extra/rule.py +65 -0
  30. fabricatio/models/generic.py +360 -88
  31. fabricatio/models/kwargs_types.py +23 -17
  32. fabricatio/models/role.py +4 -1
  33. fabricatio/models/task.py +1 -1
  34. fabricatio/models/tool.py +149 -14
  35. fabricatio/models/usages.py +61 -47
  36. fabricatio/models/utils.py +0 -46
  37. fabricatio/parser.py +7 -8
  38. fabricatio/rust.cp312-win_amd64.pyd +0 -0
  39. fabricatio/{_rust.pyi → rust.pyi} +50 -0
  40. fabricatio/{_rust_instances.py → rust_instances.py} +1 -1
  41. fabricatio/utils.py +54 -0
  42. fabricatio-0.2.8.data/scripts/tdown.exe +0 -0
  43. {fabricatio-0.2.7.dev4.dist-info → fabricatio-0.2.8.dist-info}/METADATA +2 -1
  44. fabricatio-0.2.8.dist-info/RECORD +58 -0
  45. fabricatio/_rust.cp312-win_amd64.pyd +0 -0
  46. fabricatio-0.2.7.dev4.data/scripts/tdown.exe +0 -0
  47. fabricatio-0.2.7.dev4.dist-info/RECORD +0 -47
  48. {fabricatio-0.2.7.dev4.dist-info → fabricatio-0.2.8.dist-info}/WHEEL +0 -0
  49. {fabricatio-0.2.7.dev4.dist-info → fabricatio-0.2.8.dist-info}/licenses/LICENSE +0 -0
fabricatio/actions/rag.py CHANGED
@@ -5,7 +5,7 @@ from typing import List, Optional
5
5
  from fabricatio.capabilities.rag import RAG
6
6
  from fabricatio.journal import logger
7
7
  from fabricatio.models.action import Action
8
- from fabricatio.models.generic import PrepareVectorization
8
+ from fabricatio.models.generic import Vectorizable
9
9
  from fabricatio.models.task import Task
10
10
  from questionary import text
11
11
 
@@ -15,7 +15,7 @@ class InjectToDB(Action, RAG):
15
15
 
16
16
  output_key: str = "collection_name"
17
17
 
18
- async def _execute[T: PrepareVectorization](
18
+ async def _execute[T: Vectorizable](
19
19
  self, to_inject: Optional[T] | List[Optional[T]], collection_name: str = "my_collection",override_inject:bool=False, **_
20
20
  ) -> Optional[str]:
21
21
  if not isinstance(to_inject, list):
@@ -27,7 +27,7 @@ class InjectToDB(Action, RAG):
27
27
  [
28
28
  t.prepare_vectorization(self.embedding_max_sequence_length)
29
29
  for t in to_inject
30
- if isinstance(t, PrepareVectorization)
30
+ if isinstance(t, Vectorizable)
31
31
  ],
32
32
  )
33
33
 
@@ -0,0 +1,39 @@
1
+ """A module containing the DraftRuleSet action."""
2
+
3
+ from typing import Optional
4
+
5
+ from fabricatio.capabilities.check import Check
6
+ from fabricatio.models.action import Action
7
+ from fabricatio.models.extra.rule import RuleSet
8
+ from fabricatio.utils import ok
9
+
10
+
11
+ class DraftRuleSet(Action, Check):
12
+ """Action to draft a ruleset based on a given requirement description."""
13
+
14
+ output_key: str = "drafted_ruleset"
15
+ """The key used to store the drafted ruleset in the context dictionary."""
16
+
17
+ ruleset_requirement: Optional[str] = None
18
+ """The natural language description of the desired ruleset characteristics."""
19
+ rule_count: int = 0
20
+ """The number of rules to generate in the ruleset (0 for no restriction)."""
21
+ async def _execute(
22
+ self,
23
+ ruleset_requirement: Optional[str]=None,
24
+ **_,
25
+ ) -> Optional[RuleSet]:
26
+ """Draft a ruleset based on the requirement description.
27
+
28
+ Args:
29
+ ruleset_requirement (str): Natural language description of desired ruleset characteristics
30
+ rule_count (int): Number of rules to generate (0 for no restriction)
31
+ **kwargs: Validation parameters for rule generation
32
+
33
+ Returns:
34
+ Optional[RuleSet]: Drafted ruleset object or None if generation fails
35
+ """
36
+ return await self.draft_ruleset(
37
+ ruleset_requirement=ok(ruleset_requirement or self.ruleset_requirement,"No ruleset requirement provided"),
38
+ rule_count=self.rule_count,
39
+ )
@@ -0,0 +1,23 @@
1
+ """The Capabilities module for advanced judging."""
2
+
3
+ from typing import Optional, Unpack
4
+
5
+ from fabricatio.capabilities.propose import Propose
6
+ from fabricatio.models.extra.advanced_judge import JudgeMent
7
+ from fabricatio.models.kwargs_types import ValidateKwargs
8
+
9
+
10
+ class AdvancedJudge(Propose):
11
+ """A class that judges the evidence and makes a final decision."""
12
+ async def evidently_judge(
13
+ self,
14
+ prompt: str,
15
+ **kwargs: Unpack[ValidateKwargs[JudgeMent]],
16
+ ) -> Optional[JudgeMent]:
17
+ """Judge the evidence and make a final decision."""
18
+ return await self.propose(
19
+ JudgeMent,
20
+ prompt,
21
+ **kwargs
22
+ )
23
+
@@ -0,0 +1,90 @@
1
+ """Module for censoring objects and strings based on provided rulesets.
2
+
3
+ This module includes the Censor class which inherits from both Correct and Check classes.
4
+ It provides methods to censor objects and strings by first checking them against a ruleset and then correcting them if necessary.
5
+ """
6
+
7
+ from typing import Optional, Unpack
8
+
9
+ from fabricatio.capabilities.check import Check
10
+ from fabricatio.capabilities.correct import Correct
11
+ from fabricatio.models.extra.rule import RuleSet
12
+ from fabricatio.models.generic import ProposedUpdateAble, SketchedAble
13
+ from fabricatio.models.kwargs_types import ReferencedKwargs
14
+ from fabricatio.utils import override_kwargs
15
+
16
+
17
+ class Censor(Correct, Check):
18
+ """Class to censor objects and strings based on provided rulesets.
19
+
20
+ Inherits from both Correct and Check classes.
21
+ Provides methods to censor objects and strings by first checking them against a ruleset and then correcting them if necessary.
22
+
23
+ Attributes:
24
+ ruleset (RuleSet): The ruleset to be used for censoring.
25
+ """
26
+
27
+ async def censor_obj[M: SketchedAble](
28
+ self, obj: M, ruleset: RuleSet, **kwargs: Unpack[ReferencedKwargs[M]]
29
+ ) -> Optional[M]:
30
+ """Censors an object based on the provided ruleset.
31
+
32
+ Args:
33
+ obj (M): The object to be censored.
34
+ ruleset (RuleSet): The ruleset to apply for censoring.
35
+ **kwargs: Additional keyword arguments to be passed to the check and correct methods.
36
+
37
+ Returns:
38
+ Optional[M]: The censored object if corrections were made, otherwise None.
39
+
40
+ Note:
41
+ This method first checks the object against the ruleset and then corrects it if necessary.
42
+ """
43
+ imp = await self.check_obj(obj, ruleset, **override_kwargs(kwargs, default=None))
44
+ if imp is None:
45
+ return imp
46
+ return await self.correct_obj(obj, imp, **kwargs)
47
+
48
+ async def censor_string(
49
+ self, input_text: str, ruleset: RuleSet, **kwargs: Unpack[ReferencedKwargs[str]]
50
+ ) -> Optional[str]:
51
+ """Censors a string based on the provided ruleset.
52
+
53
+ Args:
54
+ input_text (str): The string to be censored.
55
+ ruleset (RuleSet): The ruleset to apply for censoring.
56
+ **kwargs: Additional keyword arguments to be passed to the check and correct methods.
57
+
58
+ Returns:
59
+ Optional[str]: The censored string if corrections were made, otherwise None.
60
+
61
+ Note:
62
+ This method first checks the string against the ruleset and then corrects it if necessary.
63
+ """
64
+ imp = await self.check_string(input_text, ruleset, **override_kwargs(kwargs, default=None))
65
+ if imp is None:
66
+ return imp
67
+ return await self.correct_string(input_text, imp, **kwargs)
68
+
69
+ async def censor_obj_inplace[M: ProposedUpdateAble](
70
+ self, obj: M, ruleset: RuleSet, **kwargs: Unpack[ReferencedKwargs[M]]
71
+ ) -> Optional[M]:
72
+ """Censors an object in-place based on the provided ruleset.
73
+
74
+ This method modifies the object directly if corrections are needed.
75
+
76
+ Args:
77
+ obj (M): The object to be censored.
78
+ ruleset (RuleSet): The ruleset to apply for censoring.
79
+ **kwargs: Additional keyword arguments to be passed to the check and correct methods.
80
+
81
+ Returns:
82
+ Optional[M]: The censored object if corrections were made, otherwise None.
83
+
84
+ Note:
85
+ This method first checks the object against the ruleset and then corrects it in-place if necessary.
86
+ """
87
+ imp = await self.check_obj(obj, ruleset, **override_kwargs(kwargs, default=None))
88
+ if imp is None:
89
+ return imp
90
+ return await self.correct_obj_inplace(obj, improvement=imp, **kwargs)
@@ -0,0 +1,195 @@
1
+ """A class that provides the capability to check strings and objects against rules and guidelines."""
2
+
3
+ from typing import Optional, Unpack
4
+
5
+ from fabricatio import TEMPLATE_MANAGER
6
+ from fabricatio.capabilities.advanced_judge import AdvancedJudge
7
+ from fabricatio.capabilities.propose import Propose
8
+ from fabricatio.config import configs
9
+ from fabricatio.models.extra.patches import BriefingPatch
10
+ from fabricatio.models.extra.problem import Improvement
11
+ from fabricatio.models.extra.rule import Rule, RuleSet
12
+ from fabricatio.models.generic import Display, WithBriefing
13
+ from fabricatio.models.kwargs_types import ValidateKwargs
14
+ from fabricatio.utils import override_kwargs
15
+
16
+
17
+ class Check(AdvancedJudge, Propose):
18
+ """Class for validating strings/objects against predefined rules and guidelines.
19
+
20
+ This capability combines rule-based judgment and proposal generation to provide
21
+ structured validation results with actionable improvement suggestions.
22
+ """
23
+
24
+ async def draft_ruleset(
25
+ self, ruleset_requirement: str, rule_count: int = 0, **kwargs: Unpack[ValidateKwargs[Rule]]
26
+ ) -> Optional[RuleSet]:
27
+ """Generate rule set based on requirement description.
28
+
29
+ Args:
30
+ ruleset_requirement (str): Natural language description of desired ruleset characteristics
31
+ rule_count (int): Number of rules to generate (0 for default count)
32
+ **kwargs: Validation parameters for rule generation
33
+
34
+ Returns:
35
+ Optional[RuleSet]: Validated ruleset object or None if generation fails
36
+
37
+ Notes:
38
+ - Requires valid template configuration in configs.templates
39
+ - Returns None if any step in rule generation fails
40
+ - Uses `alist_str` for requirement breakdown and iterative rule proposal
41
+ """
42
+ rule_reqs = await self.alist_str(
43
+ TEMPLATE_MANAGER.render_template(
44
+ configs.templates.ruleset_requirement_breakdown_template, {"ruleset_requirement": ruleset_requirement}
45
+ ),
46
+ rule_count,
47
+ **override_kwargs(kwargs, default=None),
48
+ )
49
+
50
+ if rule_reqs is None:
51
+ return None
52
+
53
+ rules = await self.propose(Rule, rule_reqs, **kwargs)
54
+ if any(r for r in rules if r is None):
55
+ return None
56
+
57
+ ruleset_patch = await self.propose(
58
+ BriefingPatch,
59
+ f"# Rules Requirements\n{rule_reqs}\n# Generated Rules\n{Display.seq_display(rules)}\n\n"
60
+ f"You need to write a concise and detailed patch for this ruleset that can be applied to the ruleset nicely.\n"
61
+ f"Note that all fields in this patch will be directly copied to the ruleset obj, including `name` and `description`, so write when knowing the subject.\n",
62
+ **override_kwargs(kwargs, default=None),
63
+ )
64
+
65
+ if ruleset_patch is None:
66
+ return None
67
+
68
+ return ruleset_patch.apply(RuleSet(rules=rules, name="", description=""))
69
+
70
+ async def check_string_against_rule(
71
+ self,
72
+ input_text: str,
73
+ rule: Rule,
74
+ reference: str = "",
75
+ **kwargs: Unpack[ValidateKwargs[Improvement]],
76
+ ) -> Optional[Improvement]:
77
+ """Validate text against specific rule.
78
+
79
+ Args:
80
+ input_text (str): Text content to validate
81
+ rule (Rule): Rule instance for validation
82
+ reference (str): Reference text for comparison (default: "")
83
+ **kwargs: Configuration for validation process
84
+
85
+ Returns:
86
+ Optional[Improvement]: Suggested improvement if violation detected
87
+
88
+ Notes:
89
+ - Uses `evidently_judge` to determine violation presence
90
+ - Renders template using `check_string_template` for proposal
91
+ - Proposes Improvement only when violation is confirmed
92
+ """
93
+ if judge := await self.evidently_judge(
94
+ f"# Content to exam\n{input_text}\n\n# Rule Must to follow\n{rule.display()}\nDoes `Content to exam` provided above violate the `Rule Must to follow` provided above?",
95
+ **override_kwargs(kwargs, default=None),
96
+ ):
97
+ return await self.propose(
98
+ Improvement,
99
+ TEMPLATE_MANAGER.render_template(
100
+ configs.templates.check_string_template,
101
+ {"to_check": input_text, "rule": rule, "judge": judge.display(), "reference": reference},
102
+ ),
103
+ **kwargs,
104
+ )
105
+ return None
106
+
107
+ async def check_obj_against_rule[M: (Display, WithBriefing)](
108
+ self,
109
+ obj: M,
110
+ rule: Rule,
111
+ reference: str = "",
112
+ **kwargs: Unpack[ValidateKwargs[Improvement]],
113
+ ) -> Optional[Improvement]:
114
+ """Validate object against rule using text representation.
115
+
116
+ Args:
117
+ obj (M): Object implementing Display/WithBriefing interface
118
+ rule (Rule): Validation rule
119
+ reference (str): Reference text for comparison (default: "")
120
+ **kwargs: Validation configuration parameters
121
+
122
+ Returns:
123
+ Optional[Improvement]: Improvement suggestion if issues found
124
+
125
+ Notes:
126
+ - Requires obj to implement display() or briefing property
127
+ - Raises TypeError for incompatible object types
128
+ - Converts object to text before string validation
129
+ """
130
+ if isinstance(obj, Display):
131
+ input_text = obj.display()
132
+ elif isinstance(obj, WithBriefing):
133
+ input_text = obj.briefing
134
+ else:
135
+ raise TypeError("obj must be either Display or WithBriefing")
136
+
137
+ return await self.check_string_against_rule(input_text, rule, reference, **kwargs)
138
+
139
+ async def check_string(
140
+ self,
141
+ input_text: str,
142
+ ruleset: RuleSet,
143
+ reference: str = "",
144
+ **kwargs: Unpack[ValidateKwargs[Improvement]],
145
+ ) -> Optional[Improvement]:
146
+ """Validate text against full ruleset.
147
+
148
+ Args:
149
+ input_text (str): Text content to validate
150
+ ruleset (RuleSet): Collection of validation rules
151
+ reference (str): Reference text for comparison
152
+ **kwargs: Validation configuration parameters
153
+
154
+ Returns:
155
+ Optional[Improvement]: First detected improvement
156
+
157
+ Notes:
158
+ - Checks rules sequentially and returns first violation
159
+ - Halts validation after first successful improvement proposal
160
+ - Maintains rule execution order from ruleset.rules list
161
+ """
162
+ imp_seq = [
163
+ await self.check_string_against_rule(input_text, rule, reference, **kwargs) for rule in ruleset.rules
164
+ ]
165
+ if all(isinstance(i, Improvement) for i in imp_seq):
166
+ return Improvement.gather(*imp_seq) # pyright: ignore [reportArgumentType]
167
+ return None
168
+
169
+ async def check_obj[M: (Display, WithBriefing)](
170
+ self,
171
+ obj: M,
172
+ ruleset: RuleSet,
173
+ reference: str = "",
174
+ **kwargs: Unpack[ValidateKwargs[Improvement]],
175
+ ) -> Optional[Improvement]:
176
+ """Validate object against full ruleset.
177
+
178
+ Args:
179
+ obj (M): Object implementing Display/WithBriefing interface
180
+ ruleset (RuleSet): Collection of validation rules
181
+ reference (str): Reference text for comparison (default: "")
182
+ **kwargs: Validation configuration parameters
183
+
184
+ Returns:
185
+ Optional[Improvement]: First detected improvement
186
+
187
+ Notes:
188
+ - Uses check_obj_against_rule for individual rule checks
189
+ - Maintains same early termination behavior as check_string
190
+ - Validates object through text conversion mechanism
191
+ """
192
+ imp_seq = [await self.check_obj_against_rule(obj, rule, reference, **kwargs) for rule in ruleset.rules]
193
+ if all(isinstance(i, Improvement) for i in imp_seq):
194
+ return Improvement.gather(*imp_seq) # pyright: ignore [reportArgumentType]
195
+ return None