fabricatio 0.3.14.dev4__cp313-cp313-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.
Files changed (64) hide show
  1. fabricatio/__init__.py +29 -0
  2. fabricatio/actions/__init__.py +1 -0
  3. fabricatio/actions/article.py +319 -0
  4. fabricatio/actions/article_rag.py +416 -0
  5. fabricatio/actions/fs.py +25 -0
  6. fabricatio/actions/output.py +248 -0
  7. fabricatio/actions/rag.py +96 -0
  8. fabricatio/actions/rules.py +83 -0
  9. fabricatio/capabilities/__init__.py +1 -0
  10. fabricatio/capabilities/advanced_judge.py +20 -0
  11. fabricatio/capabilities/advanced_rag.py +61 -0
  12. fabricatio/capabilities/censor.py +105 -0
  13. fabricatio/capabilities/check.py +212 -0
  14. fabricatio/capabilities/correct.py +228 -0
  15. fabricatio/capabilities/extract.py +74 -0
  16. fabricatio/capabilities/persist.py +103 -0
  17. fabricatio/capabilities/propose.py +65 -0
  18. fabricatio/capabilities/rag.py +263 -0
  19. fabricatio/capabilities/rating.py +404 -0
  20. fabricatio/capabilities/review.py +114 -0
  21. fabricatio/capabilities/task.py +113 -0
  22. fabricatio/decorators.py +251 -0
  23. fabricatio/emitter.py +177 -0
  24. fabricatio/fs/__init__.py +35 -0
  25. fabricatio/fs/curd.py +153 -0
  26. fabricatio/fs/readers.py +61 -0
  27. fabricatio/journal.py +12 -0
  28. fabricatio/models/action.py +263 -0
  29. fabricatio/models/adv_kwargs_types.py +63 -0
  30. fabricatio/models/extra/__init__.py +1 -0
  31. fabricatio/models/extra/advanced_judge.py +32 -0
  32. fabricatio/models/extra/aricle_rag.py +284 -0
  33. fabricatio/models/extra/article_base.py +422 -0
  34. fabricatio/models/extra/article_essence.py +101 -0
  35. fabricatio/models/extra/article_main.py +285 -0
  36. fabricatio/models/extra/article_outline.py +46 -0
  37. fabricatio/models/extra/article_proposal.py +52 -0
  38. fabricatio/models/extra/patches.py +20 -0
  39. fabricatio/models/extra/problem.py +165 -0
  40. fabricatio/models/extra/rag.py +98 -0
  41. fabricatio/models/extra/rule.py +52 -0
  42. fabricatio/models/generic.py +812 -0
  43. fabricatio/models/kwargs_types.py +121 -0
  44. fabricatio/models/role.py +95 -0
  45. fabricatio/models/task.py +310 -0
  46. fabricatio/models/tool.py +328 -0
  47. fabricatio/models/usages.py +791 -0
  48. fabricatio/parser.py +114 -0
  49. fabricatio/py.typed +0 -0
  50. fabricatio/rust.cpython-313-x86_64-linux-gnu.so +0 -0
  51. fabricatio/rust.pyi +846 -0
  52. fabricatio/toolboxes/__init__.py +15 -0
  53. fabricatio/toolboxes/arithmetic.py +62 -0
  54. fabricatio/toolboxes/fs.py +31 -0
  55. fabricatio/utils.py +156 -0
  56. fabricatio/workflows/__init__.py +1 -0
  57. fabricatio/workflows/articles.py +24 -0
  58. fabricatio/workflows/rag.py +11 -0
  59. fabricatio-0.3.14.dev4.data/scripts/tdown +0 -0
  60. fabricatio-0.3.14.dev4.data/scripts/ttm +0 -0
  61. fabricatio-0.3.14.dev4.dist-info/METADATA +188 -0
  62. fabricatio-0.3.14.dev4.dist-info/RECORD +64 -0
  63. fabricatio-0.3.14.dev4.dist-info/WHEEL +4 -0
  64. fabricatio-0.3.14.dev4.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,422 @@
