fabricatio 0.2.0.dev12__cp312-cp312-win_amd64.whl → 0.2.0.dev13__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
@@ -17,6 +17,7 @@ from pydantic import (
17
17
  Field,
18
18
  HttpUrl,
19
19
  NonNegativeFloat,
20
+ NonNegativeInt,
20
21
  PositiveInt,
21
22
  SecretStr,
22
23
  )
@@ -295,6 +296,7 @@ class LLMUsage(Base):
295
296
  self,
296
297
  instruction: str,
297
298
  choices: List[T],
299
+ k: NonNegativeInt = 0,
298
300
  max_validations: PositiveInt = 2,
299
301
  system_message: str = "",
300
302
  model: str | None = None,
@@ -311,6 +313,7 @@ class LLMUsage(Base):
311
313
  Args:
312
314
  instruction: The user-provided instruction/question description.
313
315
  choices: A list of candidate options, requiring elements to have `name` and `briefing` fields.
316
+ k: The number of choices to select, 0 means infinite.
314
317
  max_validations: Maximum number of validation failures, default is 2.
315
318
  system_message: Custom system-level prompt, defaults to an empty string.
316
319
  model: The name of the LLM model to use.
@@ -332,14 +335,17 @@ class LLMUsage(Base):
332
335
  """
333
336
  prompt = template_manager.render_template(
334
337
  "make_choice",
335
- {"instruction": instruction, "options": [m.model_dump(include={"name", "briefing"}) for m in choices]},
338
+ {
339
+ "instruction": instruction,
340
+ "options": [m.model_dump(include={"name", "briefing"}) for m in choices],
341
+ "k": k,
342
+ },
336
343
  )
337
344
  names = [c.name for c in choices]
338
345
 
339
346
  def _validate(response: str) -> List[T] | None:
340
- cap = JsonCapture.capture(response)
341
- ret = orjson.loads(cap)
342
- if not isinstance(ret, List):
347
+ ret = JsonCapture.convert_with(response, orjson.loads)
348
+ if not isinstance(ret, List) or len(ret) != k:
343
349
  return None
344
350
  if any(n not in names for n in ret):
345
351
  return None
@@ -360,6 +366,70 @@ class LLMUsage(Base):
360
366
  max_retries=max_retries,
361
367
  )
362
368
 
369
+ async def ajudge(
370
+ self,
371
+ prompt: str,
372
+ affirm_case: str = "",
373
+ deny_case: str = "",
374
+ max_validations: PositiveInt = 2,
375
+ system_message: str = "",
376
+ model: str | None = None,
377
+ temperature: NonNegativeFloat | None = None,
378
+ stop: str | List[str] | None = None,
379
+ top_p: NonNegativeFloat | None = None,
380
+ max_tokens: PositiveInt | None = None,
381
+ stream: bool | None = None,
382
+ timeout: PositiveInt | None = None,
383
+ max_retries: PositiveInt | None = None,
384
+ ) -> bool:
385
+ """Asynchronously judges a prompt using AI validation.
386
+
387
+ Args:
388
+ prompt (str): The input prompt to be judged.
389
+ affirm_case (str, optional): The affirmative case for the AI model. Defaults to "".
390
+ deny_case (str, optional): The negative case for the AI model. Defaults to "".
391
+ max_validations (PositiveInt, optional): Maximum number of validation attempts. Defaults to 2.
392
+ system_message (str, optional): System message for the AI model. Defaults to "".
393
+ model (str | None, optional): AI model to use. Defaults to None.
394
+ temperature (NonNegativeFloat | None, optional): Sampling temperature. Defaults to None.
395
+ stop (str | List[str] | None, optional): Stop sequences. Defaults to None.
396
+ top_p (NonNegativeFloat | None, optional): Nucleus sampling parameter. Defaults to None.
397
+ max_tokens (PositiveInt | None, optional): Maximum number of tokens to generate. Defaults to None.
398
+ stream (bool | None, optional): Whether to stream the response. Defaults to None.
399
+ timeout (PositiveInt | None, optional): Timeout in seconds. Defaults to None.
400
+ max_retries (PositiveInt | None, optional): Maximum number of retries. Defaults to None.
401
+
402
+ Returns:
403
+ bool: The judgment result (True or False) based on the AI's response.
404
+
405
+ Notes:
406
+ The method uses an internal validator to ensure the response is a boolean value.
407
+ If the response cannot be converted to a boolean, it will return None.
408
+ """
409
+
410
+ def _validate(response: str) -> bool | None:
411
+ ret = JsonCapture.convert_with(response, orjson.loads)
412
+ if not isinstance(ret, bool):
413
+ return None
414
+ return ret
415
+
416
+ return await self.aask_validate(
417
+ question=template_manager.render_template(
418
+ "make_judgment", {"prompt": prompt, "affirm_case": affirm_case, "deny_case": deny_case}
419
+ ),
420
+ validator=_validate,
421
+ max_validations=max_validations,
422
+ system_message=system_message,
423
+ model=model,
424
+ temperature=temperature,
425
+ stop=stop,
426
+ top_p=top_p,
427
+ max_tokens=max_tokens,
428
+ stream=stream,
429
+ timeout=timeout,
430
+ max_retries=max_retries,
431
+ )
432
+
363
433
  def fallback_to(self, other: "LLMUsage") -> Self:
364
434
  """Fallback to another instance's attribute values if the current instance's attributes are None.
