fabricatio 0.3.14.dev0__cp312-cp312-win_amd64.whl → 0.3.14.dev2__cp312-cp312-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fabricatio/__init__.py +3 -5
- fabricatio/actions/article.py +31 -31
- fabricatio/actions/article_rag.py +58 -58
- fabricatio/actions/output.py +58 -24
- fabricatio/actions/rag.py +2 -3
- fabricatio/capabilities/advanced_judge.py +4 -7
- fabricatio/capabilities/advanced_rag.py +2 -1
- fabricatio/capabilities/censor.py +5 -4
- fabricatio/capabilities/check.py +27 -27
- fabricatio/capabilities/correct.py +22 -22
- fabricatio/capabilities/extract.py +33 -33
- fabricatio/capabilities/persist.py +103 -0
- fabricatio/capabilities/propose.py +2 -2
- fabricatio/capabilities/rag.py +37 -37
- fabricatio/capabilities/rating.py +66 -70
- fabricatio/capabilities/review.py +12 -11
- fabricatio/capabilities/task.py +19 -18
- fabricatio/decorators.py +9 -9
- fabricatio/{core.py → emitter.py} +17 -19
- fabricatio/journal.py +2 -4
- fabricatio/models/action.py +10 -12
- fabricatio/models/extra/aricle_rag.py +15 -12
- fabricatio/models/extra/article_base.py +4 -5
- fabricatio/models/extra/article_essence.py +2 -1
- fabricatio/models/extra/article_main.py +12 -12
- fabricatio/models/extra/article_outline.py +2 -1
- fabricatio/models/extra/article_proposal.py +1 -1
- fabricatio/models/extra/rag.py +2 -2
- fabricatio/models/extra/rule.py +2 -1
- fabricatio/models/generic.py +53 -136
- fabricatio/models/kwargs_types.py +1 -9
- fabricatio/models/role.py +15 -16
- fabricatio/models/task.py +3 -4
- fabricatio/models/tool.py +4 -4
- fabricatio/models/usages.py +139 -146
- fabricatio/parser.py +59 -99
- fabricatio/rust.cp312-win_amd64.pyd +0 -0
- fabricatio/rust.pyi +40 -60
- fabricatio/utils.py +37 -170
- fabricatio-0.3.14.dev2.data/scripts/tdown.exe +0 -0
- {fabricatio-0.3.14.dev0.data → fabricatio-0.3.14.dev2.data}/scripts/ttm.exe +0 -0
- {fabricatio-0.3.14.dev0.dist-info → fabricatio-0.3.14.dev2.dist-info}/METADATA +7 -7
- fabricatio-0.3.14.dev2.dist-info/RECORD +64 -0
- fabricatio-0.3.14.dev0.data/scripts/tdown.exe +0 -0
- fabricatio-0.3.14.dev0.dist-info/RECORD +0 -63
- {fabricatio-0.3.14.dev0.dist-info → fabricatio-0.3.14.dev2.dist-info}/WHEEL +0 -0
- {fabricatio-0.3.14.dev0.dist-info → fabricatio-0.3.14.dev2.dist-info}/licenses/LICENSE +0 -0
@@ -5,17 +5,16 @@ from itertools import groupby
|
|
5
5
|
from pathlib import Path
|
6
6
|
from typing import ClassVar, Dict, List, Optional, Self, Unpack
|
7
7
|
|
8
|
-
from fabricatio.rust import BibManager, blake3_hash, split_into_chunks
|
9
|
-
from more_itertools.more import first
|
10
|
-
from more_itertools.recipes import flatten, unique
|
11
|
-
from pydantic import Field
|
12
|
-
|
13
8
|
from fabricatio.fs import safe_text_read
|
14
9
|
from fabricatio.journal import logger
|
15
10
|
from fabricatio.models.extra.rag import MilvusDataBase
|
16
11
|
from fabricatio.models.generic import AsPrompt
|
17
12
|
from fabricatio.models.kwargs_types import ChunkKwargs
|
13
|
+
from fabricatio.rust import BibManager, blake3_hash, split_into_chunks
|
18
14
|
from fabricatio.utils import ok, wrapp_in_block
|
15
|
+
from more_itertools.more import first
|
16
|
+
from more_itertools.recipes import flatten, unique
|
17
|
+
from pydantic import Field
|
19
18
|
|
20
19
|
|
21
20
|
class ArticleChunk(MilvusDataBase):
|
@@ -56,6 +55,7 @@ class ArticleChunk(MilvusDataBase):
|
|
56
55
|
|
57
56
|
@property
|
58
57
|
def reference_header(self) -> str:
|
58
|
+
"""Get the reference header."""
|
59
59
|
return f"[[{ok(self._cite_number, 'You need to update cite number first.')}]] reference `{self.article_title}` from {self.as_auther_seq()}"
|
60
60
|
|
61
61
|
@property
|
@@ -68,7 +68,7 @@ class ArticleChunk(MilvusDataBase):
|
|
68
68
|
|
69
69
|
@classmethod
|
70
70
|
def from_file[P: str | Path](
|
71
|
-
|
71
|
+
cls, path: P | List[P], bib_mgr: BibManager, **kwargs: Unpack[ChunkKwargs]
|
72
72
|
) -> List[Self]:
|
73
73
|
"""Load the article chunks from the file."""
|
74
74
|
if isinstance(path, list):
|
@@ -85,9 +85,9 @@ class ArticleChunk(MilvusDataBase):
|
|
85
85
|
title_seg = path.stem.split(" - ").pop()
|
86
86
|
|
87
87
|
key = (
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
bib_mgr.get_cite_key_by_title(title_seg)
|
89
|
+
or bib_mgr.get_cite_key_by_title_fuzzy(title_seg)
|
90
|
+
or bib_mgr.get_cite_key_fuzzy(path.stem)
|
91
91
|
)
|
92
92
|
if key is None:
|
93
93
|
logger.warning(f"no cite key found for {path.as_posix()}, skip.")
|
@@ -179,7 +179,7 @@ class CitationManager(AsPrompt):
|
|
179
179
|
"""Separator for abbreviated citation numbers."""
|
180
180
|
|
181
181
|
def update_chunks(
|
182
|
-
|
182
|
+
self, article_chunks: List[ArticleChunk], set_cite_number: bool = True, dedup: bool = True
|
183
183
|
) -> Self:
|
184
184
|
"""Update article chunks."""
|
185
185
|
self.article_chunks.clear()
|
@@ -218,8 +218,9 @@ class CitationManager(AsPrompt):
|
|
218
218
|
def _as_prompt_inner(self) -> Dict[str, str]:
|
219
219
|
"""Generate prompt inner representation."""
|
220
220
|
seg = []
|
221
|
-
for k,
|
222
|
-
g = list(
|
221
|
+
for k, g_iter in groupby(self.article_chunks, key=lambda a: a.bibtex_cite_key):
|
222
|
+
g = list(g_iter)
|
223
|
+
|
223
224
|
logger.debug(f"Group [{k}]: {len(g)}")
|
224
225
|
seg.append(wrapp_in_block("\n\n".join(a.chunk for a in g), first(g).reference_header))
|
225
226
|
return {"References": "\n".join(seg)}
|
@@ -277,5 +278,7 @@ class CitationManager(AsPrompt):
|
|
277
278
|
return "".join(a.as_typst_cite() for a in chunk_seq.values())
|
278
279
|
|
279
280
|
def as_milvus_filter_expr(self, blacklist: bool = True) -> str:
|
281
|
+
"""Asynchronously fetches documents from a Milvus database based on input vectors."""
|
280
282
|
if blacklist:
|
281
283
|
return " and ".join(f'bibtex_cite_key != "{a.bibtex_cite_key}"' for a in self.article_chunks)
|
284
|
+
return " or ".join(f'bibtex_cite_key == "{a.bibtex_cite_key}"' for a in self.article_chunks)
|
@@ -5,9 +5,7 @@ from enum import StrEnum
|
|
5
5
|
from pathlib import Path
|
6
6
|
from typing import ClassVar, Generator, List, Optional, Self, Tuple, Type
|
7
7
|
|
8
|
-
from fabricatio.
|
9
|
-
from pydantic import Field
|
10
|
-
|
8
|
+
from fabricatio.capabilities.persist import PersistentAble
|
11
9
|
from fabricatio.fs import dump_text, safe_text_read
|
12
10
|
from fabricatio.fs.readers import extract_sections
|
13
11
|
from fabricatio.journal import logger
|
@@ -18,13 +16,14 @@ from fabricatio.models.generic import (
|
|
18
16
|
Introspect,
|
19
17
|
Language,
|
20
18
|
ModelHash,
|
21
|
-
PersistentAble,
|
22
19
|
ProposedUpdateAble,
|
23
20
|
SketchedAble,
|
24
21
|
Titled,
|
25
22
|
WordCount,
|
26
23
|
)
|
24
|
+
from fabricatio.rust import extract_body, inplace_update, split_out_metadata, to_metadata, word_count
|
27
25
|
from fabricatio.utils import fallback_kwargs, ok
|
26
|
+
from pydantic import Field
|
28
27
|
|
29
28
|
ARTICLE_WRAPPER = "// =-=-=-=-=-=-=-=-=-="
|
30
29
|
|
@@ -276,7 +275,7 @@ class ArticleBase[T: ChapterBase](FinalizedDumpAble, AsPrompt, FromTypstCode, To
|
|
276
275
|
)
|
277
276
|
|
278
277
|
def iter_dfs_rev(
|
279
|
-
|
278
|
+
self,
|
280
279
|
) -> Generator[ArticleOutlineBase, None, None]:
|
281
280
|
"""Performs a depth-first search (DFS) through the article structure in reverse order.
|
282
281
|
|
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
from typing import List
|
4
4
|
|
5
|
+
from fabricatio.capabilities.persist import PersistentAble
|
5
6
|
from fabricatio.models.extra.rag import MilvusDataBase
|
6
|
-
from fabricatio.models.generic import
|
7
|
+
from fabricatio.models.generic import SketchedAble
|
7
8
|
from pydantic import BaseModel
|
8
9
|
|
9
10
|
|
@@ -2,15 +2,7 @@
|
|
2
2
|
|
3
3
|
from typing import ClassVar, Dict, Generator, List, Self, Tuple, Type, override
|
4
4
|
|
5
|
-
from fabricatio.
|
6
|
-
convert_all_block_tex,
|
7
|
-
convert_all_inline_tex,
|
8
|
-
fix_misplaced_labels,
|
9
|
-
split_out_metadata,
|
10
|
-
word_count,
|
11
|
-
)
|
12
|
-
from pydantic import Field, NonNegativeInt
|
13
|
-
|
5
|
+
from fabricatio.capabilities.persist import PersistentAble
|
14
6
|
from fabricatio.decorators import precheck_package
|
15
7
|
from fabricatio.journal import logger
|
16
8
|
from fabricatio.models.extra.article_base import (
|
@@ -25,7 +17,15 @@ from fabricatio.models.extra.article_outline import (
|
|
25
17
|
ArticleSectionOutline,
|
26
18
|
ArticleSubsectionOutline,
|
27
19
|
)
|
28
|
-
from fabricatio.models.generic import Described,
|
20
|
+
from fabricatio.models.generic import Described, SequencePatch, SketchedAble, WithRef, WordCount
|
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
|
+
)
|
28
|
+
from pydantic import Field, NonNegativeInt
|
29
29
|
|
30
30
|
PARAGRAPH_SEP = "\n\n// - - -\n\n"
|
31
31
|
|
@@ -82,8 +82,8 @@ class ArticleSubsection(SubSectionBase):
|
|
82
82
|
if len(self.paragraphs) == 0:
|
83
83
|
summary += f"`{self.__class__.__name__}` titled `{self.title}` have no paragraphs, You should add some!\n"
|
84
84
|
if (
|
85
|
-
|
86
|
-
|
85
|
+
abs((wc := self.word_count) - self.expected_word_count) / self.expected_word_count
|
86
|
+
> self._max_word_count_deviation
|
87
87
|
):
|
88
88
|
summary += f"`{self.__class__.__name__}` titled `{self.title}` have {wc} words, expected {self.expected_word_count} words!"
|
89
89
|
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
from typing import ClassVar, Dict, Type
|
4
4
|
|
5
|
+
from fabricatio.capabilities.persist import PersistentAble
|
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
|
13
|
+
from fabricatio.models.generic import WithRef
|
13
14
|
|
14
15
|
|
15
16
|
class ArticleSubsectionOutline(SubSectionBase):
|
fabricatio/models/extra/rag.py
CHANGED
@@ -5,7 +5,7 @@ from functools import partial
|
|
5
5
|
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Self, Sequence, Set
|
6
6
|
|
7
7
|
from fabricatio.decorators import precheck_package
|
8
|
-
from fabricatio.models.generic import Vectorizable
|
8
|
+
from fabricatio.models.generic import Base, Vectorizable
|
9
9
|
from fabricatio.utils import ok
|
10
10
|
from pydantic import JsonValue
|
11
11
|
|
@@ -18,7 +18,7 @@ if TYPE_CHECKING:
|
|
18
18
|
from pymilvus import CollectionSchema
|
19
19
|
|
20
20
|
|
21
|
-
class MilvusDataBase(Vectorizable, ABC):
|
21
|
+
class MilvusDataBase(Base, Vectorizable, ABC):
|
22
22
|
"""A base class for Milvus data."""
|
23
23
|
|
24
24
|
primary_field_name: ClassVar[str] = "id"
|
fabricatio/models/extra/rule.py
CHANGED
@@ -10,7 +10,8 @@ complex rule management systems.
|
|
10
10
|
|
11
11
|
from typing import List, Self, Tuple, Unpack
|
12
12
|
|
13
|
-
from fabricatio.
|
13
|
+
from fabricatio.capabilities.persist import PersistentAble
|
14
|
+
from fabricatio.models.generic import Language, SketchedAble, WithBriefing
|
14
15
|
from more_itertools import flatten
|
15
16
|
|
16
17
|
|
fabricatio/models/generic.py
CHANGED
@@ -1,17 +1,19 @@
|
|
1
1
|
"""This module defines generic classes for models in the Fabricatio library, providing a foundation for various model functionalities."""
|
2
2
|
|
3
3
|
from abc import ABC, abstractmethod
|
4
|
-
from datetime import datetime
|
5
4
|
from pathlib import Path
|
6
|
-
from typing import Any, Callable, Dict, Iterable, List, Mapping, Optional, Self, Type, Union, final, overload
|
5
|
+
from typing import Any, Callable, Dict, Iterable, List, Mapping, Optional, Self, Sequence, Type, Union, final, overload
|
7
6
|
|
8
7
|
import ujson
|
8
|
+
from fabricatio.fs import dump_text
|
9
|
+
from fabricatio.fs.readers import safe_text_read
|
10
|
+
from fabricatio.journal import logger
|
9
11
|
from fabricatio.rust import CONFIG, TEMPLATE_MANAGER, blake3_hash, detect_language
|
12
|
+
from fabricatio.utils import ok
|
10
13
|
from pydantic import (
|
11
14
|
BaseModel,
|
12
15
|
ConfigDict,
|
13
16
|
Field,
|
14
|
-
HttpUrl,
|
15
17
|
NonNegativeFloat,
|
16
18
|
PositiveFloat,
|
17
19
|
PositiveInt,
|
@@ -20,14 +22,8 @@ from pydantic import (
|
|
20
22
|
)
|
21
23
|
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
|
22
24
|
|
23
|
-
from fabricatio.fs import dump_text
|
24
|
-
from fabricatio.fs.readers import safe_text_read
|
25
|
-
from fabricatio.journal import logger
|
26
|
-
from fabricatio.parser import JsonCapture
|
27
|
-
from fabricatio.utils import ok
|
28
25
|
|
29
|
-
|
30
|
-
class Base(BaseModel):
|
26
|
+
class Base(BaseModel, ABC):
|
31
27
|
"""Base class for all models with Pydantic configuration.
|
32
28
|
|
33
29
|
This class sets up the basic Pydantic configuration for all models in the Fabricatio library.
|
@@ -38,7 +34,7 @@ class Base(BaseModel):
|
|
38
34
|
model_config = ConfigDict(use_attribute_docstrings=True)
|
39
35
|
|
40
36
|
|
41
|
-
class Display(Base):
|
37
|
+
class Display(Base, ABC):
|
42
38
|
"""Class that provides formatted JSON representation utilities.
|
43
39
|
|
44
40
|
Provides methods to generate both pretty-printed and compact JSON representations of the model.
|
@@ -73,13 +69,13 @@ class Display(Base):
|
|
73
69
|
str: Combined display output with boundary markers
|
74
70
|
"""
|
75
71
|
return (
|
76
|
-
|
77
|
-
|
78
|
-
|
72
|
+
"--- Start of Extra Info Sequence ---"
|
73
|
+
+ "\n".join(d.compact() if compact else d.display() for d in seq)
|
74
|
+
+ "--- End of Extra Info Sequence ---"
|
79
75
|
)
|
80
76
|
|
81
77
|
|
82
|
-
class Named(Base):
|
78
|
+
class Named(Base, ABC):
|
83
79
|
"""Class that includes a name attribute.
|
84
80
|
|
85
81
|
This class adds a name attribute to models, which is intended to be a unique identifier.
|
@@ -89,7 +85,7 @@ class Named(Base):
|
|
89
85
|
"""The name of this object,briefly and conclusively."""
|
90
86
|
|
91
87
|
|
92
|
-
class Described(Base):
|
88
|
+
class Described(Base, ABC):
|
93
89
|
"""Class that includes a description attribute.
|
94
90
|
|
95
91
|
This class adds a description attribute to models, providing additional context or information.
|
@@ -102,19 +98,24 @@ class Described(Base):
|
|
102
98
|
this object's intent and application."""
|
103
99
|
|
104
100
|
|
105
|
-
class Titled(Base):
|
101
|
+
class Titled(Base, ABC):
|
106
102
|
"""Class that includes a title attribute."""
|
107
103
|
|
108
104
|
title: str
|
109
105
|
"""The title of this object, make it professional and concise.No prefixed heading number should be included."""
|
110
106
|
|
111
107
|
|
112
|
-
class WordCount(Base):
|
108
|
+
class WordCount(Base, ABC):
|
113
109
|
"""Class that includes a word count attribute."""
|
114
110
|
|
115
111
|
expected_word_count: int
|
116
112
|
"""Expected word count of this research component."""
|
117
113
|
|
114
|
+
@property
|
115
|
+
def exact_word_count(self) -> int:
|
116
|
+
"""Get the exact word count of this research component."""
|
117
|
+
raise NotImplementedError(f"`expected_word_count` is not implemented for {self.__class__.__name__}")
|
118
|
+
|
118
119
|
|
119
120
|
class FromMapping:
|
120
121
|
"""Class that provides a method to generate a list of objects from a mapping."""
|
@@ -125,6 +126,15 @@ class FromMapping:
|
|
125
126
|
"""Generate a list of objects from a mapping."""
|
126
127
|
|
127
128
|
|
129
|
+
class FromSequence:
|
130
|
+
"""Class that provides a method to generate a list of objects from a sequence."""
|
131
|
+
|
132
|
+
@classmethod
|
133
|
+
@abstractmethod
|
134
|
+
def from_sequence[S](cls: S, sequence: Sequence[Any], **kwargs: Any) -> List[S]:
|
135
|
+
"""Generate a list of objects from a sequence."""
|
136
|
+
|
137
|
+
|
128
138
|
class AsPrompt:
|
129
139
|
"""Class that provides a method to generate a prompt from the model.
|
130
140
|
|
@@ -154,7 +164,7 @@ class AsPrompt:
|
|
154
164
|
"""
|
155
165
|
|
156
166
|
|
157
|
-
class WithRef[T](Base):
|
167
|
+
class WithRef[T](Base, ABC):
|
158
168
|
"""Class that provides a reference to another object.
|
159
169
|
|
160
170
|
This class manages a reference to another object, allowing for easy access and updates.
|
@@ -177,16 +187,13 @@ class WithRef[T](Base):
|
|
177
187
|
)
|
178
188
|
|
179
189
|
@overload
|
180
|
-
def update_ref[S: WithRef](self: S, reference: T) -> S:
|
181
|
-
...
|
190
|
+
def update_ref[S: WithRef](self: S, reference: T) -> S: ...
|
182
191
|
|
183
192
|
@overload
|
184
|
-
def update_ref[S: WithRef](self: S, reference: "WithRef[T]") -> S:
|
185
|
-
...
|
193
|
+
def update_ref[S: WithRef](self: S, reference: "WithRef[T]") -> S: ...
|
186
194
|
|
187
195
|
@overload
|
188
|
-
def update_ref[S: WithRef](self: S, reference: None = None) -> S:
|
189
|
-
...
|
196
|
+
def update_ref[S: WithRef](self: S, reference: None = None) -> S: ...
|
190
197
|
|
191
198
|
def update_ref[S: WithRef](self: S, reference: Union[T, "WithRef[T]", None] = None) -> S:
|
192
199
|
"""Update the reference of the object.
|
@@ -204,97 +211,6 @@ class WithRef[T](Base):
|
|
204
211
|
return self
|
205
212
|
|
206
213
|
|
207
|
-
class PersistentAble(Base):
|
208
|
-
"""Class providing file persistence capabilities.
|
209
|
-
|
210
|
-
Enables saving model instances to disk with timestamped filenames and loading from persisted files.
|
211
|
-
Implements basic versioning through filename hashing and timestamping.
|
212
|
-
"""
|
213
|
-
|
214
|
-
def persist(self, path: str | Path) -> Self:
|
215
|
-
"""Save model instance to disk with versioned filename.
|
216
|
-
|
217
|
-
Args:
|
218
|
-
path (str | Path): Target directory or file path. If directory, filename is auto-generated.
|
219
|
-
|
220
|
-
Returns:
|
221
|
-
Self: Current instance for method chaining
|
222
|
-
|
223
|
-
Notes:
|
224
|
-
- Filename format: <ClassName>_<YYYYMMDD_HHMMSS>_<6-char_hash>.json
|
225
|
-
- Hash generated from JSON content ensures uniqueness
|
226
|
-
"""
|
227
|
-
p = Path(path)
|
228
|
-
out = self.model_dump_json(indent=1, by_alias=True)
|
229
|
-
|
230
|
-
# Generate a timestamp in the format YYYYMMDD_HHMMSS
|
231
|
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
232
|
-
|
233
|
-
# Generate the hash
|
234
|
-
file_hash = blake3_hash(out.encode())[:6]
|
235
|
-
|
236
|
-
# Construct the file name with timestamp and hash
|
237
|
-
file_name = f"{self.__class__.__name__}_{timestamp}_{file_hash}.json"
|
238
|
-
|
239
|
-
if p.is_dir():
|
240
|
-
p.joinpath(file_name).write_text(out, encoding="utf-8")
|
241
|
-
else:
|
242
|
-
p.mkdir(exist_ok=True, parents=True)
|
243
|
-
p.write_text(out, encoding="utf-8")
|
244
|
-
|
245
|
-
logger.info(f"Persisted `{self.__class__.__name__}` to {p.as_posix()}")
|
246
|
-
return self
|
247
|
-
|
248
|
-
@classmethod
|
249
|
-
def from_latest_persistent(cls, dir_path: str | Path) -> Optional[Self]:
|
250
|
-
"""Load most recent persisted instance from directory.
|
251
|
-
|
252
|
-
Args:
|
253
|
-
dir_path (str | Path): Directory containing persisted files
|
254
|
-
|
255
|
-
Returns:
|
256
|
-
Self: Most recently modified instance
|
257
|
-
|
258
|
-
Raises:
|
259
|
-
NotADirectoryError: If path is not a valid directory
|
260
|
-
FileNotFoundError: If no matching files found
|
261
|
-
"""
|
262
|
-
dir_path = Path(dir_path)
|
263
|
-
if not dir_path.is_dir():
|
264
|
-
return None
|
265
|
-
|
266
|
-
pattern = f"{cls.__name__}_*.json"
|
267
|
-
files = list(dir_path.glob(pattern))
|
268
|
-
|
269
|
-
if not files:
|
270
|
-
return None
|
271
|
-
|
272
|
-
def _get_timestamp(file_path: Path) -> datetime:
|
273
|
-
stem = file_path.stem
|
274
|
-
parts = stem.split("_")
|
275
|
-
return datetime.strptime(f"{parts[1]}_{parts[2]}", "%Y%m%d_%H%M%S")
|
276
|
-
|
277
|
-
files.sort(key=lambda f: _get_timestamp(f), reverse=True)
|
278
|
-
|
279
|
-
return cls.from_persistent(files.pop(0))
|
280
|
-
|
281
|
-
@classmethod
|
282
|
-
def from_persistent(cls, path: str | Path) -> Self:
|
283
|
-
"""Load an instance from a specific persisted file.
|
284
|
-
|
285
|
-
Args:
|
286
|
-
path (str | Path): Path to the JSON file.
|
287
|
-
|
288
|
-
Returns:
|
289
|
-
Self: The loaded instance from the file.
|
290
|
-
|
291
|
-
Raises:
|
292
|
-
FileNotFoundError: If the specified file does not exist.
|
293
|
-
ValueError: If the file content is invalid for the model.
|
294
|
-
"""
|
295
|
-
return cls.model_validate_json(safe_text_read(path))
|
296
|
-
|
297
|
-
|
298
214
|
class Language:
|
299
215
|
"""Class that provides a language attribute."""
|
300
216
|
|
@@ -310,7 +226,7 @@ class Language:
|
|
310
226
|
raise RuntimeError(f"Cannot determine language! class that not support language: {self.__class__.__name__}")
|
311
227
|
|
312
228
|
|
313
|
-
class ModelHash(Base):
|
229
|
+
class ModelHash(Base, ABC):
|
314
230
|
"""Class that provides a hash value for the object.
|
315
231
|
|
316
232
|
This class includes a method to calculate a hash value for the object based on its JSON representation.
|
@@ -325,7 +241,7 @@ class ModelHash(Base):
|
|
325
241
|
return hash(self.model_dump_json())
|
326
242
|
|
327
243
|
|
328
|
-
class UpdateFrom:
|
244
|
+
class UpdateFrom(ABC):
|
329
245
|
"""Class that provides a method to update the object from another object.
|
330
246
|
|
331
247
|
This class includes methods to update the current object with the attributes of another object.
|
@@ -374,7 +290,7 @@ class UpdateFrom:
|
|
374
290
|
return self.update_pre_check(other).update_from_inner(other)
|
375
291
|
|
376
292
|
|
377
|
-
class Introspect:
|
293
|
+
class Introspect(ABC):
|
378
294
|
"""Class that provides a method to introspect the object.
|
379
295
|
|
380
296
|
This class includes a method to perform internal introspection of the object.
|
@@ -389,7 +305,7 @@ class Introspect:
|
|
389
305
|
"""
|
390
306
|
|
391
307
|
|
392
|
-
class WithBriefing(Named, Described):
|
308
|
+
class WithBriefing(Named, Described, ABC):
|
393
309
|
"""Class that provides a briefing based on the name and description.
|
394
310
|
|
395
311
|
This class combines the name and description attributes to provide a brief summary of the object.
|
@@ -424,7 +340,7 @@ class UnsortGenerate(GenerateJsonSchema):
|
|
424
340
|
return value
|
425
341
|
|
426
342
|
|
427
|
-
class WithFormatedJsonSchema(Base):
|
343
|
+
class WithFormatedJsonSchema(Base, ABC):
|
428
344
|
"""Class that provides a formatted JSON schema of the model.
|
429
345
|
|
430
346
|
This class includes a method to generate a formatted JSON schema of the model.
|
@@ -442,7 +358,7 @@ class WithFormatedJsonSchema(Base):
|
|
442
358
|
)
|
443
359
|
|
444
360
|
|
445
|
-
class CreateJsonObjPrompt(WithFormatedJsonSchema):
|
361
|
+
class CreateJsonObjPrompt(WithFormatedJsonSchema, ABC):
|
446
362
|
"""Class that provides a prompt for creating a JSON object.
|
447
363
|
|
448
364
|
This class includes a method to create a prompt for creating a JSON object based on the model's schema and a requirement.
|
@@ -480,7 +396,7 @@ class CreateJsonObjPrompt(WithFormatedJsonSchema):
|
|
480
396
|
]
|
481
397
|
|
482
398
|
|
483
|
-
class InstantiateFromString(Base):
|
399
|
+
class InstantiateFromString(Base, ABC):
|
484
400
|
"""Class that provides a method to instantiate the class from a string.
|
485
401
|
|
486
402
|
This class includes a method to instantiate the class from a JSON string representation.
|
@@ -496,19 +412,21 @@ class InstantiateFromString(Base):
|
|
496
412
|
Returns:
|
497
413
|
Self | None: The instance of the class or None if the string is not valid.
|
498
414
|
"""
|
415
|
+
from fabricatio.parser import JsonCapture
|
416
|
+
|
499
417
|
obj = JsonCapture.convert_with(string, cls.model_validate_json)
|
500
418
|
logger.debug(f"Instantiate `{cls.__name__}` from string, {'Failed' if obj is None else 'Success'}.")
|
501
419
|
return obj
|
502
420
|
|
503
421
|
|
504
|
-
class ProposedAble(CreateJsonObjPrompt, InstantiateFromString):
|
422
|
+
class ProposedAble(CreateJsonObjPrompt, InstantiateFromString, ABC):
|
505
423
|
"""Class that provides a method to propose a JSON object based on the requirement.
|
506
424
|
|
507
425
|
This class combines the functionality to create a prompt for a JSON object and instantiate it from a string.
|
508
426
|
"""
|
509
427
|
|
510
428
|
|
511
|
-
class SketchedAble(ProposedAble, Display):
|
429
|
+
class SketchedAble(ProposedAble, Display, ABC):
|
512
430
|
"""Class that provides a method to scratch the object.
|
513
431
|
|
514
432
|
This class combines the functionality to propose a JSON object, instantiate it from a string, and display it.
|
@@ -522,7 +440,7 @@ class ProposedUpdateAble(SketchedAble, UpdateFrom, ABC):
|
|
522
440
|
"""
|
523
441
|
|
524
442
|
|
525
|
-
class FinalizedDumpAble(Base):
|
443
|
+
class FinalizedDumpAble(Base, ABC):
|
526
444
|
"""Class that provides a method to finalize the dump of the object.
|
527
445
|
|
528
446
|
This class includes methods to finalize the JSON representation of the object and dump it to a file.
|
@@ -549,7 +467,7 @@ class FinalizedDumpAble(Base):
|
|
549
467
|
return self
|
550
468
|
|
551
469
|
|
552
|
-
class WithDependency(Base):
|
470
|
+
class WithDependency(Base, ABC):
|
553
471
|
"""Class that manages file dependencies.
|
554
472
|
|
555
473
|
This class includes methods to manage file dependencies required for reading or writing.
|
@@ -642,7 +560,7 @@ class WithDependency(Base):
|
|
642
560
|
)
|
643
561
|
|
644
562
|
|
645
|
-
class Vectorizable:
|
563
|
+
class Vectorizable(ABC):
|
646
564
|
"""Class that prepares the vectorization of the model.
|
647
565
|
|
648
566
|
This class includes methods to prepare the model for vectorization, ensuring it fits within a specified token length.
|
@@ -675,14 +593,14 @@ class Vectorizable:
|
|
675
593
|
return chunk
|
676
594
|
|
677
595
|
|
678
|
-
class ScopedConfig(Base):
|
596
|
+
class ScopedConfig(Base, ABC):
|
679
597
|
"""Configuration holder with hierarchical fallback mechanism.
|
680
598
|
|
681
599
|
Manages LLM, embedding, and vector database configurations with fallback logic.
|
682
600
|
Allows configuration values to be overridden in a hierarchical manner.
|
683
601
|
"""
|
684
602
|
|
685
|
-
llm_api_endpoint: Optional[
|
603
|
+
llm_api_endpoint: Optional[str] = None
|
686
604
|
"""The OpenAI API endpoint."""
|
687
605
|
|
688
606
|
llm_api_key: Optional[SecretStr] = None
|
@@ -727,7 +645,7 @@ class ScopedConfig(Base):
|
|
727
645
|
llm_frequency_penalty: Optional[PositiveFloat] = None
|
728
646
|
"""The frequency penalty of the LLM model."""
|
729
647
|
|
730
|
-
embedding_api_endpoint: Optional[
|
648
|
+
embedding_api_endpoint: Optional[str] = None
|
731
649
|
"""The OpenAI API endpoint."""
|
732
650
|
|
733
651
|
embedding_api_key: Optional[SecretStr] = None
|
@@ -748,7 +666,7 @@ class ScopedConfig(Base):
|
|
748
666
|
embedding_caching: Optional[bool] = False
|
749
667
|
"""Whether to cache the embedding result."""
|
750
668
|
|
751
|
-
milvus_uri: Optional[
|
669
|
+
milvus_uri: Optional[str] = Field(default=None)
|
752
670
|
"""The URI of the Milvus server."""
|
753
671
|
|
754
672
|
milvus_token: Optional[SecretStr] = Field(default=None)
|
@@ -808,7 +726,7 @@ class ScopedConfig(Base):
|
|
808
726
|
return self
|
809
727
|
|
810
728
|
|
811
|
-
class Patch[T](ProposedAble):
|
729
|
+
class Patch[T](ProposedAble, ABC):
|
812
730
|
"""Base class for patches.
|
813
731
|
|
814
732
|
This class provides a base implementation for patches that can be applied to other objects.
|
@@ -855,15 +773,14 @@ class Patch[T](ProposedAble):
|
|
855
773
|
# copy the desc info of each corresponding fields from `ref_cls`
|
856
774
|
for field_name in [f for f in cls.model_fields if f in ref_cls.model_fields]:
|
857
775
|
my_schema["properties"][field_name]["description"] = (
|
858
|
-
|
859
|
-
"description"]
|
776
|
+
ref_cls.model_fields[field_name].description or my_schema["properties"][field_name]["description"]
|
860
777
|
)
|
861
778
|
my_schema["description"] = ref_cls.__doc__
|
862
779
|
|
863
780
|
return ujson.dumps(my_schema, indent=2, ensure_ascii=False, sort_keys=False)
|
864
781
|
|
865
782
|
|
866
|
-
class SequencePatch[T](ProposedUpdateAble):
|
783
|
+
class SequencePatch[T](ProposedUpdateAble, ABC):
|
867
784
|
"""Base class for patches.
|
868
785
|
|
869
786
|
This class provides a base implementation for patches that can be applied to sequences of objects.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""This module contains the types for the keyword arguments of the methods in the models module."""
|
2
2
|
|
3
|
-
from typing import Dict, List,
|
3
|
+
from typing import Dict, List, NotRequired, Optional, Required, TypedDict
|
4
4
|
|
5
5
|
|
6
6
|
class ChunkKwargs(TypedDict):
|
@@ -119,11 +119,3 @@ class ChooseKwargs[T](ValidateKwargs[T], total=False):
|
|
119
119
|
"""
|
120
120
|
|
121
121
|
k: int
|
122
|
-
|
123
|
-
|
124
|
-
class RerankOptions(TypedDict, total=False):
|
125
|
-
"""Optional keyword arguments for the rerank method."""
|
126
|
-
|
127
|
-
raw_scores: bool
|
128
|
-
truncate: bool
|
129
|
-
truncation_direction: Literal["Left", "Right"]
|