1
+ """A foundation for hierarchical document components with dependency tracking."""
2
+
3
+ from abc import ABC
4
+ from enum import StrEnum
5
+ from pathlib import Path
6
+ from typing import ClassVar, Generator, List, Optional, Self, Tuple, Type
7
+
8
+ from fabricatio.capabilities.persist import PersistentAble
9
+ from fabricatio.fs import dump_text, safe_text_read
10
+ from fabricatio.fs.readers import extract_sections
11
+ from fabricatio.journal import logger
12
+ from fabricatio.models.generic import (
13
+ AsPrompt,
14
+ Described,
15
+ FinalizedDumpAble,
16
+ Introspect,
17
+ Language,
18
+ ModelHash,
19
+ ProposedUpdateAble,
20
+ SketchedAble,
21
+ Titled,
22
+ WordCount,
23
+ )
24
+ from fabricatio.rust import extract_body, inplace_update, split_out_metadata, to_metadata, word_count
25
+ from fabricatio.utils import fallback_kwargs, ok
26
+ from pydantic import Field
27
+
28
+ ARTICLE_WRAPPER = "// =-=-=-=-=-=-=-=-=-="
29
+
30
+
31
+ class ReferringType(StrEnum):
32
+ """Enumeration of different types of references that can be made in an article."""
33
+
34
+ CHAPTER = "chapter"
35
+ SECTION = "section"
36
+ SUBSECTION = "subsection"
37
+
38
+
39
+ type RefKey = Tuple[str, Optional[str], Optional[str]]
40
+
41
+
42
+ class ArticleMetaData(SketchedAble, Described, WordCount, Titled, Language):
43
+ """Metadata for an article component."""
44
+
45
+ description: str = Field(
46
+ alias="elaboration",
47
+ description=Described.model_fields["description"].description,
48
+ )
49
+
50
+ title: str = Field(alias="heading", description=Titled.model_fields["title"].description)
51
+
52
+ aims: List[str]
53
+ """List of writing aims of the research component in academic style."""
54
+
55
+ @property
56
+ def typst_metadata_comment(self) -> str:
57
+ """Generates a comment for the metadata of the article component."""
58
+ return to_metadata(self.model_dump(include={"description", "aims", "expected_word_count"}, by_alias=True))
59
+
60
+
61
+ class FromTypstCode(ArticleMetaData):
62
+ """Base class for article components that can be created from a Typst code snippet."""
63
+
64
+ @classmethod
65
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
66
+ """Converts a Typst code snippet into an article component."""
67
+ data, body = split_out_metadata(body)
68
+
69
+ return cls(
70
+ heading=title,
71
+ **fallback_kwargs(
72
+ data or {},
73
+ elaboration="",
74
+ expected_word_count=word_count(body),
75
+ aims=[],
76
+ ),
77
+ **kwargs,
78
+ )
79
+
80
+
81
+ class ToTypstCode(ArticleMetaData):
82
+ """Base class for article components that can be converted to a Typst code snippet."""
83
+
84
+ def to_typst_code(self) -> str:
85
+ """Converts the component into a Typst code snippet for rendering."""
86
+ return f"{self.title}\n{self.typst_metadata_comment}\n"
87
+
88
+
89
+ class ArticleOutlineBase(
90
+ ProposedUpdateAble,
91
+ PersistentAble,
92
+ ModelHash,
93
+ Introspect,
94
+ FromTypstCode,
95
+ ToTypstCode,
96
+ ABC,
97
+ ):
98
+ """Base class for article outlines."""
99
+
100
+ @property
101
+ def metadata(self) -> ArticleMetaData:
102
+ """Returns the metadata of the article component."""
103
+ return ArticleMetaData.model_validate(self, from_attributes=True)
104
+
105
+ def update_metadata(self, other: ArticleMetaData) -> Self:
106
+ """Updates the metadata of the current instance with the attributes of another instance."""
107
+ self.aims.clear()
108
+ self.aims.extend(other.aims)
109
+ self.description = other.description
110
+ return self
111
+
112
+ def update_from_inner(self, other: Self) -> Self:
113
+ """Updates the current instance with the attributes of another instance."""
114
+ return self.update_metadata(other)
115
+
116
+
117
+ class SubSectionBase(ArticleOutlineBase):
118
+ """Base class for article sections and subsections."""
119
+
120
+ def to_typst_code(self) -> str:
121
+ """Converts the component into a Typst code snippet for rendering."""
122
+ return f"=== {super().to_typst_code()}"
123
+
124
+ def introspect(self) -> str:
125
+ """Introspects the article subsection outline."""
126
+ return ""
127
+
128
+ def resolve_update_conflict(self, other: Self) -> str:
129
+ """Resolve update errors in the article outline."""
130
+ if self.title != other.title:
131
+ return f"Title mismatched, expected `{self.title}`, got `{other.title}`"
132
+ return ""
133
+
134
+
135
+ class SectionBase[T: SubSectionBase](ArticleOutlineBase):
136
+ """Base class for article sections and subsections."""
137
+
138
+ subsections: List[T]
139
+ """Subsections of the section. Contains at least one subsection. You can also add more as needed."""
140
+
141
+ child_type: ClassVar[Type[SubSectionBase]]
142
+
143
+ def to_typst_code(self) -> str:
144
+ """Converts the section into a Typst formatted code snippet.
145
+
146
+ Returns:
147
+ str: The formatted Typst code snippet.
148
+ """
149
+ return f"== {super().to_typst_code()}" + "\n\n".join(subsec.to_typst_code() for subsec in self.subsections)
150
+
151
+ @classmethod
152
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
153
+ """Creates an Article object from the given Typst code."""
154
+ return super().from_typst_code(
155
+ title,
156
+ body,
157
+ subsections=[
158
+ cls.child_type.from_typst_code(*pack) for pack in extract_sections(body, level=3, section_char="=")
159
+ ],
160
+ )
161
+
162
+ def resolve_update_conflict(self, other: Self) -> str:
163
+ """Resolve update errors in the article outline."""
164
+ out = ""
165
+ if self.title != other.title:
166
+ out += f"Title mismatched, expected `{self.title}`, got `{other.title}`"
167
+ if len(self.subsections) != len(other.subsections):
168
+ out += f"Section count mismatched, expected `{len(self.subsections)}`, got `{len(other.subsections)}`"
169
+ return out or "\n".join(
170
+ [
171
+ conf
172
+ for s, o in zip(self.subsections, other.subsections, strict=True)
173
+ if (conf := s.resolve_update_conflict(o))
174
+ ]
175
+ )
176
+
177
+ def update_from_inner(self, other: Self) -> Self:
178
+ """Updates the current instance with the attributes of another instance."""
179
+ super().update_from_inner(other)
180
+ if len(self.subsections) == 0:
181
+ self.subsections = other.subsections
182
+ return self
183
+
184
+ for self_subsec, other_subsec in zip(self.subsections, other.subsections, strict=True):
185
+ self_subsec.update_from(other_subsec)
186
+ return self
187
+
188
+ def introspect(self) -> str:
189
+ """Introspects the article section outline."""
190
+ if len(self.subsections) == 0:
191
+ return f"Section `{self.title}` contains no subsections, expected at least one, but got 0, you can add one or more as needed."
192
+ return ""
193
+
194
+
195
+ class ChapterBase[T: SectionBase](ArticleOutlineBase):
196
+ """Base class for article chapters."""
197
+
198
+ sections: List[T]
199
+ """Sections of the chapter. Contains at least one section. You can also add more as needed."""
200
+ child_type: ClassVar[Type[SectionBase]]
201
+
202
+ def to_typst_code(self) -> str:
203
+ """Converts the chapter into a Typst formatted code snippet for rendering."""
204
+ return f"= {super().to_typst_code()}" + "\n\n".join(sec.to_typst_code() for sec in self.sections)
205
+
206
+ @classmethod
207
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
208
+ """Creates an Article object from the given Typst code."""
209
+ return super().from_typst_code(
210
+ title,
211
+ body,
212
+ sections=[
213
+ cls.child_type.from_typst_code(*pack) for pack in extract_sections(body, level=2, section_char="=")
214
+ ],
215
+ )
216
+
217
+ def resolve_update_conflict(self, other: Self) -> str:
218
+ """Resolve update errors in the article outline."""
219
+ out = ""
220
+
221
+ if self.title != other.title:
222
+ out += f"Title mismatched, expected `{self.title}`, got `{other.title}`"
223
+ if len(self.sections) == len(other.sections):
224
+ out += f"Chapter count mismatched, expected `{len(self.sections)}`, got `{len(other.sections)}`"
225
+
226
+ return out or "\n".join(
227
+ [conf for s, o in zip(self.sections, other.sections, strict=True) if (conf := s.resolve_update_conflict(o))]
228
+ )
229
+
230
+ def update_from_inner(self, other: Self) -> Self:
231
+ """Updates the current instance with the attributes of another instance."""
232
+ if len(self.sections) == 0:
233
+ self.sections = other.sections
234
+ return self
235
+
236
+ for self_sec, other_sec in zip(self.sections, other.sections, strict=True):
237
+ self_sec.update_from(other_sec)
238
+ return self
239
+
240
+ def introspect(self) -> str:
241
+ """Introspects the article chapter outline."""
242
+ if len(self.sections) == 0:
243
+ return f"Chapter `{self.title}` contains no sections, expected at least one, but got 0, you can add one or more as needed."
244
+ return ""
245
+
246
+
247
+ class ArticleBase[T: ChapterBase](FinalizedDumpAble, AsPrompt, FromTypstCode, ToTypstCode, ABC):
248
+ """Base class for article outlines."""
249
+
250
+ description: str = Field(
251
+ alias="elaboration",
252
+ )
253
+ """The abstract of this article, which serves as a concise summary of an academic article, encapsulating its core purpose, methodologies, key results,
254
+ and conclusions while enabling readers to rapidly assess the relevance and significance of the study.
255
+ Functioning as the article's distilled essence, it succinctly articulates the research problem, objectives,
256
+ and scope, providing a roadmap for the full text while also facilitating database indexing, literature reviews,
257
+ and citation tracking through standardized metadata. Additionally, it acts as an accessibility gateway,
258
+ allowing scholars to gauge the study's contribution to existing knowledge, its methodological rigor,
259
+ and its broader implications without engaging with the entire manuscript, thereby optimizing scholarly communication efficiency."""
260
+
261
+ chapters: List[T]
262
+ """Chapters of the article. Contains at least one chapter. You can also add more as needed."""
263
+
264
+ child_type: ClassVar[Type[ChapterBase]]
265
+
266
+ @classmethod
267
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
268
+ """Generates an article from the given Typst code."""
269
+ return super().from_typst_code(
270
+ title,
271
+ body,
272
+ chapters=[
273
+ cls.child_type.from_typst_code(*pack) for pack in extract_sections(body, level=1, section_char="=")
274
+ ],
275
+ )
276
+
277
+ def iter_dfs_rev(
278
+ self,
279
+ ) -> Generator[ArticleOutlineBase, None, None]:
280
+ """Performs a depth-first search (DFS) through the article structure in reverse order.
281
+
282
+ Returns:
283
+ Generator[ArticleMainBase]: Each component in the article structure in reverse order.
284
+ """
285
+ for chap in self.chapters:
286
+ for sec in chap.sections:
287
+ yield from sec.subsections
288
+ yield sec
289
+ yield chap
290
+
291
+ def iter_dfs(self) -> Generator[ArticleOutlineBase, None, None]:
292
+ """Performs a depth-first search (DFS) through the article structure.
293
+
294
+ Returns:
295
+ Generator[ArticleMainBase]: Each component in the article structure.
296
+ """
297
+ for chap in self.chapters:
298
+ yield chap
299
+ for sec in chap.sections:
300
+ yield sec
301
+ yield from sec.subsections
302
+
303
+ def iter_sections(self) -> Generator[Tuple[ChapterBase, SectionBase], None, None]:
304
+ """Iterates through all sections in the article.
305
+
306
+ Returns:
307
+ Generator[ArticleOutlineBase]: Each section in the article.
308
+ """
309
+ for chap in self.chapters:
310
+ for sec in chap.sections:
311
+ yield chap, sec
312
+
313
+ def iter_subsections(self) -> Generator[Tuple[ChapterBase, SectionBase, SubSectionBase], None, None]:
314
+ """Iterates through all subsections in the article.
315
+
316
+ Returns:
317
+ Generator[ArticleOutlineBase]: Each subsection in the article.
318
+ """
319
+ for chap, sec in self.iter_sections():
320
+ for subsec in sec.subsections:
321
+ yield chap, sec, subsec
322
+
323
+ def find_introspected(self) -> Optional[Tuple[ArticleOutlineBase, str]]:
324
+ """Finds the first introspected component in the article structure."""
325
+ summary = ""
326
+ for component in self.iter_dfs_rev():
327
+ summary += component.introspect()
328
+ if summary:
329
+ return component, summary
330
+ return None
331
+
332
+ def gather_introspected(self) -> Optional[str]:
333
+ """Gathers all introspected components in the article structure."""
334
+ return "\n".join([i for component in self.chapters if (i := component.introspect())])
335
+
336
+ def iter_chap_title(self) -> Generator[str, None, None]:
337
+ """Iterates through all chapter titles in the article."""
338
+ for chap in self.chapters:
339
+ yield chap.title
340
+
341
+ def iter_section_title(self) -> Generator[str, None, None]:
342
+ """Iterates through all section titles in the article."""
343
+ for _, sec in self.iter_sections():
344
+ yield sec.title
345
+
346
+ def iter_subsection_title(self) -> Generator[str, None, None]:
347
+ """Iterates through all subsection titles in the article."""
348
+ for _, _, subsec in self.iter_subsections():
349
+ yield subsec.title
350
+
351
+ def to_typst_code(self) -> str:
352
+ """Generates the Typst code representation of the article."""
353
+ return f"// #{super().to_typst_code()}\n\n" + "\n\n".join(a.to_typst_code() for a in self.chapters)
354
+
355
+ def finalized_dump(self) -> str:
356
+ """Generates standardized hierarchical markup for academic publishing systems.
357
+
358
+ Implements ACL 2024 outline conventions with four-level structure:
359
+ = Chapter Title (Level 1)
360
+ == Section Title (Level 2)
361
+ === Subsection Title (Level 3)
362
+ ==== Subsubsection Title (Level 4)
363
+
364
+ Returns:
365
+ str: Strictly formatted outline with academic sectioning
366
+
367
+ Example:
368
+ = Methodology
369
+ == Neural Architecture Search Framework
370
+ === Differentiable Search Space
371
+ ==== Constrained Optimization Parameters
372
+ === Implementation Details
373
+ == Evaluation Protocol
374
+ """
375
+ return self.to_typst_code()
376
+
377
+ def avg_chap_wordcount[S: "ArticleBase"](self: S) -> S:
378
+ """Set all chap have same word count sum up to be `self.expected_word_count`."""
379
+ avg = int(self.expected_word_count / len(self.chapters))
380
+ for c in self.chapters:
381
+ c.expected_word_count = avg
382
+ return self
383
+
384
+ def avg_sec_wordcount[S: "ArticleBase"](self: S) -> S:
385
+ """Set all sec have same word count sum up to be `self.expected_word_count`."""
386
+ for c in self.chapters:
387
+ avg = int(c.expected_word_count / len(c.sections))
388
+ for s in c.sections:
389
+ s.expected_word_count = avg
390
+ return self
391
+
392
+ def avg_subsec_wordcount[S: "ArticleBase"](self: S) -> S:
393
+ """Set all subsec have same word count sum up to be `self.expected_word_count`."""
394
+ for _, s in self.iter_sections():
395
+ avg = int(s.expected_word_count / len(s.subsections))
396
+ for ss in s.subsections:
397
+ ss.expected_word_count = avg
398
+ return self
399
+
400
+ def avg_wordcount_recursive[S: "ArticleBase"](self: S) -> S:
401
+ """Set all chap, sec, subsec have same word count sum up to be `self.expected_word_count`."""
402
+ return self.avg_chap_wordcount().avg_sec_wordcount().avg_subsec_wordcount()
403
+
404
+ def update_article_file(self, file: str | Path) -> Self:
405
+ """Update the article file."""
406
+ file = Path(file)
407
+ string = safe_text_read(file)
408
+ if updated := inplace_update(string, ARTICLE_WRAPPER, self.to_typst_code()):
409
+ dump_text(file, updated)
410
+ logger.success(f"Successfully updated {file.as_posix()}.")
411
+ else:
412
+ logger.warning(f"Failed to update {file.as_posix()}. Please make sure there are paired `{ARTICLE_WRAPPER}`")
413
+ return self
414
+
415
+ @classmethod
416
+ def from_article_file[S: "ArticleBase"](cls: Type[S], file: str | Path, title: str) -> S:
417
+ """Load article from file."""
418
+ file = Path(file)
419
+ string = safe_text_read(file)
420
+ return cls.from_typst_code(
421
+ title, ok(extract_body(string, ARTICLE_WRAPPER), "Failed to extract body from file.")
422
+ )
@@ -0,0 +1,101 @@
1
+ """ArticleEssence: Semantic fingerprint of academic paper for structured analysis."""
2
+
3
+ from typing import List
4
+
5
+ from fabricatio.capabilities.persist import PersistentAble
6
+ from fabricatio.models.extra.rag import MilvusDataBase
7
+ from fabricatio.models.generic import SketchedAble
8
+ from pydantic import BaseModel
9
+
10
+
11
+ class Equation(BaseModel):
12
+ """Mathematical formalism specification for research contributions."""
13
+
14
+ description: str
15
+ """Structured significance including:
16
+ 1. Conceptual meaning
17
+ 2. Technical workflow role
18
+ 3. Contribution relationship
19
+ """
20
+
21
+ latex_code: str
22
+ """Typeset-ready notation."""
23
+
24
+
25
+ class Figure(BaseModel):
26
+ """Visual component with academic captioning."""
27
+
28
+ description: str
29
+ """Interpretation guide covering:
30
+ 1. Visual element mapping
31
+ 2. Data representation method
32
+ 3. Research connection
33
+ """
34
+
35
+ figure_caption: str
36
+ """Nature-style caption containing:
37
+ 1. Overview statement
38
+ 2. Technical details
39
+ 3. Result implications
40
+ """
41
+
42
+ figure_serial_number: int
43
+ """Image serial number extracted from Markdown path"""
44
+
45
+
46
+ class Highlightings(BaseModel):
47
+ """Technical component aggregator."""
48
+
49
+ highlighted_equations: List[Equation]
50
+ """Equations that highlight the article's core contributions"""
51
+
52
+ highlighted_figures: List[Figure]
53
+ """key figures requiring:
54
+ 1. Framework overview
55
+ 2. Quantitative results
56
+ """
57
+
58
+
59
+ class ArticleEssence(SketchedAble, PersistentAble, MilvusDataBase):
60
+ """Structured representation of a scientific article's core elements in its original language."""
61
+
62
+ language: str
63
+ """Language of the original article."""
64
+
65
+ title: str
66
+ """Exact title of the original article."""
67
+
68
+ authors: List[str]
69
+ """Original author full names as they appear in the source document."""
70
+
71
+ keywords: List[str]
72
+ """Original keywords as they appear in the source document."""
73
+
74
+ publication_year: int
75
+ """Publication year in ISO 8601 (YYYY format)."""
76
+
77
+ highlightings: Highlightings
78
+ """Technical highlights including equations, algorithms, figures, and tables."""
79
+
80
+ abstract: str
81
+ """Abstract text in the original language."""
82
+
83
+ core_contributions: List[str]
84
+ """Technical contributions using CRediT taxonomy verbs."""
85
+
86
+ technical_novelty: List[str]
87
+ """Patent-style claims with technical specificity."""
88
+
89
+ research_problems: List[str]
90
+ """Problem statements as how/why questions."""
91
+
92
+ limitations: List[str]
93
+ """Technical limitations analysis."""
94
+
95
+ bibtex_cite_key: str
96
+ """Bibtex cite key of the original article."""
97
+
98
+ def _prepare_vectorization_inner(self) -> str:
99
+ return self.compact()
100
+
101
+