365
435
 
fabricatio/models/task.py CHANGED
@@ -268,8 +268,8 @@ class ProposeTask(LLMUsage, WithBriefing):
268
268
  try:
269
269
  cap = JsonCapture.capture(response)
270
270
  logger.debug(f"Response: \n{response}")
271
- logger.info(f"Captured JSON: \n{cap[0]}")
272
- return Task.model_validate_json(cap[0] if cap else response)
271
+ logger.info(f"Captured JSON: \n{cap}")
272
+ return Task.model_validate_json(cap)
273
273
  except ValidationError as e:
274
274
  logger.error(f"Failed to parse task from JSON: {e}")
275
275
  return None
fabricatio/parser.py CHANGED
@@ -1,11 +1,13 @@
1
1
  """A module to parse text using regular expressions."""
2
2
 
3
- from typing import Any, Self, Tuple
3
+ from typing import Any, Callable, Self, Tuple
4
4
 
5
5
  import regex
6
6
  from pydantic import BaseModel, ConfigDict, Field, PositiveInt, PrivateAttr
7
7
  from regex import Pattern, compile
8
8
 
9
+ from fabricatio.journal import logger
10
+
9
11
 
10
12
  class Capture(BaseModel):
11
13
  """A class to capture patterns in text using regular expressions.
@@ -32,7 +34,7 @@ class Capture(BaseModel):
32
34
  """
33
35
  self._compiled = compile(self.pattern, self.flags)
34
36
 
35
- def capture(self, text: str) -> Tuple[str, ...] | None:
37
+ def capture(self, text: str) -> Tuple[str, ...] | str | None:
36
38
  """Capture the first occurrence of the pattern in the given text.
37
39
 
38
40
  Args:
@@ -48,7 +50,25 @@ class Capture(BaseModel):
48
50
 
49
51
  if self.target_groups:
50
52
  return tuple(match.group(g) for g in self.target_groups)
51
- return (match.group(),)
53
+ return match.group(1)
54
+
55
+ def convert_with[T](self, text: str, convertor: Callable[[Tuple[str, ...]], T] | Callable[[str], T]) -> T | None:
56
+ """Convert the given text using the pattern.
57
+
58
+ Args:
59
+ text (str): The text to search the pattern in.
60
+ convertor (Callable[[Tuple[str, ...]], T] | Callable[[str], T]): The function to convert the captured text.
61
+
62
+ Returns:
63
+ str | None: The converted text if the pattern is found, otherwise None.
64
+ """
65
+ if cap := self.capture(text) is None:
66
+ return None
67
+ try:
68
+ return convertor(cap)
69
+ except ValueError as e:
70
+ logger.error(f"Failed to convert text using convertor: {convertor.__name__}, error: \n{e}")
71
+ return None
52
72
 
53
73
  @classmethod
54
74
  def capture_code_block(cls, language: str) -> Self:
@@ -60,7 +80,7 @@ class Capture(BaseModel):
60
80
  Returns:
61
81
  Self: The instance of the class with the captured code block.
62
82
  """
