fabricatio 0.2.0.dev19__cp312-cp312-win_amd64.whl → 0.2.1__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/_rust.cp312-win_amd64.pyd +0 -0
- fabricatio/_rust.pyi +3 -3
- fabricatio/actions/communication.py +6 -6
- fabricatio/actions/transmission.py +0 -11
- fabricatio/config.py +18 -3
- fabricatio/decorators.py +118 -11
- fabricatio/fs/curd.py +21 -7
- fabricatio/journal.py +7 -2
- fabricatio/models/action.py +9 -3
- fabricatio/models/advanced.py +23 -14
- fabricatio/models/generic.py +23 -2
- fabricatio/models/role.py +1 -2
- fabricatio/models/task.py +16 -10
- fabricatio/models/tool.py +25 -9
- fabricatio/models/usages.py +72 -17
- fabricatio/parser.py +8 -4
- fabricatio/toolboxes/fs.py +3 -2
- fabricatio-0.2.1.data/scripts/tdown.exe +0 -0
- fabricatio-0.2.1.dist-info/METADATA +420 -0
- fabricatio-0.2.1.dist-info/RECORD +35 -0
- fabricatio-0.2.0.dev19.data/scripts/tdown.exe +0 -0
- fabricatio-0.2.0.dev19.dist-info/METADATA +0 -233
- fabricatio-0.2.0.dev19.dist-info/RECORD +0 -35
- {fabricatio-0.2.0.dev19.dist-info → fabricatio-0.2.1.dist-info}/WHEEL +0 -0
- {fabricatio-0.2.0.dev19.dist-info → fabricatio-0.2.1.dist-info}/licenses/LICENSE +0 -0
fabricatio/models/tool.py
CHANGED
@@ -3,11 +3,11 @@
|
|
3
3
|
from importlib.machinery import ModuleSpec
|
4
4
|
from importlib.util import module_from_spec
|
5
5
|
from inspect import iscoroutinefunction, signature
|
6
|
-
from sys import modules
|
7
6
|
from types import CodeType, ModuleType
|
8
7
|
from typing import Any, Callable, Dict, List, Optional, Self, overload
|
9
8
|
|
10
9
|
from fabricatio.config import configs
|
10
|
+
from fabricatio.decorators import use_temp_module
|
11
11
|
from fabricatio.journal import logger
|
12
12
|
from fabricatio.models.generic import WithBriefing
|
13
13
|
from pydantic import BaseModel, ConfigDict, Field
|
@@ -36,7 +36,7 @@ class Tool[**P, R](WithBriefing):
|
|
36
36
|
|
37
37
|
def invoke(self, *args: P.args, **kwargs: P.kwargs) -> R:
|
38
38
|
"""Invoke the tool's source function with the provided arguments."""
|
39
|
-
logger.info(f"Invoking tool: {self.name}
|
39
|
+
logger.info(f"Invoking tool: {self.name}")
|
40
40
|
return self.source(*args, **kwargs)
|
41
41
|
|
42
42
|
@property
|
@@ -127,21 +127,37 @@ class ToolExecutor(BaseModel):
|
|
127
127
|
"""A class representing a tool executor with a sequence of tools to execute."""
|
128
128
|
|
129
129
|
model_config = ConfigDict(use_attribute_docstrings=True)
|
130
|
-
|
130
|
+
candidates: List[Tool] = Field(default_factory=list, frozen=True)
|
131
131
|
"""The sequence of tools to execute."""
|
132
132
|
|
133
|
+
data: Dict[str, Any] = Field(default_factory=dict)
|
134
|
+
"""The data that could be used when invoking the tools."""
|
135
|
+
|
133
136
|
def inject_tools[M: ModuleType](self, module: Optional[M] = None) -> M:
|
134
|
-
"""Inject the tools into the provided module."""
|
137
|
+
"""Inject the tools into the provided module or default."""
|
135
138
|
module = module or module_from_spec(spec=ModuleSpec(name=configs.toolbox.tool_module_name, loader=None))
|
136
|
-
for tool in self.
|
139
|
+
for tool in self.candidates:
|
140
|
+
logger.debug(f"Injecting tool: {tool.name}")
|
137
141
|
setattr(module, tool.name, tool.invoke)
|
138
142
|
return module
|
139
143
|
|
144
|
+
def inject_data[M: ModuleType](self, module: Optional[M] = None) -> M:
|
145
|
+
"""Inject the data into the provided module or default."""
|
146
|
+
module = module or module_from_spec(spec=ModuleSpec(name=configs.toolbox.data_module_name, loader=None))
|
147
|
+
for key, value in self.data.items():
|
148
|
+
logger.debug(f"Injecting data: {key}")
|
149
|
+
setattr(module, key, value)
|
150
|
+
return module
|
151
|
+
|
140
152
|
def execute[C: Dict[str, Any]](self, source: CodeType, cxt: Optional[C] = None) -> C:
|
141
153
|
"""Execute the sequence of tools with the provided context."""
|
142
|
-
|
143
|
-
|
144
|
-
|
154
|
+
cxt = cxt or {}
|
155
|
+
|
156
|
+
@use_temp_module([self.inject_data(), self.inject_tools()])
|
157
|
+
def _exec() -> None:
|
158
|
+
exec(source, cxt) # noqa: S102
|
159
|
+
|
160
|
+
_exec()
|
145
161
|
return cxt
|
146
162
|
|
147
163
|
@overload
|
@@ -169,4 +185,4 @@ class ToolExecutor(BaseModel):
|
|
169
185
|
for toolbox in toolboxes:
|
170
186
|
tools.append(toolbox[tool_name])
|
171
187
|
|
172
|
-
return cls(
|
188
|
+
return cls(candidates=tools)
|
fabricatio/models/usages.py
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
from typing import Callable, Dict, Iterable, List, Optional, Self, Set, Union, Unpack
|
4
4
|
|
5
|
+
import asyncstdlib
|
5
6
|
import litellm
|
6
7
|
import orjson
|
7
8
|
from fabricatio._rust_instances import template_manager
|
@@ -13,7 +14,13 @@ from fabricatio.models.task import Task
|
|
13
14
|
from fabricatio.models.tool import Tool, ToolBox
|
14
15
|
from fabricatio.models.utils import Messages
|
15
16
|
from fabricatio.parser import JsonCapture
|
16
|
-
from litellm
|
17
|
+
from litellm import stream_chunk_builder
|
18
|
+
from litellm.types.utils import (
|
19
|
+
Choices,
|
20
|
+
ModelResponse,
|
21
|
+
StreamingChoices,
|
22
|
+
)
|
23
|
+
from litellm.utils import CustomStreamWrapper
|
17
24
|
from pydantic import Field, HttpUrl, NonNegativeFloat, NonNegativeInt, PositiveInt, SecretStr
|
18
25
|
|
19
26
|
|
@@ -58,7 +65,7 @@ class LLMUsage(Base):
|
|
58
65
|
messages: List[Dict[str, str]],
|
59
66
|
n: PositiveInt | None = None,
|
60
67
|
**kwargs: Unpack[LLMKwargs],
|
61
|
-
) -> ModelResponse:
|
68
|
+
) -> ModelResponse | CustomStreamWrapper:
|
62
69
|
"""Asynchronously queries the language model to generate a response based on the provided messages and parameters.
|
63
70
|
|
64
71
|
Args:
|
@@ -105,13 +112,23 @@ class LLMUsage(Base):
|
|
105
112
|
Returns:
|
106
113
|
List[Choices | StreamingChoices]: A list of choices or streaming choices from the model response.
|
107
114
|
"""
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
+
resp = await self.aquery(
|
116
|
+
messages=Messages().add_system_message(system_message).add_user_message(question),
|
117
|
+
n=n,
|
118
|
+
**kwargs,
|
119
|
+
)
|
120
|
+
if isinstance(resp, ModelResponse):
|
121
|
+
return resp.choices
|
122
|
+
if isinstance(resp, CustomStreamWrapper):
|
123
|
+
if configs.debug.streaming_visible:
|
124
|
+
chunks = []
|
125
|
+
async for chunk in resp:
|
126
|
+
chunks.append(chunk)
|
127
|
+
print(chunk.choices[0].delta.content or "", end="") # noqa: T201
|
128
|
+
return stream_chunk_builder(chunks).choices
|
129
|
+
return stream_chunk_builder(await asyncstdlib.list()).choices
|
130
|
+
logger.critical(err := f"Unexpected response type: {type(resp)}")
|
131
|
+
raise ValueError(err)
|
115
132
|
|
116
133
|
async def aask(
|
117
134
|
self,
|
@@ -137,10 +154,8 @@ class LLMUsage(Base):
|
|
137
154
|
system_message=system_message,
|
138
155
|
**kwargs,
|
139
156
|
)
|
140
|
-
)
|
141
|
-
|
142
|
-
.message.content
|
143
|
-
)
|
157
|
+
).pop()
|
158
|
+
).message.content
|
144
159
|
|
145
160
|
async def aask_validate[T](
|
146
161
|
self,
|
@@ -210,19 +225,24 @@ class LLMUsage(Base):
|
|
210
225
|
configs.templates.make_choice_template,
|
211
226
|
{
|
212
227
|
"instruction": instruction,
|
213
|
-
"options": [
|
228
|
+
"options": [{"name": m.name, "briefing": m.briefing} for m in choices],
|
214
229
|
"k": k,
|
215
230
|
},
|
216
231
|
)
|
217
|
-
names =
|
232
|
+
names = {c.name for c in choices}
|
233
|
+
logger.debug(f"Start choosing between {names} with prompt: \n{prompt}")
|
218
234
|
|
219
235
|
def _validate(response: str) -> List[T] | None:
|
220
236
|
ret = JsonCapture.convert_with(response, orjson.loads)
|
221
|
-
|
237
|
+
|
238
|
+
if not isinstance(ret, List) or (0 < k != len(ret)):
|
239
|
+
logger.error(f"Incorrect Type or length of response: \n{ret}")
|
222
240
|
return None
|
223
241
|
if any(n not in names for n in ret):
|
242
|
+
logger.error(f"Invalid choice in response: \n{ret}")
|
224
243
|
return None
|
225
|
-
|
244
|
+
|
245
|
+
return [next(toolbox for toolbox in choices if toolbox.name == toolbox_str) for toolbox_str in ret]
|
226
246
|
|
227
247
|
return await self.aask_validate(
|
228
248
|
question=prompt,
|
@@ -232,6 +252,41 @@ class LLMUsage(Base):
|
|
232
252
|
**kwargs,
|
233
253
|
)
|
234
254
|
|
255
|
+
async def apick[T: WithBriefing](
|
256
|
+
self,
|
257
|
+
instruction: str,
|
258
|
+
choices: List[T],
|
259
|
+
max_validations: PositiveInt = 2,
|
260
|
+
system_message: str = "",
|
261
|
+
**kwargs: Unpack[LLMKwargs],
|
262
|
+
) -> T:
|
263
|
+
"""Asynchronously picks a single choice from a list of options using AI validation.
|
264
|
+
|
265
|
+
This method is a convenience wrapper around `achoose` that always selects exactly one item.
|
266
|
+
|
267
|
+
Args:
|
268
|
+
instruction (str): The user-provided instruction/question description.
|
269
|
+
choices (List[T]): A list of candidate options, requiring elements to have `name` and `briefing` fields.
|
270
|
+
max_validations (PositiveInt): Maximum number of validation failures, default is 2.
|
271
|
+
system_message (str): Custom system-level prompt, defaults to an empty string.
|
272
|
+
**kwargs (Unpack[LLMKwargs]): Additional keyword arguments for the LLM usage, such as `model`,
|
273
|
+
`temperature`, `stop`, `top_p`, `max_tokens`, `stream`, `timeout`, and `max_retries`.
|
274
|
+
|
275
|
+
Returns:
|
276
|
+
T: The single selected item from the choices list.
|
277
|
+
|
278
|
+
Raises:
|
279
|
+
ValueError: If validation fails after maximum attempts or if no valid selection is made.
|
280
|
+
"""
|
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,
|
288
|
+
)[0]
|
289
|
+
|
235
290
|
async def ajudge(
|
236
291
|
self,
|
237
292
|
prompt: str,
|
fabricatio/parser.py
CHANGED
@@ -49,8 +49,12 @@ class Capture(BaseModel):
|
|
49
49
|
return None
|
50
50
|
|
51
51
|
if self.target_groups:
|
52
|
-
|
53
|
-
|
52
|
+
cap = tuple(match.group(g) for g in self.target_groups)
|
53
|
+
logger.debug(f"Captured text: {'\n\n'.join(cap)}")
|
54
|
+
return cap
|
55
|
+
cap = match.group(1)
|
56
|
+
logger.debug(f"Captured text: \n{cap}")
|
57
|
+
return cap
|
54
58
|
|
55
59
|
def convert_with[T](self, text: str, convertor: Callable[[Tuple[str, ...]], T] | Callable[[str], T]) -> T | None:
|
56
60
|
"""Convert the given text using the pattern.
|
@@ -62,12 +66,12 @@ class Capture(BaseModel):
|
|
62
66
|
Returns:
|
63
67
|
str | None: The converted text if the pattern is found, otherwise None.
|
64
68
|
"""
|
65
|
-
if cap := self.capture(text) is None:
|
69
|
+
if (cap := self.capture(text)) is None:
|
66
70
|
return None
|
67
71
|
try:
|
68
72
|
return convertor(cap)
|
69
73
|
except (ValueError, SyntaxError) as e:
|
70
|
-
logger.error(f"Failed to convert text using
|
74
|
+
logger.error(f"Failed to convert text using {convertor.__name__} to convert.\nerror: {e}\n {cap}")
|
71
75
|
return None
|
72
76
|
|
73
77
|
@classmethod
|
fabricatio/toolboxes/fs.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
"""File system tool box."""
|
2
2
|
|
3
|
-
from fabricatio.fs.curd import copy_file, create_directory, delete_directory, delete_file, move_file, tree
|
3
|
+
from fabricatio.fs.curd import copy_file, create_directory, delete_directory, delete_file, dump_text, move_file, tree
|
4
4
|
from fabricatio.models.tool import ToolBox
|
5
5
|
|
6
6
|
fs_toolbox = (
|
7
|
-
ToolBox(name="FsToolBox", description="A toolbox for file system operations.")
|
7
|
+
ToolBox(name="FsToolBox", description="A toolbox for basic file system operations.")
|
8
|
+
.add_tool(dump_text)
|
8
9
|
.add_tool(copy_file)
|
9
10
|
.add_tool(move_file)
|
10
11
|
.add_tool(delete_file)
|
Binary file
|
@@ -0,0 +1,420 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: fabricatio
|
3
|
+
Version: 0.2.1
|
4
|
+
Classifier: License :: OSI Approved :: MIT License
|
5
|
+
Classifier: Programming Language :: Rust
|
6
|
+
Classifier: Programming Language :: Python :: 3.12
|
7
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
8
|
+
Classifier: Framework :: AsyncIO
|
9
|
+
Classifier: Framework :: Pydantic :: 2
|
10
|
+
Classifier: Typing :: Typed
|
11
|
+
Requires-Dist: appdirs>=1.4.4
|
12
|
+
Requires-Dist: asyncio>=3.4.3
|
13
|
+
Requires-Dist: asyncstdlib>=3.13.0
|
14
|
+
Requires-Dist: code2prompt
|
15
|
+
Requires-Dist: gitpython>=3.1.44
|
16
|
+
Requires-Dist: litellm>=1.60.0
|
17
|
+
Requires-Dist: loguru>=0.7.3
|
18
|
+
Requires-Dist: magika>=0.5.1
|
19
|
+
Requires-Dist: orjson>=3.10.15
|
20
|
+
Requires-Dist: pydantic>=2.10.6
|
21
|
+
Requires-Dist: pydantic-settings>=2.7.1
|
22
|
+
Requires-Dist: pymitter>=1.0.0
|
23
|
+
Requires-Dist: questionary>=2.1.0
|
24
|
+
Requires-Dist: regex>=2024.11.6
|
25
|
+
Requires-Dist: rich>=13.9.4
|
26
|
+
Requires-Dist: faiss-cpu>=1.10.0 ; extra == 'rag'
|
27
|
+
Requires-Dist: pymilvus>=2.5.4 ; extra == 'rag'
|
28
|
+
Requires-Dist: fabricatio[rag] ; extra == 'full'
|
29
|
+
Provides-Extra: rag
|
30
|
+
Provides-Extra: full
|
31
|
+
License-File: LICENSE
|
32
|
+
Summary: A LLM multi-agent framework.
|
33
|
+
Keywords: ai,agents,multi-agent,llm,pyo3
|
34
|
+
Author-email: Whth <zettainspector@foxmail.com>
|
35
|
+
Requires-Python: >=3.12
|
36
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
37
|
+
Project-URL: Homepage, https://github.com/Whth/fabricatio
|
38
|
+
Project-URL: Repository, https://github.com/Whth/fabricatio
|
39
|
+
Project-URL: Issues, https://github.com/Whth/fabricatio/issues
|
40
|
+
|
41
|
+
|
42
|
+
# Fabricatio
|
43
|
+
|
44
|
+

|
45
|
+

|
46
|
+

|
47
|
+
|
48
|
+
Fabricatio is a Python library designed for building LLM (Large Language Model) applications using an event-based agent structure. It integrates Rust for performance-critical tasks, utilizes Handlebars for templating, and employs PyO3 for Python bindings.
|
49
|
+
|
50
|
+
## Features
|
51
|
+
|
52
|
+
- **Event-Based Architecture**: Utilizes an EventEmitter pattern for robust task management.
|
53
|
+
- **LLM Integration**: Supports interactions with large language models for intelligent task processing.
|
54
|
+
- **Templating Engine**: Uses Handlebars for dynamic content generation.
|
55
|
+
- **Toolboxes**: Provides predefined toolboxes for common operations like file manipulation and arithmetic.
|
56
|
+
- **Async Support**: Fully asynchronous for efficient execution.
|
57
|
+
- **Extensible**: Easy to extend with custom actions, workflows, and tools.
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
|
61
|
+
### Using UV (Recommended)
|
62
|
+
|
63
|
+
To install Fabricatio using `uv` (a package manager for Python):
|
64
|
+
|
65
|
+
```bash
|
66
|
+
# Install uv if not already installed
|
67
|
+
pip install uv
|
68
|
+
|
69
|
+
# Clone the repository
|
70
|
+
git clone https://github.com/Whth/fabricatio.git
|
71
|
+
cd fabricatio
|
72
|
+
|
73
|
+
# Install the package in development mode with uv
|
74
|
+
uv --with-editable . maturin develop --uv -r
|
75
|
+
```
|
76
|
+
|
77
|
+
### Building Distribution
|
78
|
+
|
79
|
+
For production builds:
|
80
|
+
|
81
|
+
```bash
|
82
|
+
# Build distribution packages
|
83
|
+
make bdist
|
84
|
+
```
|
85
|
+
|
86
|
+
This will generate distribution files in the `dist` directory.
|
87
|
+
|
88
|
+
## Usage
|
89
|
+
|
90
|
+
### Basic Example
|
91
|
+
|
92
|
+
Below are some basic examples demonstrating how to use Fabricatio for different purposes.
|
93
|
+
|
94
|
+
#### Simple Hello World Program
|
95
|
+
|
96
|
+
```python
|
97
|
+
import asyncio
|
98
|
+
from fabricatio import Action, Role, Task, logger
|
99
|
+
|
100
|
+
|
101
|
+
class Hello(Action):
|
102
|
+
"""Action that says hello."""
|
103
|
+
|
104
|
+
name: str = "hello"
|
105
|
+
output_key: str = "task_output"
|
106
|
+
|
107
|
+
async def _execute(self, task_input: Task[str], **_) -> Any:
|
108
|
+
ret = "Hello fabricatio!"
|
109
|
+
logger.info("executing talk action")
|
110
|
+
return ret
|
111
|
+
|
112
|
+
|
113
|
+
async def main() -> None:
|
114
|
+
"""Main function."""
|
115
|
+
role = Role(
|
116
|
+
name="talker",
|
117
|
+
description="talker role",
|
118
|
+
registry={Task.pending_label: WorkFlow(name="talk", steps=(Hello,))}
|
119
|
+
)
|
120
|
+
|
121
|
+
task = Task(name="say hello", goal="say hello", description="say hello to the world")
|
122
|
+
result = await task.delegate()
|
123
|
+
logger.success(f"Result: {result}")
|
124
|
+
|
125
|
+
|
126
|
+
if __name__ == "__main__":
|
127
|
+
asyncio.run(main())
|
128
|
+
```
|
129
|
+
|
130
|
+
#### Writing and Dumping Code
|
131
|
+
|
132
|
+
```python
|
133
|
+
import asyncio
|
134
|
+
from fabricatio import Action, Event, PythonCapture, Role, Task, ToolBox, WorkFlow, fs_toolbox, logger
|
135
|
+
|
136
|
+
|
137
|
+
class WriteCode(Action):
|
138
|
+
"""Action that writes code based on a prompt."""
|
139
|
+
|
140
|
+
name: str = "write code"
|
141
|
+
output_key: str = "source_code"
|
142
|
+
|
143
|
+
async def _execute(self, task_input: Task[str], **_) -> str:
|
144
|
+
return await self.aask_validate(
|
145
|
+
task_input.briefing,
|
146
|
+
validator=PythonCapture.capture,
|
147
|
+
)
|
148
|
+
|
149
|
+
|
150
|
+
class DumpCode(Action):
|
151
|
+
"""Action that dumps code to the file system."""
|
152
|
+
|
153
|
+
name: str = "dump code"
|
154
|
+
description: str = "Dump code to file system"
|
155
|
+
toolboxes: set[ToolBox] = {fs_toolbox}
|
156
|
+
output_key: str = "task_output"
|
157
|
+
|
158
|
+
async def _execute(self, task_input: Task, source_code: str, **_) -> Any:
|
159
|
+
path = await self.handle_fin_grind(task_input, {"source_code": source_code})
|
160
|
+
return path[0] if path else None
|
161
|
+
|
162
|
+
|
163
|
+
async def main() -> None:
|
164
|
+
"""Main function."""
|
165
|
+
role = Role(
|
166
|
+
name="Coder",
|
167
|
+
description="A python coder who can write and document code",
|
168
|
+
registry={
|
169
|
+
Event.instantiate_from("coding.*").push("pending"): WorkFlow(
|
170
|
+
name="write code", steps=(WriteCode, DumpCode)
|
171
|
+
),
|
172
|
+
},
|
173
|
+
)
|
174
|
+
|
175
|
+
prompt = "write a Python CLI app which prints 'hello world' n times with detailed Google-style docstring. Write the source code to `cli.py`."
|
176
|
+
|
177
|
+
proposed_task = await role.propose(prompt)
|
178
|
+
path = await proposed_task.move_to("coding").delegate()
|
179
|
+
logger.success(f"Code Path: {path}")
|
180
|
+
|
181
|
+
|
182
|
+
if __name__ == "__main__":
|
183
|
+
asyncio.run(main())
|
184
|
+
```
|
185
|
+
|
186
|
+
#### Proposing Tasks
|
187
|
+
|
188
|
+
```python
|
189
|
+
import asyncio
|
190
|
+
from typing import Any
|
191
|
+
|
192
|
+
from fabricatio import Action, Role, Task, WorkFlow, logger
|
193
|
+
|
194
|
+
|
195
|
+
class WriteDocumentation(Action):
|
196
|
+
"""Action that generates documentation for the code in markdown format."""
|
197
|
+
|
198
|
+
name: str = "write documentation"
|
199
|
+
description: str = "Write detailed documentation for the provided code."
|
200
|
+
output_key: str = "task_output"
|
201
|
+
|
202
|
+
async def _execute(self, task_input: Task[str], **_) -> str:
|
203
|
+
return await self.aask(task_input.briefing)
|
204
|
+
|
205
|
+
|
206
|
+
async def main() -> None:
|
207
|
+
"""Main function."""
|
208
|
+
role = Role(
|
209
|
+
name="Documenter",
|
210
|
+
description="Role responsible for writing documentation.",
|
211
|
+
registry={
|
212
|
+
"doc.*": WorkFlow(name="write documentation", steps=(WriteDocumentation,))
|
213
|
+
}
|
214
|
+
)
|
215
|
+
|
216
|
+
prompt = "write a Rust clap CLI that downloads an HTML page"
|
217
|
+
proposed_task = await role.propose(prompt)
|
218
|
+
documentation = await proposed_task.move_to("doc").delegate()
|
219
|
+
logger.success(f"Documentation:\n{documentation}")
|
220
|
+
|
221
|
+
|
222
|
+
if __name__ == "__main__":
|
223
|
+
asyncio.run(main())
|
224
|
+
```
|
225
|
+
|
226
|
+
#### Complex Workflow Handling
|
227
|
+
|
228
|
+
```python
|
229
|
+
import asyncio
|
230
|
+
from fabricatio import Action, Event, Role, Task, WorkFlow, logger
|
231
|
+
|
232
|
+
|
233
|
+
class WriteCode(Action):
|
234
|
+
"""Action that writes code based on a prompt."""
|
235
|
+
|
236
|
+
name: str = "write code"
|
237
|
+
output_key: str = "source_code"
|
238
|
+
|
239
|
+
async def _execute(self, task_input: Task[str], **_) -> str:
|
240
|
+
return await self.aask_validate(
|
241
|
+
task_input.briefing,
|
242
|
+
validator=PythonCapture.capture,
|
243
|
+
)
|
244
|
+
|
245
|
+
|
246
|
+
class WriteDocumentation(Action):
|
247
|
+
"""Action that generates documentation for the code in markdown format."""
|
248
|
+
|
249
|
+
name: str = "write documentation"
|
250
|
+
description: str = "Write detailed documentation for the provided code."
|
251
|
+
output_key: str = "task_output"
|
252
|
+
|
253
|
+
async def _execute(self, task_input: Task[str], **_) -> str:
|
254
|
+
return await self.aask(task_input.briefing)
|
255
|
+
|
256
|
+
|
257
|
+
async def main() -> None:
|
258
|
+
"""Main function."""
|
259
|
+
role = Role(
|
260
|
+
name="Developer",
|
261
|
+
description="A developer who can write code and documentation.",
|
262
|
+
registry={
|
263
|
+
Event.instantiate_from("coding.*").push("pending"): WorkFlow(
|
264
|
+
name="write code", steps=(WriteCode,)
|
265
|
+
),
|
266
|
+
Event.instantiate_from("doc.*").push("pending"): WorkFlow(
|
267
|
+
name="write documentation", steps=(WriteDocumentation,)
|
268
|
+
),
|
269
|
+
}
|
270
|
+
)
|
271
|
+
|
272
|
+
# Propose a coding task
|
273
|
+
code_task_prompt = "write a Python CLI app which adds numbers from a file."
|
274
|
+
proposed_task = await role.propose(code_task_prompt)
|
275
|
+
code = await proposed_task.move_to("coding").delegate()
|
276
|
+
logger.success(f"Code:\n{code}")
|
277
|
+
|
278
|
+
# Propose a documentation task
|
279
|
+
doc_task_prompt = f"{code}\n\nwrite Readme.md file for the above code."
|
280
|
+
proposed_doc_task = await role.propose(doc_task_prompt)
|
281
|
+
documentation = await proposed_doc_task.move_to("doc").delegate()
|
282
|
+
logger.success(f"Documentation:\n{documentation}")
|
283
|
+
|
284
|
+
|
285
|
+
if __name__ == "__main__":
|
286
|
+
asyncio.run(main())
|
287
|
+
```
|
288
|
+
|
289
|
+
### Advanced Examples
|
290
|
+
|
291
|
+
#### Template Management and Rendering
|
292
|
+
|
293
|
+
```python
|
294
|
+
from fabricatio._rust_instances import template_manager
|
295
|
+
|
296
|
+
template_name = "claude-xml.hbs"
|
297
|
+
data = {
|
298
|
+
"absolute_code_path": "/path/to/project",
|
299
|
+
"source_tree": "source tree content",
|
300
|
+
"files": [{"path": "file1.py", "code": "print('Hello')"}],
|
301
|
+
}
|
302
|
+
|
303
|
+
rendered_template = template_manager.render_template(template_name, data)
|
304
|
+
print(rendered_template)
|
305
|
+
```
|
306
|
+
|
307
|
+
#### Handling Security Vulnerabilities
|
308
|
+
|
309
|
+
```python
|
310
|
+
from fabricatio.models.usages import ToolBoxUsage
|
311
|
+
from fabricatio.models.task import Task
|
312
|
+
|
313
|
+
toolbox_usage = ToolBoxUsage()
|
314
|
+
|
315
|
+
async def handle_security_vulnerabilities():
|
316
|
+
task = Task(
|
317
|
+
name="Security Check",
|
318
|
+
goal=["Identify security vulnerabilities"],
|
319
|
+
description="Perform a thorough security review on the project.",
|
320
|
+
dependencies=["./src/main.py"]
|
321
|
+
)
|
322
|
+
|
323
|
+
vulnerabilities = await toolbox_usage.gather_tools_fine_grind(task)
|
324
|
+
for vulnerability in vulnerabilities:
|
325
|
+
print(f"Found vulnerability: {vulnerability.name}")
|
326
|
+
```
|
327
|
+
|
328
|
+
#### Managing CTF Challenges
|
329
|
+
|
330
|
+
```python
|
331
|
+
import asyncio
|
332
|
+
|
333
|
+
from fabricatio.models.usages import ToolBoxUsage
|
334
|
+
from fabricatio.models.task import Task
|
335
|
+
|
336
|
+
toolbox_usage = ToolBoxUsage()
|
337
|
+
|
338
|
+
async def solve_ctf_challenge(challenge_name: str, challenge_description: str, files: list[str]):
|
339
|
+
task = Task(
|
340
|
+
name=challenge_name,
|
341
|
+
goal=[f"Solve {challenge_name} challenge"],
|
342
|
+
description=challenge_description,
|
343
|
+
dependencies=files
|
344
|
+
)
|
345
|
+
|
346
|
+
solution = await toolbox_usage.gather_tools_fine_grind(task)
|
347
|
+
print(f"Challenge Solved: {solution}")
|
348
|
+
|
349
|
+
if __name__ == "__main__":
|
350
|
+
asyncio.run(solve_ctf_challenge("Binary Exploitation", "CTF Binary Exploitation Challenge", ["./challenges/binary_exploit"]))
|
351
|
+
```
|
352
|
+
|
353
|
+
### Configuration
|
354
|
+
|
355
|
+
The configuration for Fabricatio is managed via environment variables or TOML files. The default configuration file (`config.toml`) can be overridden by specifying a custom path.
|
356
|
+
|
357
|
+
Example `config.toml`:
|
358
|
+
|
359
|
+
```toml
|
360
|
+
[llm]
|
361
|
+
api_endpoint = "https://api.openai.com"
|
362
|
+
api_key = "your_openai_api_key"
|
363
|
+
timeout = 300
|
364
|
+
max_retries = 3
|
365
|
+
model = "gpt-3.5-turbo"
|
366
|
+
temperature = 1.0
|
367
|
+
stop_sign = ["\n\n\n", "User:"]
|
368
|
+
top_p = 0.35
|
369
|
+
generation_count = 1
|
370
|
+
stream = false
|
371
|
+
max_tokens = 8192
|
372
|
+
```
|
373
|
+
|
374
|
+
### Development Setup
|
375
|
+
|
376
|
+
To set up a development environment for Fabricatio:
|
377
|
+
|
378
|
+
1. **Clone the Repository**:
|
379
|
+
```bash
|
380
|
+
git clone https://github.com/Whth/fabricatio.git
|
381
|
+
cd fabricatio
|
382
|
+
```
|
383
|
+
|
384
|
+
2. **Install Dependencies**:
|
385
|
+
```bash
|
386
|
+
uv --with-editable . maturin develop --uv -r
|
387
|
+
```
|
388
|
+
|
389
|
+
3. **Run Tests**:
|
390
|
+
```bash
|
391
|
+
make test
|
392
|
+
```
|
393
|
+
|
394
|
+
4. **Build Documentation**:
|
395
|
+
```bash
|
396
|
+
make docs
|
397
|
+
```
|
398
|
+
|
399
|
+
### Contributing
|
400
|
+
|
401
|
+
Contributions are welcome! Please follow these guidelines when contributing:
|
402
|
+
|
403
|
+
1. Fork the repository.
|
404
|
+
2. Create your feature branch (`git checkout -b feature/new-feature`).
|
405
|
+
3. Commit your changes (`git commit -am 'Add new feature'`).
|
406
|
+
4. Push to the branch (`git push origin feature/new-feature`).
|
407
|
+
5. Create a new Pull Request.
|
408
|
+
|
409
|
+
### License
|
410
|
+
|
411
|
+
Fabricatio is licensed under the MIT License. See [LICENSE](LICENSE) for more details.
|
412
|
+
|
413
|
+
### Acknowledgments
|
414
|
+
|
415
|
+
Special thanks to the contributors and maintainers of:
|
416
|
+
- [PyO3](https://github.com/PyO3/pyo3)
|
417
|
+
- [Maturin](https://github.com/PyO3/maturin)
|
418
|
+
- [Handlebars.rs](https://github.com/sunng87/handlebars-rust)
|
419
|
+
|
420
|
+
|