fabricatio 0.2.9.dev1__cp312-cp312-manylinux_2_34_x86_64.whl → 0.2.9.dev3__cp312-cp312-manylinux_2_34_x86_64.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/actions/article.py +82 -101
- fabricatio/actions/article_rag.py +7 -9
- fabricatio/actions/output.py +60 -4
- fabricatio/actions/rules.py +37 -4
- fabricatio/capabilities/censor.py +22 -8
- fabricatio/capabilities/check.py +30 -18
- fabricatio/capabilities/correct.py +32 -12
- fabricatio/capabilities/rating.py +8 -8
- fabricatio/config.py +1 -1
- fabricatio/models/action.py +49 -34
- fabricatio/models/extra/article_base.py +83 -19
- fabricatio/models/extra/article_main.py +26 -8
- fabricatio/models/extra/article_outline.py +2 -3
- fabricatio/models/extra/article_proposal.py +4 -4
- fabricatio/models/extra/problem.py +20 -7
- fabricatio/models/extra/rule.py +16 -4
- fabricatio/models/generic.py +91 -40
- fabricatio/models/usages.py +1 -12
- fabricatio/rust.cpython-312-x86_64-linux-gnu.so +0 -0
- fabricatio/rust.pyi +45 -5
- fabricatio/utils.py +5 -5
- fabricatio/workflows/articles.py +3 -5
- {fabricatio-0.2.9.dev1.data → fabricatio-0.2.9.dev3.data}/scripts/tdown +0 -0
- {fabricatio-0.2.9.dev1.dist-info → fabricatio-0.2.9.dev3.dist-info}/METADATA +1 -1
- {fabricatio-0.2.9.dev1.dist-info → fabricatio-0.2.9.dev3.dist-info}/RECORD +27 -27
- {fabricatio-0.2.9.dev1.dist-info → fabricatio-0.2.9.dev3.dist-info}/WHEEL +0 -0
- {fabricatio-0.2.9.dev1.dist-info → fabricatio-0.2.9.dev3.dist-info}/licenses/LICENSE +0 -0
fabricatio/actions/article.py
CHANGED
@@ -2,24 +2,23 @@
|
|
2
2
|
|
3
3
|
from asyncio import gather
|
4
4
|
from pathlib import Path
|
5
|
-
from typing import
|
5
|
+
from typing import Callable, List, Optional
|
6
6
|
|
7
7
|
from more_itertools import filter_map
|
8
8
|
|
9
9
|
from fabricatio.capabilities.censor import Censor
|
10
|
-
from fabricatio.capabilities.correct import Correct
|
11
10
|
from fabricatio.capabilities.propose import Propose
|
12
11
|
from fabricatio.fs import safe_text_read
|
13
12
|
from fabricatio.journal import logger
|
14
13
|
from fabricatio.models.action import Action
|
15
|
-
from fabricatio.models.extra.article_base import
|
14
|
+
from fabricatio.models.extra.article_base import SubSectionBase
|
16
15
|
from fabricatio.models.extra.article_essence import ArticleEssence
|
17
16
|
from fabricatio.models.extra.article_main import Article
|
18
17
|
from fabricatio.models.extra.article_outline import ArticleOutline
|
19
18
|
from fabricatio.models.extra.article_proposal import ArticleProposal
|
20
19
|
from fabricatio.models.extra.rule import RuleSet
|
21
20
|
from fabricatio.models.task import Task
|
22
|
-
from fabricatio.rust import BibManager
|
21
|
+
from fabricatio.rust import BibManager, detect_language
|
23
22
|
from fabricatio.utils import ok
|
24
23
|
|
25
24
|
|
@@ -53,7 +52,7 @@ class ExtractArticleEssence(Action, Propose):
|
|
53
52
|
for ess in await self.propose(
|
54
53
|
ArticleEssence,
|
55
54
|
[
|
56
|
-
f"{c}\n\n\nBased the provided academic article above, you need to extract the essence from it
|
55
|
+
f"{c}\n\n\nBased the provided academic article above, you need to extract the essence from it.\n\nWrite the value string using `{detect_language(c)}`"
|
57
56
|
for c in contents
|
58
57
|
],
|
59
58
|
):
|
@@ -113,21 +112,20 @@ class GenerateArticleProposal(Action, Propose):
|
|
113
112
|
logger.error("Task not approved, since all inputs are None.")
|
114
113
|
return None
|
115
114
|
|
115
|
+
briefing = article_briefing or safe_text_read(
|
116
|
+
ok(
|
117
|
+
article_briefing_path
|
118
|
+
or await self.awhich_pathstr(
|
119
|
+
f"{ok(task_input).briefing}\nExtract the path of file which contains the article briefing."
|
120
|
+
),
|
121
|
+
"Could not find the path of file to read.",
|
122
|
+
)
|
123
|
+
)
|
124
|
+
|
116
125
|
proposal = ok(
|
117
126
|
await self.propose(
|
118
127
|
ArticleProposal,
|
119
|
-
briefing
|
120
|
-
article_briefing
|
121
|
-
or safe_text_read(
|
122
|
-
ok(
|
123
|
-
article_briefing_path
|
124
|
-
or await self.awhich_pathstr(
|
125
|
-
f"{ok(task_input).briefing}\nExtract the path of file which contains the article briefing."
|
126
|
-
),
|
127
|
-
"Could not find the path of file to read.",
|
128
|
-
)
|
129
|
-
)
|
130
|
-
),
|
128
|
+
f"{briefing}\n\nWrite the value string using `{detect_language(briefing)}`",
|
131
129
|
),
|
132
130
|
"Could not generate the proposal.",
|
133
131
|
).update_ref(briefing)
|
@@ -151,7 +149,8 @@ class GenerateInitialOutline(Action, Propose):
|
|
151
149
|
return ok(
|
152
150
|
await self.propose(
|
153
151
|
ArticleOutline,
|
154
|
-
article_proposal.as_prompt()
|
152
|
+
f"{(p := article_proposal.as_prompt())}\n\nNote that you should use `{detect_language(p)}` to write the `ArticleOutline`\n"
|
153
|
+
f"You Must make sure every chapter have sections, and every section have subsections.",
|
155
154
|
),
|
156
155
|
"Could not generate the initial outline.",
|
157
156
|
).update_ref(article_proposal)
|
@@ -165,25 +164,33 @@ class FixIntrospectedErrors(Action, Censor):
|
|
165
164
|
|
166
165
|
ruleset: Optional[RuleSet] = None
|
167
166
|
"""The ruleset to use to fix the introspected errors."""
|
167
|
+
max_error_count: Optional[int] = None
|
168
|
+
"""The maximum number of errors to fix."""
|
168
169
|
|
169
170
|
async def _execute(
|
170
171
|
self,
|
171
172
|
article_outline: ArticleOutline,
|
172
|
-
|
173
|
+
intro_fix_ruleset: Optional[RuleSet] = None,
|
173
174
|
**_,
|
174
175
|
) -> Optional[ArticleOutline]:
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
176
|
+
counter = 0
|
177
|
+
origin = article_outline
|
178
|
+
while pack := article_outline.gather_introspected():
|
179
|
+
logger.info(f"Found {counter}th introspected errors")
|
180
|
+
logger.warning(f"Found introspected error: {pack}")
|
181
|
+
article_outline = ok(
|
179
182
|
await self.censor_obj(
|
180
|
-
|
181
|
-
ruleset=ok(
|
182
|
-
reference=f"
|
183
|
+
article_outline,
|
184
|
+
ruleset=ok(intro_fix_ruleset or self.ruleset, "No ruleset provided"),
|
185
|
+
reference=f"{article_outline.as_prompt()}\n # Fatal Error of the Original Article Outline\n{pack}",
|
183
186
|
),
|
184
187
|
"Could not correct the component.",
|
185
|
-
)
|
186
|
-
|
188
|
+
).update_ref(origin)
|
189
|
+
|
190
|
+
if self.max_error_count and counter > self.max_error_count:
|
191
|
+
logger.warning("Max error count reached, stopping.")
|
192
|
+
break
|
193
|
+
counter += 1
|
187
194
|
|
188
195
|
return article_outline
|
189
196
|
|
@@ -196,27 +203,36 @@ class FixIllegalReferences(Action, Censor):
|
|
196
203
|
|
197
204
|
ruleset: Optional[RuleSet] = None
|
198
205
|
"""Ruleset to use to fix the illegal references."""
|
206
|
+
max_error_count: Optional[int] = None
|
207
|
+
"""The maximum number of errors to fix."""
|
199
208
|
|
200
209
|
async def _execute(
|
201
210
|
self,
|
202
211
|
article_outline: ArticleOutline,
|
203
|
-
|
212
|
+
ref_fix_ruleset: Optional[RuleSet] = None,
|
204
213
|
**_,
|
205
214
|
) -> Optional[ArticleOutline]:
|
215
|
+
counter = 0
|
206
216
|
while pack := article_outline.find_illegal_ref(gather_identical=True):
|
207
|
-
|
217
|
+
logger.info(f"Found {counter}th illegal references")
|
218
|
+
ref_seq, err = ok(pack)
|
208
219
|
logger.warning(f"Found illegal referring error: {err}")
|
209
|
-
|
220
|
+
new = ok(
|
210
221
|
await self.censor_obj(
|
211
|
-
|
212
|
-
ruleset=ok(
|
213
|
-
reference=f"
|
214
|
-
)
|
222
|
+
ref_seq[0],
|
223
|
+
ruleset=ok(ref_fix_ruleset or self.ruleset, "No ruleset provided"),
|
224
|
+
reference=f"{article_outline.as_prompt()}\n# Some Basic errors found that need to be fixed\n{err}",
|
225
|
+
),
|
226
|
+
"Could not correct the component",
|
215
227
|
)
|
216
|
-
for
|
217
|
-
|
228
|
+
for r in ref_seq:
|
229
|
+
r.update_from(new)
|
230
|
+
if self.max_error_count and counter > self.max_error_count:
|
231
|
+
logger.warning("Max error count reached, stopping.")
|
232
|
+
break
|
233
|
+
counter += 1
|
218
234
|
|
219
|
-
return article_outline
|
235
|
+
return article_outline
|
220
236
|
|
221
237
|
|
222
238
|
class TweakOutlineForwardRef(Action, Censor):
|
@@ -230,32 +246,36 @@ class TweakOutlineForwardRef(Action, Censor):
|
|
230
246
|
"""Ruleset to use to fix the illegal references."""
|
231
247
|
|
232
248
|
async def _execute(
|
233
|
-
self, article_outline: ArticleOutline,
|
249
|
+
self, article_outline: ArticleOutline, ref_twk_ruleset: Optional[RuleSet] = None, **cxt
|
234
250
|
) -> ArticleOutline:
|
235
251
|
return await self._inner(
|
236
252
|
article_outline,
|
237
|
-
ruleset=ok(
|
253
|
+
ruleset=ok(ref_twk_ruleset or self.ruleset, "No ruleset provided"),
|
238
254
|
field_name="support_to",
|
239
255
|
)
|
240
256
|
|
241
257
|
async def _inner(self, article_outline: ArticleOutline, ruleset: RuleSet, field_name: str) -> ArticleOutline:
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
):
|
247
|
-
patch = ArticleRefSequencePatch.default()
|
248
|
-
patch.tweaked = getattr(a, field_name)
|
249
|
-
|
250
|
-
await self.censor_obj_inplace(
|
251
|
-
patch,
|
252
|
-
ruleset=ruleset,
|
253
|
-
reference=f"{article_outline.as_prompt()}\n"
|
254
|
-
f"The Article component titled `{a.title}` whose `{field_name}` field needs to be extended or tweaked.\n"
|
255
|
-
f"# Judgement\n{judge.display()}",
|
256
|
-
)
|
258
|
+
await gather(
|
259
|
+
*[self._loop(a[-1], article_outline, field_name, ruleset) for a in article_outline.iter_subsections()],
|
260
|
+
)
|
261
|
+
|
257
262
|
return article_outline
|
258
263
|
|
264
|
+
async def _loop(
|
265
|
+
self, a: SubSectionBase, article_outline: ArticleOutline, field_name: str, ruleset: RuleSet
|
266
|
+
) -> None:
|
267
|
+
if judge := await self.evidently_judge(
|
268
|
+
f"{article_outline.as_prompt()}\n\n{a.display()}\n"
|
269
|
+
f"Does the `{a.__class__.__name__}`'s `{field_name}` field need to be extended or tweaked?"
|
270
|
+
):
|
271
|
+
await self.censor_obj_inplace(
|
272
|
+
a,
|
273
|
+
ruleset=ruleset,
|
274
|
+
reference=f"{article_outline.as_prompt()}\n"
|
275
|
+
f"The Article component titled `{a.title}` whose `{field_name}` field needs to be extended or tweaked.\n"
|
276
|
+
f"# Judgement\n{judge.display()}",
|
277
|
+
)
|
278
|
+
|
259
279
|
|
260
280
|
class TweakOutlineBackwardRef(TweakOutlineForwardRef):
|
261
281
|
"""Tweak the backward references in the article outline.
|
@@ -267,11 +287,11 @@ class TweakOutlineBackwardRef(TweakOutlineForwardRef):
|
|
267
287
|
ruleset: Optional[RuleSet] = None
|
268
288
|
|
269
289
|
async def _execute(
|
270
|
-
self, article_outline: ArticleOutline,
|
290
|
+
self, article_outline: ArticleOutline, ref_twk_ruleset: Optional[RuleSet] = None, **cxt
|
271
291
|
) -> ArticleOutline:
|
272
292
|
return await self._inner(
|
273
293
|
article_outline,
|
274
|
-
ruleset=ok(
|
294
|
+
ruleset=ok(ref_twk_ruleset or self.ruleset, "No ruleset provided"),
|
275
295
|
field_name="depend_on",
|
276
296
|
)
|
277
297
|
|
@@ -286,7 +306,7 @@ class GenerateArticle(Action, Censor):
|
|
286
306
|
async def _execute(
|
287
307
|
self,
|
288
308
|
article_outline: ArticleOutline,
|
289
|
-
|
309
|
+
article_gen_ruleset: Optional[RuleSet] = None,
|
290
310
|
**_,
|
291
311
|
) -> Optional[Article]:
|
292
312
|
article: Article = Article.from_outline(ok(article_outline, "Article outline not specified.")).update_ref(
|
@@ -297,51 +317,12 @@ class GenerateArticle(Action, Censor):
|
|
297
317
|
*[
|
298
318
|
self.censor_obj_inplace(
|
299
319
|
subsec,
|
300
|
-
ruleset=ok(
|
301
|
-
reference=f"
|
320
|
+
ruleset=ok(article_gen_ruleset or self.ruleset, "No ruleset provided"),
|
321
|
+
reference=f"{article_outline.as_prompt()}\n# Error Need to be fixed\n{err}",
|
302
322
|
)
|
303
|
-
for _,
|
304
|
-
if (err := subsec.introspect())
|
323
|
+
for _, _, subsec in article.iter_subsections()
|
324
|
+
if (err := subsec.introspect()) and logger.warning(f"Found Introspection Error:\n{err}") is None
|
305
325
|
],
|
306
|
-
return_exceptions=True,
|
307
326
|
)
|
308
327
|
|
309
328
|
return article
|
310
|
-
|
311
|
-
|
312
|
-
class CorrectProposal(Action, Censor):
|
313
|
-
"""Correct the proposal of the article."""
|
314
|
-
|
315
|
-
output_key: str = "corrected_proposal"
|
316
|
-
|
317
|
-
async def _execute(self, article_proposal: ArticleProposal, **_) -> Any:
|
318
|
-
raise NotImplementedError("Not implemented.")
|
319
|
-
|
320
|
-
|
321
|
-
class CorrectOutline(Action, Correct):
|
322
|
-
"""Correct the outline of the article."""
|
323
|
-
|
324
|
-
output_key: str = "corrected_outline"
|
325
|
-
"""The key of the output data."""
|
326
|
-
|
327
|
-
async def _execute(
|
328
|
-
self,
|
329
|
-
article_outline: ArticleOutline,
|
330
|
-
**_,
|
331
|
-
) -> ArticleOutline:
|
332
|
-
raise NotImplementedError("Not implemented.")
|
333
|
-
|
334
|
-
|
335
|
-
class CorrectArticle(Action, Correct):
|
336
|
-
"""Correct the article based on the outline."""
|
337
|
-
|
338
|
-
output_key: str = "corrected_article"
|
339
|
-
"""The key of the output data."""
|
340
|
-
|
341
|
-
async def _execute(
|
342
|
-
self,
|
343
|
-
article: Article,
|
344
|
-
article_outline: ArticleOutline,
|
345
|
-
**_,
|
346
|
-
) -> Article:
|
347
|
-
raise NotImplementedError("Not implemented.")
|
@@ -6,7 +6,7 @@ from typing import Optional
|
|
6
6
|
from fabricatio.capabilities.censor import Censor
|
7
7
|
from fabricatio.capabilities.rag import RAG
|
8
8
|
from fabricatio.models.action import Action
|
9
|
-
from fabricatio.models.extra.article_main import Article,
|
9
|
+
from fabricatio.models.extra.article_main import Article, ArticleSubsection
|
10
10
|
from fabricatio.models.extra.rule import RuleSet
|
11
11
|
from fabricatio.utils import ok
|
12
12
|
|
@@ -33,7 +33,7 @@ class TweakArticleRAG(Action, RAG, Censor):
|
|
33
33
|
self,
|
34
34
|
article: Article,
|
35
35
|
collection_name: str = "article_essence",
|
36
|
-
|
36
|
+
twk_rag_ruleset: Optional[RuleSet] = None,
|
37
37
|
parallel: bool = False,
|
38
38
|
**cxt,
|
39
39
|
) -> Optional[Article]:
|
@@ -45,7 +45,7 @@ class TweakArticleRAG(Action, RAG, Censor):
|
|
45
45
|
Args:
|
46
46
|
article (Article): The article to be processed.
|
47
47
|
collection_name (str): The name of the collection to view for processing.
|
48
|
-
|
48
|
+
twk_rag_ruleset (Optional[RuleSet]): The ruleset to apply for censoring. If not provided, the class's ruleset is used.
|
49
49
|
parallel (bool): If True, process subsections in parallel. Otherwise, process them sequentially.
|
50
50
|
**cxt: Additional context parameters.
|
51
51
|
|
@@ -57,14 +57,14 @@ class TweakArticleRAG(Action, RAG, Censor):
|
|
57
57
|
if parallel:
|
58
58
|
await gather(
|
59
59
|
*[
|
60
|
-
self._inner(article, subsec, ok(
|
60
|
+
self._inner(article, subsec, ok(twk_rag_ruleset or self.ruleset, "No ruleset provided!"))
|
61
61
|
for _, __, subsec in article.iter_subsections()
|
62
62
|
],
|
63
63
|
return_exceptions=True,
|
64
64
|
)
|
65
65
|
else:
|
66
66
|
for _, __, subsec in article.iter_subsections():
|
67
|
-
await self._inner(article, subsec, ok(
|
67
|
+
await self._inner(article, subsec, ok(twk_rag_ruleset or self.ruleset, "No ruleset provided!"))
|
68
68
|
return article
|
69
69
|
|
70
70
|
async def _inner(self, article: Article, subsec: ArticleSubsection, ruleset: RuleSet) -> None:
|
@@ -88,13 +88,11 @@ class TweakArticleRAG(Action, RAG, Censor):
|
|
88
88
|
f"{subsec.display()}\n"
|
89
89
|
f"# Requirement\n"
|
90
90
|
f"Search related articles in the base to find reference candidates, "
|
91
|
-
f"prioritizing both original article language and English usage",
|
91
|
+
f"prioritizing both original article language and English usage, which can return multiple candidates.",
|
92
92
|
)
|
93
93
|
)
|
94
|
-
patch = ArticleParagraphSequencePatch.default()
|
95
|
-
patch.tweaked = subsec.paragraphs
|
96
94
|
await self.censor_obj_inplace(
|
97
|
-
|
95
|
+
subsec,
|
98
96
|
ruleset=ruleset,
|
99
97
|
reference=await self.aretrieve_compact(refind_q, final_limit=30),
|
100
98
|
)
|
fabricatio/actions/output.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
"""Dump the finalized output to a file."""
|
2
2
|
|
3
3
|
from pathlib import Path
|
4
|
-
from typing import Iterable, List, Optional, Type
|
4
|
+
from typing import Any, Iterable, List, Optional, Type
|
5
5
|
|
6
6
|
from fabricatio.journal import logger
|
7
7
|
from fabricatio.models.action import Action
|
@@ -33,6 +33,7 @@ class DumpFinalizedOutput(Action):
|
|
33
33
|
"Could not find the path of file to dump the data.",
|
34
34
|
)
|
35
35
|
)
|
36
|
+
logger.info(f"Saving output to {dump_path.as_posix()}")
|
36
37
|
ok(to_dump, "Could not dump the data since the path is not specified.").finalized_dump_to(dump_path)
|
37
38
|
return dump_path.as_posix()
|
38
39
|
|
@@ -41,7 +42,11 @@ class PersistentAll(Action):
|
|
41
42
|
"""Persist all the data to a file."""
|
42
43
|
|
43
44
|
output_key: str = "persistent_count"
|
45
|
+
"""The number of objects persisted."""
|
44
46
|
persist_dir: Optional[str] = None
|
47
|
+
"""The directory to persist the data."""
|
48
|
+
override: bool = False
|
49
|
+
"""Whether to remove the existing dir before dumping."""
|
45
50
|
|
46
51
|
async def _execute(
|
47
52
|
self,
|
@@ -64,16 +69,22 @@ class PersistentAll(Action):
|
|
64
69
|
if persist_dir.is_file():
|
65
70
|
logger.warning("Dump should be a directory, but it is a file. Skip dumping.")
|
66
71
|
return count
|
67
|
-
|
72
|
+
if self.override and persist_dir.is_dir():
|
73
|
+
logger.info(f"Override the existing directory {persist_dir.as_posix()}.")
|
74
|
+
persist_dir.rmdir()
|
75
|
+
logger.info(f"Starting persistence in directory {persist_dir}")
|
68
76
|
for k, v in cxt.items():
|
69
77
|
final_dir = persist_dir.joinpath(k)
|
78
|
+
logger.debug(f"Checking key {k} for persistence")
|
70
79
|
if isinstance(v, PersistentAble):
|
80
|
+
logger.info(f"Persisting object {k} to {final_dir}")
|
71
81
|
final_dir.mkdir(parents=True, exist_ok=True)
|
72
82
|
v.persist(final_dir)
|
73
83
|
count += 1
|
74
84
|
if isinstance(v, Iterable) and any(
|
75
85
|
persistent_ables := (pers for pers in v if isinstance(pers, PersistentAble))
|
76
86
|
):
|
87
|
+
logger.info(f"Persisting collection {k} to {final_dir}")
|
77
88
|
final_dir.mkdir(parents=True, exist_ok=True)
|
78
89
|
for per in persistent_ables:
|
79
90
|
per.persist(final_dir)
|
@@ -92,11 +103,56 @@ class RetrieveFromPersistent[T: PersistentAble](Action):
|
|
92
103
|
retrieve_cls: Type[T]
|
93
104
|
"""The class of the object to retrieve."""
|
94
105
|
|
95
|
-
async def _execute(self, /, **
|
96
|
-
logger.info(f"Retrieve `{self.retrieve_cls.__name__}` from
|
106
|
+
async def _execute(self, /, **_) -> Optional[T | List[T]]:
|
107
|
+
logger.info(f"Retrieve `{self.retrieve_cls.__name__}` from {self.load_path}")
|
97
108
|
if not (p := Path(self.load_path)).exists():
|
109
|
+
logger.warning(f"Path {self.load_path} does not exist")
|
98
110
|
return None
|
99
111
|
|
100
112
|
if p.is_dir():
|
113
|
+
logger.info(f"Found directory with {len(list(p.glob('*')))} items")
|
101
114
|
return [self.retrieve_cls.from_persistent(per) for per in p.glob("*")]
|
102
115
|
return self.retrieve_cls.from_persistent(self.load_path)
|
116
|
+
|
117
|
+
|
118
|
+
class RetrieveFromLatest[T: PersistentAble](RetrieveFromPersistent[T]):
|
119
|
+
"""Retrieve the object from the latest persistent file in the dir at `load_path`."""
|
120
|
+
|
121
|
+
async def _execute(self, /, **_) -> Optional[T]:
|
122
|
+
logger.info(f"Retrieve latest `{self.retrieve_cls.__name__}` from {self.load_path}")
|
123
|
+
if not (p := Path(self.load_path)).exists():
|
124
|
+
logger.warning(f"Path {self.load_path} does not exist")
|
125
|
+
return None
|
126
|
+
|
127
|
+
if p.is_dir():
|
128
|
+
logger.info(f"Found directory with {len(list(p.glob('*')))} items")
|
129
|
+
return self.retrieve_cls.from_latest_persistent(self.load_path)
|
130
|
+
logger.error(f"Path {self.load_path} is not a directory")
|
131
|
+
return None
|
132
|
+
|
133
|
+
|
134
|
+
class GatherAsList(Action):
|
135
|
+
"""Gather the objects from the context as a list.
|
136
|
+
|
137
|
+
Notes:
|
138
|
+
If both `gather_suffix` and `gather_prefix` are specified, only the objects with the suffix will be gathered.
|
139
|
+
"""
|
140
|
+
|
141
|
+
output_key: str = "gathered"
|
142
|
+
"""Gather the objects from the context as a list."""
|
143
|
+
gather_suffix: Optional[str] = None
|
144
|
+
"""Gather the objects from the context as a list."""
|
145
|
+
gather_prefix: Optional[str] = None
|
146
|
+
"""Gather the objects from the context as a list."""
|
147
|
+
|
148
|
+
async def _execute(self, **cxt) -> List[Any]:
|
149
|
+
if self.gather_suffix is not None:
|
150
|
+
result = [cxt[k] for k in cxt if k.endswith(self.gather_suffix)]
|
151
|
+
logger.debug(f"Gathered {len(result)} items with suffix {self.gather_suffix}")
|
152
|
+
return result
|
153
|
+
if self.gather_prefix is None:
|
154
|
+
logger.error(err := "Either `gather_suffix` or `gather_prefix` must be specified.")
|
155
|
+
raise ValueError(err)
|
156
|
+
result = [cxt[k] for k in cxt if k.startswith(self.gather_prefix)]
|
157
|
+
logger.debug(f"Gathered {len(result)} items with prefix {self.gather_prefix}")
|
158
|
+
return result
|
fabricatio/actions/rules.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
"""A module containing the DraftRuleSet action."""
|
2
2
|
|
3
|
-
from typing import Optional
|
3
|
+
from typing import List, Optional
|
4
4
|
|
5
5
|
from fabricatio.capabilities.check import Check
|
6
|
+
from fabricatio.journal import logger
|
6
7
|
from fabricatio.models.action import Action
|
7
8
|
from fabricatio.models.extra.rule import RuleSet
|
8
9
|
from fabricatio.utils import ok
|
@@ -18,9 +19,10 @@ class DraftRuleSet(Action, Check):
|
|
18
19
|
"""The natural language description of the desired ruleset characteristics."""
|
19
20
|
rule_count: int = 0
|
20
21
|
"""The number of rules to generate in the ruleset (0 for no restriction)."""
|
22
|
+
|
21
23
|
async def _execute(
|
22
24
|
self,
|
23
|
-
ruleset_requirement: Optional[str]=None,
|
25
|
+
ruleset_requirement: Optional[str] = None,
|
24
26
|
**_,
|
25
27
|
) -> Optional[RuleSet]:
|
26
28
|
"""Draft a ruleset based on the requirement description.
|
@@ -33,7 +35,38 @@ class DraftRuleSet(Action, Check):
|
|
33
35
|
Returns:
|
34
36
|
Optional[RuleSet]: Drafted ruleset object or None if generation fails
|
35
37
|
"""
|
36
|
-
|
37
|
-
ruleset_requirement=ok(ruleset_requirement or self.ruleset_requirement,"No ruleset requirement provided"),
|
38
|
+
ruleset = await self.draft_ruleset(
|
39
|
+
ruleset_requirement=ok(ruleset_requirement or self.ruleset_requirement, "No ruleset requirement provided"),
|
38
40
|
rule_count=self.rule_count,
|
39
41
|
)
|
42
|
+
if ruleset:
|
43
|
+
logger.info(f"Drafted Ruleset length: {len(ruleset.rules)}\n{ruleset.display()}")
|
44
|
+
else:
|
45
|
+
logger.warning(f"Drafting Rule Failed for:\n{ruleset_requirement}")
|
46
|
+
return ruleset
|
47
|
+
|
48
|
+
|
49
|
+
class GatherRuleset(Action):
|
50
|
+
"""Action to gather a ruleset from a given requirement description."""
|
51
|
+
|
52
|
+
output_key: str = "gathered_ruleset"
|
53
|
+
"""The key used to store the drafted ruleset in the context dictionary."""
|
54
|
+
|
55
|
+
to_gather: List[str]
|
56
|
+
"""the cxt name of RuleSet to gather"""
|
57
|
+
|
58
|
+
async def _execute(self, **cxt) -> RuleSet:
|
59
|
+
logger.info(f"Gathering Ruleset from {self.to_gather}")
|
60
|
+
# Fix for not_found
|
61
|
+
not_found = next((t for t in self.to_gather if t not in cxt), None)
|
62
|
+
if not_found:
|
63
|
+
raise ValueError(
|
64
|
+
f"Not all required keys found in context: {self.to_gather}|`{not_found}` not found in context."
|
65
|
+
)
|
66
|
+
|
67
|
+
# Fix for invalid RuleSet check
|
68
|
+
invalid = next((t for t in self.to_gather if not isinstance(cxt[t], RuleSet)), None)
|
69
|
+
if invalid is not None:
|
70
|
+
raise TypeError(f"Invalid RuleSet instance for key `{invalid}`")
|
71
|
+
|
72
|
+
return RuleSet.gather(*[cxt[t] for t in self.to_gather])
|
@@ -8,6 +8,8 @@ from typing import Optional, Unpack
|
|
8
8
|
|
9
9
|
from fabricatio.capabilities.check import Check
|
10
10
|
from fabricatio.capabilities.correct import Correct
|
11
|
+
from fabricatio.journal import logger
|
12
|
+
from fabricatio.models.extra.problem import Improvement
|
11
13
|
from fabricatio.models.extra.rule import RuleSet
|
12
14
|
from fabricatio.models.generic import ProposedUpdateAble, SketchedAble
|
13
15
|
from fabricatio.models.kwargs_types import ReferencedKwargs
|
@@ -20,8 +22,6 @@ class Censor(Correct, Check):
|
|
20
22
|
Inherits from both Correct and Check classes.
|
21
23
|
Provides methods to censor objects and strings by first checking them against a ruleset and then correcting them if necessary.
|
22
24
|
|
23
|
-
Attributes:
|
24
|
-
ruleset (RuleSet): The ruleset to be used for censoring.
|
25
25
|
"""
|
26
26
|
|
27
27
|
async def censor_obj[M: SketchedAble](
|
@@ -42,8 +42,12 @@ class Censor(Correct, Check):
|
|
42
42
|
"""
|
43
43
|
imp = await self.check_obj(obj, ruleset, **override_kwargs(kwargs, default=None))
|
44
44
|
if imp is None:
|
45
|
-
return
|
46
|
-
|
45
|
+
return None
|
46
|
+
if not imp:
|
47
|
+
logger.info(f"No improvement found for `{obj.__class__.__name__}`.")
|
48
|
+
return obj
|
49
|
+
logger.info(f'Generated {len(imp)} improvement(s) for `{obj.__class__.__name__}')
|
50
|
+
return await self.correct_obj(obj, Improvement.gather(*imp), **kwargs)
|
47
51
|
|
48
52
|
async def censor_string(
|
49
53
|
self, input_text: str, ruleset: RuleSet, **kwargs: Unpack[ReferencedKwargs[str]]
|
@@ -63,8 +67,13 @@ class Censor(Correct, Check):
|
|
63
67
|
"""
|
64
68
|
imp = await self.check_string(input_text, ruleset, **override_kwargs(kwargs, default=None))
|
65
69
|
if imp is None:
|
66
|
-
|
67
|
-
|
70
|
+
logger.warning(f"Censor failed for string:\n{input_text}")
|
71
|
+
return None
|
72
|
+
if not imp:
|
73
|
+
logger.info("No improvement found for string.")
|
74
|
+
return input_text
|
75
|
+
logger.info(f'Generated {len(imp)} improvement(s) for string.')
|
76
|
+
return await self.correct_string(input_text, Improvement.gather(*imp), **kwargs)
|
68
77
|
|
69
78
|
async def censor_obj_inplace[M: ProposedUpdateAble](
|
70
79
|
self, obj: M, ruleset: RuleSet, **kwargs: Unpack[ReferencedKwargs[M]]
|
@@ -86,5 +95,10 @@ class Censor(Correct, Check):
|
|
86
95
|
"""
|
87
96
|
imp = await self.check_obj(obj, ruleset, **override_kwargs(kwargs, default=None))
|
88
97
|
if imp is None:
|
89
|
-
|
90
|
-
|
98
|
+
logger.warning(f"Censor failed for `{obj.__class__.__name__}`")
|
99
|
+
return None
|
100
|
+
if not imp:
|
101
|
+
logger.info(f"No improvement found for `{obj.__class__.__name__}`.")
|
102
|
+
return obj
|
103
|
+
logger.info(f'Generated {len(imp)} improvement(s) for `{obj.__class__.__name__}')
|
104
|
+
return await self.correct_obj_inplace(obj, improvement=Improvement.gather(*imp), **kwargs)
|