63
- return cls(pattern=f"```{language}\n(.*?)\n```", target_groups=(1,))
83
+ return cls(pattern=f"```{language}\n(.*?)\n```")
64
84
 
65
85
 
66
86
  JsonCapture = Capture.capture_code_block("json")
@@ -1,3 +1,5 @@
1
+ """Arithmetic tools for Fabricatio."""
2
+
1
3
  from fabricatio.models.tool import ToolBox
2
4
 
3
5
  arithmetic_tools = ToolBox(name="ArithmeticToolBox", description="A toolbox for arithmetic operations.")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fabricatio
3
- Version: 0.2.0.dev12
3
+ Version: 0.2.0.dev13
4
4
  Classifier: License :: OSI Approved :: MIT License
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Programming Language :: Python :: 3.12
@@ -28,6 +28,9 @@ Keywords: ai,agents,multi-agent,llm,pyo3
28
28
  Author-email: Whth <zettainspector@foxmail.com>
29
29
  Requires-Python: >=3.12
30
30
  Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
31
+ Project-URL: Homepage, https://github.com/Whth/fabricatio
32
+ Project-URL: Repository, https://github.com/Whth/fabricatio
33
+ Project-URL: Issues, https://github.com/Whth/fabricatio/issues
31
34
 
32
35
  # Fabricatio
33
36
 
@@ -1,6 +1,6 @@
1
- fabricatio-0.2.0.dev12.dist-info/METADATA,sha256=YEajxaRO-nlzAs-7NthqNeuxmcT2qjYECILk1gKZBSw,6017
2
- fabricatio-0.2.0.dev12.dist-info/WHEEL,sha256=tpW5AN9B-9qsM9WW2FXG2r193YXiqexDadpKp0A2daI,96
3
- fabricatio-0.2.0.dev12.dist-info/licenses/LICENSE,sha256=do7J7EiCGbq0QPbMAL_FqLYufXpHnCnXBOuqVPwSV8Y,1088
1
+ fabricatio-0.2.0.dev13.dist-info/METADATA,sha256=tNqBJKjBYmikfL1MjQOp6dVJQjkQ2dzzO3vcDUP6BkU,6198
2
+ fabricatio-0.2.0.dev13.dist-info/WHEEL,sha256=tpW5AN9B-9qsM9WW2FXG2r193YXiqexDadpKp0A2daI,96
3
+ fabricatio-0.2.0.dev13.dist-info/licenses/LICENSE,sha256=do7J7EiCGbq0QPbMAL_FqLYufXpHnCnXBOuqVPwSV8Y,1088
4
4
  fabricatio/actions/communication.py,sha256=tmsr3H_w-V-b2WxLEyWByGuwSCLgHIHTdHYAgHrdUxc,425
5
5
  fabricatio/actions/transmission.py,sha256=PedZ6XsflKdT5ikzaqWr_6h8jci0kekAHfwygzKBUns,1188
6
6
  fabricatio/actions/__init__.py,sha256=eFmFVPQvtNgFynIXBVr3eP-vWQDWCPng60YY5LXvZgg,115
@@ -12,19 +12,19 @@ fabricatio/fs/__init__.py,sha256=lWcKYg0v3mv2LnnSegOQaTtlVDODU0vtw_s6iKU5IqQ,122
12
12
  fabricatio/journal.py,sha256=z5K5waad9xmGr1hGrqSgFDRH3wiDQ5Oqfe0o98DaM-k,707
