fabricatio 0.2.1.dev0__cp312-cp312-manylinux_2_34_x86_64.whl → 0.2.1.dev1__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/fs/readers.py CHANGED
@@ -1,24 +1,43 @@
1
1
  """Filesystem readers for Fabricatio."""
2
2
 
3
3
  from pathlib import Path
4
+ from typing import Dict
4
5
 
5
6
  from magika import Magika
7
+ from orjson import orjson
6
8
 
7
9
  from fabricatio.config import configs
8
10
 
9
11
  magika = Magika(model_dir=configs.magika.model_dir)
10
12
 
11
13
 
12
- def safe_text_read(path: Path) -> str:
14
+ def safe_text_read(path: Path | str) -> str:
13
15
  """Safely read the text from a file.
14
16
 
15
17
  Args:
16
- path (Path): The path to the file.
18
+ path (Path|str): The path to the file.
17
19
 
18
20
  Returns:
19
21
  str: The text from the file.
20
22
  """
23
+ path = Path(path)
21
24
  try:
22
25
  return path.read_text(encoding="utf-8")
23
26
  except (UnicodeDecodeError, IsADirectoryError, FileNotFoundError):
24
27
  return ""
28
+
29
+
30
+ def safe_json_read(path: Path | str) -> Dict:
31
+ """Safely read the JSON from a file.
32
+
33
+ Args:
34
+ path (Path|str): The path to the file.
35
+
36
+ Returns:
37
+ dict: The JSON from the file.
38
+ """
39
+ path = Path(path)
40
+ try:
41
+ return orjson.loads(path.read_text(encoding="utf-8"))
42
+ except (orjson.JSONDecodeError, IsADirectoryError, FileNotFoundError):
43
+ return {}
@@ -1,7 +1,7 @@
1
1
  """This module defines generic classes for models in the Fabricatio library."""
2
2
 
3
3
  from pathlib import Path
4
- from typing import List, Self
4
+ from typing import Callable, List, Self
5
5
 
6
6
  import orjson
7
7
  from fabricatio._rust import blake3_hash
@@ -108,17 +108,25 @@ class WithDependency(Base):
108
108
  self.dependencies.clear()
109
109
  return self
110
110
 
111
- def override_dependencies[P: str | Path](self, dependencies: List[P]) -> Self:
111
+ def override_dependencies[P: str | Path](self, dependencies: List[P] | P) -> Self:
112
112
  """Override the file dependencies of the task.
113
113
 
114
114
  Args:
115
- dependencies (List[str | Path]): The file dependencies to override the task's dependencies.
115
+ dependencies (List[str | Path] | str | Path): The file dependencies to override the task's dependencies.
116
116
 
117
117
  Returns:
118
118
  Self: The current instance of the task.
119
119
  """
120
120
  return self.clear_dependencies().add_dependency(dependencies)
121
121
 
122
+ def pop_dependence[T](self, idx: int = -1, reader: Callable[[str], T] = safe_text_read) -> T:
123
+ """Pop the file dependencies from the task.
124
+
125
+ Returns:
126
+ str: The popped file dependency
127
+ """
128
+ return reader(self.dependencies.pop(idx))
129
+
122
130
  @property
123
131
  def dependencies_prompt(self) -> str:
124
132
  """Generate a prompt for the task based on the file dependencies.
@@ -1,6 +1,7 @@
1
1
  """This module contains classes that manage the usage of language models and tools in tasks."""
2
2
 
3
- from typing import Callable, Dict, Iterable, List, Optional, Self, Set, Union, Unpack
3
+ from asyncio import gather
4
+ from typing import Callable, Dict, Iterable, List, Optional, Self, Set, Union, Unpack, overload
4
5
 
5
6
  import asyncstdlib
6
7
  import litellm
@@ -130,12 +131,42 @@ class LLMUsage(Base):
130
131
  logger.critical(err := f"Unexpected response type: {type(resp)}")
131
132
  raise ValueError(err)
132
133
 
134
+ @overload
135
+ async def aask(
136
+ self,
137
+ question: List[str],
138
+ system_message: Optional[List[str]] = None,
139
+ **kwargs: Unpack[LLMKwargs],
140
+ ) -> List[str]: ...
141
+ @overload
133
142
  async def aask(
134
143
  self,
135
144
  question: str,
136
- system_message: str = "",
145
+ system_message: Optional[List[str]] = None,
146
+ **kwargs: Unpack[LLMKwargs],
147
+ ) -> List[str]: ...
148
+ @overload
149
+ async def aask(
150
+ self,
151
+ question: List[str],
152
+ system_message: Optional[str] = None,
137
153
  **kwargs: Unpack[LLMKwargs],
138
- ) -> str:
154
+ ) -> List[str]: ...
155
+
156
+ @overload
157
+ async def aask(
158
+ self,
159
+ question: str,
160
+ system_message: Optional[str] = None,
161
+ **kwargs: Unpack[LLMKwargs],
162
+ ) -> str: ...
163
+
164
+ async def aask(
165
+ self,
166
+ question: str | List[str],
167
+ system_message: Optional[str | List[str]] = None,
168
+ **kwargs: Unpack[LLMKwargs],
169
+ ) -> str | List[str]:
139
170
  """Asynchronously asks the language model a question and returns the response content.
