fabricatio 0.2.0.dev13__cp312-cp312-manylinux_2_34_x86_64.whl → 0.2.0.dev15__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/_rust.pyi CHANGED
@@ -1,13 +1,13 @@
1
1
  from pathlib import Path
2
- from typing import Dict, List, Any
3
-
2
+ from typing import Any, Dict, List, Optional
4
3
 
5
4
  class TemplateManager:
6
- def __init__(self, template_dirs: List[Path]) -> None:
7
- """
8
- Initialize the template manager.
5
+ def __init__(self, template_dirs: List[Path], suffix: Optional[str] = None) -> None:
6
+ """Initialize the template manager.
7
+
9
8
  Args:
10
9
  template_dirs (List[Path]): A list of paths to directories containing templates.
10
+ suffix (str, optional): The suffix of template files. None means 'hbs' suffix .
11
11
  """
12
12
 
13
13
  @property
@@ -19,8 +19,8 @@ class TemplateManager:
19
19
  """Get a list of template names."""
20
20
 
21
21
  def get_template(self, name: str) -> str:
22
- """
23
- Get a template by name.
22
+ """Get a template by name.
23
+
24
24
  Args:
25
25
  name (str): The name of the template to retrieve.
26
26
 
@@ -29,8 +29,8 @@ class TemplateManager:
29
29
  """
30
30
 
31
31
  def get_template_source(self, name: str) -> str:
32
- """
33
- Get the source path of a template by name.
32
+ """Get the source path of a template by name.
33
+
34
34
  Args:
35
35
  name (str): The name of the template to retrieve.
36
36
 
@@ -42,8 +42,8 @@ class TemplateManager:
42
42
  """Discover templates in the specified directories."""
43
43
 
44
44
  def render_template(self, name: str, data: Dict[str, Any]) -> str:
45
- """
46
- Render a template with the given name and data.
45
+ """Render a template with the given name and data.
46
+
47
47
  Args:
48
48
  name (str): The name of the template to render.
49
49
  data (Dict[str, Any]): The data to pass to the template.
@@ -1,4 +1,4 @@
1
1
  from fabricatio._rust import TemplateManager
2
2
  from fabricatio.config import configs
3
3
 
4
- template_manager = TemplateManager(configs.code2prompt.template_dir)
4
+ template_manager = TemplateManager(template_dirs=configs.templates.template_dir, suffix=configs.templates.template_suffix)
fabricatio/config.py CHANGED
@@ -116,17 +116,17 @@ class DebugConfig(BaseModel):
116
116
  """The log file of the application."""
117
117
 
118
118
 
119
- class Code2PromptConfig(BaseModel):
120
- """Code2Prompt configuration class."""
119
+ class TemplateConfig(BaseModel):
120
+ """Template configuration class."""
121
121
 
122
122
  model_config = ConfigDict(use_attribute_docstrings=True)
123
123
  template_dir: List[DirectoryPath] = Field(
124
124
  default_factory=lambda: [DirectoryPath(r".\templates"), DirectoryPath(rf"{ROAMING_DIR}\templates")]
125
125
  )
126
- """The directory containing the templates for code2prompt."""
126
+ """The directory containing the templates."""
127
127
 
128
- template_suffix: str = Field(default=".hbs", frozen=True)
129
- """The suffix of the template files for code2prompt."""
128
+ template_suffix: str = Field(default="hbs", frozen=True)
129
+ """The suffix of the templates."""
130
130
 
131
131
 
132
132
  class MagikaConfig(BaseModel):
@@ -137,6 +137,17 @@ class MagikaConfig(BaseModel):
137
137
  """The directory containing the models for magika."""
138
138
 
139
139
 
140
+ class GeneralConfig(BaseModel):
141
+ """Global configuration class."""
142
+
143
+ model_config = ConfigDict(use_attribute_docstrings=True)
144
+ workspace: DirectoryPath = Field(default=DirectoryPath(r"."))
145
+ """The workspace directory for the application."""
146
+
147
+ confirm_on_fs_ops: bool = Field(default=True)
148
+ """Whether to confirm on file system operations."""
149
+
150
+
140
151
  class Settings(BaseSettings):