13
13
  fabricatio/models/action.py,sha256=5s3pgFav3AOeZzzAoFAyL3f-uiHIpEE86W_w0qC2YIM,5009
14
14
  fabricatio/models/events.py,sha256=DDdcexweKV7jmPLHx51PIQ6eIByRrFyAMyx2VMhl9JY,2650
15
- fabricatio/models/generic.py,sha256=TqfVFxEm56l3LiIN4m4WS9CylzQacGfYGElkItkhF3A,19724
15
+ fabricatio/models/generic.py,sha256=s2xz8yOlfSMnD2R9iRIe2xl12JNJXILBDfFZWzhl-xc,22932
16
16
  fabricatio/models/role.py,sha256=sgsympwkp6HIbWkaAt4gMU2WdVO-bHwX0Gy6DNyhoLA,1016
17
- fabricatio/models/task.py,sha256=bP3tKj9JXmtC_n3Y8-8Lgs0qhjcXxsuCDRD5DoYWsA4,9372
17
+ fabricatio/models/task.py,sha256=rw4RCvziUDRixE5N5miZolhYl6mvfV9dGgDxrFbJZS0,9345
18
18
  fabricatio/models/tool.py,sha256=vSuFe-SsZHRGqSdpoCSumVZYoS9ktzQBFnJjDqOtZys,5031
19
19
  fabricatio/models/utils.py,sha256=i_kpcQpct04mQFk1nbcVGV-pl1YThWu4Qk3wbewzKkc,2535
20
- fabricatio/parser.py,sha256=M0-ywN2V512dHxomTo9hcdu80-pkXDh_vG4Wc_udOl8,2468
20
+ fabricatio/parser.py,sha256=foEhrO_e-hhRhmABcttwdMyciyJx422MpNqCZOUx8bg,3278
21
21
  fabricatio/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
- fabricatio/toolboxes/arithmetic.py,sha256=5eWTrwGFn_ap1pfAbDyXpw-NADk_QtD1dmxOr-A7rdk,1317
22
+ fabricatio/toolboxes/arithmetic.py,sha256=aCBEUTe8nNop5tAgZsBIsuJYl-u8KI9n0PtgVVoEu5E,1359
23
23
  fabricatio/toolboxes/task.py,sha256=xgyPetm2R_HlQwpzE8YPnBN7QOYLd0-T8E6QPZG1PPQ,204
24
24
  fabricatio/toolboxes/__init__.py,sha256=bjefmPd7wBaWhbZzdMPXvrjMTeRzlUh_Dev2PUAc124,158
25
25
  fabricatio/_rust.pyi,sha256=IHNv9SHdjve24PBWhdRGCqWYdo2tSAkxYR9CddHhzX8,1540
26
26
  fabricatio/_rust_instances.py,sha256=PJC8TAkcH9f1Ak-0CKxSom_HTFHQEXZ8-aSDrYw7dwI,157
27
27
  fabricatio/__init__.py,sha256=C9r6OVyMBb8IqwERNUq8lKDLe4BqN7fiu-O4TsXZ5xU,913
28
- fabricatio/_rust.cp312-win_amd64.pyd,sha256=Rz8qC0BE2eYcGVxBZC3BoUuLw9Z0PF768Pyece2KWcA,1128960
29
- fabricatio-0.2.0.dev12.data/scripts/tdown.exe,sha256=3CNAK6oj52AI-WUdjB4DCmuB1Z4JByPxFvRkATtBw1A,3385856
30
- fabricatio-0.2.0.dev12.dist-info/RECORD,,
28
+ fabricatio/_rust.cp312-win_amd64.pyd,sha256=JLo7LIETUZieXFbRXdNLb2WRIOxbYvkEOS11LfxID2g,1125888
29
+ fabricatio-0.2.0.dev13.data/scripts/tdown.exe,sha256=1TDXTUKOGjcQ0SoFEZUNad9kf3swM_UWz1MySMdF-sA,3383296
30
+ fabricatio-0.2.0.dev13.dist-info/RECORD,,