140
171
 
141
172
  Args:
@@ -146,16 +177,39 @@ class LLMUsage(Base):
146
177
  Returns:
147
178
  str: The content of the model's response message.
148
179
  """
149
- return (
150
- (
151
- await self.ainvoke(
152
- n=1,
153
- question=question,
154
- system_message=system_message,
155
- **kwargs,
180
+ system_message = system_message or ""
181
+ match (isinstance(question, list), isinstance(system_message, list)):
182
+ case (True, True):
183
+ res = await gather(
184
+ *[
185
+ self.ainvoke(n=1, question=q, system_message=sm, **kwargs)
186
+ for q, sm in zip(question, system_message, strict=True)
187
+ ]
188
+ )
189
+ return [r.pop().message.content for r in res]
190
+ case (True, False):
191
+ res = await gather(
192
+ *[self.ainvoke(n=1, question=q, system_message=system_message, **kwargs) for q in question]
156
193
  )
157
- ).pop()
158
- ).message.content
194
+ return [r.pop().message.content for r in res]
195
+ case (False, True):
196
+ res = await gather(
197
+ *[self.ainvoke(n=1, question=question, system_message=sm, **kwargs) for sm in system_message]
198
+ )
199
+ return [r.pop().message.content for r in res]
200
+ case (False, False):
201
+ return (
202
+ (
203
+ await self.ainvoke(
204
+ n=1,
205
+ question=question,
206
+ system_message=system_message,
207
+ **kwargs,
208
+ )
209
+ ).pop()
210
+ ).message.content
211
+ case _:
212
+ raise RuntimeError("Should not reach here.")
159
213
 
160
214
  async def aask_validate[T](
161
215
  self,
@@ -278,13 +332,15 @@ class LLMUsage(Base):
278
332
  Raises:
279
333
  ValueError: If validation fails after maximum attempts or if no valid selection is made.
280
334
  """
