fabricatio 0.2.8.dev2__cp312-cp312-win_amd64.whl → 0.2.8.dev4__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.
- fabricatio/_rust.cp312-win_amd64.pyd +0 -0
- fabricatio/_rust.pyi +1 -1
- fabricatio/actions/article.py +112 -121
- fabricatio/actions/article_rag.py +55 -45
- fabricatio/actions/output.py +4 -3
- fabricatio/actions/rag.py +3 -3
- fabricatio/capabilities/censor.py +87 -0
- fabricatio/capabilities/check.py +194 -0
- fabricatio/capabilities/correct.py +142 -98
- fabricatio/capabilities/propose.py +20 -4
- fabricatio/capabilities/rag.py +3 -2
- fabricatio/capabilities/rating.py +67 -22
- fabricatio/capabilities/review.py +18 -187
- fabricatio/capabilities/task.py +8 -9
- fabricatio/config.py +11 -3
- fabricatio/models/action.py +8 -5
- fabricatio/models/adv_kwargs_types.py +25 -0
- fabricatio/models/extra/advanced_judge.py +10 -7
- fabricatio/models/extra/article_base.py +47 -27
- fabricatio/models/extra/article_essence.py +40 -209
- fabricatio/models/extra/article_main.py +3 -4
- fabricatio/models/extra/patches.py +7 -0
- fabricatio/models/extra/problem.py +153 -0
- fabricatio/models/extra/rule.py +21 -0
- fabricatio/models/generic.py +71 -37
- fabricatio/models/kwargs_types.py +23 -17
- fabricatio/models/role.py +4 -1
- fabricatio/models/usages.py +17 -20
- fabricatio/models/utils.py +0 -46
- fabricatio/parser.py +7 -8
- fabricatio/utils.py +54 -0
- {fabricatio-0.2.8.dev2.data → fabricatio-0.2.8.dev4.data}/scripts/tdown.exe +0 -0
- {fabricatio-0.2.8.dev2.dist-info → fabricatio-0.2.8.dev4.dist-info}/METADATA +2 -1
- fabricatio-0.2.8.dev4.dist-info/RECORD +56 -0
- fabricatio-0.2.8.dev2.dist-info/RECORD +0 -49
- {fabricatio-0.2.8.dev2.dist-info → fabricatio-0.2.8.dev4.dist-info}/WHEEL +0 -0
- {fabricatio-0.2.8.dev2.dist-info → fabricatio-0.2.8.dev4.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,87 @@
|
|
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
|
+
|
24
|
+
async def censor_obj[M: SketchedAble](
|
25
|
+
self, obj: M, ruleset: RuleSet, **kwargs: Unpack[ReferencedKwargs[M]]
|
26
|
+
) -> Optional[M]:
|
27
|
+
"""Censors an object based on the provided ruleset.
|
28
|
+
|
29
|
+
Args:
|
30
|
+
obj (M): The object to be censored.
|
31
|
+
ruleset (RuleSet): The ruleset to apply for censoring.
|
32
|
+
**kwargs: Additional keyword arguments to be passed to the check and correct methods.
|
33
|
+
|
34
|
+
Returns:
|
35
|
+
Optional[M]: The censored object if corrections were made, otherwise None.
|
36
|
+
|
37
|
+
Note:
|
38
|
+
This method first checks the object against the ruleset and then corrects it if necessary.
|
39
|
+
"""
|
40
|
+
imp = await self.check_obj(obj, ruleset, **override_kwargs(kwargs, default=None))
|
41
|
+
if imp is None:
|
42
|
+
return imp
|
43
|
+
return await self.correct_obj(obj, imp, **kwargs)
|
44
|
+
|
45
|
+
async def censor_string(
|
46
|
+
self, input_text: str, ruleset: RuleSet, **kwargs: Unpack[ReferencedKwargs[str]]
|
47
|
+
) -> Optional[str]:
|
48
|
+
"""Censors a string based on the provided ruleset.
|
49
|
+
|
50
|
+
Args:
|
51
|
+
input_text (str): The string to be censored.
|
52
|
+
ruleset (RuleSet): The ruleset to apply for censoring.
|
53
|
+
**kwargs: Additional keyword arguments to be passed to the check and correct methods.
|
54
|
+
|
55
|
+
Returns:
|
56
|
+
Optional[str]: The censored string if corrections were made, otherwise None.
|
57
|
+
|
58
|
+
Note:
|
59
|
+
This method first checks the string against the ruleset and then corrects it if necessary.
|
60
|
+
"""
|
61
|
+
imp = await self.check_string(input_text, ruleset, **override_kwargs(kwargs, default=None))
|
62
|
+
if imp is None:
|
63
|
+
return imp
|
64
|
+
return await self.correct_string(input_text, imp, **kwargs)
|
65
|
+
|
66
|
+
async def censor_obj_inplace[M: ProposedUpdateAble](
|
67
|
+
self, obj: M, ruleset: RuleSet, **kwargs: Unpack[ReferencedKwargs[M]]
|
68
|
+
) -> Optional[M]:
|
69
|
+
"""Censors an object in-place based on the provided ruleset.
|
70
|
+
|
71
|
+
This method modifies the object directly if corrections are needed.
|
72
|
+
|
73
|
+
Args:
|
74
|
+
obj (M): The object to be censored.
|
75
|
+
ruleset (RuleSet): The ruleset to apply for censoring.
|
76
|
+
**kwargs: Additional keyword arguments to be passed to the check and correct methods.
|
77
|
+
|
78
|
+
Returns:
|
79
|
+
Optional[M]: The censored object if corrections were made, otherwise None.
|
80
|
+
|
81
|
+
Note:
|
82
|
+
This method first checks the object against the ruleset and then corrects it in-place if necessary.
|
83
|
+
"""
|
84
|
+
imp = await self.check_obj(obj, ruleset, **override_kwargs(kwargs, default=None))
|
85
|
+
if imp is None:
|
86
|
+
return imp
|
87
|
+
return await self.correct_obj_inplace(obj, improvement=imp, **kwargs)
|
@@ -0,0 +1,194 @@
|
|
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",
|
61
|
+
**override_kwargs(kwargs, default=None),
|
62
|
+
)
|
63
|
+
|
64
|
+
if ruleset_patch is None:
|
65
|
+
return None
|
66
|
+
|
67
|
+
return ruleset_patch.apply(RuleSet(rules=rules, name="", description=""))
|
68
|
+
|
69
|
+
async def check_string_against_rule(
|
70
|
+
self,
|
71
|
+
input_text: str,
|
72
|
+
rule: Rule,
|
73
|
+
reference: str = "",
|
74
|
+
**kwargs: Unpack[ValidateKwargs[Improvement]],
|
75
|
+
) -> Optional[Improvement]:
|
76
|
+
"""Validate text against specific rule.
|
77
|
+
|
78
|
+
Args:
|
79
|
+
input_text (str): Text content to validate
|
80
|
+
rule (Rule): Rule instance for validation
|
81
|
+
reference (str): Reference text for comparison (default: "")
|
82
|
+
**kwargs: Configuration for validation process
|
83
|
+
|
84
|
+
Returns:
|
85
|
+
Optional[Improvement]: Suggested improvement if violation detected
|
86
|
+
|
87
|
+
Notes:
|
88
|
+
- Uses `evidently_judge` to determine violation presence
|
89
|
+
- Renders template using `check_string_template` for proposal
|
90
|
+
- Proposes Improvement only when violation is confirmed
|
91
|
+
"""
|
92
|
+
if judge := await self.evidently_judge(
|
93
|
+
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?",
|
94
|
+
**override_kwargs(kwargs, default=None),
|
95
|
+
):
|
96
|
+
return await self.propose(
|
97
|
+
Improvement,
|
98
|
+
TEMPLATE_MANAGER.render_template(
|
99
|
+
configs.templates.check_string_template,
|
100
|
+
{"to_check": input_text, "rule": rule, "judge": judge.display(), "reference": reference},
|
101
|
+
),
|
102
|
+
**kwargs,
|
103
|
+
)
|
104
|
+
return None
|
105
|
+
|
106
|
+
async def check_obj_against_rule[M: (Display, WithBriefing)](
|
107
|
+
self,
|
108
|
+
obj: M,
|
109
|
+
rule: Rule,
|
110
|
+
reference: str = "",
|
111
|
+
**kwargs: Unpack[ValidateKwargs[Improvement]],
|
112
|
+
) -> Optional[Improvement]:
|
113
|
+
"""Validate object against rule using text representation.
|
114
|
+
|
115
|
+
Args:
|
116
|
+
obj (M): Object implementing Display/WithBriefing interface
|
117
|
+
rule (Rule): Validation rule
|
118
|
+
reference (str): Reference text for comparison (default: "")
|
119
|
+
**kwargs: Validation configuration parameters
|
120
|
+
|
121
|
+
Returns:
|
122
|
+
Optional[Improvement]: Improvement suggestion if issues found
|
123
|
+
|
124
|
+
Notes:
|
125
|
+
- Requires obj to implement display() or briefing property
|
126
|
+
- Raises TypeError for incompatible object types
|
127
|
+
- Converts object to text before string validation
|
128
|
+
"""
|
129
|
+
if isinstance(obj, Display):
|
130
|
+
input_text = obj.display()
|
131
|
+
elif isinstance(obj, WithBriefing):
|
132
|
+
input_text = obj.briefing
|
133
|
+
else:
|
134
|
+
raise TypeError("obj must be either Display or WithBriefing")
|
135
|
+
|
136
|
+
return await self.check_string_against_rule(input_text, rule, reference, **kwargs)
|
137
|
+
|
138
|
+
async def check_string(
|
139
|
+
self,
|
140
|
+
input_text: str,
|
141
|
+
ruleset: RuleSet,
|
142
|
+
reference: str = "",
|
143
|
+
**kwargs: Unpack[ValidateKwargs[Improvement]],
|
144
|
+
) -> Optional[Improvement]:
|
145
|
+
"""Validate text against full ruleset.
|
146
|
+
|
147
|
+
Args:
|
148
|
+
input_text (str): Text content to validate
|
149
|
+
ruleset (RuleSet): Collection of validation rules
|
150
|
+
reference (str): Reference text for comparison
|
151
|
+
**kwargs: Validation configuration parameters
|
152
|
+
|
153
|
+
Returns:
|
154
|
+
Optional[Improvement]: First detected improvement
|
155
|
+
|
156
|
+
Notes:
|
157
|
+
- Checks rules sequentially and returns first violation
|
158
|
+
- Halts validation after first successful improvement proposal
|
159
|
+
- Maintains rule execution order from ruleset.rules list
|
160
|
+
"""
|
161
|
+
imp_seq = [
|
162
|
+
await self.check_string_against_rule(input_text, rule, reference, **kwargs) for rule in ruleset.rules
|
163
|
+
]
|
164
|
+
if all(isinstance(i, Improvement) for i in imp_seq):
|
165
|
+
return Improvement.gather(*imp_seq) # pyright: ignore [reportArgumentType]
|
166
|
+
return None
|
167
|
+
|
168
|
+
async def check_obj[M: (Display, WithBriefing)](
|
169
|
+
self,
|
170
|
+
obj: M,
|
171
|
+
ruleset: RuleSet,
|
172
|
+
reference: str = "",
|
173
|
+
**kwargs: Unpack[ValidateKwargs[Improvement]],
|
174
|
+
) -> Optional[Improvement]:
|
175
|
+
"""Validate object against full ruleset.
|
176
|
+
|
177
|
+
Args:
|
178
|
+
obj (M): Object implementing Display/WithBriefing interface
|
179
|
+
ruleset (RuleSet): Collection of validation rules
|
180
|
+
reference (str): Reference text for comparison (default: "")
|
181
|
+
**kwargs: Validation configuration parameters
|
182
|
+
|
183
|
+
Returns:
|
184
|
+
Optional[Improvement]: First detected improvement
|
185
|
+
|
186
|
+
Notes:
|
187
|
+
- Uses check_obj_against_rule for individual rule checks
|
188
|
+
- Maintains same early termination behavior as check_string
|
189
|
+
- Validates object through text conversion mechanism
|
190
|
+
"""
|
191
|
+
imp_seq = [await self.check_obj_against_rule(obj, rule, reference, **kwargs) for rule in ruleset.rules]
|
192
|
+
if all(isinstance(i, Improvement) for i in imp_seq):
|
193
|
+
return Improvement.gather(*imp_seq) # pyright: ignore [reportArgumentType]
|
194
|
+
return None
|
@@ -1,159 +1,203 @@
|
|
1
|
-
"""Correct capability
|
1
|
+
"""A module containing the Correct capability for reviewing, validating, and improving objects."""
|
2
2
|
|
3
|
-
|
4
|
-
to provide mechanisms for reviewing, validating, and correcting various objects and tasks
|
5
|
-
based on predefined criteria and templates.
|
6
|
-
"""
|
7
|
-
|
8
|
-
from typing import Optional, Unpack, cast
|
3
|
+
from typing import Optional, Type, Unpack, cast
|
9
4
|
|
10
5
|
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
11
|
-
from fabricatio.capabilities.
|
6
|
+
from fabricatio.capabilities.propose import Propose
|
7
|
+
from fabricatio.capabilities.rating import Rating
|
12
8
|
from fabricatio.config import configs
|
13
|
-
from fabricatio.
|
14
|
-
from fabricatio.models.
|
15
|
-
from fabricatio.models.
|
16
|
-
from
|
17
|
-
from
|
9
|
+
from fabricatio.journal import logger
|
10
|
+
from fabricatio.models.adv_kwargs_types import CorrectKwargs
|
11
|
+
from fabricatio.models.extra.problem import Improvement, ProblemSolutions
|
12
|
+
from fabricatio.models.generic import ProposedUpdateAble, SketchedAble
|
13
|
+
from fabricatio.models.kwargs_types import (
|
14
|
+
BestKwargs,
|
15
|
+
ValidateKwargs,
|
16
|
+
)
|
17
|
+
from fabricatio.utils import ok, override_kwargs
|
18
|
+
|
19
|
+
|
20
|
+
class Correct(Rating, Propose):
|
21
|
+
"""A class that provides the capability to correct objects."""
|
22
|
+
|
23
|
+
async def decide_solution(
|
24
|
+
self, problem_solutions: ProblemSolutions, **kwargs: Unpack[BestKwargs]
|
25
|
+
) -> ProblemSolutions:
|
26
|
+
"""Decide the best solution from a list of problem solutions.
|
27
|
+
|
28
|
+
Args:
|
29
|
+
problem_solutions (ProblemSolutions): The problem solutions to evaluate.
|
30
|
+
**kwargs (Unpack[BestKwargs]): Additional keyword arguments for the decision process.
|
18
31
|
|
32
|
+
Returns:
|
33
|
+
ProblemSolutions: The problem solutions with the best solution selected.
|
34
|
+
"""
|
35
|
+
if (leng := len(problem_solutions.solutions)) == 0:
|
36
|
+
logger.error(f"No solutions found in ProblemSolutions, Skip: {problem_solutions.problem}")
|
37
|
+
if leng > 1:
|
38
|
+
problem_solutions.solutions = await self.best(problem_solutions.solutions, **kwargs)
|
39
|
+
return problem_solutions
|
19
40
|
|
20
|
-
|
21
|
-
|
41
|
+
async def decide_improvement(self, improvement: Improvement, **kwargs: Unpack[BestKwargs]) -> Improvement:
|
42
|
+
"""Decide the best solution for each problem solution in an improvement.
|
22
43
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
the required interfaces, applying corrections based on templated review processes.
|
27
|
-
"""
|
44
|
+
Args:
|
45
|
+
improvement (Improvement): The improvement containing problem solutions to evaluate.
|
46
|
+
**kwargs (Unpack[BestKwargs]): Additional keyword arguments for the decision process.
|
28
47
|
|
29
|
-
|
48
|
+
Returns:
|
49
|
+
Improvement: The improvement with the best solutions selected for each problem solution.
|
50
|
+
"""
|
51
|
+
if (leng := len(improvement.problem_solutions)) == 0:
|
52
|
+
logger.error(f"No problem_solutions found in Improvement, Skip: {improvement}")
|
53
|
+
if leng > 1:
|
54
|
+
for ps in improvement.problem_solutions:
|
55
|
+
ps.solutions = await self.best(ps.solutions, **kwargs)
|
56
|
+
return improvement
|
57
|
+
|
58
|
+
async def fix_troubled_obj[M: SketchedAble](
|
30
59
|
self,
|
31
60
|
obj: M,
|
61
|
+
problem_solutions: ProblemSolutions,
|
32
62
|
reference: str = "",
|
33
|
-
|
34
|
-
**kwargs: Unpack[ReviewKwargs[ReviewResult[str]]],
|
63
|
+
**kwargs: Unpack[ValidateKwargs[M]],
|
35
64
|
) -> Optional[M]:
|
36
|
-
"""
|
37
|
-
|
38
|
-
This method first conducts a review of the given object, then uses the review results
|
39
|
-
to generate a corrected version of the object using appropriate templates.
|
65
|
+
"""Fix a troubled object based on problem solutions.
|
40
66
|
|
41
67
|
Args:
|
42
|
-
obj (M): The object to be
|
68
|
+
obj (M): The object to be fixed.
|
69
|
+
problem_solutions (ProblemSolutions): The problem solutions to apply.
|
43
70
|
reference (str): A reference or contextual information for the object.
|
44
|
-
|
45
|
-
**kwargs: Review configuration parameters including criteria and review options.
|
71
|
+
**kwargs (Unpack[ValidateKwargs[M]]): Additional keyword arguments for the validation process.
|
46
72
|
|
47
73
|
Returns:
|
48
|
-
Optional[M]:
|
49
|
-
|
50
|
-
Raises:
|
51
|
-
TypeError: If the provided object doesn't implement Display or WithBriefing interfaces.
|
74
|
+
Optional[M]: The fixed object, or None if fixing fails.
|
52
75
|
"""
|
53
|
-
if not isinstance(obj, (Display, WithBriefing)):
|
54
|
-
raise TypeError(f"Expected Display or WithBriefing, got {type(obj)}")
|
55
|
-
|
56
|
-
review_res = await self.review_obj(obj, **kwargs)
|
57
|
-
if supervisor_check:
|
58
|
-
await review_res.supervisor_check()
|
59
|
-
if "default" in kwargs:
|
60
|
-
cast("ReviewKwargs[None]", kwargs)["default"] = None
|
61
76
|
return await self.propose(
|
62
|
-
obj.__class__,
|
77
|
+
cast("Type[M]", obj.__class__),
|
63
78
|
TEMPLATE_MANAGER.render_template(
|
64
|
-
configs.templates.
|
79
|
+
configs.templates.fix_troubled_obj_template,
|
65
80
|
{
|
66
|
-
"
|
67
|
-
"
|
81
|
+
"problem": problem_solutions.problem,
|
82
|
+
"solution": ok(
|
83
|
+
problem_solutions.final_solution(),
|
84
|
+
f"No solution found for problem: {problem_solutions.problem}",
|
85
|
+
),
|
86
|
+
"reference": reference,
|
68
87
|
},
|
69
88
|
),
|
70
89
|
**kwargs,
|
71
90
|
)
|
72
91
|
|
73
|
-
async def
|
74
|
-
self,
|
92
|
+
async def fix_troubled_string(
|
93
|
+
self,
|
94
|
+
input_text: str,
|
95
|
+
problem_solutions: ProblemSolutions,
|
96
|
+
reference: str = "",
|
97
|
+
**kwargs: Unpack[ValidateKwargs[str]],
|
75
98
|
) -> Optional[str]:
|
76
|
-
"""
|
77
|
-
|
78
|
-
This method applies the review process to the input text and generates
|
79
|
-
a corrected version based on the review results.
|
99
|
+
"""Fix a troubled string based on problem solutions.
|
80
100
|
|
81
101
|
Args:
|
82
|
-
input_text (str): The
|
83
|
-
|
84
|
-
|
102
|
+
input_text (str): The string to be fixed.
|
103
|
+
problem_solutions (ProblemSolutions): The problem solutions to apply.
|
104
|
+
reference (str): A reference or contextual information for the string.
|
105
|
+
**kwargs (Unpack[ValidateKwargs[str]]): Additional keyword arguments for the validation process.
|
85
106
|
|
86
107
|
Returns:
|
87
|
-
Optional[str]: The
|
108
|
+
Optional[str]: The fixed string, or None if fixing fails.
|
88
109
|
"""
|
89
|
-
review_res = await self.review_string(input_text, **kwargs)
|
90
|
-
if supervisor_check:
|
91
|
-
await review_res.supervisor_check()
|
92
|
-
|
93
|
-
if "default" in kwargs:
|
94
|
-
cast("ReviewKwargs[None]", kwargs)["default"] = None
|
95
110
|
return await self.ageneric_string(
|
96
111
|
TEMPLATE_MANAGER.render_template(
|
97
|
-
configs.templates.
|
112
|
+
configs.templates.fix_troubled_string_template,
|
113
|
+
{
|
114
|
+
"problem": problem_solutions.problem,
|
115
|
+
"solution": ok(
|
116
|
+
problem_solutions.final_solution(),
|
117
|
+
f"No solution found for problem: {problem_solutions.problem}",
|
118
|
+
),
|
119
|
+
"reference": reference,
|
120
|
+
"string_to_fix": input_text,
|
121
|
+
},
|
98
122
|
),
|
99
123
|
**kwargs,
|
100
124
|
)
|
101
125
|
|
102
|
-
async def
|
103
|
-
self,
|
104
|
-
|
105
|
-
|
126
|
+
async def correct_obj[M: SketchedAble](
|
127
|
+
self,
|
128
|
+
obj: M,
|
129
|
+
improvement: Improvement,
|
130
|
+
reference: str = "",
|
131
|
+
**kwargs: Unpack[ValidateKwargs[M]],
|
132
|
+
) -> Optional[M]:
|
133
|
+
"""Review and correct an object based on defined criteria and templates.
|
106
134
|
|
107
|
-
This
|
108
|
-
|
135
|
+
This method first conducts a review of the given object, then uses the review results
|
136
|
+
to generate a corrected version of the object using appropriate templates.
|
109
137
|
|
110
138
|
Args:
|
111
|
-
|
112
|
-
|
139
|
+
obj (M): The object to be reviewed and corrected. Must implement ProposedAble.
|
140
|
+
improvement (Improvement): The improvement object containing the review results.
|
141
|
+
reference (str): A reference or contextual information for the object.
|
142
|
+
**kwargs (Unpack[ValidateKwargs[M]]): Review configuration parameters including criteria and review options.
|
113
143
|
|
114
144
|
Returns:
|
115
|
-
Optional[
|
145
|
+
Optional[M]: A corrected version of the input object, or None if correction fails.
|
146
|
+
|
147
|
+
Raises:
|
148
|
+
TypeError: If the provided object doesn't implement Display or WithBriefing interfaces.
|
116
149
|
"""
|
117
|
-
|
150
|
+
if not improvement.decided():
|
151
|
+
improvement = await self.decide_improvement(improvement, **override_kwargs(kwargs, default=None))
|
152
|
+
|
153
|
+
for ps in improvement.problem_solutions:
|
154
|
+
fixed_obj = await self.fix_troubled_obj(obj, ps, reference, **kwargs)
|
155
|
+
if fixed_obj is None:
|
156
|
+
logger.error(
|
157
|
+
f"Failed to fix troubling obj {obj.__class__.__name__} when deal with problem: {ps.problem}",
|
158
|
+
)
|
159
|
+
return None
|
160
|
+
obj = fixed_obj
|
161
|
+
return obj
|
162
|
+
|
163
|
+
async def correct_string(
|
164
|
+
self, input_text: str, improvement: Improvement, reference: str = "", **kwargs: Unpack[ValidateKwargs[str]]
|
165
|
+
) -> Optional[str]:
|
166
|
+
"""Review and correct a string based on defined criteria and templates.
|
118
167
|
|
119
|
-
|
120
|
-
|
121
|
-
) -> M:
|
122
|
-
"""Censor and correct an object based on defined criteria and templates.
|
168
|
+
This method first conducts a review of the given string, then uses the review results
|
169
|
+
to generate a corrected version of the string using appropriate templates.
|
123
170
|
|
124
171
|
Args:
|
125
|
-
|
126
|
-
|
172
|
+
input_text (str): The string to be reviewed and corrected.
|
173
|
+
improvement (Improvement): The improvement object containing the review results.
|
174
|
+
reference (str): A reference or contextual information for the string.
|
175
|
+
**kwargs (Unpack[ValidateKwargs[str]]): Review configuration parameters including criteria and review options.
|
127
176
|
|
128
177
|
Returns:
|
129
|
-
|
178
|
+
Optional[str]: A corrected version of the input string, or None if correction fails.
|
130
179
|
"""
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
last_modified_obj,
|
140
|
-
topic=topic,
|
141
|
-
**kwargs,
|
180
|
+
if not improvement.decided():
|
181
|
+
improvement = await self.decide_improvement(improvement, **override_kwargs(kwargs, default=None))
|
182
|
+
|
183
|
+
for ps in improvement.problem_solutions:
|
184
|
+
fixed_string = await self.fix_troubled_string(input_text, ps, reference, **kwargs)
|
185
|
+
if fixed_string is None:
|
186
|
+
logger.error(
|
187
|
+
f"Failed to fix troubling string when deal with problem: {ps.problem}",
|
142
188
|
)
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
rprint(last_modified_obj.finalized_dump())
|
147
|
-
return modified_obj or last_modified_obj
|
189
|
+
return None
|
190
|
+
input_text = fixed_string
|
191
|
+
return input_text
|
148
192
|
|
149
193
|
async def correct_obj_inplace[M: ProposedUpdateAble](
|
150
|
-
self, obj: M, **kwargs: Unpack[CorrectKwargs[
|
194
|
+
self, obj: M, **kwargs: Unpack[CorrectKwargs[M]]
|
151
195
|
) -> Optional[M]:
|
152
196
|
"""Correct an object in place based on defined criteria and templates.
|
153
197
|
|
154
198
|
Args:
|
155
199
|
obj (M): The object to be corrected.
|
156
|
-
**kwargs (Unpack[
|
200
|
+
**kwargs (Unpack[CorrectKwargs[M]]): Additional keyword arguments for the correction process.
|
157
201
|
|
158
202
|
Returns:
|
159
203
|
Optional[M]: The corrected object, or None if correction fails.
|
@@ -10,28 +10,43 @@ from fabricatio.models.usages import LLMUsage
|
|
10
10
|
class Propose(LLMUsage):
|
11
11
|
"""A class that proposes an Obj based on a prompt."""
|
12
12
|
|
13
|
+
@overload
|
14
|
+
async def propose[M: ProposedAble](
|
15
|
+
self,
|
16
|
+
cls: Type[M],
|
17
|
+
prompt: List[str],
|
18
|
+
**kwargs: Unpack[ValidateKwargs[None]],
|
19
|
+
) -> List[Optional[M]]: ...
|
20
|
+
|
13
21
|
@overload
|
14
22
|
async def propose[M: ProposedAble](
|
15
23
|
self,
|
16
24
|
cls: Type[M],
|
17
25
|
prompt: List[str],
|
18
26
|
**kwargs: Unpack[ValidateKwargs[M]],
|
19
|
-
) ->
|
27
|
+
) -> List[M]: ...
|
20
28
|
|
21
29
|
@overload
|
22
30
|
async def propose[M: ProposedAble](
|
23
31
|
self,
|
24
32
|
cls: Type[M],
|
25
33
|
prompt: str,
|
26
|
-
**kwargs: Unpack[ValidateKwargs[
|
34
|
+
**kwargs: Unpack[ValidateKwargs[None]],
|
27
35
|
) -> Optional[M]: ...
|
36
|
+
@overload
|
37
|
+
async def propose[M: ProposedAble](
|
38
|
+
self,
|
39
|
+
cls: Type[M],
|
40
|
+
prompt: str,
|
41
|
+
**kwargs: Unpack[ValidateKwargs[M]],
|
42
|
+
) -> M: ...
|
28
43
|
|
29
44
|
async def propose[M: ProposedAble](
|
30
45
|
self,
|
31
46
|
cls: Type[M],
|
32
47
|
prompt: List[str] | str,
|
33
|
-
**kwargs: Unpack[ValidateKwargs[M]],
|
34
|
-
) -> Optional[List[M] | M]:
|
48
|
+
**kwargs: Unpack[ValidateKwargs[Optional[M]]],
|
49
|
+
) -> Optional[M] | List[Optional[M]] | M | List[M]:
|
35
50
|
"""Asynchronously proposes a task based on a given prompt and parameters.
|
36
51
|
|
37
52
|
Parameters:
|
@@ -47,3 +62,4 @@ class Propose(LLMUsage):
|
|
47
62
|
validator=cls.instantiate_from_string,
|
48
63
|
**kwargs,
|
49
64
|
)
|
65
|
+
|
fabricatio/capabilities/rag.py
CHANGED
@@ -22,7 +22,8 @@ from fabricatio.models.kwargs_types import (
|
|
22
22
|
RetrievalKwargs,
|
23
23
|
)
|
24
24
|
from fabricatio.models.usages import EmbeddingUsage
|
25
|
-
from fabricatio.models.utils import MilvusData
|
25
|
+
from fabricatio.models.utils import MilvusData
|
26
|
+
from fabricatio.utils import ok
|
26
27
|
from more_itertools.recipes import flatten, unique
|
27
28
|
from pydantic import Field, PrivateAttr
|
28
29
|
|
@@ -376,7 +377,7 @@ class RAG(EmbeddingUsage):
|
|
376
377
|
Returns:
|
377
378
|
List[str]: A list of refined questions.
|
378
379
|
"""
|
379
|
-
return await self.
|
380
|
+
return await self.alist_str(
|
380
381
|
TEMPLATE_MANAGER.render_template(
|
381
382
|
configs.templates.refined_query_template,
|
382
383
|
{"question": [question] if isinstance(question, str) else question},
|