fabricatio 0.2.12.dev1__cp312-cp312-win_amd64.whl → 0.2.12.dev3__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.
@@ -4,9 +4,10 @@ from asyncio import gather
4
4
  from pathlib import Path
5
5
  from typing import List, Optional
6
6
 
7
- from pydantic import PositiveInt
7
+ from pydantic import Field, PositiveInt
8
8
 
9
9
  from fabricatio import BibManager
10
+ from fabricatio.capabilities.advanced_rag import AdvancedRAG
10
11
  from fabricatio.capabilities.censor import Censor
11
12
  from fabricatio.capabilities.extract import Extract
12
13
  from fabricatio.capabilities.rag import RAG
@@ -18,6 +19,7 @@ from fabricatio.models.extra.article_essence import ArticleEssence
18
19
  from fabricatio.models.extra.article_main import Article, ArticleChapter, ArticleSection, ArticleSubsection
19
20
  from fabricatio.models.extra.article_outline import ArticleOutline
20
21
  from fabricatio.models.extra.rule import RuleSet
22
+ from fabricatio.models.kwargs_types import ChooseKwargs, LLMKwargs
21
23
  from fabricatio.utils import ask_retain, ok
22
24
 
23
25
  TYPST_CITE_USAGE = (
@@ -27,17 +29,17 @@ TYPST_CITE_USAGE = (
27
29
  "Illegal citing syntax examples(seperated by |): [[1],[2],[3]]|[[1],[1-2]]\n"
28
30
  "Those reference mark shall not be omitted during the extraction\n"
29
31
  "It's recommended to cite multiple references that supports your conclusion at a time.\n"
30
- "Wrapp inline expression using $ $,like '$>5m$' '$89%$' , and wrapp block equation using $$ $$. if you are using '$' as the money unit, you should add a '\\' before it to avoid being interpreted as a inline equation. For example 'The pants worths 5\\$.'\n"
32
+ "Wrap inline expression with '\\(' and '\\)',like '\\(>5m\\)' '\\(89%\\)', and wrap block equation with '\\[' and '\\]'.\n"
31
33
  "In addition to that, you can add a label outside the block equation which can be used as a cross reference identifier, the label is a string wrapped in `<` and `>` like `<energy-release-rate-equation>`.Note that the label string should be a summarizing title for the equation being labeled.\n"
32
34
  "you can refer to that label by using the syntax with prefix of `@eqt:`, which indicate that this notation is citing a label from the equations. For example ' @eqt:energy-release-rate-equation ' DO remember that the notation shall have both suffixed and prefixed space char which enable the compiler to distinguish the notation from the plaintext."
33
- "Below is a usage example:\n"
35
+ "Below is two usage example:\n"
34
36
  "```typst\n"
35
37
  "See @eqt:mass-energy-equation , it's the foundation of physics.\n"
36
- "$$\n"
38
+ "\\[\n"
37
39
  "E = m c^2\n"
38
- "$$ <mass-energy-equation>\n\n\n"
39
- "In @eqt:mass-energy-equation , $m$ stands for mass, $c$ stands for speed of light, and $E$ stands for energy. \n"
40
- "```"
40
+ "\\] <mass-energy-equation>\n\n\n"
41
+ "In @eqt:mass-energy-equation , \\(m\\) stands for mass, \\(c\\) stands for speed of light, and \\(E\\) stands for energy. \n"
42
+ "```\n"
41
43
  )
42
44
 
43
45
 
@@ -50,9 +52,9 @@ class WriteArticleContentRAG(Action, RAG, Extract):
50
52
  """The limit of references to be retrieved"""
51
53
  threshold: float = 0.62
52
54
  """The threshold of relevance"""
53
- extractor_model: str
55
+ extractor_model: LLMKwargs
54
56
  """The model to use for extracting the content from the retrieved references."""
55
- query_model: str
57
+ query_model: LLMKwargs
56
58
  """The model to use for querying the database"""
57
59
  supervisor: bool = False
58
60
  """Whether to use supervisor mode"""
@@ -154,12 +156,12 @@ class WriteArticleContentRAG(Action, RAG, Extract):
154
156
  f"Above is the subsection titled `{subsec.title}`.\n"
155
157
  f"I need you to extract the content to update my subsection obj provided below.\n{self.req}"
156
158
  f"{subsec.display()}\n",
157
- model=self.extractor_model,
159
+ **self.extractor_model,
158
160
  ),
159
161
  "Failed to propose new subsection.",
160
162
  )
161
163
  for p in new_subsec.paragraphs:
162
- p.content = cm.apply(p.content).replace("$$", "\n$$\n")
164
+ p.content = cm.apply(p.content)
163
165
  subsec.update_from(new_subsec)
164
166
  logger.debug(f"{subsec.title}:rpl\n{subsec.display()}")
165
167
  return subsec
@@ -175,28 +177,16 @@ class WriteArticleContentRAG(Action, RAG, Extract):
175
177
  extra_instruction: str = "",
176
178
  ) -> str:
177
179
  """Write the raw paragraphs of the subsec."""
