fabricatio 0.2.1.dev0__cp312-cp312-win_amd64.whl → 0.2.1.dev1__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.
Binary file
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,6 +1,6 @@
1
- fabricatio-0.2.1.dev0.dist-info/METADATA,sha256=x6D37kGKzWuvkI5qEifdCCJLFhrHPoBBsmxatVxXB1w,12351
2
- fabricatio-0.2.1.dev0.dist-info/WHEEL,sha256=tpW5AN9B-9qsM9WW2FXG2r193YXiqexDadpKp0A2daI,96
3
- fabricatio-0.2.1.dev0.dist-info/licenses/LICENSE,sha256=do7J7EiCGbq0QPbMAL_FqLYufXpHnCnXBOuqVPwSV8Y,1088
1
+ fabricatio-0.2.1.dev1.dist-info/METADATA,sha256=RPmruN8dtRCXoGz9b8Yy59VVAnGecm_eEDPMZnF_XIU,12351
2
+ fabricatio-0.2.1.dev1.dist-info/WHEEL,sha256=tpW5AN9B-9qsM9WW2FXG2r193YXiqexDadpKp0A2daI,96
3
+ fabricatio-0.2.1.dev1.dist-info/licenses/LICENSE,sha256=do7J7EiCGbq0QPbMAL_FqLYufXpHnCnXBOuqVPwSV8Y,1088
4
4
  fabricatio/actions/communication.py,sha256=NZxIIncKgJSDyBrqNebUtH_haqtxHa8ld2TZxT3CMdU,429
5
5
  fabricatio/actions/transmission.py,sha256=xpvKqbXqgpi1BWy-vUUvmd8NZ1GhRNfsYUBp-l2jLyk,862
6
6
  fabricatio/actions/__init__.py,sha256=eFmFVPQvtNgFynIXBVr3eP-vWQDWCPng60YY5LXvZgg,115
@@ -8,18 +8,18 @@ fabricatio/config.py,sha256=TVge_fujKzvEZhOZYiYOMXdVn0YZ9SL5L1_mDzyeFUg,10260
8
8
  fabricatio/core.py,sha256=yQK2ZrbPYDJOaNDp0Bky3muTkB-ZaQ1ld_Qfflm2dY0,5938
9
9
  fabricatio/decorators.py,sha256=uzsP4tFKQNjDHBkofsjjoJA0IUAaYOtt6YVedoyOqlo,6551
10
10
  fabricatio/fs/curd.py,sha256=faMstgGUiQ4k2AW3OXfvvWWTldTtKXco7QINYaMjmyA,3981
11
- fabricatio/fs/readers.py,sha256=eDL9QhKEd6xfJBZaiwraObpGJWWpUd9NVYHoIvWgVqY,551
11
+ fabricatio/fs/readers.py,sha256=Pz1-cdZYtmqr032dsroImlkFXAd0kCYY_9qVpD4UrG4,1045
12
12
  fabricatio/fs/__init__.py,sha256=lWcKYg0v3mv2LnnSegOQaTtlVDODU0vtw_s6iKU5IqQ,122
13
13
  fabricatio/journal.py,sha256=siqimKF0M_QaaOCMxtjr_BJVNyUIAQWILzE9Q4T6-7c,781
14
14
  fabricatio/models/action.py,sha256=1V3upL79e3ekyGXA4bSY70N60yeL0aWak7ZPBVsFE3A,5652
15
15
  fabricatio/models/advanced.py,sha256=xAt0rgxzpw-gw-1LwRhvIbYKoNI8kzzcYPvcL7Q1z1U,5027
16
16
  fabricatio/models/events.py,sha256=mrihNEFgQ5o7qFWja1z_qX8dnaTLwPBoJdVlzxQV5oM,2719
17
- fabricatio/models/generic.py,sha256=HRslLNvP6v6wWag0tGda1FtAbjbjn096583u_IrixWs,4823
17
+ fabricatio/models/generic.py,sha256=WEjZ96rTyBjaBjkM6e8E4Pg_Naot4xWRvGJteqBiCCI,5133
18
18
  fabricatio/models/kwargs_types.py,sha256=lSZAxOnhFdQwRkm-NrbJVMSyBbfdeuVNx807LvJpEOo,901
19
19
  fabricatio/models/role.py,sha256=RPdOcjmM6KNyH310Vx4524JQG94P5sXl_3doSrRpiKQ,1802
20
20
  fabricatio/models/task.py,sha256=hogTBx74IAKfKDWghhWFR7i-LVQAn7L9PQM5tyOA5Rk,9257
21
21
  fabricatio/models/tool.py,sha256=WTFnpF6xZ1nJbmIOonLsGQcM-kkDCeZiAFqyil9xg2U,6988
22
- fabricatio/models/usages.py,sha256=JwVanhu5v87LUbnPjwFPriFI5O3HG4SppesYvYLAb50,23636
22
+ fabricatio/models/usages.py,sha256=6qoh9w1xeC-olkQjiH1oZYgqxohp0GX4dwOmSrhMn0E,25756
23
23
  fabricatio/models/utils.py,sha256=i_kpcQpct04mQFk1nbcVGV-pl1YThWu4Qk3wbewzKkc,2535
24
24
  fabricatio/parser.py,sha256=uLabsvF07wRKW1PoTGuGEENCx3P4mhmuO8JkmOEkKko,3522
25
25
  fabricatio/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -30,6 +30,6 @@ fabricatio/toolboxes/__init__.py,sha256=b13KmASO8q5fBLwew964fn9oH86ER5g-S1PgA4fZ
30
30
  fabricatio/_rust.pyi,sha256=0wCqtwWkVxxoqprvk8T27T8QYKIAKHS7xgsmdMNjQKc,1756
31
31
  fabricatio/_rust_instances.py,sha256=dl0-yZ4UvT5g20tQgnPJpmqtkjFGXNG_YK4eLfi_ugQ,279
32
32
  fabricatio/__init__.py,sha256=opIrN8lGyT-h2If4Qez0bRuWBa3uIT9GsM9CZy7_XJ0,1100
33
- fabricatio/_rust.cp312-win_amd64.pyd,sha256=JrZsNCEZJqdoeo2x9MlTG-H7kdQo_3uTKek0P9DdG60,1260544
34
- fabricatio-0.2.1.dev0.data/scripts/tdown.exe,sha256=n9vd_Mp4D-DcGm24C7exfhzphS2HIyS_lRb8lMPJzdk,3396608
35
- fabricatio-0.2.1.dev0.dist-info/RECORD,,
33
+ fabricatio/_rust.cp312-win_amd64.pyd,sha256=MBHhCwM2VaYRbZlDgshZ2fGy3owWguBKWmSsngMwL0A,1261568
34
+ fabricatio-0.2.1.dev1.data/scripts/tdown.exe,sha256=WwgYGM0hyWe1vzFIOwAAS3unnwygxCLw-GrNgwDTAT4,3396608
35
+ fabricatio-0.2.1.dev1.dist-info/RECORD,,