-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathtool.py
286 lines (236 loc) · 10.1 KB
/
tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright 2025 © BeeAI a Series of LF Projects, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import inspect
import typing
from abc import ABC, abstractmethod
from collections.abc import Callable
from functools import cached_property
from typing import Any, Generic, Self, TypeAlias
from pydantic import BaseModel, ConfigDict, ValidationError, create_model
from typing_extensions import TypeVar
from beeai_framework.cache.base import BaseCache
from beeai_framework.cache.null_cache import NullCache
from beeai_framework.context import Run, RunContext
from beeai_framework.emitter.emitter import Emitter
from beeai_framework.errors import FrameworkError
from beeai_framework.logger import Logger
from beeai_framework.retryable import Retryable, RetryableConfig, RetryableContext, RetryableInput
from beeai_framework.tools.errors import ToolError, ToolInputValidationError
from beeai_framework.tools.events import (
ToolErrorEvent,
ToolRetryEvent,
ToolStartEvent,
ToolSuccessEvent,
tool_event_types,
)
from beeai_framework.tools.types import StringToolOutput, ToolOutput, ToolRunOptions
from beeai_framework.utils.strings import to_safe_word
logger = Logger(__name__)
TInput = TypeVar("TInput", bound=BaseModel)
TRunOptions = TypeVar("TRunOptions", bound=ToolRunOptions, default=ToolRunOptions)
TOutput = TypeVar("TOutput", bound=ToolOutput, default=ToolOutput)
class Tool(Generic[TInput, TRunOptions, TOutput], ABC):
def __init__(self, options: dict[str, Any] | None = None) -> None:
self._options: dict[str, Any] | None = options or None
self._cache = self.options.get("cache", NullCache[TOutput]()) if self.options else NullCache[TOutput]()
@property
def options(self) -> dict[str, Any] | None:
return self._options
@property
def cache(self) -> BaseCache[TOutput]:
return self._cache
@property
@abstractmethod
def name(self) -> str:
pass
@property
@abstractmethod
def description(self) -> str:
pass
@property
@abstractmethod
def input_schema(self) -> type[TInput]:
pass
@cached_property
def emitter(self) -> Emitter:
emitter = self._create_emitter()
emitter.events = tool_event_types
return emitter
@abstractmethod
def _create_emitter(self) -> Emitter:
pass
@abstractmethod
async def _run(self, input: TInput, options: TRunOptions | None, context: RunContext) -> TOutput:
pass
def _generate_key(self, input: TInput | dict[str, Any], options: TRunOptions | None = None) -> str:
options_dict = options.model_dump(exclude_none=True) if options else {}
options_dict.pop("signal", None)
options_dict.pop("retry_options", None)
return BaseCache.generate_key(input, options_dict)
async def clear_cache(self) -> None:
await self.cache.clear()
def _validate_input(self, input: TInput | dict[str, Any]) -> TInput:
try:
return self.input_schema.model_validate(input)
except ValidationError as e:
raise ToolInputValidationError("Tool input validation error", cause=e)
def run(self, input: TInput | dict[str, Any], options: TRunOptions | None = None) -> Run[TOutput]:
async def handler(context: RunContext) -> TOutput:
error_propagated = False
try:
validated_input = self._validate_input(input)
async def executor(_: RetryableContext) -> TOutput:
nonlocal error_propagated
error_propagated = False
await context.emitter.emit("start", ToolStartEvent(input=validated_input, options=options))
if self.cache.enabled:
cache_key = self._generate_key(input, options)
result = await self.cache.get(cache_key)
if result:
return result
result = await self._run(validated_input, options, context)
if self.cache.enabled:
await self.cache.set(cache_key, result)
return result
async def on_error(error: Exception, _: RetryableContext) -> None:
nonlocal error_propagated
error_propagated = True
err = ToolError.ensure(error)
await context.emitter.emit(
"error", ToolErrorEvent(error=err, input=validated_input, options=options)
)
if FrameworkError.is_fatal(err) is True:
raise err
async def on_retry(ctx: RetryableContext, last_error: Exception) -> None:
err = ToolError.ensure(last_error)
await context.emitter.emit(
"retry", ToolRetryEvent(error=err, input=validated_input, options=options)
)
output = await Retryable(
RetryableInput(
executor=executor,
on_error=on_error,
on_retry=on_retry,
config=RetryableConfig(
max_retries=(
(options.retry_options.max_retries or 0) if options and options.retry_options else 0
),
factor=((options.retry_options.factor or 1) if options and options.retry_options else 1),
signal=context.signal,
),
)
).get()
await context.emitter.emit(
"success", ToolSuccessEvent(output=output, input=validated_input, options=options)
)
return output
except Exception as e:
err = ToolError.ensure(e, tool=self)
if not error_propagated:
await context.emitter.emit("error", ToolErrorEvent(error=err, input=input, options=options))
raise err
finally:
await context.emitter.emit("finish", None)
return RunContext.enter(
self,
handler,
signal=options.signal if options else None,
run_params={"input": input, "options": options},
)
async def clone(self) -> Self:
cloned = type(self)(self._options.copy() if self._options else None)
cloned._cache = await self._cache.clone()
return cloned
# this method was inspired by the discussion that was had in this issue:
# https://github.com/pydantic/pydantic/issues/1391
@typing.no_type_check
def get_input_schema(tool_function: Callable) -> type[BaseModel]:
input_model_name = tool_function.__name__
args, _, _, defaults, kwonlyargs, kwonlydefaults, annotations = inspect.getfullargspec(tool_function)
defaults = defaults or []
args = args or []
non_default_args = len(args) - len(defaults)
try:
defaults = (...,) * non_default_args + defaults
except TypeError:
defaults = [
...,
] * non_default_args + defaults
keyword_only_params = {param: kwonlydefaults.get(param, Any) for param in kwonlyargs}
params = {param: (annotations.get(param, Any), default) for param, default in zip(args, defaults, strict=False)}
input_model = create_model(
input_model_name,
**params,
**keyword_only_params,
__config__=ConfigDict(extra="allow", arbitrary_types_allowed=True),
)
return input_model
TFunction = Callable[..., Any]
AnyTool: TypeAlias = Tool[Any, Any, Any]
@typing.overload
def tool(
tool_function: TFunction,
/,
*,
name: str | None = ...,
description: str | None = ...,
input_schema: type[BaseModel] | None = ...,
) -> AnyTool: ...
@typing.overload
def tool(
*,
name: str | None = ...,
description: str | None = ...,
input_schema: type[BaseModel] | None = ...,
) -> Callable[[TFunction], AnyTool]: ...
def tool(
tool_function: TFunction | None = None,
/,
*,
name: str | None = None,
description: str | None = None,
input_schema: type[BaseModel] | None = None,
) -> AnyTool | Callable[[TFunction], AnyTool]:
def create_tool(fn: TFunction) -> AnyTool:
tool_name = name or fn.__name__
tool_description = description or inspect.getdoc(fn)
tool_input = input_schema or get_input_schema(fn)
if tool_description is None:
raise ValueError("No tool description provided.")
class FunctionTool(Tool[Any, ToolRunOptions, ToolOutput]):
name = tool_name
description = tool_description or ""
input_schema = tool_input
def __init__(self, options: dict[str, Any] | None = None) -> None:
super().__init__(options)
def _create_emitter(self) -> Emitter:
return Emitter.root().child(
namespace=["tool", "custom", to_safe_word(self.name)],
creator=self,
)
async def _run(self, input: Any, options: ToolRunOptions | None, context: RunContext) -> ToolOutput:
tool_input_dict = input.model_dump()
if inspect.iscoroutinefunction(fn):
result = await fn(**tool_input_dict)
else:
result = fn(**tool_input_dict)
if isinstance(result, ToolOutput):
return result
else:
return StringToolOutput(result=str(result))
return FunctionTool()
if tool_function is None:
return create_tool
else:
return create_tool(tool_function)