178
- return (
179
- (
180
- await self.aask(
181
- f"{cm.as_prompt()}\nAbove is some related reference from other auther retrieved for you."
182
- f"{article_outline.finalized_dump()}\n\nAbove is my article outline, I m writing graduate thesis titled `{article.title}`. "
183
- f"More specifically, i m witting the Chapter `{chap.title}` >> Section `{sec.title}` >> Subsection `{subsec.title}`.\n"
184
- f"Please help me write the paragraphs of the subsec mentioned above, which is `{subsec.title}`.\n"
185
- f"{self.req}\n"
186
- f"You SHALL use `{article.language}` as writing language.\n{extra_instruction}\n"
187
- f"Do not use numbered list to display the outcome, you should regard you are writing the main text of the thesis.\n"
188
- f"You should not copy others' works from the references directly on to my thesis, we can only harness the conclusion they have drawn.\n"
189
- f"No extra explanation is allowed."
190
- )
191
- )
192
- .replace(r" \( ", "$")
193
- .replace(r" \) ", "$")
194
- .replace(r"\(", "$")
195
- .replace(r"\)", "$")
196
- .replace("\\[\n", "$$\n")
197
- .replace("\\[ ", "$$\n")
198
- .replace("\n\\]", "\n$$")
199
- .replace(" \\]", "\n$$")
180
+ return await self.aask(
181
+ f"{cm.as_prompt()}\nAbove is some related reference from other auther retrieved for you."
182
+ f"{article_outline.finalized_dump()}\n\nAbove is my article outline, I m writing graduate thesis titled `{article.title}`. "
183
+ f"More specifically, i m witting the Chapter `{chap.title}` >> Section `{sec.title}` >> Subsection `{subsec.title}`.\n"
184
+ f"Please help me write the paragraphs of the subsec mentioned above, which is `{subsec.title}`.\n"
185
+ f"{self.req}\n"
186
+ f"You SHALL use `{article.language}` as writing language.\n{extra_instruction}\n"
187
+ f"Do not use numbered list to display the outcome, you should regard you are writing the main text of the thesis.\n"
188
+ f"You should not copy others' works from the references directly on to my thesis, we can only harness the conclusion they have drawn.\n"
189
+ f"No extra explanation is allowed."
200
190
  )
201
191
 
202
192
  async def search_database(
@@ -222,7 +212,7 @@ class WriteArticleContentRAG(Action, RAG, Extract):
222
212
  ref_q = ok(
223
213
  await self.arefined_query(
224
214
  search_req,
225
- model=self.query_model,
215
+ **self.query_model,
226
216
  ),
227
217
  "Failed to refine query.",
228
218
  )
@@ -232,7 +222,7 @@ class WriteArticleContentRAG(Action, RAG, Extract):
232
222
  ret = await self.aretrieve(
233
223
  ref_q,
234
224
  ArticleChunk,
235
- final_limit=self.ref_limit,
225
+ max_accepted=self.ref_limit,
236
226
  result_per_query=self.result_per_query,
237
227
  similarity_threshold=self.threshold,
238
228
  )
@@ -240,7 +230,7 @@ class WriteArticleContentRAG(Action, RAG, Extract):
240
230
  cm.add_chunks(ok(ret))
241
231
  ref_q = await self.arefined_query(
242
232
  f"{cm.as_prompt()}\n\nAbove is the retrieved references in the first RAG, now we need to perform the second RAG.\n\n{search_req}",
243
- model=self.query_model,
233
+ **self.query_model,
244
234
  )
245
235
 
246
236
  if ref_q is None:
@@ -252,7 +242,7 @@ class WriteArticleContentRAG(Action, RAG, Extract):
252
242
  ret = await self.aretrieve(
253
243
  ref_q,
254
244
  ArticleChunk,
255
- final_limit=int(self.ref_limit * self.search_increment_multiplier),
245
+ max_accepted=int(self.ref_limit * self.search_increment_multiplier),
256
246
  result_per_query=int(self.result_per_query * self.search_increment_multiplier),
257
247
  similarity_threshold=self.threshold,
258
248
  )
@@ -262,18 +252,19 @@ class WriteArticleContentRAG(Action, RAG, Extract):
262
252
  cm.add_chunks(ret)
263
253
 
264
254
 
265
- class ArticleConsultRAG(Action, RAG):
255
+ class ArticleConsultRAG(Action, AdvancedRAG):
266
256
  """Write an article based on the provided outline."""
267
257
 
268
258
  output_key: str = "consult_count"
269
-
259
+ search_increment_multiplier: float = 1.6
260
+ """The multiplier to increase the limit of references to retrieve per query."""
270
261
  ref_limit: int = 20
271
262
  """The final limit of references."""
272
263
  ref_per_q: int = 3
273
264
  """The limit of references to retrieve per query."""
274
265
  similarity_threshold: float = 0.62
275
266
  """The similarity threshold of references to retrieve."""
276
- ref_q_model: Optional[str] = None
267
+ ref_q_model: ChooseKwargs = Field(default_factory=ChooseKwargs)
277
268
  """The model to use for refining query."""
278
269
  req: str = TYPST_CITE_USAGE
279
270
  """The request for the rag model."""
@@ -285,7 +276,7 @@ class ArticleConsultRAG(Action, RAG):
285
276
  from questionary import confirm, text
286
277
  from rich import print as r_print
287
278
 
288
- from fabricatio.rust import convert_all_block_tex, convert_all_inline_tex
279
+ from fabricatio.rust import convert_all_block_tex, convert_all_inline_tex, fix_misplaced_labels
289
280
 
290
281
  self.target_collection = collection_name or self.safe_target_collection
291
282
 
@@ -295,16 +286,19 @@ class ArticleConsultRAG(Action, RAG):
295
286
  while (req := await text("User: ").ask_async()) is not None:
296
287
  if await confirm("Empty the cm?").ask_async():
297
288
  cm.empty()
298
- ref_q = await self.arefined_query(req, model=self.ref_q_model)
299
- refs = await self.aretrieve(
300
- ok(ref_q, "Failed to refine query."),
301
- ArticleChunk,
302
- final_limit=self.ref_limit,
289
+ await self.clued_search(
290
+ req,
291
+ cm,
292
+ refinery_kwargs=self.ref_q_model,
293
+ expand_multiplier=self.search_increment_multiplier,
294
+ base_accepted=self.ref_limit,
303
295
  result_per_query=self.ref_per_q,
304
296
  similarity_threshold=self.similarity_threshold,
305
297
  )
306
298
 
307
- ret = await self.aask(f"{cm.add_chunks(refs).as_prompt()}\n{self.req}\n{req}")
299
+ ret = await self.aask(f"{cm.as_prompt()}\n{self.req}\n{req}")
300
+
301
+ ret = fix_misplaced_labels(ret)
308
302
  ret = convert_all_inline_tex(ret)
309
303
  ret = convert_all_block_tex(ret)
310
304
  ret = cm.apply(ret)
@@ -396,7 +390,7 @@ class TweakArticleRAG(Action, RAG, Censor):
396
390
  await self.censor_obj_inplace(
397
391
  subsec,
398
392
  ruleset=ruleset,
399
- reference=f"{'\n\n'.join(d.display() for d in await self.aretrieve(refind_q, document_model=ArticleEssence, final_limit=self.ref_limit))}\n\n"
393
+ reference=f"{'\n\n'.join(d.display() for d in await self.aretrieve(refind_q, document_model=ArticleEssence, max_accepted=self.ref_limit))}\n\n"
400
394
  f"You can use Reference above to rewrite the `{subsec.__class__.__name__}`.\n"
401
395
  f"You should Always use `{subsec.language}` as written language, "
402
396
  f"which is the original language of the `{subsec.title}`. "
@@ -1,9 +1,10 @@
1
1
  """Dump the finalized output to a file."""
2
2
 
3
3
  from pathlib import Path
4
- from typing import Any, Dict, Iterable, List, Mapping, Optional, Type
4
+ from typing import Any, Iterable, List, Mapping, Optional, Type
5
5
 
6
6
  from fabricatio import TEMPLATE_MANAGER
7
+ from fabricatio.fs import dump_text
7
8
  from fabricatio.journal import logger
8
9
  from fabricatio.models.action import Action
9
10
  from fabricatio.models.generic import FinalizedDumpAble, FromMapping, PersistentAble
@@ -51,7 +52,7 @@ class RenderedDump(Action, LLMUsage):
51
52
 
52
53
  async def _execute(
53
54
  self,
54
- to_dump: Dict[str, Any],
55
+ to_dump: FinalizedDumpAble,
55
56
  task_input: Optional[Task] = None,
56
57
  dump_path: Optional[str | Path] = None,
57
58
  **_,
@@ -66,8 +67,16 @@ class RenderedDump(Action, LLMUsage):
66
67
  "Could not find the path of file to dump the data.",
67
68
  )
68
69
  )
70
+
69
71
  logger.info(f"Saving output to {dump_path.as_posix()}")
70
- return TEMPLATE_MANAGER.render_template(self.template_name, to_dump)
72
+ dump_text(
73
+ dump_path,
74
+ TEMPLATE_MANAGER.render_template(
75
+ self.template_name, {to_dump.__class__.__name__: to_dump.finalized_dump()}
76
+ ),
77
+ )
78
+
79
+ return dump_path.as_posix()
71
80
 
72
81
 
73
82
  class PersistentAll(Action, LLMUsage):
@@ -0,0 +1,56 @@
1
+ """Advanced RAG (Retrieval Augmented Generation) model."""
2
+
3
+ from typing import Optional, Unpack
4
+
5
+ from fabricatio.capabilities.rag import RAG
6
+ from fabricatio.journal import logger
7
+ from fabricatio.models.adv_kwargs_types import FetchKwargs
8
+ from fabricatio.models.extra.aricle_rag import ArticleChunk, CitationManager
9
+ from fabricatio.models.kwargs_types import ChooseKwargs
10
+
11
+
12
+ class AdvancedRAG(RAG):
13
+ """A class representing the Advanced RAG (Retrieval Augmented Generation) model."""
14
+
15
+ async def clued_search(
16
+ self,
17
+ requirement: str,
18
+ cm: CitationManager,
19
+ max_capacity: int = 40,
20
+ max_round: int = 3,
21
+ expand_multiplier: float = 1.4,
22
+ base_accepted: int = 12,
23
+ refinery_kwargs: Optional[ChooseKwargs] = None,
24
+ **kwargs: Unpack[FetchKwargs],
25
+ ) -> CitationManager:
26
+ """Asynchronously performs a clued search based on a given requirement and citation manager."""
27
+ if max_round<=0:
28
+ raise ValueError("max_round should be greater than 0")
29
+ if max_round == 1:
30
+ logger.warning(
31
+ "max_round should be greater than 1, otherwise it behaves nothing different from the `self.aretrieve`"
32
+ )
33
+
34
+ refinery_kwargs = refinery_kwargs or {}
35
+
36
+ for i in range(max_round + 1, 1):
37
+ logger.info(f"Round [{i + 1}/{max_round}] search started.")
38
+ ref_q = await self.arefined_query(
39
+ f"{cm.as_prompt()}\n\nAbove is the retrieved references in the {i - 1}th RAG, now we need to perform the {i}th RAG."
40
+ f"\n\n{requirement}",
41
+ **refinery_kwargs,
42
+ )
43
+ if ref_q is None:
44
+ logger.error(f"At round [{i + 1}/{max_round}] search, failed to refine the query, exit.")
45
+ return cm
46
+ refs = await self.aretrieve(ref_q, ArticleChunk, base_accepted, **kwargs)
47
+
48
+ if (max_capacity := max_capacity - len(refs)) < 0:
49
+ cm.add_chunks(refs[0:max_capacity])
50
+ logger.debug(f"At round [{i + 1}/{max_round}] search, the capacity is not enough, exit.")
51
+ return cm
52
+
53
+ cm.add_chunks(refs)
54
+ base_accepted = int(base_accepted * expand_multiplier)
55
+ logger.debug(f"Exceeded max_round: {max_round}, exit.")
56
+ return cm
@@ -189,7 +189,7 @@ class RAG(EmbeddingUsage):
189
189
  self,
190
190
  query: List[str] | str,
191
191
  document_model: Type[D],
192
- final_limit: int = 20,
192
+ max_accepted: int = 20,
193
193
  **kwargs: Unpack[FetchKwargs],
194
194
  ) -> List[D]:
195
195
  """Retrieve data from the collection.
@@ -197,7 +197,7 @@ class RAG(EmbeddingUsage):
197
197
  Args:
198
198
  query (List[str] | str): The query to be used for retrieval.
199
199
  document_model (Type[D]): The model class used to convert retrieved data into document objects.
200
- final_limit (int): The final limit on the number of results to return.
200
+ max_accepted (int): The final limit on the number of results to return.
201
201
  **kwargs (Unpack[FetchKwargs]): Additional keyword arguments for retrieval.
202
202
 
203
203
  Returns:
@@ -211,9 +211,9 @@ class RAG(EmbeddingUsage):
211
211
  document_model=document_model,
212
212
  **kwargs,
213
213
  )
214
- )[:final_limit]
214
+ )[:max_accepted]
215
215
 
216
- async def arefined_query(self, question: List[str] | str, **kwargs: Unpack[ChooseKwargs]) -> Optional[List[str]]:
216
+ async def arefined_query(self, question: List[str] | str, **kwargs: Unpack[ChooseKwargs[Optional[List[str]]]]) -> Optional[List[str]]:
217
217
  """Refines the given question using a template.
218
218
 
219
219
  Args:
@@ -1,6 +1,6 @@
1
1
  """A foundation for hierarchical document components with dependency tracking."""
2
2
 
3
- from abc import ABC, abstractmethod
3
+ from abc import ABC
4
4
  from enum import StrEnum
5
5
  from typing import Generator, List, Optional, Self, Tuple
6
6
 
@@ -18,7 +18,8 @@ from fabricatio.models.generic import (
18
18
  Titled,
19
19
  WordCount,
20
20
  )
21
- from fabricatio.rust import comment
21
+ from fabricatio.rust import split_out_metadata, to_metadata, word_count
22
+ from fabricatio.utils import fallback_kwargs
22
23
  from pydantic import Field
23
24
 
24
25
 
@@ -49,21 +50,46 @@ class ArticleMetaData(SketchedAble, Described, WordCount, Titled, Language):
49
50
  @property
50
51
  def typst_metadata_comment(self) -> str:
51
52
  """Generates a comment for the metadata of the article component."""
52
- return comment(
53
- (f"Desc:\n {self.description}\n" if self.description else "")
54
- + (f"Aims:\n {'\n '.join(self.aims)}\n" if self.aims else "")
55
- + (f"Expected Word Count:{self.expected_word_count}" if self.expected_word_count else "")
56
-
53
+ return to_metadata(self.model_dump(include={"description", "aims", "expected_word_count"}, by_alias=True))
54
+
55
+
56
+ class FromTypstCode(ArticleMetaData):
57
+ """Base class for article components that can be created from a Typst code snippet."""
58
+
59
+ @classmethod
60
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
61
+ """Converts a Typst code snippet into an article component."""
62
+ data, body = split_out_metadata(body)
63
+
64
+ return cls(
65
+ heading=title,
66
+ **fallback_kwargs(
67
+ data or {},
68
+ elaboration="",
69
+ expected_word_count=word_count(body),
70
+ aims=[],
71
+ ),
72
+ **kwargs,
57
73
  )
58
74
 
59
75
 
76
+ class ToTypstCode(ArticleMetaData):
77
+ """Base class for article components that can be converted to a Typst code snippet."""
78
+
79
+ def to_typst_code(self) -> str:
80
+ """Converts the component into a Typst code snippet for rendering."""
81
+ return f"{self.title}\n{self.typst_metadata_comment}\n"
82
+
83
+
60
84
  class ArticleOutlineBase(
61
- ArticleMetaData,
62
85
  ResolveUpdateConflict,
63
86
  ProposedUpdateAble,
64
87
  PersistentAble,
65
88
  ModelHash,
66
89
  Introspect,
90
+ FromTypstCode,
91
+ ToTypstCode,
92
+ ABC,
67
93
  ):
68
94
  """Base class for article outlines."""
69
95
 
@@ -89,17 +115,13 @@ class ArticleOutlineBase(
89
115
  """Updates the current instance with the attributes of another instance."""
90
116
  return self.update_metadata(other)
91
117
 
92
- @abstractmethod
93
- def to_typst_code(self) -> str:
94
- """Converts the component into a Typst code snippet for rendering."""
95
-
96
118
 
97
119
  class SubSectionBase(ArticleOutlineBase):
98
120
  """Base class for article sections and subsections."""
99
121
 
100
122
  def to_typst_code(self) -> str:
101
123
  """Converts the component into a Typst code snippet for rendering."""
102
- return f"=== {self.title}\n{self.typst_metadata_comment}\n"
124
+ return f"=== {super().to_typst_code()}"
103
125
 
104
126
  def introspect(self) -> str:
105
127
  """Introspects the article subsection outline."""
@@ -124,9 +146,7 @@ class SectionBase[T: SubSectionBase](ArticleOutlineBase):
124
146
  Returns:
125
147
  str: The formatted Typst code snippet.
126
148
  """
127
- return f"== {self.title}\n{self.typst_metadata_comment}\n" + "\n\n".join(
128
- subsec.to_typst_code() for subsec in self.subsections
129
- )
149
+ return f"== {super().to_typst_code()}" + "\n\n".join(subsec.to_typst_code() for subsec in self.subsections)
130
150
 
131
151
  def resolve_update_conflict(self, other: Self) -> str:
132
152
  """Resolve update errors in the article outline."""
@@ -169,9 +189,7 @@ class ChapterBase[T: SectionBase](ArticleOutlineBase):
169
189
 
170
190
  def to_typst_code(self) -> str:
171
191
  """Converts the chapter into a Typst formatted code snippet for rendering."""
172
- return f"= {self.title}\n{self.typst_metadata_comment}\n" + "\n\n".join(
173
- sec.to_typst_code() for sec in self.sections
174
- )
192
+ return f"= {super().to_typst_code()}" + "\n\n".join(sec.to_typst_code() for sec in self.sections)
175
193
 
176
194
  def resolve_update_conflict(self, other: Self) -> str:
177
195
  """Resolve update errors in the article outline."""
@@ -203,12 +221,13 @@ class ChapterBase[T: SectionBase](ArticleOutlineBase):
203
221
  return ""
204
222
 
205
223
 
206
- class ArticleBase[T: ChapterBase](FinalizedDumpAble, AsPrompt, WordCount, Described, Titled, Language, ABC):
224
+ class ArticleBase[T: ChapterBase](FinalizedDumpAble, AsPrompt, FromTypstCode, ToTypstCode, ABC):
207
225
  """Base class for article outlines."""
208
226
 
209
- title: str = Field(alias="heading", description=Titled.model_fields["title"].description)
210
- description: str = Field(alias="abstract")
211
- """The abstract serves as a concise summary of an academic article, encapsulating its core purpose, methodologies, key results,
227
+ description: str = Field(
228
+ alias="elaboration",
229
+ )
230
+ """The abstract of this article, which serves as a concise summary of an academic article, encapsulating its core purpose, methodologies, key results,
212
231
  and conclusions while enabling readers to rapidly assess the relevance and significance of the study.
213
232
  Functioning as the article's distilled essence, it succinctly articulates the research problem, objectives,
214
233
  and scope, providing a roadmap for the full text while also facilitating database indexing, literature reviews,
@@ -293,6 +312,10 @@ class ArticleBase[T: ChapterBase](FinalizedDumpAble, AsPrompt, WordCount, Descri
293
312
  for _, _, subsec in self.iter_subsections():
294
313
  yield subsec.title
295
314
 
315
+ def to_typst_code(self) -> str:
316
+ """Generates the Typst code representation of the article."""
317
+ return f"// #{super().to_typst_code()}\n\n" + "\n\n".join(a.to_typst_code() for a in self.chapters)
318
+
296
319
  def finalized_dump(self) -> str:
297
320
  """Generates standardized hierarchical markup for academic publishing systems.
298
321
 
@@ -313,26 +336,16 @@ class ArticleBase[T: ChapterBase](FinalizedDumpAble, AsPrompt, WordCount, Descri
313
336
  === Implementation Details
314
337
  == Evaluation Protocol
315
338
  """
316
- return (
317
- comment(
318
- f"Title:{self.title}\n"
319
- + (f"Desc:\n{self.description}\n" if self.description else "")
320
- + f"Word Count:{self.expected_word_count}"
321
- if self.expected_word_count
322
- else ""
323
- )
324
- + "\n\n"
325
- + "\n\n".join(a.to_typst_code() for a in self.chapters)
326
- )
339
+ return self.to_typst_code()
327
340
 
328
- def avg_chap_wordcount[S](self: S) -> S:
341
+ def avg_chap_wordcount[S: "ArticleBase"](self: S) -> S:
329
342
  """Set all chap have same word count sum up to be `self.expected_word_count`."""
330
343
  avg = int(self.expected_word_count / len(self.chapters))
331
344
  for c in self.chapters:
332
345
  c.expected_word_count = avg
333
346
  return self
334
347
 
335
- def avg_sec_wordcount[S](self: S) -> S:
348
+ def avg_sec_wordcount[S: "ArticleBase"](self: S) -> S:
336
349
  """Set all sec have same word count sum up to be `self.expected_word_count`."""
337
350
  for c in self.chapters:
338
351
  avg = int(c.expected_word_count / len(c.sections))
@@ -340,7 +353,7 @@ class ArticleBase[T: ChapterBase](FinalizedDumpAble, AsPrompt, WordCount, Descri
340
353
  s.expected_word_count = avg
341
354
  return self
342
355
 
343
- def avg_subsec_wordcount[S](self: S) -> S:
356
+ def avg_subsec_wordcount[S: "ArticleBase"](self: S) -> S:
344
357
  """Set all subsec have same word count sum up to be `self.expected_word_count`."""
345
358
  for _, s in self.iter_sections():
346
359
  avg = int(s.expected_word_count / len(s.subsections))
@@ -348,6 +361,6 @@ class ArticleBase[T: ChapterBase](FinalizedDumpAble, AsPrompt, WordCount, Descri
348
361
  ss.expected_word_count = avg
349
362
  return self
350
363
 
351
- def avg_wordcount_recursive(self) -> Self:
364
+ def avg_wordcount_recursive[S: "ArticleBase"](self: S) -> S:
352
365
  """Set all chap, sec, subsec have same word count sum up to be `self.expected_word_count`."""
353
- return self.avg_chap_wordcount().avg_sec_wordcount().avg_sec_wordcount()
366
+ return self.avg_chap_wordcount().avg_sec_wordcount().avg_subsec_wordcount()
@@ -18,11 +18,16 @@ from fabricatio.models.extra.article_outline import (
18
18
  ArticleSubsectionOutline,
19
19
  )
20
20
  from fabricatio.models.generic import Described, PersistentAble, SequencePatch, SketchedAble, WithRef, WordCount
21
- from fabricatio.rust import convert_all_block_tex, convert_all_inline_tex, word_count
22
- from fabricatio.utils import fallback_kwargs
21
+ from fabricatio.rust import (
22
+ convert_all_block_tex,
23
+ convert_all_inline_tex,
24
+ fix_misplaced_labels,
25
+ split_out_metadata,
26
+ word_count,
27
+ )
23
28
  from pydantic import Field, NonNegativeInt
24
29
 
25
- PARAGRAPH_SEP = "// - - -"
30
+ PARAGRAPH_SEP = "\n\n// - - -\n\n"
26
31
 
27
32
 
28
33
  class Paragraph(SketchedAble, WordCount, Described):
@@ -93,17 +98,17 @@ class ArticleSubsection(SubSectionBase):
93
98
  Returns:
94
99
  str: Typst code snippet for rendering.
95
100
  """
96
- return super().to_typst_code() + f"\n\n{PARAGRAPH_SEP}\n\n".join(p.content for p in self.paragraphs)
101
+ return super().to_typst_code() + PARAGRAPH_SEP.join(p.content for p in self.paragraphs)
97
102
 
98
103
  @classmethod
99
- def from_typst_code(cls, title: str, body: str) -> Self:
104
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
100
105
  """Creates an Article object from the given Typst code."""
101
- return cls(
102
- heading=title,
103
- elaboration="",
104
- paragraphs=[Paragraph.from_content(p) for p in body.split(PARAGRAPH_SEP)],
105
- expected_word_count=word_count(body),
106
- aims=[],
106
+ _, para_body = split_out_metadata(body)
107
+
108
+ return super().from_typst_code(
109
+ title,
110
+ body,
111
+ paragraphs=[Paragraph.from_content(p) for p in para_body.split(PARAGRAPH_SEP)],
107
112
  )
108
113
 
109
114
 
@@ -111,16 +116,14 @@ class ArticleSection(SectionBase[ArticleSubsection]):
111
116
  """Atomic argumentative unit with high-level specificity."""
112
117
 
113
118
  @classmethod
114
- def from_typst_code(cls, title: str, body: str) -> Self:
119
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
115
120
  """Creates an Article object from the given Typst code."""
116
- return cls(
121
+ return super().from_typst_code(
122
+ title,
123
+ body,
117
124
  subsections=[
118
125
  ArticleSubsection.from_typst_code(*pack) for pack in extract_sections(body, level=3, section_char="=")
119
126
  ],
120
- heading=title,
121
- elaboration="",
122
- expected_word_count=word_count(body),
123
- aims=[],
124
127
  )
125
128
 
126
129
 
@@ -128,21 +131,18 @@ class ArticleChapter(ChapterBase[ArticleSection]):
128
131
  """Thematic progression implementing research function."""
129
132
 
130
133
  @classmethod
131
- def from_typst_code(cls, title: str, body: str) -> Self:
134
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
132
135
  """Creates an Article object from the given Typst code."""
133
- return cls(
136
+ return super().from_typst_code(
137
+ title,
138
+ body,
134
139
  sections=[
135
140
  ArticleSection.from_typst_code(*pack) for pack in extract_sections(body, level=2, section_char="=")
136
141
  ],
137
- heading=title,
138
- elaboration="",
139
- expected_word_count=word_count(body),
140
- aims=[],
141
142
  )
142
143
 
143
144
 
144
145
  class Article(
145
- SketchedAble,
146
146
  WithRef[ArticleOutline],
147
147
  PersistentAble,
148
148
  ArticleBase[ArticleChapter],
@@ -165,22 +165,11 @@ class Article(
165
165
  """Convert tex to typst code."""
166
166
  for _, _, subsec in self.iter_subsections():
167
167
  for p in subsec.paragraphs:
168
+ p.content = fix_misplaced_labels(p.content)
168
169
  p.content = convert_all_inline_tex(p.content)
169
170
  p.content = convert_all_block_tex(p.content)
170
171
  return self
171
172
 
172
- def fix_wrapper(self) -> Self:
173
- """Fix wrapper."""
174
- for _, _, subsec in self.iter_subsections():
175
- for p in subsec.paragraphs:
176
- p.content = (
177
- p.content.replace(r" \( ", "$")
178
- .replace(r" \) ", "$")
179
- .replace("\\[\n", "$$\n")
180
- .replace("\n\\]", "\n$$")
181
- )
182
- return self
183
-
184
173
  @override
185
174
  def iter_subsections(self) -> Generator[Tuple[ArticleChapter, ArticleSection, ArticleSubsection], None, None]:
186
175
  return super().iter_subsections() # pyright: ignore [reportReturnType]
@@ -268,16 +257,12 @@ class Article(
268
257
  @classmethod
269
258
  def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
270
259
  """Generates an article from the given Typst code."""
271
- return cls(
260
+ return super().from_typst_code(
261
+ title,
262
+ body,
272
263
  chapters=[
273
264
  ArticleChapter.from_typst_code(*pack) for pack in extract_sections(body, level=1, section_char="=")
274
265
  ],
275
- heading=title,
276
- **fallback_kwargs(
277
- kwargs,
278
- expected_word_count=word_count(body),
279
- abstract="",
280
- ),
281
266
  )
282
267
 
283
268
  @classmethod
@@ -1,7 +1,8 @@
1
1
  """A module containing the ArticleOutline class, which represents the outline of an academic paper."""
2
2
 
3
- from typing import Dict
3
+ from typing import Dict, Self
4
4
 
5
+ from fabricatio.fs.readers import extract_sections
5
6
  from fabricatio.models.extra.article_base import (
6
7
  ArticleBase,
7
8
  ChapterBase,
@@ -9,7 +10,7 @@ from fabricatio.models.extra.article_base import (
9
10
  SubSectionBase,
10
11
  )
11
12
  from fabricatio.models.extra.article_proposal import ArticleProposal
12
- from fabricatio.models.generic import PersistentAble, SketchedAble, WithRef
13
+ from fabricatio.models.generic import PersistentAble, WithRef
13
14
 
14
15
 
15
16
  class ArticleSubsectionOutline(SubSectionBase):
@@ -18,14 +19,39 @@ class ArticleSubsectionOutline(SubSectionBase):
18
19
 
19
20
  class ArticleSectionOutline(SectionBase[ArticleSubsectionOutline]):
20
21
  """A slightly more detailed research component specification for academic paper generation, Must contain subsections."""
22
+ @classmethod
23
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
24
+ """Parse the given Typst code into an ArticleSectionOutline instance."""
25
+ return super().from_typst_code(
26
+ title,
27
+ body,
28
+ subsections=[
29
+ ArticleSubsectionOutline.from_typst_code(*pack)
30
+ for pack in extract_sections(body, level=3, section_char="=")
31
+ ],
32
+ )
33
+
21
34
 
22
35
 
23
36
  class ArticleChapterOutline(ChapterBase[ArticleSectionOutline]):
24
37
  """Macro-structural unit implementing standard academic paper organization. Must contain sections."""
25
38
 
39
+ @classmethod
40
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
41
+ """Parse the given Typst code into an ArticleChapterOutline instance."""
42
+ return super().from_typst_code(
43
+ title,
44
+ body,
45
+ sections=[
46
+ ArticleSectionOutline.from_typst_code(*pack)
47
+ for pack in extract_sections(body, level=2, section_char="=")
48
+ ],
49
+
50
+ )
51
+
52
+
26
53
 
27
54
  class ArticleOutline(
28
- SketchedAble,
29
55
  WithRef[ArticleProposal],
30
56
  PersistentAble,
31
57
  ArticleBase[ArticleChapterOutline],
@@ -38,3 +64,15 @@ class ArticleOutline(
38
64
  "Original Article Proposal": self.referenced.display(),
39
65
  "Original Article Outline": self.display(),
40
66
  }
67
+
68
+ @classmethod
69
+ def from_typst_code(cls, title: str, body: str, **kwargs) -> Self:
70
+ """Parse the given Typst code into an ArticleOutline instance."""
71
+ return super().from_typst_code(
72
+ title,
73
+ body,
74
+ chapters=[
75
+ ArticleChapterOutline.from_typst_code(*pack)
76
+ for pack in extract_sections(body, level=1, section_char="=")
77
+ ],
78
+ )
Binary file
fabricatio/rust.pyi CHANGED
@@ -12,7 +12,9 @@ Key Features:
12
12
  """
13
13
 
14
14
  from pathlib import Path
15
- from typing import Any, Dict, List, Optional, overload
15
+ from typing import Any, Dict, List, Optional, Tuple, overload
16
+
17
+ from pydantic import JsonValue
16
18
 
17
19
  class TemplateManager:
18
20
  """Template rendering engine using Handlebars templates.
@@ -325,6 +327,16 @@ def convert_all_block_tex(string: str) -> str:
325
327
  The converted string with block TeX code replaced.
326
328
  """
327
329
 
330
+ def fix_misplaced_labels(string: str) -> str:
331
+ """A func to fix labels in a string.
332
+
333
+ Args:
334
+ string: The input string containing misplaced labels.
335
+
336
+ Returns:
337
+ The fixed string with labels properly placed.
338
+ """
339
+
328
340
  def comment(string: str) -> str:
329
341
  """Add comment to the string.
330
342
 
@@ -344,3 +356,24 @@ def uncomment(string: str) -> str:
344
356
  Returns:
345
357
  The string with comments (lines starting with '// ' or '//') removed.
346
358
  """
359
+
360
+ def split_out_metadata(string: str) -> Tuple[Optional[JsonValue], str]:
361
+ """Split out metadata from a string.
362
+
363
+ Args:
364
+ string: The input string containing metadata.
365
+
366
+ Returns:
367
+ A tuple containing the metadata as a Python object (if parseable) and the remaining string.
368
+ """
369
+
370
+ def to_metadata(data: JsonValue) -> str:
371
+ """Convert a Python object to a YAML string.
372
+
373
+ Args:
374
+ data: The Python object to be converted to YAML.
375
+
376
+ Returns:
377
+ The YAML string representation of the input data.
378
+ """
379
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fabricatio
3
- Version: 0.2.12.dev1
3
+ Version: 0.2.12.dev3
4
4
  Classifier: License :: OSI Approved :: MIT License
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Programming Language :: Python :: 3.12
@@ -1,20 +1,21 @@
1
- fabricatio-0.2.12.dev1.dist-info/METADATA,sha256=rE72BRRmswpE0cWYnDFdoklrXGTLORE-lDpDJahO-0o,5263
2
- fabricatio-0.2.12.dev1.dist-info/WHEEL,sha256=jABKVkLC9kJr8mi_er5jOqpiQUjARSLXDUIIxDqsS50,96
3
- fabricatio-0.2.12.dev1.dist-info/licenses/LICENSE,sha256=do7J7EiCGbq0QPbMAL_FqLYufXpHnCnXBOuqVPwSV8Y,1088
1
+ fabricatio-0.2.12.dev3.dist-info/METADATA,sha256=iMt48rBNujWl2lSSwiAHtT22G-K7zpXLPwnV8K_32mw,5263
2
+ fabricatio-0.2.12.dev3.dist-info/WHEEL,sha256=jABKVkLC9kJr8mi_er5jOqpiQUjARSLXDUIIxDqsS50,96
3
+ fabricatio-0.2.12.dev3.dist-info/licenses/LICENSE,sha256=do7J7EiCGbq0QPbMAL_FqLYufXpHnCnXBOuqVPwSV8Y,1088
4
4
  fabricatio/actions/article.py,sha256=SFl1zc0hz9vW2sW4VJm9-w8E7kLEty-2LzXi9wgWMmE,10905
5
- fabricatio/actions/article_rag.py,sha256=P3wAwLZK92_LDTtzODrmtIGdLGPiOejMKGY8ydLnnPU,18938
5
+ fabricatio/actions/article_rag.py,sha256=dJJfPkfwnx8P4BebESOpPaeTfvER-1U68aPXpwMdDCQ,18723
6
6
  fabricatio/actions/fs.py,sha256=gJR14U4ln35nt8Z7OWLVAZpqGaLnED-r1Yi-lX22tkI,959
7
- fabricatio/actions/output.py,sha256=FoSEbvhPdWe8ARhgID7wkSH7kS8jb3BDqRUqpvIkPM0,8095
7
+ fabricatio/actions/output.py,sha256=lX0HkDse3ypCzZgeF-8Dr-EnNFdBiE-WQ1iLPFlGM1g,8302
8
8
  fabricatio/actions/rag.py,sha256=KN-OWgcQjGmNgSZ-s5B8m4LpYKSGFJR8eq72mo2CP9k,3592
9
9
  fabricatio/actions/rules.py,sha256=dkvCgNDjt2KSO1VgPRsxT4YBmIIMeetZb5tiz-slYkU,3640
10
10
  fabricatio/actions/__init__.py,sha256=wVENCFtpVb1rLFxoOFJt9-8smLWXuJV7IwA8P3EfFz4,48
11
11
  fabricatio/capabilities/advanced_judge.py,sha256=selB0Gwf1F4gGJlwBiRo6gI4KOUROgh3WnzO3mZFEls,706
12
+ fabricatio/capabilities/advanced_rag.py,sha256=gt4ccqyMSVwQLwsEyJrjPc-RXMZZBwObIjsh8A8ILbM,2386
12
13
  fabricatio/capabilities/censor.py,sha256=bBT5qy-kp7fh8g4Lz3labSwxwJ60gGd_vrkc6k1cZ1U,4719
13
14
  fabricatio/capabilities/check.py,sha256=kYqzohhv2bZfl1aKSUt7a8snT8YEl2zgha_ZdAdMMfQ,8622
14
15
  fabricatio/capabilities/correct.py,sha256=W_cInqlciNEhyMK0YI53jk4EvW9uAdge90IO9OElUmA,10420
15
16
  fabricatio/capabilities/extract.py,sha256=PMjkWvbsv57IYT7zzd_xbIu4eQqQjpcmBtJzqlWZhHY,2495
16
17
  fabricatio/capabilities/propose.py,sha256=hkBeSlmcTdfYWT-ph6nlbtHXBozi_JXqXlWcnBy3W78,2007
17
- fabricatio/capabilities/rag.py,sha256=kqcunWBC6oA4P1rzIG2Xu9zqSg73H3uKPF41JJQ1HVI,9595
18
+ fabricatio/capabilities/rag.py,sha256=a48dEaWE-sniwLhNtGz4xlSmRi_-PdOgDzNaDwnam_g,9619
18
19
  fabricatio/capabilities/rating.py,sha256=iMtQs3H6vCjuEjiuuz4SRKMVaX7yff7MHWz-slYvi5g,17835
19
20
  fabricatio/capabilities/review.py,sha256=-EMZe0ADFPT6fPGmra16UPjJC1M3rAs6dPFdTZ88Fgg,5060
20
21
  fabricatio/capabilities/task.py,sha256=uks1U-4LNCUdwdRxAbJJjMc31hOw6jlrcYriuQQfb04,4475
@@ -32,10 +33,10 @@ fabricatio/models/adv_kwargs_types.py,sha256=kUO-SiZtFuz5cZCmMLnJJ9tjQ4-Zd_foo6R
32
33
  fabricatio/models/events.py,sha256=wiirk_ASg3iXDOZU_gIimci1VZVzWE1nDmxy-hQVJ9M,4150
33
34
  fabricatio/models/extra/advanced_judge.py,sha256=INUl_41C8jkausDekkjnEmTwNfLCJ23TwFjq2cM23Cw,1092
34
35
  fabricatio/models/extra/aricle_rag.py,sha256=p91JI8FmFSrHVg5KbhJq4w8vQdx4VUW75SrtQUi9ju4,10987
35
- fabricatio/models/extra/article_base.py,sha256=kUd9wSi1E0GcM1xBaHyrNyICzM0oYf_SyV6wjcTHmlM,14061
36
+ fabricatio/models/extra/article_base.py,sha256=R8EOlQBs7hadjP9cxG_uc24DJDpiVpJW9Wb9DA6XmXE,14516
36
37
  fabricatio/models/extra/article_essence.py,sha256=mlIkkRMR3I1RtqiiOnmIE3Vy623L4eECumkRzryE1pw,2749
37
- fabricatio/models/extra/article_main.py,sha256=Oz6X7tHztb6zwnJQnw9XlgVwTp8kfI9ywkWUY3_fe_E,11824
38
- fabricatio/models/extra/article_outline.py,sha256=w7O0SHgC7exbptWVbR62FMHAueMgBpyWKVYMGGl_oj8,1427
38
+ fabricatio/models/extra/article_main.py,sha256=zbWhXAvDPBfg3EHZRf7kVEChPZagqWbzeYFJdJtaDOY,11211
39
+ fabricatio/models/extra/article_outline.py,sha256=C9WNZNSz6gyuw9lDp29qidPvHBTENaeqcHfCoE_Y7F4,2793
39
40
  fabricatio/models/extra/article_proposal.py,sha256=NbyjW-7UiFPtnVD9nte75re4xL2pD4qL29PpNV4Cg_M,1870
40
41
  fabricatio/models/extra/patches.py,sha256=_WNCxtYzzsVfUxI16vu4IqsLahLYRHdbQN9er9tqhC0,997
41
42
  fabricatio/models/extra/problem.py,sha256=8tTU-3giFHOi5j7NJsvH__JJyYcaGrcfsRnkzQNm0Ew,7216
@@ -50,7 +51,7 @@ fabricatio/models/tool.py,sha256=jQ51g4lwTPfsMF1nbreDJtBczbxIHoXcPuLSOqHliq8,125
50
51
  fabricatio/models/usages.py,sha256=0bzITf0vug9ZaN6qnjNfFB7T8BAvpXE0bvx0otFYLLA,33356
51
52
  fabricatio/parser.py,sha256=-RbW2yzfJiu2ARq-lZw4tfgsjY2rIZWtJpoUmaE6gJQ,6637
52
53
  fabricatio/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
53
- fabricatio/rust.pyi,sha256=GJRLeeQ1UIaf5kOgJWX2GkwIacoTBk3yBKuD5cKW8i4,10489
54
+ fabricatio/rust.pyi,sha256=kU-I0PD-tG0RGTy5xUobneXY-E06Qopjp4v1awpYmGw,11339
54
55
  fabricatio/rust_instances.py,sha256=Byeo8KHW_dJiXujJq7YPGDLBX5bHNDYbBc4sY3uubVY,313
55
56
  fabricatio/toolboxes/arithmetic.py,sha256=WLqhY-Pikv11Y_0SGajwZx3WhsLNpHKf9drzAqOf_nY,1369
56
57
  fabricatio/toolboxes/fs.py,sha256=l4L1CVxJmjw9Ld2XUpIlWfV0_Fu_2Og6d3E13I-S4aE,736
@@ -60,6 +61,6 @@ fabricatio/workflows/articles.py,sha256=ObYTFUqLUk_CzdmmnX6S7APfxcGmPFqnFr9pdjU7
60
61
  fabricatio/workflows/rag.py,sha256=-YYp2tlE9Vtfgpg6ROpu6QVO8j8yVSPa6yDzlN3qVxs,520
61
62
  fabricatio/workflows/__init__.py,sha256=5ScFSTA-bvhCesj3U9Mnmi6Law6N1fmh5UKyh58L3u8,51
62
63
  fabricatio/__init__.py,sha256=Rmvq2VgdS2u68vnOi2i5RbeWbAwrJDbk8D8D883PJWE,1022
63
- fabricatio/rust.cp312-win_amd64.pyd,sha256=GQijCHR-8G4LJIApA7L4158SZJCyzJHd9AGqi593EUs,4143616
64
- fabricatio-0.2.12.dev1.data/scripts/tdown.exe,sha256=HzNOjmihQ39UQc4s3Ko8tyZm9RJgwXruIiI8XU2Tk2o,3349504
65
- fabricatio-0.2.12.dev1.dist-info/RECORD,,
64
+ fabricatio/rust.cp312-win_amd64.pyd,sha256=YARjZHId5jH51qUqMMIKn7RotIiwm8oT6UqBv8PotKI,4435456
65
+ fabricatio-0.2.12.dev3.data/scripts/tdown.exe,sha256=rpmuLrxy-ZZiqVzrMYH_oFpanqkpE6dlI1aY9rCxCiA,3350016
66
+ fabricatio-0.2.12.dev3.dist-info/RECORD,,