141
152
  """Application settings class.
142
153
 
@@ -144,7 +155,7 @@ class Settings(BaseSettings):
144
155
  llm (LLMConfig): LLM Configuration
145
156
  debug (DebugConfig): Debug Configuration
146
157
  pymitter (PymitterConfig): Pymitter Configuration
147
- code2prompt (Code2PromptConfig): Code2Prompt Configuration
158
+ templates (TemplateConfig): Template Configuration
148
159
  magika (MagikaConfig): Magika Configuration
149
160
  """
150
161
 
@@ -167,20 +178,22 @@ class Settings(BaseSettings):
167
178
  pymitter: PymitterConfig = Field(default_factory=PymitterConfig)
168
179
  """Pymitter Configuration"""
169
180
 
170
- code2prompt: Code2PromptConfig = Field(default_factory=Code2PromptConfig)
171
- """Code2Prompt Configuration"""
181
+ templates: TemplateConfig = Field(default_factory=TemplateConfig)
182
+ """Template Configuration"""
172
183
 
173
184
  magika: MagikaConfig = Field(default_factory=MagikaConfig)
174
185
  """Magika Configuration"""
175
186
 
187
+ general: GeneralConfig = Field(default_factory=GeneralConfig)
188
+
176
189
  @classmethod
177
190
  def settings_customise_sources(
178
- cls,
179
- settings_cls: type[BaseSettings],
180
- init_settings: PydanticBaseSettingsSource,
181
- env_settings: PydanticBaseSettingsSource,
182
- dotenv_settings: PydanticBaseSettingsSource,
183
- file_secret_settings: PydanticBaseSettingsSource,
191
+ cls,
192
+ settings_cls: type[BaseSettings],
193
+ init_settings: PydanticBaseSettingsSource,
194
+ env_settings: PydanticBaseSettingsSource,
195
+ dotenv_settings: PydanticBaseSettingsSource,
196
+ file_secret_settings: PydanticBaseSettingsSource,
184
197
  ) -> tuple[PydanticBaseSettingsSource, ...]:
185
198
  """Customize settings sources.
186
199
 
fabricatio/decorators.py CHANGED
@@ -1,16 +1,23 @@
1
1
  from functools import wraps
2
+ from inspect import signature
2
3
  from shutil import which
3
- from typing import Callable
4
+ from typing import Callable, Optional
4
5
 
6
+ from questionary import confirm
7
+
8
+ from fabricatio.config import configs
5
9
  from fabricatio.journal import logger
6
10
 
7
11
 
8
- def depend_on_external_cmd[**P, R](bin_name: str, install_tip: str) -> Callable[[Callable[P, R]], Callable[P, R]]:
12
+ def depend_on_external_cmd[**P, R](
13
+ bin_name: str, install_tip: Optional[str], homepage: Optional[str] = None
14
+ ) -> Callable[[Callable[P, R]], Callable[P, R]]:
9
15
  """Decorator to check for the presence of an external command.
10
16
 
11
17
  Args:
12
18
  bin_name (str): The name of the required binary.
13
- install_tip (str): Installation instructions for the required binary.
19
+ install_tip (Optional[str]): Installation instructions for the required binary.
20
+ homepage (Optional[str]): The homepage of the required binary.
14
21
 
15
22
  Returns:
16
23
  Callable[[Callable[P, R]], Callable[P, R]]: A decorator that wraps the function to check for the binary.