281
- return await self.achoose(
282
- instruction=instruction,
283
- choices=choices,
284
- k=1,
285
- max_validations=max_validations,
286
- system_message=system_message,
287
- **kwargs,
335
+ return (
336
+ await self.achoose(
337
+ instruction=instruction,
338
+ choices=choices,
339
+ k=1,
340
+ max_validations=max_validations,
341
+ system_message=system_message,
342
+ **kwargs,
343
+ )
288
344
  )[0]
289
345
 
290
346
  async def ajudge(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fabricatio
3
- Version: 0.2.1.dev0
3
+ Version: 0.2.1.dev1
4
4
  Classifier: License :: OSI Approved :: MIT License
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Programming Language :: Python :: 3.12
@@ -1,8 +1,8 @@
1
- fabricatio-0.2.1.dev0.dist-info/METADATA,sha256=WJK231huI6SxJLItH3s3Yp2_T0ATZWcePe1wvlfijD8,11972
2
- fabricatio-0.2.1.dev0.dist-info/WHEEL,sha256=RIvmwLDYujv60MYBx2jxyP4vdn1DD7X0kBgz1TQvZuc,108
3
- fabricatio-0.2.1.dev0.dist-info/licenses/LICENSE,sha256=yDZaTLnOi03bi3Dk6f5IjhLUc5old2yOsihHWU0z-i0,1067
1
+ fabricatio-0.2.1.dev1.dist-info/METADATA,sha256=_O09SWlzb0GQzgGetoZTN2L3YxDOlOQf9F4nhRA3i7M,11972
2
+ fabricatio-0.2.1.dev1.dist-info/WHEEL,sha256=RIvmwLDYujv60MYBx2jxyP4vdn1DD7X0kBgz1TQvZuc,108
3
+ fabricatio-0.2.1.dev1.dist-info/licenses/LICENSE,sha256=yDZaTLnOi03bi3Dk6f5IjhLUc5old2yOsihHWU0z-i0,1067
4
4
  fabricatio/fs/__init__.py,sha256=bYE9r8uR0dtknzbg_YaGv_6Wwa27ntkQt0Tl7Kb3HFI,117
5
- fabricatio/fs/readers.py,sha256=Q-rqgXAsNcVu6WGFTvK70gZFyi088C3cuBdZOhn1oRI,527
5
+ fabricatio/fs/readers.py,sha256=lqMrM5Nt3l6QJmPDoBno2PSaN2BbFwfUjBqaghrbK3A,1002
6
6
  fabricatio/fs/curd.py,sha256=g74Pc2WPSkPlCXX0cWj2jyiDYTfkLwpwM480eyCkmc8,3851
7
7
  fabricatio/toolboxes/__init__.py,sha256=iEBsH0aRPLccIe6eyZ6iH5GdK8yYNQTnCwLzw4pWXAA,465
8
8
  fabricatio/toolboxes/fs.py,sha256=oZjGOmtN8ZbkMXxCMatqvdPavVXyQ87AEDc1lx9jimY,483
@@ -13,7 +13,7 @@ fabricatio/core.py,sha256=apwXgI94DCWpGujGlsmXsTZQvJOQMB9llmuUo7ohd-4,5771
13
13
  fabricatio/config.py,sha256=qpqqB38LivPEqkEdnTRswKSR4pVi-lsJb-Ohz3hqjcE,9997
14
14
  fabricatio/decorators.py,sha256=cJHsxxbnMhc4SzPl4454CPLuDP3H0qbTrzV_U2rLPrs,6372
15
15
  fabricatio/journal.py,sha256=bzxZay48ZWI0VIkkDXm4Wc_Cc9lBQYa2VGx3Hxy_PtA,753
16
- fabricatio/models/generic.py,sha256=U5MfGx4z_SMab3ZgOYUkvC3U4r3iUex6CfxQl-uRPnY,4679
16
+ fabricatio/models/generic.py,sha256=4j6DRNkHLNhx8U6aijf0Cz9HSahtEPQBh22i6d_PGTs,4981
17
17
  fabricatio/models/kwargs_types.py,sha256=INQIZiCOfGVkRbPmUMp-ZYtSkPwZmgFowid-D3A3TpE,875
18
18
  fabricatio/models/action.py,sha256=Kjq0ozqgPjHK0tP61k0yhrSZBxzE9m68MJCWZUUgh1c,5513
19
19
  fabricatio/models/task.py,sha256=0HXaf8Ig47UeMBN7n3iUp0oyOvN6-DlKxQForHnt8RI,8981
@@ -21,7 +21,7 @@ fabricatio/models/role.py,sha256=TT_SxC1M8IawVA72BRI3h3KrK3XMb88UV7F897PuKyU,175
21
21
  fabricatio/models/tool.py,sha256=wgNXtobDSWZTVmIrSSaS5oyxhSUYWXhS0S2o9acE2ls,6800
22
22
  fabricatio/models/events.py,sha256=TkMSJYSzRgurbfY2knWIc8gYw7rmaWqB7KeYzy5jeWU,2637
23
23
  fabricatio/models/utils.py,sha256=u-eR_v-o0BSFSmCprP3mOyAO-WOyg_P68jjzLjyit_w,2457
24
- fabricatio/models/usages.py,sha256=XYTJjxDVFE2Ek6QBcrk1iNz5OjUPtwIArWuG4LkeLw8,23121
24
+ fabricatio/models/usages.py,sha256=xpUF6luEiQUyAvjc6xBdXwA5gZHGckPsT-g-49McwIg,25185
25
25
  fabricatio/models/advanced.py,sha256=RzQE4fsN1uuEgG43Ym_C2N5gZI-9keGYzkaXE4hYGws,4899
26
26
  fabricatio/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
27
27
  fabricatio/_rust_instances.py,sha256=JAtO-vL8ihvduf1SHLNf0w7ZSVGCJeIv6zZ9Ekyy1hY,271
@@ -30,6 +30,6 @@ fabricatio/actions/communication.py,sha256=MrL_revv1zm1VXqjDMKZkg4PHmyMxYThAmmw2
30
30
  fabricatio/actions/transmission.py,sha256=WM-kssiqzTLZuZSiqbRvBaC2qynd9kjrKXya4f9Zcb0,839
31
31
  fabricatio/_rust.pyi,sha256=clhcURuiB9zlFo4m3VyoWQ8Xs4tvg6KNHXpF-ok9h4o,1703
32
32
  fabricatio/parser.py,sha256=Q4laV79Svggl09UKa3lAw6NYPuDz1sP2F-6U4tsSvQw,3429
33
- fabricatio/_rust.cpython-312-x86_64-linux-gnu.so,sha256=eA0p5X5kUSF3rjbfCaiAgNJh4W_B0AQ00N16ts7z-7A,1337976
34
- fabricatio-0.2.1.dev0.data/scripts/tdown,sha256=HjD-SO1toTf3nXRCB7FpknfvS4KmohZfRyRwI7xIDOs,4571816
35
- fabricatio-0.2.1.dev0.dist-info/RECORD,,
33
+ fabricatio/_rust.cpython-312-x86_64-linux-gnu.so,sha256=QbhnJVpJF98TxjmOERX1yujuxipSkTEdS-U-uLVeF6E,1339504
34
+ fabricatio-0.2.1.dev1.data/scripts/tdown,sha256=zC9af9TkkwUb7kbwHqOdjVevGf9XY36tW5j5kGEP6gY,4570976
35
+ fabricatio-0.2.1.dev1.dist-info/RECORD,,