fabricatio 0.2.7.dev4__cp312-cp312-win_amd64.whl → 0.2.8__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/__init__.py +4 -11
- fabricatio/actions/article.py +226 -92
- fabricatio/actions/article_rag.py +86 -21
- fabricatio/actions/output.py +71 -3
- fabricatio/actions/rag.py +3 -3
- fabricatio/actions/rules.py +39 -0
- fabricatio/capabilities/advanced_judge.py +23 -0
- fabricatio/capabilities/censor.py +90 -0
- fabricatio/capabilities/check.py +195 -0
- fabricatio/capabilities/correct.py +160 -96
- fabricatio/capabilities/propose.py +20 -4
- fabricatio/capabilities/rag.py +5 -4
- fabricatio/capabilities/rating.py +68 -23
- fabricatio/capabilities/review.py +21 -190
- fabricatio/capabilities/task.py +9 -10
- fabricatio/config.py +11 -3
- fabricatio/fs/curd.py +4 -0
- fabricatio/models/action.py +24 -10
- fabricatio/models/adv_kwargs_types.py +25 -0
- fabricatio/models/extra/__init__.py +1 -0
- fabricatio/models/extra/advanced_judge.py +32 -0
- fabricatio/models/extra/article_base.py +324 -89
- fabricatio/models/extra/article_essence.py +49 -176
- fabricatio/models/extra/article_main.py +48 -127
- fabricatio/models/extra/article_outline.py +12 -152
- fabricatio/models/extra/article_proposal.py +29 -13
- fabricatio/models/extra/patches.py +7 -0
- fabricatio/models/extra/problem.py +153 -0
- fabricatio/models/extra/rule.py +65 -0
- fabricatio/models/generic.py +360 -88
- fabricatio/models/kwargs_types.py +23 -17
- fabricatio/models/role.py +4 -1
- fabricatio/models/task.py +1 -1
- fabricatio/models/tool.py +149 -14
- fabricatio/models/usages.py +61 -47
- fabricatio/models/utils.py +0 -46
- fabricatio/parser.py +7 -8
- fabricatio/rust.cp312-win_amd64.pyd +0 -0
- fabricatio/{_rust.pyi → rust.pyi} +50 -0
- fabricatio/{_rust_instances.py → rust_instances.py} +1 -1
- fabricatio/utils.py +54 -0
- fabricatio-0.2.8.data/scripts/tdown.exe +0 -0
- {fabricatio-0.2.7.dev4.dist-info → fabricatio-0.2.8.dist-info}/METADATA +2 -1
- fabricatio-0.2.8.dist-info/RECORD +58 -0
- fabricatio/_rust.cp312-win_amd64.pyd +0 -0
- fabricatio-0.2.7.dev4.data/scripts/tdown.exe +0 -0
- fabricatio-0.2.7.dev4.dist-info/RECORD +0 -47
- {fabricatio-0.2.7.dev4.dist-info → fabricatio-0.2.8.dist-info}/WHEEL +0 -0
- {fabricatio-0.2.7.dev4.dist-info → fabricatio-0.2.8.dist-info}/licenses/LICENSE +0 -0
@@ -1,7 +1,7 @@
|
|
1
1
|
"""This module contains the types for the keyword arguments of the methods in the models module."""
|
2
2
|
|
3
3
|
from importlib.util import find_spec
|
4
|
-
from typing import Any, Dict, Required, TypedDict
|
4
|
+
from typing import Any, Dict, List, Optional, Required, TypedDict
|
5
5
|
|
6
6
|
from litellm.caching.caching import CacheMode
|
7
7
|
from litellm.types.caching import CachingSupportedCallTypes
|
@@ -95,11 +95,30 @@ class ValidateKwargs[T](GenerateKwargs, total=False):
|
|
95
95
|
such as limiting the number of validation attempts.
|
96
96
|
"""
|
97
97
|
|
98
|
-
default: T
|
98
|
+
default: Optional[T]
|
99
99
|
max_validations: int
|
100
100
|
co_extractor: GenerateKwargs
|
101
101
|
|
102
102
|
|
103
|
+
class CompositeScoreKwargs(ValidateKwargs[List[Dict[str, float]]], total=False):
|
104
|
+
"""Arguments for composite score generation operations.
|
105
|
+
|
106
|
+
Extends GenerateKwargs with parameters for generating composite scores
|
107
|
+
based on specific criteria and weights.
|
108
|
+
"""
|
109
|
+
|
110
|
+
topic: str
|
111
|
+
criteria: set[str]
|
112
|
+
weights: Dict[str, float]
|
113
|
+
manual: Dict[str, str]
|
114
|
+
|
115
|
+
|
116
|
+
class BestKwargs(CompositeScoreKwargs, total=False):
|
117
|
+
"""Arguments for choose top-k operations."""
|
118
|
+
|
119
|
+
k: int
|
120
|
+
|
121
|
+
|
103
122
|
class ReviewInnerKwargs[T](ValidateKwargs[T], total=False):
|
104
123
|
"""Arguments for content review operations."""
|
105
124
|
|
@@ -118,22 +137,9 @@ class ReviewKwargs[T](ReviewInnerKwargs[T], total=False):
|
|
118
137
|
topic: Required[str]
|
119
138
|
|
120
139
|
|
121
|
-
class
|
122
|
-
"""Arguments for content
|
123
|
-
|
124
|
-
Extends GenerateKwargs with parameters for correcting content based on
|
125
|
-
specific criteria and templates.
|
126
|
-
"""
|
127
|
-
|
128
|
-
reference: str
|
129
|
-
supervisor_check: bool
|
130
|
-
|
131
|
-
|
132
|
-
class CensoredCorrectKwargs[T](ReviewInnerKwargs[T], total=False):
|
133
|
-
"""Arguments for content censorship operations."""
|
134
|
-
|
140
|
+
class ReferencedKwargs[T](ValidateKwargs[T], total=False):
|
141
|
+
"""Arguments for content review operations."""
|
135
142
|
reference: str
|
136
|
-
supervisor_check: bool
|
137
143
|
|
138
144
|
|
139
145
|
# noinspection PyTypedDict
|
fabricatio/models/role.py
CHANGED
@@ -8,11 +8,12 @@ from fabricatio.core import env
|
|
8
8
|
from fabricatio.journal import logger
|
9
9
|
from fabricatio.models.action import WorkFlow
|
10
10
|
from fabricatio.models.events import Event
|
11
|
+
from fabricatio.models.generic import WithBriefing
|
11
12
|
from fabricatio.models.tool import ToolBox
|
12
13
|
from pydantic import Field
|
13
14
|
|
14
15
|
|
15
|
-
class Role(ProposeTask, HandleTask, Correct):
|
16
|
+
class Role(WithBriefing, ProposeTask, HandleTask, Correct):
|
16
17
|
"""Class that represents a role with a registry of events and workflows.
|
17
18
|
|
18
19
|
A Role serves as a container for workflows, managing their registration to events
|
@@ -22,6 +23,8 @@ class Role(ProposeTask, HandleTask, Correct):
|
|
22
23
|
registry: Mapping of events to workflows that handle them
|
23
24
|
toolboxes: Set of toolboxes available to this role and its workflows
|
24
25
|
"""
|
26
|
+
description:str =""
|
27
|
+
"""A brief description of the role's responsibilities and capabilities."""
|
25
28
|
|
26
29
|
registry: dict[Event | str, WorkFlow] = Field(default_factory=dict)
|
27
30
|
"""The registry of events and workflows."""
|
fabricatio/models/task.py
CHANGED
@@ -6,13 +6,13 @@ It includes methods to manage the task's lifecycle, such as starting, finishing,
|
|
6
6
|
from asyncio import Queue
|
7
7
|
from typing import Any, List, Optional, Self
|
8
8
|
|
9
|
-
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
10
9
|
from fabricatio.config import configs
|
11
10
|
from fabricatio.core import env
|
12
11
|
from fabricatio.journal import logger
|
13
12
|
from fabricatio.models.events import Event, EventLike
|
14
13
|
from fabricatio.models.generic import ProposedAble, WithBriefing, WithDependency
|
15
14
|
from fabricatio.models.utils import TaskStatus
|
15
|
+
from fabricatio.rust_instances import TEMPLATE_MANAGER
|
16
16
|
from pydantic import Field, PrivateAttr
|
17
17
|
|
18
18
|
|
fabricatio/models/tool.py
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
"""A module for defining tools and toolboxes.
|
1
|
+
"""A module for defining tools and toolboxes.
|
2
|
+
|
3
|
+
This module provides classes for defining tools and toolboxes, which can be used to manage and execute callable functions
|
4
|
+
with additional functionalities such as logging, execution info, and briefing.
|
5
|
+
"""
|
2
6
|
|
3
7
|
from importlib.machinery import ModuleSpec
|
4
8
|
from importlib.util import module_from_spec
|
@@ -14,7 +18,16 @@ from pydantic import BaseModel, ConfigDict, Field
|
|
14
18
|
|
15
19
|
|
16
20
|
class Tool[**P, R](WithBriefing):
|
17
|
-
"""A class representing a tool with a callable source function.
|
21
|
+
"""A class representing a tool with a callable source function.
|
22
|
+
|
23
|
+
This class encapsulates a callable function (source) and provides methods to invoke it, log its execution, and generate
|
24
|
+
a brief description (briefing) of the tool.
|
25
|
+
|
26
|
+
Attributes:
|
27
|
+
name (str): The name of the tool.
|
28
|
+
description (str): The description of the tool.
|
29
|
+
source (Callable[P, R]): The source function of the tool.
|
30
|
+
"""
|
18
31
|
|
19
32
|
name: str = Field(default="")
|
20
33
|
"""The name of the tool."""
|
@@ -26,7 +39,16 @@ class Tool[**P, R](WithBriefing):
|
|
26
39
|
"""The source function of the tool."""
|
27
40
|
|
28
41
|
def model_post_init(self, __context: Any) -> None:
|
29
|
-
"""Initialize the tool with a name and a source function.
|
42
|
+
"""Initialize the tool with a name and a source function.
|
43
|
+
|
44
|
+
This method sets the tool's name and description based on the source function's name and docstring.
|
45
|
+
|
46
|
+
Args:
|
47
|
+
__context (Any): Context passed during model initialization.
|
48
|
+
|
49
|
+
Raises:
|
50
|
+
RuntimeError: If the tool does not have a source function.
|
51
|
+
"""
|
30
52
|
self.name = self.name or self.source.__name__
|
31
53
|
|
32
54
|
if not self.name:
|
@@ -36,7 +58,17 @@ class Tool[**P, R](WithBriefing):
|
|
36
58
|
self.description = self.description.strip()
|
37
59
|
|
38
60
|
def invoke(self, *args: P.args, **kwargs: P.kwargs) -> R:
|
39
|
-
"""Invoke the tool's source function with the provided arguments.
|
61
|
+
"""Invoke the tool's source function with the provided arguments.
|
62
|
+
|
63
|
+
This method logs the invocation of the tool and then calls the source function with the given arguments.
|
64
|
+
|
65
|
+
Args:
|
66
|
+
*args (P.args): Positional arguments to be passed to the source function.
|
67
|
+
**kwargs (P.kwargs): Keyword arguments to be passed to the source function.
|
68
|
+
|
69
|
+
Returns:
|
70
|
+
R: The result of the source function.
|
71
|
+
"""
|
40
72
|
logger.info(f"Invoking tool: {self.name}")
|
41
73
|
return self.source(*args, **kwargs)
|
42
74
|
|
@@ -44,6 +76,8 @@ class Tool[**P, R](WithBriefing):
|
|
44
76
|
def briefing(self) -> str:
|
45
77
|
"""Return a brief description of the tool.
|
46
78
|
|
79
|
+
This method generates a brief description of the tool, including its name, signature, and description.
|
80
|
+
|
47
81
|
Returns:
|
48
82
|
str: A brief description of the tool.
|
49
83
|
"""
|
@@ -59,7 +93,18 @@ def _desc_wrapper(desc: str) -> str:
|
|
59
93
|
|
60
94
|
|
61
95
|
class ToolBox(WithBriefing):
|
62
|
-
"""A class representing a collection of tools.
|
96
|
+
"""A class representing a collection of tools.
|
97
|
+
|
98
|
+
This class manages a list of tools and provides methods to add tools, retrieve tools by name, and generate a brief
|
99
|
+
description (briefing) of the toolbox.
|
100
|
+
|
101
|
+
Attributes:
|
102
|
+
description (str): The description of the toolbox.
|
103
|
+
tools (List[Tool]): A list of tools in the toolbox.
|
104
|
+
"""
|
105
|
+
|
106
|
+
description: str = ""
|
107
|
+
"""The description of the toolbox."""
|
63
108
|
|
64
109
|
tools: List[Tool] = Field(default_factory=list, frozen=True)
|
65
110
|
"""A list of tools in the toolbox."""
|
@@ -67,6 +112,8 @@ class ToolBox(WithBriefing):
|
|
67
112
|
def collect_tool[**P, R](self, func: Callable[P, R]) -> Callable[P, R]:
|
68
113
|
"""Add a callable function to the toolbox as a tool.
|
69
114
|
|
115
|
+
This method wraps the function with logging execution info and adds it to the toolbox.
|
116
|
+
|
70
117
|
Args:
|
71
118
|
func (Callable[P, R]): The function to be added as a tool.
|
72
119
|
|
@@ -79,6 +126,8 @@ class ToolBox(WithBriefing):
|
|
79
126
|
def add_tool[**P, R](self, func: Callable[P, R]) -> Self:
|
80
127
|
"""Add a callable function to the toolbox as a tool.
|
81
128
|
|
129
|
+
This method wraps the function with logging execution info and adds it to the toolbox.
|
130
|
+
|
82
131
|
Args:
|
83
132
|
func (Callable): The function to be added as a tool.
|
84
133
|
|
@@ -92,6 +141,8 @@ class ToolBox(WithBriefing):
|
|
92
141
|
def briefing(self) -> str:
|
93
142
|
"""Return a brief description of the toolbox.
|
94
143
|
|
144
|
+
This method generates a brief description of the toolbox, including its name, description, and a list of tools.
|
145
|
+
|
95
146
|
Returns:
|
96
147
|
str: A brief description of the toolbox.
|
97
148
|
"""
|
@@ -102,6 +153,8 @@ class ToolBox(WithBriefing):
|
|
102
153
|
def get[**P, R](self, name: str) -> Tool[P, R]:
|
103
154
|
"""Invoke a tool by name with the provided arguments.
|
104
155
|
|
156
|
+
This method retrieves a tool by its name from the toolbox.
|
157
|
+
|
105
158
|
Args:
|
106
159
|
name (str): The name of the tool to invoke.
|
107
160
|
|
@@ -120,13 +173,24 @@ class ToolBox(WithBriefing):
|
|
120
173
|
return tool
|
121
174
|
|
122
175
|
def __hash__(self) -> int:
|
123
|
-
"""Return a hash of the toolbox based on its briefing.
|
176
|
+
"""Return a hash of the toolbox based on its briefing.
|
177
|
+
|
178
|
+
Returns:
|
179
|
+
int: A hash value based on the toolbox's briefing.
|
180
|
+
"""
|
124
181
|
return hash(self.briefing)
|
125
182
|
|
126
183
|
|
127
184
|
class ToolExecutor(BaseModel):
|
128
|
-
"""A class representing a tool executor with a sequence of tools to execute.
|
185
|
+
"""A class representing a tool executor with a sequence of tools to execute.
|
186
|
+
|
187
|
+
This class manages a sequence of tools and provides methods to inject tools and data into a module, execute the tools,
|
188
|
+
and retrieve specific outputs.
|
129
189
|
|
190
|
+
Attributes:
|
191
|
+
candidates (List[Tool]): The sequence of tools to execute.
|
192
|
+
data (Dict[str, Any]): The data that could be used when invoking the tools.
|
193
|
+
"""
|
130
194
|
model_config = ConfigDict(use_attribute_docstrings=True)
|
131
195
|
candidates: List[Tool] = Field(default_factory=list, frozen=True)
|
132
196
|
"""The sequence of tools to execute."""
|
@@ -135,7 +199,16 @@ class ToolExecutor(BaseModel):
|
|
135
199
|
"""The data that could be used when invoking the tools."""
|
136
200
|
|
137
201
|
def inject_tools[M: ModuleType](self, module: Optional[M] = None) -> M:
|
138
|
-
"""Inject the tools into the provided module or default.
|
202
|
+
"""Inject the tools into the provided module or default.
|
203
|
+
|
204
|
+
This method injects the tools into the provided module or creates a new module if none is provided.
|
205
|
+
|
206
|
+
Args:
|
207
|
+
module (Optional[M]): The module to inject tools into. If None, a new module is created.
|
208
|
+
|
209
|
+
Returns:
|
210
|
+
M: The module with injected tools.
|
211
|
+
"""
|
139
212
|
module = module or cast(
|
140
213
|
"M", module_from_spec(spec=ModuleSpec(name=configs.toolbox.tool_module_name, loader=None))
|
141
214
|
)
|
@@ -145,7 +218,16 @@ class ToolExecutor(BaseModel):
|
|
145
218
|
return module
|
146
219
|
|
147
220
|
def inject_data[M: ModuleType](self, module: Optional[M] = None) -> M:
|
148
|
-
"""Inject the data into the provided module or default.
|
221
|
+
"""Inject the data into the provided module or default.
|
222
|
+
|
223
|
+
This method injects the data into the provided module or creates a new module if none is provided.
|
224
|
+
|
225
|
+
Args:
|
226
|
+
module (Optional[M]): The module to inject data into. If None, a new module is created.
|
227
|
+
|
228
|
+
Returns:
|
229
|
+
M: The module with injected data.
|
230
|
+
"""
|
149
231
|
module = module or cast(
|
150
232
|
'M', module_from_spec(spec=ModuleSpec(name=configs.toolbox.data_module_name, loader=None))
|
151
233
|
)
|
@@ -155,7 +237,17 @@ class ToolExecutor(BaseModel):
|
|
155
237
|
return module
|
156
238
|
|
157
239
|
def execute[C: Dict[str, Any]](self, source: CodeType, cxt: Optional[C] = None) -> C:
|
158
|
-
"""Execute the sequence of tools with the provided context.
|
240
|
+
"""Execute the sequence of tools with the provided context.
|
241
|
+
|
242
|
+
This method executes the tools in the sequence with the provided context.
|
243
|
+
|
244
|
+
Args:
|
245
|
+
source (CodeType): The source code to execute.
|
246
|
+
cxt (Optional[C]): The context to execute the tools with. If None, an empty dictionary is used.
|
247
|
+
|
248
|
+
Returns:
|
249
|
+
C: The context after executing the tools.
|
250
|
+
"""
|
159
251
|
cxt = cxt or {}
|
160
252
|
|
161
253
|
@use_temp_module([self.inject_data(), self.inject_tools()])
|
@@ -167,16 +259,49 @@ class ToolExecutor(BaseModel):
|
|
167
259
|
|
168
260
|
@overload
|
169
261
|
def take[C: Dict[str, Any]](self, keys: List[str], source: CodeType, cxt: Optional[C] = None) -> C:
|
170
|
-
"""Check the output of the tools with the provided context.
|
262
|
+
"""Check the output of the tools with the provided context.
|
263
|
+
|
264
|
+
This method executes the tools and retrieves specific keys from the context.
|
265
|
+
|
266
|
+
Args:
|
267
|
+
keys (List[str]): The keys to retrieve from the context.
|
268
|
+
source (CodeType): The source code to execute.
|
269
|
+
cxt (Optional[C]): The context to execute the tools with. If None, an empty dictionary is used.
|
270
|
+
|
271
|
+
Returns:
|
272
|
+
C: A dictionary containing the retrieved keys and their values.
|
273
|
+
"""
|
171
274
|
...
|
172
275
|
|
173
276
|
@overload
|
174
277
|
def take[C: Dict[str, Any]](self, keys: str, source: CodeType, cxt: Optional[C] = None) -> Any:
|
175
|
-
"""Check the output of the tools with the provided context.
|
278
|
+
"""Check the output of the tools with the provided context.
|
279
|
+
|
280
|
+
This method executes the tools and retrieves a specific key from the context.
|
281
|
+
|
282
|
+
Args:
|
283
|
+
keys (str): The key to retrieve from the context.
|
284
|
+
source (CodeType): The source code to execute.
|
285
|
+
cxt (Optional[C]): The context to execute the tools with. If None, an empty dictionary is used.
|
286
|
+
|
287
|
+
Returns:
|
288
|
+
Any: The value of the retrieved key.
|
289
|
+
"""
|
176
290
|
...
|
177
291
|
|
178
292
|
def take[C: Dict[str, Any]](self, keys: List[str] | str, source: CodeType, cxt: Optional[C] = None) -> C | Any:
|
179
|
-
"""Check the output of the tools with the provided context.
|
293
|
+
"""Check the output of the tools with the provided context.
|
294
|
+
|
295
|
+
This method executes the tools and retrieves specific keys or a specific key from the context.
|
296
|
+
|
297
|
+
Args:
|
298
|
+
keys (List[str] | str): The keys to retrieve from the context. Can be a single key or a list of keys.
|
299
|
+
source (CodeType): The source code to execute.
|
300
|
+
cxt (Optional[C]): The context to execute the tools with. If None, an empty dictionary is used.
|
301
|
+
|
302
|
+
Returns:
|
303
|
+
C | Any: A dictionary containing the retrieved keys and their values, or the value of the retrieved key.
|
304
|
+
"""
|
180
305
|
cxt = self.execute(source, cxt)
|
181
306
|
if isinstance(keys, str):
|
182
307
|
return cxt[keys]
|
@@ -184,7 +309,17 @@ class ToolExecutor(BaseModel):
|
|
184
309
|
|
185
310
|
@classmethod
|
186
311
|
def from_recipe(cls, recipe: List[str], toolboxes: List[ToolBox]) -> Self:
|
187
|
-
"""Create a tool executor from a recipe and a list of toolboxes.
|
312
|
+
"""Create a tool executor from a recipe and a list of toolboxes.
|
313
|
+
|
314
|
+
This method creates a tool executor by retrieving tools from the provided toolboxes based on the recipe.
|
315
|
+
|
316
|
+
Args:
|
317
|
+
recipe (List[str]): The recipe specifying the names of the tools to be added.
|
318
|
+
toolboxes (List[ToolBox]): The list of toolboxes to retrieve tools from.
|
319
|
+
|
320
|
+
Returns:
|
321
|
+
Self: A new instance of the tool executor with the specified tools.
|
322
|
+
"""
|
188
323
|
tools = []
|
189
324
|
while tool_name := recipe.pop(0):
|
190
325
|
for toolbox in toolboxes:
|
fabricatio/models/usages.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
"""This module contains classes that manage the usage of language models and tools in tasks."""
|
2
2
|
|
3
|
+
import traceback
|
3
4
|
from asyncio import gather
|
4
|
-
from typing import Callable, Dict, Iterable, List, Optional, Self, Sequence, Set,
|
5
|
+
from typing import Callable, Dict, Iterable, List, Optional, Self, Sequence, Set, Union, Unpack, overload
|
5
6
|
|
6
7
|
import asyncstdlib
|
7
8
|
import litellm
|
8
|
-
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
9
9
|
from fabricatio.config import configs
|
10
10
|
from fabricatio.decorators import logging_exec_time
|
11
11
|
from fabricatio.journal import logger
|
@@ -13,8 +13,10 @@ from fabricatio.models.generic import ScopedConfig, WithBriefing
|
|
13
13
|
from fabricatio.models.kwargs_types import ChooseKwargs, EmbeddingKwargs, GenerateKwargs, LLMKwargs, ValidateKwargs
|
14
14
|
from fabricatio.models.task import Task
|
15
15
|
from fabricatio.models.tool import Tool, ToolBox
|
16
|
-
from fabricatio.models.utils import Messages
|
16
|
+
from fabricatio.models.utils import Messages
|
17
17
|
from fabricatio.parser import GenericCapture, JsonCapture
|
18
|
+
from fabricatio.rust_instances import TEMPLATE_MANAGER
|
19
|
+
from fabricatio.utils import ok
|
18
20
|
from litellm import RateLimitError, Router, stream_chunk_builder # pyright: ignore [reportPrivateImportUsage]
|
19
21
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
20
22
|
from litellm.types.utils import (
|
@@ -38,21 +40,30 @@ ROUTER = Router(
|
|
38
40
|
allowed_fails=configs.routing.allowed_fails,
|
39
41
|
retry_after=configs.routing.retry_after,
|
40
42
|
cooldown_time=configs.routing.cooldown_time,
|
43
|
+
cache_responses=configs.cache.enabled,
|
44
|
+
cache_kwargs=configs.cache.params,
|
41
45
|
)
|
42
46
|
|
43
47
|
|
44
48
|
class LLMUsage(ScopedConfig):
|
45
|
-
"""Class that manages LLM (Large Language Model) usage parameters and methods.
|
49
|
+
"""Class that manages LLM (Large Language Model) usage parameters and methods.
|
50
|
+
|
51
|
+
This class provides methods to deploy LLMs, query them for responses, and handle various configurations
|
52
|
+
related to LLM usage such as API keys, endpoints, and rate limits.
|
53
|
+
"""
|
46
54
|
|
47
55
|
def _deploy(self, deployment: Deployment) -> Router:
|
48
|
-
"""Add a deployment to the router.
|
56
|
+
"""Add a deployment to the router.
|
57
|
+
|
58
|
+
Args:
|
59
|
+
deployment (Deployment): The deployment to be added to the router.
|
60
|
+
|
61
|
+
Returns:
|
62
|
+
Router: The updated router with the added deployment.
|
63
|
+
"""
|
49
64
|
self._added_deployment = ROUTER.upsert_deployment(deployment)
|
50
65
|
return ROUTER
|
51
66
|
|
52
|
-
@classmethod
|
53
|
-
def _scoped_model(cls) -> Type["LLMUsage"]:
|
54
|
-
return LLMUsage
|
55
|
-
|
56
67
|
# noinspection PyTypeChecker,PydanticTypeChecker
|
57
68
|
async def aquery(
|
58
69
|
self,
|
@@ -132,10 +143,10 @@ class LLMUsage(ScopedConfig):
|
|
132
143
|
**kwargs (Unpack[LLMKwargs]): Additional keyword arguments for the LLM usage.
|
133
144
|
|
134
145
|
Returns:
|
135
|
-
|
146
|
+
Sequence[TextChoices | Choices | StreamingChoices]: A sequence of choices or streaming choices from the model response.
|
136
147
|
"""
|
137
148
|
resp = await self.aquery(
|
138
|
-
messages=Messages().add_system_message(system_message).add_user_message(question),
|
149
|
+
messages=Messages().add_system_message(system_message).add_user_message(question).as_list(),
|
139
150
|
n=n,
|
140
151
|
**kwargs,
|
141
152
|
)
|
@@ -277,34 +288,32 @@ class LLMUsage(ScopedConfig):
|
|
277
288
|
question: str | List[str],
|
278
289
|
validator: Callable[[str], T | None],
|
279
290
|
default: Optional[T] = None,
|
280
|
-
max_validations: PositiveInt =
|
291
|
+
max_validations: PositiveInt = 3,
|
281
292
|
co_extractor: Optional[GenerateKwargs] = None,
|
282
293
|
**kwargs: Unpack[GenerateKwargs],
|
283
294
|
) -> Optional[T] | List[Optional[T]] | List[T] | T:
|
284
295
|
"""Asynchronously asks a question and validates the response using a given validator.
|
285
296
|
|
286
297
|
Args:
|
287
|
-
question (str): The question to ask.
|
298
|
+
question (str | List[str]): The question to ask.
|
288
299
|
validator (Callable[[str], T | None]): A function to validate the response.
|
289
300
|
default (T | None): Default value to return if validation fails. Defaults to None.
|
290
|
-
max_validations (PositiveInt): Maximum number of validation attempts. Defaults to
|
301
|
+
max_validations (PositiveInt): Maximum number of validation attempts. Defaults to 3.
|
291
302
|
co_extractor (Optional[GenerateKwargs]): Keyword arguments for the co-extractor, if provided will enable co-extraction.
|
292
|
-
**kwargs (Unpack[
|
303
|
+
**kwargs (Unpack[GenerateKwargs]): Additional keyword arguments for the LLM usage.
|
293
304
|
|
294
305
|
Returns:
|
295
|
-
T: The validated response.
|
296
|
-
|
306
|
+
Optional[T] | List[Optional[T]] | List[T] | T: The validated response.
|
297
307
|
"""
|
298
308
|
|
299
309
|
async def _inner(q: str) -> Optional[T]:
|
300
310
|
for lap in range(max_validations):
|
301
311
|
try:
|
302
|
-
if (
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
response := await self.aask(
|
312
|
+
if ((validated := validator(response := await self.aask(question=q, **kwargs))) is not None) or (
|
313
|
+
co_extractor
|
314
|
+
and (
|
315
|
+
validated := validator(
|
316
|
+
await self.aask(
|
308
317
|
question=(
|
309
318
|
TEMPLATE_MANAGER.render_template(
|
310
319
|
configs.templates.co_validation_template,
|
@@ -315,7 +324,8 @@ class LLMUsage(ScopedConfig):
|
|
315
324
|
)
|
316
325
|
)
|
317
326
|
)
|
318
|
-
|
327
|
+
is not None
|
328
|
+
):
|
319
329
|
logger.debug(f"Successfully validated the response at {lap}th attempt.")
|
320
330
|
return validated
|
321
331
|
|
@@ -324,6 +334,7 @@ class LLMUsage(ScopedConfig):
|
|
324
334
|
continue
|
325
335
|
except Exception as e: # noqa: BLE001
|
326
336
|
logger.error(f"Error during validation: \n{e}")
|
337
|
+
logger.debug(traceback.format_exc())
|
327
338
|
break
|
328
339
|
if not kwargs.get("no_cache"):
|
329
340
|
kwargs["no_cache"] = True
|
@@ -334,7 +345,7 @@ class LLMUsage(ScopedConfig):
|
|
334
345
|
|
335
346
|
return await (gather(*[_inner(q) for q in question]) if isinstance(question, list) else _inner(question))
|
336
347
|
|
337
|
-
async def
|
348
|
+
async def alist_str(
|
338
349
|
self, requirement: str, k: NonNegativeInt = 0, **kwargs: Unpack[ValidateKwargs[List[str]]]
|
339
350
|
) -> Optional[List[str]]:
|
340
351
|
"""Asynchronously generates a list of strings based on a given requirement.
|
@@ -345,7 +356,7 @@ class LLMUsage(ScopedConfig):
|
|
345
356
|
**kwargs (Unpack[ValidateKwargs]): Additional keyword arguments for the LLM usage.
|
346
357
|
|
347
358
|
Returns:
|
348
|
-
List[str]: The validated response as a list of strings.
|
359
|
+
Optional[List[str]]: The validated response as a list of strings.
|
349
360
|
"""
|
350
361
|
return await self.aask_validate(
|
351
362
|
TEMPLATE_MANAGER.render_template(
|
@@ -364,9 +375,9 @@ class LLMUsage(ScopedConfig):
|
|
364
375
|
**kwargs (Unpack[ChooseKwargs]): Additional keyword arguments for the LLM usage.
|
365
376
|
|
366
377
|
Returns:
|
367
|
-
List[str]: The validated response as a list of strings.
|
378
|
+
Optional[List[str]]: The validated response as a list of strings.
|
368
379
|
"""
|
369
|
-
return await self.
|
380
|
+
return await self.alist_str(
|
370
381
|
TEMPLATE_MANAGER.render_template(
|
371
382
|
configs.templates.pathstr_template,
|
372
383
|
{"requirement": requirement},
|
@@ -382,7 +393,7 @@ class LLMUsage(ScopedConfig):
|
|
382
393
|
**kwargs (Unpack[ValidateKwargs]): Additional keyword arguments for the LLM usage.
|
383
394
|
|
384
395
|
Returns:
|
385
|
-
str: The validated response as a single string.
|
396
|
+
Optional[str]: The validated response as a single string.
|
386
397
|
"""
|
387
398
|
if paths := await self.apathstr(
|
388
399
|
requirement,
|
@@ -401,7 +412,7 @@ class LLMUsage(ScopedConfig):
|
|
401
412
|
**kwargs (Unpack[GenerateKwargs]): Additional keyword arguments for the LLM usage.
|
402
413
|
|
403
414
|
Returns:
|
404
|
-
str: The generated string.
|
415
|
+
Optional[str]: The generated string.
|
405
416
|
"""
|
406
417
|
return await self.aask_validate( # pyright: ignore [reportReturnType]
|
407
418
|
TEMPLATE_MANAGER.render_template(
|
@@ -428,12 +439,7 @@ class LLMUsage(ScopedConfig):
|
|
428
439
|
**kwargs (Unpack[ValidateKwargs]): Additional keyword arguments for the LLM usage.
|
429
440
|
|
430
441
|
Returns:
|
431
|
-
List[T]: The final validated selection result list, with element types matching the input `choices`.
|
432
|
-
|
433
|
-
Important:
|
434
|
-
- Uses a template engine to generate structured prompts.
|
435
|
-
- Ensures response compliance through JSON parsing and format validation.
|
436
|
-
- Relies on `aask_validate` to implement retry mechanisms with validation.
|
442
|
+
Optional[List[T]]: The final validated selection result list, with element types matching the input `choices`.
|
437
443
|
"""
|
438
444
|
if dup := duplicates_everseen(choices, key=lambda x: x.name):
|
439
445
|
logger.error(err := f"Redundant choices: {dup}")
|
@@ -521,7 +527,10 @@ class LLMUsage(ScopedConfig):
|
|
521
527
|
|
522
528
|
|
523
529
|
class EmbeddingUsage(LLMUsage):
|
524
|
-
"""A class representing the embedding model.
|
530
|
+
"""A class representing the embedding model.
|
531
|
+
|
532
|
+
This class extends LLMUsage and provides methods to generate embeddings for input text using various models.
|
533
|
+
"""
|
525
534
|
|
526
535
|
async def aembedding(
|
527
536
|
self,
|
@@ -540,14 +549,13 @@ class EmbeddingUsage(LLMUsage):
|
|
540
549
|
timeout (Optional[PositiveInt]): The timeout for the embedding request. Defaults to the instance's `llm_timeout` or the global configuration.
|
541
550
|
caching (Optional[bool]): Whether to cache the embedding result. Defaults to False.
|
542
551
|
|
543
|
-
|
544
552
|
Returns:
|
545
553
|
EmbeddingResponse: The response containing the embeddings.
|
546
554
|
"""
|
547
555
|
# check seq length
|
548
556
|
max_len = self.embedding_max_sequence_length or configs.embedding.max_sequence_length
|
549
|
-
if max_len and any(
|
550
|
-
logger.error(err := f"Input text exceeds maximum sequence length {max_len}.")
|
557
|
+
if max_len and any(length := (token_counter(text=t)) > max_len for t in input_text):
|
558
|
+
logger.error(err := f"Input text exceeds maximum sequence length {max_len}, got {length}.")
|
551
559
|
raise ValueError(err)
|
552
560
|
|
553
561
|
return await litellm.aembedding(
|
@@ -598,14 +606,21 @@ class EmbeddingUsage(LLMUsage):
|
|
598
606
|
|
599
607
|
|
600
608
|
class ToolBoxUsage(LLMUsage):
|
601
|
-
"""A class representing the usage of tools in a task.
|
609
|
+
"""A class representing the usage of tools in a task.
|
610
|
+
|
611
|
+
This class extends LLMUsage and provides methods to manage and use toolboxes and tools within tasks.
|
612
|
+
"""
|
602
613
|
|
603
614
|
toolboxes: Set[ToolBox] = Field(default_factory=set)
|
604
615
|
"""A set of toolboxes used by the instance."""
|
605
616
|
|
606
617
|
@property
|
607
618
|
def available_toolbox_names(self) -> List[str]:
|
608
|
-
"""Return a list of available toolbox names.
|
619
|
+
"""Return a list of available toolbox names.
|
620
|
+
|
621
|
+
Returns:
|
622
|
+
List[str]: A list of names of the available toolboxes.
|
623
|
+
"""
|
609
624
|
return [toolbox.name for toolbox in self.toolboxes]
|
610
625
|
|
611
626
|
async def choose_toolboxes(
|
@@ -617,11 +632,10 @@ class ToolBoxUsage(LLMUsage):
|
|
617
632
|
|
618
633
|
Args:
|
619
634
|
task (Task): The task for which to choose toolboxes.
|
620
|
-
system_message (str): Custom system-level prompt, defaults to an empty string.
|
621
635
|
**kwargs (Unpack[LLMKwargs]): Additional keyword arguments for the LLM usage.
|
622
636
|
|
623
637
|
Returns:
|
624
|
-
List[ToolBox]: The selected toolboxes.
|
638
|
+
Optional[List[ToolBox]]: The selected toolboxes.
|
625
639
|
"""
|
626
640
|
if not self.toolboxes:
|
627
641
|
logger.warning("No toolboxes available.")
|
@@ -646,7 +660,7 @@ class ToolBoxUsage(LLMUsage):
|
|
646
660
|
**kwargs (Unpack[LLMKwargs]): Additional keyword arguments for the LLM usage.
|
647
661
|
|
648
662
|
Returns:
|
649
|
-
List[Tool]: The selected tools.
|
663
|
+
Optional[List[Tool]]: The selected tools.
|
650
664
|
"""
|
651
665
|
if not toolbox.tools:
|
652
666
|
logger.warning(f"No tools available in toolbox {toolbox.name}.")
|
@@ -708,7 +722,7 @@ class ToolBoxUsage(LLMUsage):
|
|
708
722
|
"""
|
709
723
|
if isinstance(others, ToolBoxUsage):
|
710
724
|
others = [others]
|
711
|
-
for other in others:
|
725
|
+
for other in (x for x in others if isinstance(x, ToolBoxUsage)):
|
712
726
|
self.toolboxes.update(other.toolboxes)
|
713
727
|
return self
|
714
728
|
|
@@ -724,6 +738,6 @@ class ToolBoxUsage(LLMUsage):
|
|
724
738
|
"""
|
725
739
|
if isinstance(others, ToolBoxUsage):
|
726
740
|
others = [others]
|
727
|
-
for other in others:
|
741
|
+
for other in (x for x in others if isinstance(x, ToolBoxUsage)):
|
728
742
|
other.toolboxes.update(self.toolboxes)
|
729
743
|
return self
|