@@ -23,13 +30,41 @@ def depend_on_external_cmd[**P, R](bin_name: str, install_tip: str) -> Callable[
23
30
  @wraps(func)
24
31
  def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
25
32
  if which(bin_name) is None:
26
- err = (
27
- f"{bin_name} is required to run function: {func.__name__}, please install it first.\n{install_tip}"
28
- )
29
- logger.critical(err)
33
+ err = f"`{bin_name}` is required to run {func.__name__}{signature(func)}, please install it the to `PATH` first."
34
+ if install_tip is not None:
35
+ err += f"\nInstall tip: {install_tip}"
36
+ if homepage is not None:
37
+ err += f"\nHomepage: {homepage}"
38
+ logger.error(err)
30
39
  raise RuntimeError(err)
31
40
  return func(*args, **kwargs)
32
41
 
33
42
  return _wrapper
34
43
 
35
44
  return _decorator
45
+
46
+
47
+ def confirm_to_execute[**P, R](func: Callable[P, R]) -> Callable[P, Optional[R]] | Callable[P, R]:
48
+ """Decorator to confirm before executing a function.
49
+
50
+ Args:
51
+ func (Callable): The function to be executed
52
+
53
+ Returns:
54
+ Callable: A decorator that wraps the function to confirm before execution.
55
+ """
56
+ if not configs.general.confirm_on_fs_ops:
57
+ # Skip confirmation if the configuration is set to False
58
+ return func
59
+
60
+ @wraps(func)
61
+ def _wrapper(*args: P.args, **kwargs: P.kwargs) -> Optional[R]:
62
+ if confirm(
63
+ f"Are you sure to execute function: {func.__name__}{signature(func)} \n📦 Args:{args}\n🔑 Kwargs:{kwargs}\n",
64
+ instruction="Please input [Yes/No] to proceed (default: Yes):",
65
+ ).ask():
66
+ return func(*args, **kwargs)
67
+ logger.warning(f"Function: {func.__name__}{signature(func)} canceled by user.")
68
+ return None
69
+
70
+ return _wrapper
fabricatio/fs/curd.py ADDED
@@ -0,0 +1,110 @@
1
+ """File system create, update, read, delete operations."""
2
+
3
+ import shutil
4
+ import subprocess
5
+ from pathlib import Path
6
+ from typing import Union
7
+
8
+ from fabricatio.decorators import depend_on_external_cmd
9
+ from fabricatio.journal import logger
10
+
11
+
12
+ def copy_file(src: Union[str, Path], dst: Union[str, Path]) -> None:
13
+ """Copy a file from source to destination.
14
+
15
+ Args:
16
+ src: Source file path
17
+ dst: Destination file path
18
+
19
+ Raises:
20
+ FileNotFoundError: If source file doesn't exist
21
+ shutil.SameFileError: If source and destination are the same
22
+ """
23
+ try:
24
+ shutil.copy(src, dst)
25
+ logger.info(f"Copied file from {src} to {dst}")
26
+ except OSError as e:
27
+ logger.error(f"Failed to copy file from {src} to {dst}: {e!s}")
28
+ raise
29
+
30
+
31
+ def move_file(src: Union[str, Path], dst: Union[str, Path]) -> None:
32
+ """Move a file from source to destination.
33
+
34
+ Args:
35
+ src: Source file path
36
+ dst: Destination file path
37
+
38
+ Raises:
39
+ FileNotFoundError: If source file doesn't exist
40
+ shutil.SameFileError: If source and destination are the same
41
+ """
42
+ try:
43
+ shutil.move(src, dst)
44
+ logger.info(f"Moved file from {src} to {dst}")
45
+ except OSError as e:
46
+ logger.error(f"Failed to move file from {src} to {dst}: {e!s}")
47
+ raise
48
+
49
+
50
+ def delete_file(file_path: Union[str, Path]) -> None:
51
+ """Delete a file.
52
+
53
+ Args:
54
+ file_path: Path to the file to be deleted
55
+
56
+ Raises:
57
+ FileNotFoundError: If file doesn't exist
58
+ PermissionError: If no permission to delete the file
59
+ """
60
+ try:
61
+ Path(file_path).unlink()
62
+ logger.info(f"Deleted file: {file_path}")
63
+ except OSError as e:
64
+ logger.error(f"Failed to delete file {file_path}: {e!s}")
65
+ raise
66
+
67
+
68
+ def create_directory(dir_path: Union[str, Path], parents: bool = True, exist_ok: bool = True) -> None:
69
+ """Create a directory.
70
+
71
+ Args:
72
+ dir_path: Path to the directory to create
73
+ parents: Create parent directories if they don't exist
74
+ exist_ok: Don't raise error if directory already exists
75
+ """
76
+ try:
77
+ Path(dir_path).mkdir(parents=parents, exist_ok=exist_ok)
78
+ logger.info(f"Created directory: {dir_path}")
79
+ except OSError as e:
80
+ logger.error(f"Failed to create directory {dir_path}: {e!s}")
81
+ raise
82
+
83
+
84
+ @depend_on_external_cmd(
85
+ "erd",
86
+ "Please install `erd` using `cargo install erdtree` or `scoop install erdtree`.",
87
+ "https://github.com/solidiquis/erdtree",
88
+ )
89
+ def tree(dir_path: Union[str, Path]) -> str:
90
+ """Generate a tree representation of the directory structure. Requires `erd` to be installed."""
91
+ dir_path = Path(dir_path)
92
+ return subprocess.check_output(("erd", dir_path.as_posix()), encoding="utf-8") # noqa: S603
93
+
94
+
95
+ def delete_directory(dir_path: Union[str, Path]) -> None:
96
+ """Delete a directory and its contents.
97
+
98
+ Args:
99
+ dir_path: Path to the directory to delete
100
+
101
+ Raises:
102
+ FileNotFoundError: If directory doesn't exist
103
+ OSError: If directory is not empty and can't be removed
104
+ """
105
+ try:
106
+ shutil.rmtree(dir_path)
107
+ logger.info(f"Deleted directory: {dir_path}")
108
+ except OSError as e:
109
+ logger.error(f"Failed to delete directory {dir_path}: {e!s}")
110
+ raise
fabricatio/fs/readers.py CHANGED
@@ -1,3 +1,5 @@
1
+ """Filesystem readers for Fabricatio."""
2
+
1
3
  from magika import Magika
2
4
 
3
5
  from fabricatio.config import configs
@@ -6,12 +6,14 @@ from asyncio import Queue
6
6
  from typing import Any, Dict, Self, Tuple, Type, Unpack
7
7
 
8
8
  from fabricatio.journal import logger
9
- from fabricatio.models.generic import LLMUsage, WithBriefing
10
- from fabricatio.models.task import ProposeTask, Task
9
+ from fabricatio.models.advanced import ProposeTask
10
+ from fabricatio.models.generic import WithBriefing
11
+ from fabricatio.models.task import Task
12
+ from fabricatio.models.usages import ToolBoxUsage
11
13
  from pydantic import Field, PrivateAttr
12
14
 
13
15
 
14
- class Action(ProposeTask):
16
+ class Action(ProposeTask, ToolBoxUsage):
15
17
  """Class that represents an action to be executed in a workflow."""
16
18
 
17
19
  personality: str = Field(default="")
@@ -50,7 +52,7 @@ class Action(ProposeTask):
50
52
  return f"# The action you are going to perform: \n{super().briefing}"
51
53
 
52
54
 
53
- class WorkFlow[A: Type[Action] | Action](WithBriefing, LLMUsage):
55
+ class WorkFlow[A: Type[Action] | Action](WithBriefing, ToolBoxUsage):
54
56
  """Class that represents a workflow to be executed in a task."""
55
57
 
56
58
  _context: Queue[Dict[str, Any]] = PrivateAttr(default_factory=lambda: Queue(maxsize=1))
@@ -121,7 +123,12 @@ class WorkFlow[A: Type[Action] | Action](WithBriefing, LLMUsage):
121
123
  logger.debug(f"Initializing context for workflow: {self.name}")
122
124
  await self._context.put({self.task_input_key: None, **dict(self.extra_init_context)})
123
125
 
124
- def fallback_to_self(self) -> Self:
126
+ def steps_fallback_to_self(self) -> Self:
125
127
  """Set the fallback for each step to the workflow itself."""
126
128
  self.hold_to(self._instances)
127
129
  return self
130
+
131
+ def steps_supply_tools_from_self(self) -> Self:
132
+ """Supply the tools from the workflow to each step."""
133
+ self.provide_tools_to(self._instances)
134
+ return self
@@ -0,0 +1,93 @@
1
+ """A module for advanced models and functionalities."""
2
+
3
+ from typing import List
4
+
5
+ from fabricatio._rust_instances import template_manager
6
+ from fabricatio.models.generic import WithBriefing
7
+ from fabricatio.models.task import Task
8
+ from fabricatio.models.usages import LLMUsage, ToolBoxUsage
9
+ from fabricatio.parser import JsonCapture
10
+ from loguru import logger
11
+ from pydantic import NonNegativeFloat, PositiveInt, ValidationError
12
+
13
+
14
+ class ProposeTask(LLMUsage, WithBriefing):
15
+ """A class that proposes a task based on a prompt."""
16
+
17
+ async def propose(
18
+ self,
19
+ prompt: str,
20
+ max_validations: PositiveInt = 2,
21
+ model: str | None = None,
22
+ temperature: NonNegativeFloat | None = None,
23
+ stop: str | List[str] | None = None,
24
+ top_p: NonNegativeFloat | None = None,
25
+ max_tokens: PositiveInt | None = None,
26
+ stream: bool | None = None,
27
+ timeout: PositiveInt | None = None,
28
+ max_retries: PositiveInt | None = None,
29
+ ) -> Task:
30
+ """Asynchronously proposes a task based on a given prompt and parameters.
31
+
32
+ Parameters:
33
+ prompt: The prompt text for proposing a task, which is a string that must be provided.
34
+ max_validations: The maximum number of validations allowed, default is 2.
35
+ model: The model to be used, default is None.
36
+ temperature: The sampling temperature, default is None.
37
+ stop: The stop sequence(s) for generation, default is None.
38
+ top_p: The nucleus sampling parameter, default is None.
39
+ max_tokens: The maximum number of tokens to be generated, default is None.
40
+ stream: Whether to stream the output, default is None.
41
+ timeout: The timeout for the operation, default is None.
42
+ max_retries: The maximum number of retries for the operation, default is None.
43
+
44
+ Returns:
45
+ A Task object based on the proposal result.
46
+ """
47
+ assert prompt, "Prompt must be provided."
48
+
49
+ def _validate_json(response: str) -> None | Task:
50
+ try:
51
+ cap = JsonCapture.capture(response)
52
+ logger.debug(f"Response: \n{response}")
53
+ logger.info(f"Captured JSON: \n{cap}")
54
+ return Task.model_validate_json(cap)
55
+ except ValidationError as e:
56
+ logger.error(f"Failed to parse task from JSON: {e}")
57
+ return None
58
+
59
+ template_data = {"prompt": prompt, "json_example": Task.json_example()}
60
+ return await self.aask_validate(
61
+ question=template_manager.render_template("propose_task", template_data),
62
+ validator=_validate_json,
63
+ system_message=f"# your personal briefing: \n{self.briefing}",
64
+ max_validations=max_validations,
65
+ model=model,
66
+ temperature=temperature,
67
+ stop=stop,
68
+ top_p=top_p,
69
+ max_tokens=max_tokens,
70
+ stream=stream,
71
+ timeout=timeout,
72
+ max_retries=max_retries,
73
+ )
74
+
75
+
76
+ class HandleTask(WithBriefing, ToolBoxUsage):
77
+ """A class that handles a task based on a task object."""
78
+
79
+ async def handle[T](
80
+ self,
81
+ task: Task[T],
82
+ max_validations: PositiveInt = 2,
83
+ model: str | None = None,
84
+ temperature: NonNegativeFloat | None = None,
85
+ stop: str | List[str] | None = None,
86
+ top_p: NonNegativeFloat | None = None,
87
+ max_tokens: PositiveInt | None = None,
88
+ stream: bool | None = None,
89
+ timeout: PositiveInt | None = None,
90
+ max_retries: PositiveInt | None = None,
91
+ ) -> T:
92
+ """Asynchronously handles a task based on a given task object and parameters."""
93
+ # TODO: Implement the handle method