fabricatio 0.2.8.dev3__cp312-cp312-manylinux_2_34_x86_64.whl → 0.2.9.dev0__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/__init__.py +4 -11
- fabricatio/actions/__init__.py +1 -0
- fabricatio/actions/article.py +63 -87
- fabricatio/actions/article_rag.py +54 -43
- fabricatio/actions/rag.py +2 -1
- fabricatio/actions/rules.py +39 -0
- fabricatio/capabilities/__init__.py +1 -0
- fabricatio/capabilities/censor.py +90 -0
- fabricatio/capabilities/check.py +127 -29
- fabricatio/capabilities/correct.py +143 -100
- fabricatio/capabilities/rag.py +5 -4
- fabricatio/capabilities/rating.py +65 -16
- fabricatio/capabilities/review.py +1 -1
- fabricatio/capabilities/task.py +2 -1
- fabricatio/config.py +11 -3
- fabricatio/models/action.py +14 -7
- fabricatio/models/adv_kwargs_types.py +25 -0
- fabricatio/models/extra/__init__.py +1 -0
- fabricatio/models/extra/advanced_judge.py +5 -2
- fabricatio/models/extra/article_base.py +3 -20
- fabricatio/models/extra/article_main.py +2 -3
- fabricatio/models/extra/patches.py +20 -0
- fabricatio/models/extra/problem.py +41 -8
- fabricatio/models/extra/rule.py +26 -9
- fabricatio/models/generic.py +310 -55
- fabricatio/models/kwargs_types.py +23 -17
- fabricatio/models/task.py +1 -1
- fabricatio/models/tool.py +149 -14
- fabricatio/models/usages.py +50 -42
- fabricatio/parser.py +7 -8
- fabricatio/rust.cpython-312-x86_64-linux-gnu.so +0 -0
- fabricatio/{_rust_instances.py → rust_instances.py} +1 -1
- fabricatio/workflows/__init__.py +1 -0
- fabricatio-0.2.9.dev0.data/scripts/tdown +0 -0
- {fabricatio-0.2.8.dev3.dist-info → fabricatio-0.2.9.dev0.dist-info}/METADATA +1 -1
- fabricatio-0.2.9.dev0.dist-info/RECORD +61 -0
- fabricatio/_rust.cpython-312-x86_64-linux-gnu.so +0 -0
- fabricatio-0.2.8.dev3.data/scripts/tdown +0 -0
- fabricatio-0.2.8.dev3.dist-info/RECORD +0 -53
- /fabricatio/{_rust.pyi → rust.pyi} +0 -0
- {fabricatio-0.2.8.dev3.dist-info → fabricatio-0.2.9.dev0.dist-info}/WHEEL +0 -0
- {fabricatio-0.2.8.dev3.dist-info → fabricatio-0.2.9.dev0.dist-info}/licenses/LICENSE +0 -0
fabricatio/__init__.py
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
"""Fabricatio is a Python library for building llm app using event-based agent structure."""
|
2
2
|
|
3
|
-
from
|
4
|
-
|
5
|
-
from fabricatio import actions, toolboxes, workflows
|
6
|
-
from fabricatio._rust import BibManager
|
7
|
-
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
3
|
+
from fabricatio import actions, capabilities, toolboxes, workflows
|
8
4
|
from fabricatio.core import env
|
9
5
|
from fabricatio.journal import logger
|
10
6
|
from fabricatio.models import extra
|
@@ -14,6 +10,8 @@ from fabricatio.models.role import Role
|
|
14
10
|
from fabricatio.models.task import Task
|
15
11
|
from fabricatio.models.tool import ToolBox
|
16
12
|
from fabricatio.parser import Capture, GenericCapture, JsonCapture, PythonCapture
|
13
|
+
from fabricatio.rust import BibManager
|
14
|
+
from fabricatio.rust_instances import TEMPLATE_MANAGER
|
17
15
|
|
18
16
|
__all__ = [
|
19
17
|
"TEMPLATE_MANAGER",
|
@@ -29,15 +27,10 @@ __all__ = [
|
|
29
27
|
"ToolBox",
|
30
28
|
"WorkFlow",
|
31
29
|
"actions",
|
30
|
+
"capabilities",
|
32
31
|
"env",
|
33
32
|
"extra",
|
34
33
|
"logger",
|
35
34
|
"toolboxes",
|
36
35
|
"workflows",
|
37
36
|
]
|
38
|
-
|
39
|
-
|
40
|
-
if find_spec("pymilvus"):
|
41
|
-
from fabricatio.capabilities.rag import RAG
|
42
|
-
|
43
|
-
__all__ += ["RAG"]
|
@@ -0,0 +1 @@
|
|
1
|
+
"""A module containing some builtin actins."""
|
fabricatio/actions/article.py
CHANGED
@@ -4,22 +4,26 @@ from asyncio import gather
|
|
4
4
|
from pathlib import Path
|
5
5
|
from typing import Any, Callable, List, Optional
|
6
6
|
|
7
|
-
from
|
8
|
-
|
7
|
+
from more_itertools import filter_map
|
8
|
+
|
9
|
+
from fabricatio.capabilities.censor import Censor
|
10
|
+
from fabricatio.capabilities.correct import Correct
|
11
|
+
from fabricatio.capabilities.propose import Propose
|
9
12
|
from fabricatio.fs import safe_text_read
|
10
13
|
from fabricatio.journal import logger
|
11
14
|
from fabricatio.models.action import Action
|
12
|
-
from fabricatio.models.extra.article_base import
|
15
|
+
from fabricatio.models.extra.article_base import ArticleRefSequencePatch
|
13
16
|
from fabricatio.models.extra.article_essence import ArticleEssence
|
14
17
|
from fabricatio.models.extra.article_main import Article
|
15
18
|
from fabricatio.models.extra.article_outline import ArticleOutline
|
16
19
|
from fabricatio.models.extra.article_proposal import ArticleProposal
|
20
|
+
from fabricatio.models.extra.rule import RuleSet
|
17
21
|
from fabricatio.models.task import Task
|
22
|
+
from fabricatio.rust import BibManager
|
18
23
|
from fabricatio.utils import ok
|
19
|
-
from more_itertools import filter_map
|
20
24
|
|
21
25
|
|
22
|
-
class ExtractArticleEssence(Action):
|
26
|
+
class ExtractArticleEssence(Action, Propose):
|
23
27
|
"""Extract the essence of article(s) in text format from the paths specified in the task dependencies.
|
24
28
|
|
25
29
|
Notes:
|
@@ -47,11 +51,11 @@ class ExtractArticleEssence(Action):
|
|
47
51
|
out = []
|
48
52
|
|
49
53
|
for ess in await self.propose(
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
ArticleEssence,
|
55
|
+
[
|
56
|
+
f"{c}\n\n\nBased the provided academic article above, you need to extract the essence from it."
|
57
|
+
for c in contents
|
58
|
+
],
|
55
59
|
):
|
56
60
|
if ess is None:
|
57
61
|
logger.warning("Could not extract article essence")
|
@@ -91,7 +95,7 @@ class FixArticleEssence(Action):
|
|
91
95
|
return out
|
92
96
|
|
93
97
|
|
94
|
-
class GenerateArticleProposal(Action):
|
98
|
+
class GenerateArticleProposal(Action, Propose):
|
95
99
|
"""Generate an outline for the article based on the extracted essence."""
|
96
100
|
|
97
101
|
output_key: str = "article_proposal"
|
@@ -133,7 +137,7 @@ class GenerateArticleProposal(Action):
|
|
133
137
|
return proposal
|
134
138
|
|
135
139
|
|
136
|
-
class GenerateInitialOutline(Action):
|
140
|
+
class GenerateInitialOutline(Action, Propose):
|
137
141
|
"""Generate the initial article outline based on the article proposal."""
|
138
142
|
|
139
143
|
output_key: str = "initial_article_outline"
|
@@ -153,38 +157,29 @@ class GenerateInitialOutline(Action):
|
|
153
157
|
).update_ref(article_proposal)
|
154
158
|
|
155
159
|
|
156
|
-
class FixIntrospectedErrors(Action):
|
160
|
+
class FixIntrospectedErrors(Action, Censor):
|
157
161
|
"""Fix introspected errors in the article outline."""
|
158
162
|
|
159
163
|
output_key: str = "introspected_errors_fixed_outline"
|
160
164
|
"""The key of the output data."""
|
161
165
|
|
166
|
+
ruleset: Optional[RuleSet] = None
|
167
|
+
"""The ruleset to use to fix the introspected errors."""
|
168
|
+
|
162
169
|
async def _execute(
|
163
170
|
self,
|
164
171
|
article_outline: ArticleOutline,
|
165
|
-
|
172
|
+
ruleset: Optional[RuleSet] = None,
|
166
173
|
**_,
|
167
174
|
) -> Optional[ArticleOutline]:
|
168
|
-
introspect_manual = ok(
|
169
|
-
await self.draft_rating_manual(
|
170
|
-
topic=(
|
171
|
-
intro_topic
|
172
|
-
:= "Fix the error in the article outline, make sure there is no more error in the article outline."
|
173
|
-
),
|
174
|
-
),
|
175
|
-
"Could not generate the rating manual.",
|
176
|
-
)
|
177
|
-
|
178
175
|
while pack := article_outline.find_introspected():
|
179
176
|
component, err = ok(pack)
|
180
177
|
logger.warning(f"Found introspected error: {err}")
|
181
178
|
corrected = ok(
|
182
|
-
await self.
|
179
|
+
await self.censor_obj(
|
183
180
|
component,
|
184
|
-
|
185
|
-
|
186
|
-
rating_manual=introspect_manual,
|
187
|
-
supervisor_check=supervisor_check,
|
181
|
+
ruleset=ok(ruleset or self.ruleset, "No ruleset provided"),
|
182
|
+
reference=f"# Original Article Outline\n{article_outline.display()}\n# Some Basic errors found from `{component.title}` that need to be fixed\n{err}",
|
188
183
|
),
|
189
184
|
"Could not correct the component.",
|
190
185
|
)
|
@@ -193,38 +188,29 @@ class FixIntrospectedErrors(Action):
|
|
193
188
|
return article_outline
|
194
189
|
|
195
190
|
|
196
|
-
class FixIllegalReferences(Action):
|
191
|
+
class FixIllegalReferences(Action, Censor):
|
197
192
|
"""Fix illegal references in the article outline."""
|
198
193
|
|
199
194
|
output_key: str = "illegal_references_fixed_outline"
|
200
195
|
"""The key of the output data."""
|
201
196
|
|
197
|
+
ruleset: Optional[RuleSet] = None
|
198
|
+
"""Ruleset to use to fix the illegal references."""
|
199
|
+
|
202
200
|
async def _execute(
|
203
201
|
self,
|
204
202
|
article_outline: ArticleOutline,
|
205
|
-
|
203
|
+
ruleset: Optional[RuleSet] = None,
|
206
204
|
**_,
|
207
205
|
) -> Optional[ArticleOutline]:
|
208
|
-
ref_manual = ok(
|
209
|
-
await self.draft_rating_manual(
|
210
|
-
topic=(
|
211
|
-
ref_topic
|
212
|
-
:= "Fix the internal referring error, make sure there is no more `ArticleRef` pointing to a non-existing article component."
|
213
|
-
),
|
214
|
-
),
|
215
|
-
"Could not generate the rating manual.",
|
216
|
-
)
|
217
|
-
|
218
206
|
while pack := article_outline.find_illegal_ref(gather_identical=True):
|
219
207
|
refs, err = ok(pack)
|
220
208
|
logger.warning(f"Found illegal referring error: {err}")
|
221
209
|
corrected_ref = ok(
|
222
|
-
await self.
|
210
|
+
await self.censor_obj(
|
223
211
|
refs[0], # pyright: ignore [reportIndexIssue]
|
224
|
-
|
225
|
-
|
226
|
-
rating_manual=ref_manual,
|
227
|
-
supervisor_check=supervisor_check,
|
212
|
+
ruleset=ok(ruleset or self.ruleset, "No ruleset provided"),
|
213
|
+
reference=f"# Original Article Outline\n{article_outline.display()}\n# Some Basic errors found that need to be fixed\n{err}",
|
228
214
|
)
|
229
215
|
)
|
230
216
|
for ref in refs:
|
@@ -233,43 +219,40 @@ class FixIllegalReferences(Action):
|
|
233
219
|
return article_outline.update_ref(article_outline)
|
234
220
|
|
235
221
|
|
236
|
-
class TweakOutlineForwardRef(Action,
|
222
|
+
class TweakOutlineForwardRef(Action, Censor):
|
237
223
|
"""Tweak the forward references in the article outline.
|
238
224
|
|
239
225
|
Ensures that the conclusions of the current chapter effectively support the analysis of subsequent chapters.
|
240
226
|
"""
|
241
227
|
|
242
228
|
output_key: str = "article_outline_fw_ref_checked"
|
229
|
+
ruleset: Optional[RuleSet] = None
|
230
|
+
"""Ruleset to use to fix the illegal references."""
|
243
231
|
|
244
|
-
async def _execute(
|
232
|
+
async def _execute(
|
233
|
+
self, article_outline: ArticleOutline, ruleset: Optional[RuleSet] = None, **cxt
|
234
|
+
) -> ArticleOutline:
|
245
235
|
return await self._inner(
|
246
236
|
article_outline,
|
247
|
-
|
248
|
-
topic="Ensure conclusions support the analysis of subsequent chapters, sections or subsections.",
|
237
|
+
ruleset=ok(ruleset or self.ruleset, "No ruleset provided"),
|
249
238
|
field_name="support_to",
|
250
239
|
)
|
251
240
|
|
252
|
-
async def _inner(
|
253
|
-
self, article_outline: ArticleOutline, supervisor_check: bool, topic: str, field_name: str
|
254
|
-
) -> ArticleOutline:
|
255
|
-
tweak_support_to_manual = ok(
|
256
|
-
await self.draft_rating_manual(topic),
|
257
|
-
"Could not generate the rating manual.",
|
258
|
-
)
|
241
|
+
async def _inner(self, article_outline: ArticleOutline, ruleset: RuleSet, field_name: str) -> ArticleOutline:
|
259
242
|
for a in article_outline.iter_dfs():
|
260
|
-
if await self.evidently_judge(
|
243
|
+
if judge := await self.evidently_judge(
|
261
244
|
f"{article_outline.as_prompt()}\n\n{a.display()}\n"
|
262
245
|
f"Does the `{a.__class__.__name__}`'s `{field_name}` field need to be extended or tweaked?"
|
263
246
|
):
|
264
|
-
patch =
|
247
|
+
patch = ArticleRefSequencePatch.default()
|
265
248
|
patch.tweaked = getattr(a, field_name)
|
266
249
|
|
267
|
-
await self.
|
250
|
+
await self.censor_obj_inplace(
|
268
251
|
patch,
|
269
|
-
|
270
|
-
reference=f"{article_outline.as_prompt()}\
|
271
|
-
|
272
|
-
|
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()}",
|
273
256
|
)
|
274
257
|
return article_outline
|
275
258
|
|
@@ -281,44 +264,41 @@ class TweakOutlineBackwardRef(TweakOutlineForwardRef):
|
|
281
264
|
"""
|
282
265
|
|
283
266
|
output_key: str = "article_outline_bw_ref_checked"
|
267
|
+
ruleset: Optional[RuleSet] = None
|
284
268
|
|
285
|
-
async def _execute(
|
269
|
+
async def _execute(
|
270
|
+
self, article_outline: ArticleOutline, ruleset: Optional[RuleSet] = None, **cxt
|
271
|
+
) -> ArticleOutline:
|
286
272
|
return await self._inner(
|
287
273
|
article_outline,
|
288
|
-
|
289
|
-
topic="Ensure the dependencies of the current chapter are neither abused nor missing.",
|
274
|
+
ruleset=ok(ruleset or self.ruleset, "No ruleset provided"),
|
290
275
|
field_name="depend_on",
|
291
276
|
)
|
292
277
|
|
293
278
|
|
294
|
-
class GenerateArticle(Action):
|
279
|
+
class GenerateArticle(Action, Censor):
|
295
280
|
"""Generate the article based on the outline."""
|
296
281
|
|
297
282
|
output_key: str = "article"
|
298
283
|
"""The key of the output data."""
|
284
|
+
ruleset: Optional[RuleSet] = None
|
299
285
|
|
300
286
|
async def _execute(
|
301
287
|
self,
|
302
288
|
article_outline: ArticleOutline,
|
303
|
-
|
289
|
+
ruleset: Optional[RuleSet] = None,
|
304
290
|
**_,
|
305
291
|
) -> Optional[Article]:
|
306
292
|
article: Article = Article.from_outline(ok(article_outline, "Article outline not specified.")).update_ref(
|
307
293
|
article_outline
|
308
294
|
)
|
309
295
|
|
310
|
-
write_para_manual = ok(
|
311
|
-
await self.draft_rating_manual(w_topic := "write the following paragraph in the subsection.")
|
312
|
-
)
|
313
|
-
|
314
296
|
await gather(
|
315
297
|
*[
|
316
|
-
self.
|
298
|
+
self.censor_obj_inplace(
|
317
299
|
subsec,
|
300
|
+
ruleset=ok(ruleset or self.ruleset, "No ruleset provided"),
|
318
301
|
reference=f"# Original Article Outline\n{article_outline.display()}\n# Error Need to be fixed\n{err}",
|
319
|
-
topic=w_topic,
|
320
|
-
rating_manual=write_para_manual,
|
321
|
-
supervisor_check=supervisor_check,
|
322
302
|
)
|
323
303
|
for _, __, subsec in article.iter_subsections()
|
324
304
|
if (err := subsec.introspect())
|
@@ -329,18 +309,16 @@ class GenerateArticle(Action):
|
|
329
309
|
return article
|
330
310
|
|
331
311
|
|
332
|
-
class CorrectProposal(Action):
|
312
|
+
class CorrectProposal(Action, Censor):
|
333
313
|
"""Correct the proposal of the article."""
|
334
314
|
|
335
315
|
output_key: str = "corrected_proposal"
|
336
316
|
|
337
317
|
async def _execute(self, article_proposal: ArticleProposal, **_) -> Any:
|
338
|
-
|
339
|
-
article_proposal
|
340
|
-
)
|
318
|
+
raise NotImplementedError("Not implemented.")
|
341
319
|
|
342
320
|
|
343
|
-
class CorrectOutline(Action):
|
321
|
+
class CorrectOutline(Action, Correct):
|
344
322
|
"""Correct the outline of the article."""
|
345
323
|
|
346
324
|
output_key: str = "corrected_outline"
|
@@ -351,12 +329,10 @@ class CorrectOutline(Action):
|
|
351
329
|
article_outline: ArticleOutline,
|
352
330
|
**_,
|
353
331
|
) -> ArticleOutline:
|
354
|
-
|
355
|
-
article_outline
|
356
|
-
)
|
332
|
+
raise NotImplementedError("Not implemented.")
|
357
333
|
|
358
334
|
|
359
|
-
class CorrectArticle(Action):
|
335
|
+
class CorrectArticle(Action, Correct):
|
360
336
|
"""Correct the article based on the outline."""
|
361
337
|
|
362
338
|
output_key: str = "corrected_article"
|
@@ -368,4 +344,4 @@ class CorrectArticle(Action):
|
|
368
344
|
article_outline: ArticleOutline,
|
369
345
|
**_,
|
370
346
|
) -> Article:
|
371
|
-
|
347
|
+
raise NotImplementedError("Not implemented.")
|
@@ -1,73 +1,86 @@
|
|
1
1
|
"""A module for writing articles using RAG (Retrieval-Augmented Generation) capabilities."""
|
2
2
|
|
3
3
|
from asyncio import gather
|
4
|
-
from typing import
|
4
|
+
from typing import Optional
|
5
5
|
|
6
|
+
from fabricatio.capabilities.censor import Censor
|
6
7
|
from fabricatio.capabilities.rag import RAG
|
7
8
|
from fabricatio.models.action import Action
|
8
|
-
from fabricatio.models.extra.article_main import Article,
|
9
|
+
from fabricatio.models.extra.article_main import Article, ArticleParagraphSequencePatch, ArticleSubsection
|
10
|
+
from fabricatio.models.extra.rule import RuleSet
|
9
11
|
from fabricatio.utils import ok
|
10
12
|
|
11
13
|
|
12
|
-
class TweakArticleRAG(Action, RAG):
|
13
|
-
"""Write an article based on the provided outline.
|
14
|
+
class TweakArticleRAG(Action, RAG, Censor):
|
15
|
+
"""Write an article based on the provided outline.
|
16
|
+
|
17
|
+
This class inherits from `Action`, `RAG`, and `Censor` to provide capabilities for writing and refining articles
|
18
|
+
using Retrieval-Augmented Generation (RAG) techniques. It processes an article outline, enhances subsections by
|
19
|
+
searching for related references, and applies censoring rules to ensure compliance with the provided ruleset.
|
20
|
+
|
21
|
+
Attributes:
|
22
|
+
output_key (str): The key used to store the output of the action.
|
23
|
+
ruleset (Optional[RuleSet]): The ruleset to be used for censoring the article.
|
24
|
+
"""
|
14
25
|
|
15
26
|
output_key: str = "rag_tweaked_article"
|
27
|
+
"""The key used to store the output of the action."""
|
28
|
+
|
29
|
+
ruleset: Optional[RuleSet] = None
|
30
|
+
"""The ruleset to be used for censoring the article."""
|
16
31
|
|
17
32
|
async def _execute(
|
18
33
|
self,
|
19
34
|
article: Article,
|
20
35
|
collection_name: str = "article_essence",
|
21
|
-
|
22
|
-
"Use correct citation format based on author count. Cite using author surnames and year:"
|
23
|
-
"For 3+ authors: 'Author1, Author2 et al. (YYYY)'"
|
24
|
-
"For 2 authors: 'Author1 & Author2 (YYYY)'"
|
25
|
-
"Single author: 'Author1 (YYYY)'"
|
26
|
-
"Multiple citations: 'Author1 (YYYY), Author2 (YYYY)'"
|
27
|
-
"Prioritize formulas from reference highlights."
|
28
|
-
"Specify authors/years only."
|
29
|
-
"You can create numeric citation numbers for article whose `bibtex_cite_key` is 'wangWind2024' by using notation like `#cite(<wangWind2024>)`."
|
30
|
-
"Paragraphs must exceed 2-3 sentences",
|
31
|
-
supervisor_check: bool = False,
|
36
|
+
ruleset: Optional[RuleSet] = None,
|
32
37
|
parallel: bool = False,
|
33
38
|
**cxt,
|
34
39
|
) -> Optional[Article]:
|
35
|
-
"""Write an article based on the provided outline.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
"""Write an article based on the provided outline.
|
41
|
+
|
42
|
+
This method processes the article outline, either in parallel or sequentially, by enhancing each subsection
|
43
|
+
with relevant references and applying censoring rules.
|
44
|
+
|
45
|
+
Args:
|
46
|
+
article (Article): The article to be processed.
|
47
|
+
collection_name (str): The name of the collection to view for processing.
|
48
|
+
ruleset (Optional[RuleSet]): The ruleset to apply for censoring. If not provided, the class's ruleset is used.
|
49
|
+
parallel (bool): If True, process subsections in parallel. Otherwise, process them sequentially.
|
50
|
+
**cxt: Additional context parameters.
|
44
51
|
|
45
|
-
|
52
|
+
Returns:
|
53
|
+
Optional[Article]: The processed article with enhanced subsections and applied censoring rules.
|
54
|
+
"""
|
46
55
|
self.view(collection_name)
|
47
56
|
|
48
57
|
if parallel:
|
49
58
|
await gather(
|
50
59
|
*[
|
51
|
-
self._inner(article, subsec,
|
60
|
+
self._inner(article, subsec, ok(ruleset or self.ruleset, "No ruleset provided!"))
|
52
61
|
for _, __, subsec in article.iter_subsections()
|
53
62
|
],
|
54
63
|
return_exceptions=True,
|
55
64
|
)
|
56
65
|
else:
|
57
66
|
for _, __, subsec in article.iter_subsections():
|
58
|
-
await self._inner(article, subsec,
|
59
|
-
|
67
|
+
await self._inner(article, subsec, ok(ruleset or self.ruleset, "No ruleset provided!"))
|
60
68
|
return article
|
61
69
|
|
62
|
-
async def _inner(
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
70
|
+
async def _inner(self, article: Article, subsec: ArticleSubsection, ruleset: RuleSet) -> None:
|
71
|
+
"""Enhance a subsection of the article with references and apply censoring rules.
|
72
|
+
|
73
|
+
This method refines the query for the subsection, retrieves related references, and applies censoring rules
|
74
|
+
to the subsection's paragraphs.
|
75
|
+
|
76
|
+
Args:
|
77
|
+
article (Article): The article containing the subsection.
|
78
|
+
subsec (ArticleSubsection): The subsection to be enhanced.
|
79
|
+
ruleset (RuleSet): The ruleset to apply for censoring.
|
80
|
+
|
81
|
+
Returns:
|
82
|
+
None
|
83
|
+
"""
|
71
84
|
refind_q = ok(
|
72
85
|
await self.arefined_query(
|
73
86
|
f"{article.referenced.as_prompt()}\n"
|
@@ -78,12 +91,10 @@ class TweakArticleRAG(Action, RAG):
|
|
78
91
|
f"prioritizing both original article language and English usage",
|
79
92
|
)
|
80
93
|
)
|
81
|
-
patch =
|
94
|
+
patch = ArticleParagraphSequencePatch.default()
|
82
95
|
patch.tweaked = subsec.paragraphs
|
83
|
-
await self.
|
96
|
+
await self.censor_obj_inplace(
|
84
97
|
patch,
|
85
|
-
|
86
|
-
|
87
|
-
rating_manual=tweak_manual,
|
88
|
-
supervisor_check=supervisor_check,
|
98
|
+
ruleset=ruleset,
|
99
|
+
reference=await self.aretrieve_compact(refind_q, final_limit=30),
|
89
100
|
)
|
fabricatio/actions/rag.py
CHANGED
@@ -2,12 +2,13 @@
|
|
2
2
|
|
3
3
|
from typing import List, Optional
|
4
4
|
|
5
|
+
from questionary import text
|
6
|
+
|
5
7
|
from fabricatio.capabilities.rag import RAG
|
6
8
|
from fabricatio.journal import logger
|
7
9
|
from fabricatio.models.action import Action
|
8
10
|
from fabricatio.models.generic import Vectorizable
|
9
11
|
from fabricatio.models.task import Task
|
10
|
-
from questionary import text
|
11
12
|
|
12
13
|
|
13
14
|
class InjectToDB(Action, RAG):
|
@@ -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 @@
|
|
1
|
+
"""A module containing some high level capabilities."""
|
@@ -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)
|