fabricatio 0.2.8.dev1__cp312-cp312-win_amd64.whl → 0.2.8.dev3__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 +50 -0
- fabricatio/actions/article.py +103 -65
- fabricatio/actions/article_rag.py +73 -19
- fabricatio/actions/output.py +39 -6
- fabricatio/actions/rag.py +3 -3
- fabricatio/capabilities/check.py +97 -0
- fabricatio/capabilities/correct.py +7 -6
- fabricatio/capabilities/propose.py +20 -4
- fabricatio/capabilities/rag.py +3 -2
- fabricatio/capabilities/rating.py +7 -10
- fabricatio/capabilities/review.py +18 -187
- fabricatio/capabilities/task.py +8 -9
- fabricatio/config.py +2 -0
- fabricatio/fs/curd.py +4 -0
- fabricatio/models/action.py +10 -5
- fabricatio/models/extra/advanced_judge.py +16 -9
- fabricatio/models/extra/article_base.py +53 -10
- fabricatio/models/extra/article_essence.py +47 -171
- fabricatio/models/extra/article_main.py +6 -1
- fabricatio/models/extra/article_proposal.py +19 -1
- fabricatio/models/extra/problem.py +120 -0
- fabricatio/models/extra/rule.py +23 -0
- fabricatio/models/generic.py +50 -42
- fabricatio/models/role.py +4 -1
- fabricatio/models/usages.py +8 -6
- fabricatio/models/utils.py +0 -46
- fabricatio/utils.py +54 -0
- fabricatio-0.2.8.dev3.data/scripts/tdown.exe +0 -0
- {fabricatio-0.2.8.dev1.dist-info → fabricatio-0.2.8.dev3.dist-info}/METADATA +2 -1
- fabricatio-0.2.8.dev3.dist-info/RECORD +53 -0
- fabricatio-0.2.8.dev1.data/scripts/tdown.exe +0 -0
- fabricatio-0.2.8.dev1.dist-info/RECORD +0 -49
- {fabricatio-0.2.8.dev1.dist-info → fabricatio-0.2.8.dev3.dist-info}/WHEEL +0 -0
- {fabricatio-0.2.8.dev1.dist-info → fabricatio-0.2.8.dev3.dist-info}/licenses/LICENSE +0 -0
@@ -8,8 +8,9 @@ based on predefined criteria and templates.
|
|
8
8
|
from typing import Optional, Unpack, cast
|
9
9
|
|
10
10
|
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
11
|
-
from fabricatio.capabilities.review import Review
|
11
|
+
from fabricatio.capabilities.review import Review
|
12
12
|
from fabricatio.config import configs
|
13
|
+
from fabricatio.models.extra.problem import Improvement
|
13
14
|
from fabricatio.models.generic import CensoredAble, Display, ProposedAble, ProposedUpdateAble, WithBriefing
|
14
15
|
from fabricatio.models.kwargs_types import CensoredCorrectKwargs, CorrectKwargs, ReviewKwargs
|
15
16
|
from fabricatio.models.task import Task
|
@@ -31,7 +32,7 @@ class Correct(Review):
|
|
31
32
|
obj: M,
|
32
33
|
reference: str = "",
|
33
34
|
supervisor_check: bool = True,
|
34
|
-
**kwargs: Unpack[ReviewKwargs[
|
35
|
+
**kwargs: Unpack[ReviewKwargs[Improvement]],
|
35
36
|
) -> Optional[M]:
|
36
37
|
"""Review and correct an object based on defined criteria and templates.
|
37
38
|
|
@@ -71,7 +72,7 @@ class Correct(Review):
|
|
71
72
|
)
|
72
73
|
|
73
74
|
async def correct_string(
|
74
|
-
self, input_text: str, supervisor_check: bool = True, **kwargs: Unpack[ReviewKwargs[
|
75
|
+
self, input_text: str, supervisor_check: bool = True, **kwargs: Unpack[ReviewKwargs[Improvement]]
|
75
76
|
) -> Optional[str]:
|
76
77
|
"""Review and correct a string based on defined criteria and templates.
|
77
78
|
|
@@ -100,7 +101,7 @@ class Correct(Review):
|
|
100
101
|
)
|
101
102
|
|
102
103
|
async def correct_task[T](
|
103
|
-
self, task: Task[T], **kwargs: Unpack[CorrectKwargs[
|
104
|
+
self, task: Task[T], **kwargs: Unpack[CorrectKwargs[Improvement]]
|
104
105
|
) -> Optional[Task[T]]:
|
105
106
|
"""Review and correct a task object based on defined criteria.
|
106
107
|
|
@@ -117,7 +118,7 @@ class Correct(Review):
|
|
117
118
|
return await self.correct_obj(task, **kwargs)
|
118
119
|
|
119
120
|
async def censor_obj[M: CensoredAble](
|
120
|
-
self, obj: M, **kwargs: Unpack[CensoredCorrectKwargs[
|
121
|
+
self, obj: M, **kwargs: Unpack[CensoredCorrectKwargs[Improvement]]
|
121
122
|
) -> M:
|
122
123
|
"""Censor and correct an object based on defined criteria and templates.
|
123
124
|
|
@@ -147,7 +148,7 @@ class Correct(Review):
|
|
147
148
|
return modified_obj or last_modified_obj
|
148
149
|
|
149
150
|
async def correct_obj_inplace[M: ProposedUpdateAble](
|
150
|
-
self, obj: M, **kwargs: Unpack[CorrectKwargs[
|
151
|
+
self, obj: M, **kwargs: Unpack[CorrectKwargs[Improvement]]
|
151
152
|
) -> Optional[M]:
|
152
153
|
"""Correct an object in place based on defined criteria and templates.
|
153
154
|
|
@@ -10,28 +10,43 @@ from fabricatio.models.usages import LLMUsage
|
|
10
10
|
class Propose(LLMUsage):
|
11
11
|
"""A class that proposes an Obj based on a prompt."""
|
12
12
|
|
13
|
+
@overload
|
14
|
+
async def propose[M: ProposedAble](
|
15
|
+
self,
|
16
|
+
cls: Type[M],
|
17
|
+
prompt: List[str],
|
18
|
+
**kwargs: Unpack[ValidateKwargs[None]],
|
19
|
+
) -> List[Optional[M]]: ...
|
20
|
+
|
13
21
|
@overload
|
14
22
|
async def propose[M: ProposedAble](
|
15
23
|
self,
|
16
24
|
cls: Type[M],
|
17
25
|
prompt: List[str],
|
18
26
|
**kwargs: Unpack[ValidateKwargs[M]],
|
19
|
-
) ->
|
27
|
+
) -> List[M]: ...
|
20
28
|
|
21
29
|
@overload
|
22
30
|
async def propose[M: ProposedAble](
|
23
31
|
self,
|
24
32
|
cls: Type[M],
|
25
33
|
prompt: str,
|
26
|
-
**kwargs: Unpack[ValidateKwargs[
|
34
|
+
**kwargs: Unpack[ValidateKwargs[None]],
|
27
35
|
) -> Optional[M]: ...
|
36
|
+
@overload
|
37
|
+
async def propose[M: ProposedAble](
|
38
|
+
self,
|
39
|
+
cls: Type[M],
|
40
|
+
prompt: str,
|
41
|
+
**kwargs: Unpack[ValidateKwargs[M]],
|
42
|
+
) -> M: ...
|
28
43
|
|
29
44
|
async def propose[M: ProposedAble](
|
30
45
|
self,
|
31
46
|
cls: Type[M],
|
32
47
|
prompt: List[str] | str,
|
33
|
-
**kwargs: Unpack[ValidateKwargs[M]],
|
34
|
-
) -> Optional[List[M] | M]:
|
48
|
+
**kwargs: Unpack[ValidateKwargs[Optional[M]]],
|
49
|
+
) -> Optional[M] | List[Optional[M]] | M | List[M]:
|
35
50
|
"""Asynchronously proposes a task based on a given prompt and parameters.
|
36
51
|
|
37
52
|
Parameters:
|
@@ -47,3 +62,4 @@ class Propose(LLMUsage):
|
|
47
62
|
validator=cls.instantiate_from_string,
|
48
63
|
**kwargs,
|
49
64
|
)
|
65
|
+
|
fabricatio/capabilities/rag.py
CHANGED
@@ -22,7 +22,8 @@ from fabricatio.models.kwargs_types import (
|
|
22
22
|
RetrievalKwargs,
|
23
23
|
)
|
24
24
|
from fabricatio.models.usages import EmbeddingUsage
|
25
|
-
from fabricatio.models.utils import MilvusData
|
25
|
+
from fabricatio.models.utils import MilvusData
|
26
|
+
from fabricatio.utils import ok
|
26
27
|
from more_itertools.recipes import flatten, unique
|
27
28
|
from pydantic import Field, PrivateAttr
|
28
29
|
|
@@ -376,7 +377,7 @@ class RAG(EmbeddingUsage):
|
|
376
377
|
Returns:
|
377
378
|
List[str]: A list of refined questions.
|
378
379
|
"""
|
379
|
-
return await self.
|
380
|
+
return await self.alist_str(
|
380
381
|
TEMPLATE_MANAGER.render_template(
|
381
382
|
configs.templates.refined_query_template,
|
382
383
|
{"question": [question] if isinstance(question, str) else question},
|
@@ -7,16 +7,15 @@ from typing import Dict, List, Optional, Set, Tuple, Union, Unpack, overload
|
|
7
7
|
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
8
8
|
from fabricatio.config import configs
|
9
9
|
from fabricatio.journal import logger
|
10
|
-
from fabricatio.models.generic import WithBriefing
|
11
10
|
from fabricatio.models.kwargs_types import ValidateKwargs
|
12
11
|
from fabricatio.models.usages import LLMUsage
|
13
|
-
from fabricatio.models.utils import override_kwargs
|
14
12
|
from fabricatio.parser import JsonCapture
|
13
|
+
from fabricatio.utils import override_kwargs
|
15
14
|
from more_itertools import flatten, windowed
|
16
15
|
from pydantic import NonNegativeInt, PositiveInt
|
17
16
|
|
18
17
|
|
19
|
-
class Rating(
|
18
|
+
class Rating(LLMUsage):
|
20
19
|
"""A class that provides functionality to rate tasks based on a rating manual and score range.
|
21
20
|
|
22
21
|
References:
|
@@ -149,9 +148,7 @@ class Rating(WithBriefing, LLMUsage):
|
|
149
148
|
return json_data
|
150
149
|
return None
|
151
150
|
|
152
|
-
criteria = criteria or await self.draft_rating_criteria(
|
153
|
-
topic, **self.prepend_sys_msg(override_kwargs(dict(kwargs), default=None))
|
154
|
-
)
|
151
|
+
criteria = criteria or await self.draft_rating_criteria(topic, **override_kwargs(dict(kwargs), default=None))
|
155
152
|
|
156
153
|
if criteria is None:
|
157
154
|
logger.error(f"Failed to draft rating criteria for topic {topic}")
|
@@ -168,7 +165,7 @@ class Rating(WithBriefing, LLMUsage):
|
|
168
165
|
)
|
169
166
|
),
|
170
167
|
validator=_validator,
|
171
|
-
**
|
168
|
+
**kwargs,
|
172
169
|
)
|
173
170
|
|
174
171
|
async def draft_rating_criteria(
|
@@ -200,7 +197,7 @@ class Rating(WithBriefing, LLMUsage):
|
|
200
197
|
validator=lambda resp: set(out)
|
201
198
|
if (out := JsonCapture.validate_with(resp, list, str, criteria_count)) is not None
|
202
199
|
else out,
|
203
|
-
**
|
200
|
+
**kwargs,
|
204
201
|
)
|
205
202
|
|
206
203
|
async def draft_rating_criteria_from_examples(
|
@@ -253,7 +250,7 @@ class Rating(WithBriefing, LLMUsage):
|
|
253
250
|
validator=lambda resp: JsonCapture.validate_with(
|
254
251
|
resp, target_type=list, elements_type=str, length=reasons_count
|
255
252
|
),
|
256
|
-
**
|
253
|
+
**kwargs,
|
257
254
|
)
|
258
255
|
)
|
259
256
|
# extract certain mount of criteria from reasons according to their importance and frequency
|
@@ -310,7 +307,7 @@ class Rating(WithBriefing, LLMUsage):
|
|
310
307
|
for pair in windows
|
311
308
|
],
|
312
309
|
validator=lambda resp: JsonCapture.validate_with(resp, target_type=float),
|
313
|
-
**
|
310
|
+
**kwargs,
|
314
311
|
)
|
315
312
|
weights = [1]
|
316
313
|
for rw in relative_weights:
|
@@ -1,178 +1,16 @@
|
|
1
1
|
"""A module that provides functionality to rate tasks based on a rating manual and score range."""
|
2
2
|
|
3
|
-
from typing import Dict,
|
3
|
+
from typing import Dict, Optional, Set, Unpack
|
4
4
|
|
5
5
|
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
6
6
|
from fabricatio.capabilities.propose import Propose
|
7
7
|
from fabricatio.capabilities.rating import Rating
|
8
8
|
from fabricatio.config import configs
|
9
|
-
from fabricatio.models.
|
9
|
+
from fabricatio.models.extra.problem import Improvement
|
10
|
+
from fabricatio.models.generic import Display, WithBriefing
|
10
11
|
from fabricatio.models.kwargs_types import ReviewKwargs, ValidateKwargs
|
11
12
|
from fabricatio.models.task import Task
|
12
|
-
from fabricatio.
|
13
|
-
from questionary import Choice, checkbox, text
|
14
|
-
from questionary import print as q_print
|
15
|
-
from rich import print as r_print
|
16
|
-
|
17
|
-
|
18
|
-
class ProblemSolutions(Base):
|
19
|
-
"""Represents a problem-solution pair identified during a review process.
|
20
|
-
|
21
|
-
This class encapsulates a single problem with its corresponding potential solutions,
|
22
|
-
providing a structured way to manage review findings.
|
23
|
-
|
24
|
-
Attributes:
|
25
|
-
problem (str): The problem statement identified during review.
|
26
|
-
solutions (List[str]): A collection of potential solutions to address the problem.
|
27
|
-
"""
|
28
|
-
|
29
|
-
problem: str
|
30
|
-
"""The problem identified in the review."""
|
31
|
-
solutions: List[str]
|
32
|
-
"""A collection of potential solutions to address the problem."""
|
33
|
-
|
34
|
-
def update_problem(self, problem: str) -> Self:
|
35
|
-
"""Update the problem description.
|
36
|
-
|
37
|
-
Args:
|
38
|
-
problem (str): The new problem description to replace the current one.
|
39
|
-
|
40
|
-
Returns:
|
41
|
-
Self: The current instance with updated problem description.
|
42
|
-
"""
|
43
|
-
self.problem = problem
|
44
|
-
return self
|
45
|
-
|
46
|
-
def update_solutions(self, solutions: List[str]) -> Self:
|
47
|
-
"""Update the list of potential solutions.
|
48
|
-
|
49
|
-
Args:
|
50
|
-
solutions (List[str]): The new collection of solutions to replace the current ones.
|
51
|
-
|
52
|
-
Returns:
|
53
|
-
Self: The current instance with updated solutions.
|
54
|
-
"""
|
55
|
-
self.solutions = solutions
|
56
|
-
return self
|
57
|
-
|
58
|
-
async def edit_problem(self) -> Self:
|
59
|
-
"""Interactively edit the problem description using a prompt.
|
60
|
-
|
61
|
-
Returns:
|
62
|
-
Self: The current instance with updated problem description.
|
63
|
-
"""
|
64
|
-
self.problem = await text("Please edit the problem below:", default=self.problem).ask_async()
|
65
|
-
return self
|
66
|
-
|
67
|
-
async def edit_solutions(self) -> Self:
|
68
|
-
"""Interactively edit the list of potential solutions using a prompt.
|
69
|
-
|
70
|
-
Returns:
|
71
|
-
Self: The current instance with updated solutions.
|
72
|
-
"""
|
73
|
-
q_print(self.problem, style="bold cyan")
|
74
|
-
self.solutions = await ask_edit(self.solutions)
|
75
|
-
return self
|
76
|
-
|
77
|
-
|
78
|
-
class ReviewResult[T](ProposedAble, Display):
|
79
|
-
"""Represents the outcome of a review process with identified problems and solutions.
|
80
|
-
|
81
|
-
This class maintains a structured collection of problems found during a review,
|
82
|
-
their proposed solutions, and a reference to the original reviewed object.
|
83
|
-
|
84
|
-
Attributes:
|
85
|
-
review_topic (str): The subject or focus area of the review.
|
86
|
-
problem_solutions (List[ProblemSolutions]): Collection of problems identified
|
87
|
-
during review along with their potential solutions.
|
88
|
-
|
89
|
-
Type Parameters:
|
90
|
-
T: The type of the object being reviewed.
|
91
|
-
"""
|
92
|
-
|
93
|
-
review_topic: str
|
94
|
-
"""The subject or focus area of the review."""
|
95
|
-
|
96
|
-
problem_solutions: List[ProblemSolutions]
|
97
|
-
"""Collection of problems identified during review along with their potential solutions."""
|
98
|
-
|
99
|
-
_ref: T
|
100
|
-
"""Reference to the original object that was reviewed."""
|
101
|
-
|
102
|
-
def update_topic(self, topic: str) -> Self:
|
103
|
-
"""Update the review topic.
|
104
|
-
|
105
|
-
Args:
|
106
|
-
topic (str): The new topic to be associated with this review.
|
107
|
-
|
108
|
-
Returns:
|
109
|
-
Self: The current instance with updated review topic.
|
110
|
-
"""
|
111
|
-
self.review_topic = topic
|
112
|
-
return self
|
113
|
-
|
114
|
-
def update_ref[K](self, ref: K) -> "ReviewResult[K]":
|
115
|
-
"""Update the reference to the reviewed object.
|
116
|
-
|
117
|
-
Args:
|
118
|
-
ref (K): The new reference object to be associated with this review.
|
119
|
-
|
120
|
-
Returns:
|
121
|
-
ReviewResult[K]: The current instance with updated reference type.
|
122
|
-
"""
|
123
|
-
self._ref = ref # pyright: ignore [reportAttributeAccessIssue]
|
124
|
-
return cast("ReviewResult[K]", self)
|
125
|
-
|
126
|
-
def deref(self) -> T:
|
127
|
-
"""Retrieve the referenced object that was reviewed.
|
128
|
-
|
129
|
-
Returns:
|
130
|
-
T: The original object that was reviewed.
|
131
|
-
"""
|
132
|
-
return self._ref
|
133
|
-
|
134
|
-
async def supervisor_check(self, check_solutions: bool = True) -> Self:
|
135
|
-
"""Perform an interactive review session to filter problems and solutions.
|
136
|
-
|
137
|
-
Presents an interactive prompt allowing a supervisor to select which
|
138
|
-
problems (and optionally solutions) should be retained in the final review.
|
139
|
-
|
140
|
-
Args:
|
141
|
-
check_solutions (bool, optional): When True, also prompts for filtering
|
142
|
-
individual solutions for each retained problem. Defaults to False.
|
143
|
-
|
144
|
-
Returns:
|
145
|
-
Self: The current instance with filtered problems and solutions.
|
146
|
-
"""
|
147
|
-
if isinstance(self._ref, str):
|
148
|
-
display = self._ref
|
149
|
-
elif isinstance(self._ref, WithBriefing):
|
150
|
-
display = self._ref.briefing
|
151
|
-
elif isinstance(self._ref, Display):
|
152
|
-
display = self._ref.display()
|
153
|
-
else:
|
154
|
-
raise TypeError(f"Unsupported type for review: {type(self._ref)}")
|
155
|
-
# Choose the problems to retain
|
156
|
-
r_print(display)
|
157
|
-
chosen_ones: List[ProblemSolutions] = await checkbox(
|
158
|
-
f"Please choose the problems you want to retain.(Default: retain all)\n\t`{self.review_topic}`",
|
159
|
-
choices=[Choice(p.problem, p, checked=True) for p in self.problem_solutions],
|
160
|
-
).ask_async()
|
161
|
-
self.problem_solutions = [await p.edit_problem() for p in chosen_ones]
|
162
|
-
if not check_solutions:
|
163
|
-
return self
|
164
|
-
|
165
|
-
# Choose the solutions to retain
|
166
|
-
for to_exam in self.problem_solutions:
|
167
|
-
to_exam.update_solutions(
|
168
|
-
await checkbox(
|
169
|
-
f"Please choose the solutions you want to retain.(Default: retain all)\n\t`{to_exam.problem}`",
|
170
|
-
choices=[Choice(s, s, checked=True) for s in to_exam.solutions],
|
171
|
-
).ask_async()
|
172
|
-
)
|
173
|
-
await to_exam.edit_solutions()
|
174
|
-
|
175
|
-
return self
|
13
|
+
from fabricatio.utils import ok
|
176
14
|
|
177
15
|
|
178
16
|
class Review(Rating, Propose):
|
@@ -185,7 +23,7 @@ class Review(Rating, Propose):
|
|
185
23
|
appropriate topic and criteria.
|
186
24
|
"""
|
187
25
|
|
188
|
-
async def review_task[T](self, task: Task[T], **kwargs: Unpack[ReviewKwargs]) ->
|
26
|
+
async def review_task[T](self, task: Task[T], **kwargs: Unpack[ReviewKwargs]) -> Optional[Improvement]:
|
189
27
|
"""Review a task using specified review criteria.
|
190
28
|
|
191
29
|
This method analyzes a task object to identify problems and propose solutions
|
@@ -197,10 +35,10 @@ class Review(Rating, Propose):
|
|
197
35
|
including topic and optional criteria.
|
198
36
|
|
199
37
|
Returns:
|
200
|
-
|
38
|
+
Improvement[Task[T]]: A review result containing identified problems and proposed solutions,
|
201
39
|
with a reference to the original task.
|
202
40
|
"""
|
203
|
-
return
|
41
|
+
return await self.review_obj(task, **kwargs)
|
204
42
|
|
205
43
|
async def review_string(
|
206
44
|
self,
|
@@ -208,8 +46,8 @@ class Review(Rating, Propose):
|
|
208
46
|
topic: str,
|
209
47
|
criteria: Optional[Set[str]] = None,
|
210
48
|
rating_manual: Optional[Dict[str, str]] = None,
|
211
|
-
**kwargs: Unpack[ValidateKwargs[
|
212
|
-
) ->
|
49
|
+
**kwargs: Unpack[ValidateKwargs[Improvement]],
|
50
|
+
) -> Optional[Improvement]:
|
213
51
|
"""Review a string based on specified topic and criteria.
|
214
52
|
|
215
53
|
This method analyzes a text string to identify problems and propose solutions
|
@@ -224,7 +62,7 @@ class Review(Rating, Propose):
|
|
224
62
|
**kwargs (Unpack[ValidateKwargs]): Additional keyword arguments for the LLM usage.
|
225
63
|
|
226
64
|
Returns:
|
227
|
-
|
65
|
+
Improvement: A review result containing identified problems and proposed solutions,
|
228
66
|
with a reference to the original text.
|
229
67
|
"""
|
230
68
|
default = None
|
@@ -232,28 +70,21 @@ class Review(Rating, Propose):
|
|
232
70
|
# this `default` is the default for the `propose` method
|
233
71
|
default = kwargs.pop("default")
|
234
72
|
|
235
|
-
criteria = criteria or (await self.draft_rating_criteria(topic, **kwargs))
|
236
|
-
if not criteria:
|
237
|
-
raise ValueError("No criteria provided for review.")
|
73
|
+
criteria = ok(criteria or (await self.draft_rating_criteria(topic, **kwargs))," No criteria could be use.")
|
238
74
|
manual = rating_manual or await self.draft_rating_manual(topic, criteria, **kwargs)
|
239
75
|
|
240
76
|
if default is not None:
|
241
77
|
kwargs["default"] = default
|
242
|
-
|
243
|
-
|
78
|
+
return await self.propose(
|
79
|
+
Improvement,
|
244
80
|
TEMPLATE_MANAGER.render_template(
|
245
81
|
configs.templates.review_string_template,
|
246
82
|
{"text": input_text, "topic": topic, "criteria_manual": manual},
|
247
83
|
),
|
248
84
|
**kwargs,
|
249
85
|
)
|
250
|
-
if not res:
|
251
|
-
raise ValueError("Failed to generate review result.")
|
252
|
-
return res.update_ref(input_text).update_topic(topic)
|
253
86
|
|
254
|
-
async def review_obj[M: (Display, WithBriefing)](
|
255
|
-
self, obj: M, **kwargs: Unpack[ReviewKwargs[ReviewResult[str]]]
|
256
|
-
) -> ReviewResult[M]:
|
87
|
+
async def review_obj[M: (Display, WithBriefing)](self, obj: M, **kwargs: Unpack[ReviewKwargs[Improvement]]) -> Optional[Improvement]:
|
257
88
|
"""Review an object that implements Display or WithBriefing interface.
|
258
89
|
|
259
90
|
This method extracts displayable text from the object and performs a review
|
@@ -268,14 +99,14 @@ class Review(Rating, Propose):
|
|
268
99
|
TypeError: If the object does not implement Display or WithBriefing.
|
269
100
|
|
270
101
|
Returns:
|
271
|
-
|
102
|
+
Improvement: A review result containing identified problems and proposed solutions,
|
272
103
|
with a reference to the original object.
|
273
104
|
"""
|
274
105
|
if isinstance(obj, Display):
|
275
|
-
|
106
|
+
text_to_review = obj.display()
|
276
107
|
elif isinstance(obj, WithBriefing):
|
277
|
-
|
108
|
+
text_to_review = obj.briefing
|
278
109
|
else:
|
279
110
|
raise TypeError(f"Unsupported type for review: {type(obj)}")
|
280
111
|
|
281
|
-
return
|
112
|
+
return await self.review_string(text_to_review, **kwargs)
|
fabricatio/capabilities/task.py
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
"""A module for the task capabilities of the Fabricatio library."""
|
2
2
|
|
3
3
|
from types import CodeType
|
4
|
-
from typing import Any, Dict, List, Optional, Tuple, Unpack
|
4
|
+
from typing import Any, Dict, List, Optional, Tuple, Unpack
|
5
5
|
|
6
6
|
import orjson
|
7
7
|
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
8
8
|
from fabricatio.capabilities.propose import Propose
|
9
9
|
from fabricatio.config import configs
|
10
10
|
from fabricatio.journal import logger
|
11
|
-
from fabricatio.models.generic import WithBriefing
|
12
11
|
from fabricatio.models.kwargs_types import ChooseKwargs, ValidateKwargs
|
13
12
|
from fabricatio.models.task import Task
|
14
13
|
from fabricatio.models.tool import Tool, ToolExecutor
|
@@ -16,7 +15,7 @@ from fabricatio.models.usages import ToolBoxUsage
|
|
16
15
|
from fabricatio.parser import JsonCapture, PythonCapture
|
17
16
|
|
18
17
|
|
19
|
-
class ProposeTask(
|
18
|
+
class ProposeTask(Propose):
|
20
19
|
"""A class that proposes a task based on a prompt."""
|
21
20
|
|
22
21
|
async def propose_task[T](
|
@@ -34,13 +33,13 @@ class ProposeTask(WithBriefing, Propose):
|
|
34
33
|
A Task object based on the proposal result.
|
35
34
|
"""
|
36
35
|
if not prompt:
|
37
|
-
logger.error(err :=
|
36
|
+
logger.error(err := "Prompt must be provided.")
|
38
37
|
raise ValueError(err)
|
39
38
|
|
40
|
-
return await self.propose(Task, prompt, **
|
39
|
+
return await self.propose(Task, prompt, **kwargs)
|
41
40
|
|
42
41
|
|
43
|
-
class HandleTask(
|
42
|
+
class HandleTask(ToolBoxUsage):
|
44
43
|
"""A class that handles a task based on a task object."""
|
45
44
|
|
46
45
|
async def draft_tool_usage_code(
|
@@ -54,7 +53,7 @@ class HandleTask(WithBriefing, ToolBoxUsage):
|
|
54
53
|
logger.info(f"Drafting tool usage code for task: {task.briefing}")
|
55
54
|
|
56
55
|
if not tools:
|
57
|
-
err =
|
56
|
+
err = "Tools must be provided to draft the tool usage code."
|
58
57
|
logger.error(err)
|
59
58
|
raise ValueError(err)
|
60
59
|
|
@@ -81,7 +80,7 @@ class HandleTask(WithBriefing, ToolBoxUsage):
|
|
81
80
|
return await self.aask_validate(
|
82
81
|
question=q,
|
83
82
|
validator=_validator,
|
84
|
-
**
|
83
|
+
**kwargs,
|
85
84
|
)
|
86
85
|
|
87
86
|
async def handle_fine_grind(
|
@@ -96,7 +95,7 @@ class HandleTask(WithBriefing, ToolBoxUsage):
|
|
96
95
|
logger.info(f"Handling task: \n{task.briefing}")
|
97
96
|
|
98
97
|
tools = await self.gather_tools_fine_grind(task, box_choose_kwargs, tool_choose_kwargs)
|
99
|
-
logger.info(f"
|
98
|
+
logger.info(f"Gathered {[t.name for t in tools]}")
|
100
99
|
|
101
100
|
if tools and (pack := await self.draft_tool_usage_code(task, tools, data, **kwargs)):
|
102
101
|
executor = ToolExecutor(candidates=tools, data=data)
|
fabricatio/config.py
CHANGED
@@ -238,6 +238,8 @@ class TemplateConfig(BaseModel):
|
|
238
238
|
as_prompt_template: str = Field(default="as_prompt")
|
239
239
|
"""The name of the as prompt template which will be used to convert a string to a prompt."""
|
240
240
|
|
241
|
+
check_string_template: str = Field(default="check_string")
|
242
|
+
"""The name of the check string template which will be used to check a string."""
|
241
243
|
|
242
244
|
class MagikaConfig(BaseModel):
|
243
245
|
"""Magika configuration class."""
|
fabricatio/fs/curd.py
CHANGED
@@ -145,5 +145,9 @@ def gather_files(directory: str | Path | PathLike, extension: str) -> list[str]:
|
|
145
145
|
|
146
146
|
Returns:
|
147
147
|
list[str]: A list of file paths with the specified extension.
|
148
|
+
|
149
|
+
Example:
|
150
|
+
>>> gather_files('/path/to/directory', 'txt')
|
151
|
+
['/path/to/directory/file1.txt', '/path/to/directory/file2.txt']
|
148
152
|
"""
|
149
153
|
return [file.as_posix() for file in Path(directory).rglob(f"*.{extension}")]
|
fabricatio/models/action.py
CHANGED
@@ -18,7 +18,7 @@ from fabricatio.models.usages import ToolBoxUsage
|
|
18
18
|
from pydantic import Field, PrivateAttr
|
19
19
|
|
20
20
|
|
21
|
-
class Action(HandleTask, ProposeTask, Correct):
|
21
|
+
class Action(WithBriefing, HandleTask, ProposeTask, Correct):
|
22
22
|
"""Class that represents an action to be executed in a workflow.
|
23
23
|
|
24
24
|
Actions are the atomic units of work in a workflow. Each action performs
|
@@ -97,6 +97,8 @@ class WorkFlow(WithBriefing, ToolBoxUsage):
|
|
97
97
|
A workflow manages the execution of multiple actions in sequence, passing
|
98
98
|
a shared context between them and handling task lifecycle events.
|
99
99
|
"""
|
100
|
+
description:str =""
|
101
|
+
"""The description of the workflow, which describes the workflow's purpose and requirements."""
|
100
102
|
|
101
103
|
_context: Queue[Dict[str, Any]] = PrivateAttr(default_factory=lambda: Queue(maxsize=1))
|
102
104
|
"""Queue for storing the workflow execution context."""
|
@@ -104,7 +106,9 @@ class WorkFlow(WithBriefing, ToolBoxUsage):
|
|
104
106
|
_instances: Tuple[Action, ...] = PrivateAttr(default_factory=tuple)
|
105
107
|
"""Instantiated action objects to be executed in this workflow."""
|
106
108
|
|
107
|
-
steps: Tuple[Union[Type[Action], Action], ...] = Field(
|
109
|
+
steps: Tuple[Union[Type[Action], Action], ...] = Field(
|
110
|
+
frozen=True,
|
111
|
+
)
|
108
112
|
"""The sequence of actions to be executed, can be action classes or instances."""
|
109
113
|
|
110
114
|
task_input_key: str = Field(default="task_input")
|
@@ -157,13 +161,14 @@ class WorkFlow(WithBriefing, ToolBoxUsage):
|
|
157
161
|
# Process each action in sequence
|
158
162
|
for step in self._instances:
|
159
163
|
current_action = step.name
|
160
|
-
logger.info(f"Executing step
|
164
|
+
logger.info(f"Executing step >> {current_action}")
|
161
165
|
|
162
166
|
# Get current context and execute action
|
163
167
|
context = await self._context.get()
|
164
168
|
act_task = create_task(step.act(context))
|
165
169
|
# Handle task cancellation
|
166
170
|
if task.is_cancelled():
|
171
|
+
logger.warning(f"Task cancelled by task: {task.name}")
|
167
172
|
act_task.cancel(f"Cancelled by task: {task.name}")
|
168
173
|
break
|
169
174
|
|
@@ -171,7 +176,7 @@ class WorkFlow(WithBriefing, ToolBoxUsage):
|
|
171
176
|
modified_ctx = await act_task
|
172
177
|
logger.success(f"Step execution finished: {current_action}")
|
173
178
|
if step.output_key:
|
174
|
-
logger.success(f"Setting output
|
179
|
+
logger.success(f"Setting output to `{step.output_key}`")
|
175
180
|
await self._context.put(modified_ctx)
|
176
181
|
|
177
182
|
logger.success(f"Workflow execution finished: {self.name}")
|
@@ -182,7 +187,7 @@ class WorkFlow(WithBriefing, ToolBoxUsage):
|
|
182
187
|
|
183
188
|
if self.task_output_key not in final_ctx:
|
184
189
|
logger.warning(
|
185
|
-
f"Task output key: {self.task_output_key} not found in the context, None will be returned. "
|
190
|
+
f"Task output key: `{self.task_output_key}` not found in the context, None will be returned. "
|
186
191
|
f"You can check if `Action.output_key` is set the same as `WorkFlow.task_output_key`."
|
187
192
|
)
|
188
193
|
|
@@ -1,22 +1,29 @@
|
|
1
|
-
"""
|
1
|
+
"""Module containing the JudgeMent class for holding judgment results."""
|
2
2
|
|
3
3
|
from typing import List
|
4
4
|
|
5
|
-
from fabricatio.models.generic import ProposedAble
|
5
|
+
from fabricatio.models.generic import Display, ProposedAble
|
6
6
|
|
7
7
|
|
8
|
-
class JudgeMent(ProposedAble):
|
9
|
-
"""
|
8
|
+
class JudgeMent(ProposedAble,Display):
|
9
|
+
"""Represents a judgment result containing supporting/denying evidence and final verdict.
|
10
|
+
|
11
|
+
The class stores both affirmative and denies evidence, truth and reasons lists along with the final boolean judgment.
|
12
|
+
"""
|
13
|
+
deny_evidence: List[str]
|
14
|
+
"""List of clues supporting the denial."""
|
10
15
|
|
11
16
|
affirm_evidence: List[str]
|
12
|
-
"""List of
|
17
|
+
"""List of clues supporting the affirmation."""
|
13
18
|
|
14
|
-
deny_evidence: List[str]
|
15
|
-
"""List of evidence supporting the denial."""
|
16
19
|
|
17
20
|
final_judgement: bool
|
18
|
-
"""The final
|
21
|
+
"""The final judgment made according to all extracted clues."""
|
19
22
|
|
20
23
|
def __bool__(self) -> bool:
|
21
|
-
"""Return the final
|
24
|
+
"""Return the final judgment value.
|
25
|
+
|
26
|
+
Returns:
|
27
|
+
bool: The stored final_judgement value indicating the judgment result.
|
28
|
+
"""
|
22
29
|
return self.final_judgement
|