-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtool.py
More file actions
334 lines (279 loc) · 11 KB
/
tool.py
File metadata and controls
334 lines (279 loc) · 11 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from collections.abc import Mapping
from enum import Enum, StrEnum
from typing import Any, Union
from pydantic import (
BaseModel,
Field,
field_validator,
model_validator,
)
from dify_plugin.core.documentation.schema_doc import docs
from dify_plugin.core.utils.yaml_loader import load_yaml_file
from dify_plugin.entities import I18nObject, ParameterOption
from dify_plugin.entities.invoke_message import InvokeMessage
from dify_plugin.entities.model.message import PromptMessageTool
from dify_plugin.entities.oauth import OAuthSchema
from dify_plugin.entities.provider_config import (
CommonParameterType,
CredentialType,
ProviderConfig,
)
class ToolRuntime(BaseModel):
credentials: dict[str, Any]
credential_type: CredentialType = CredentialType.API_KEY
user_id: str | None
session_id: str | None
class ToolInvokeMessage(InvokeMessage):
pass
@docs(
description="The identity of the tool",
)
class ToolIdentity(BaseModel):
author: str = Field(..., description="The author of the tool")
name: str = Field(..., description="The name of the tool")
label: I18nObject = Field(..., description="The label of the tool")
@docs(
description="The option of the tool parameter",
)
class ToolParameterOption(ParameterOption):
pass
@docs(
description="The auto generate of the parameter",
)
class ParameterAutoGenerate(BaseModel):
@docs(
description="The type of the auto generate",
)
class Type(StrEnum):
PROMPT_INSTRUCTION = "prompt_instruction"
type: Type
@docs(
description="The template of the parameter",
)
class ParameterTemplate(BaseModel):
enabled: bool = Field(..., description="Whether the parameter is jinja enabled")
@docs(
description="The type of the parameter",
)
class ToolParameter(BaseModel):
@docs(
description="Tool parameter type (aliases CommonParameterType values)",
)
class ToolParameterType(str, Enum):
STRING = CommonParameterType.STRING.value
NUMBER = CommonParameterType.NUMBER.value
BOOLEAN = CommonParameterType.BOOLEAN.value
SELECT = CommonParameterType.SELECT.value
SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
FILE = CommonParameterType.FILE.value
FILES = CommonParameterType.FILES.value
MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value
APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
CHECKBOX = CommonParameterType.CHECKBOX.value
# TOOL_SELECTOR = CommonParameterType.TOOL_SELECTOR.value
ANY = CommonParameterType.ANY.value
# MCP object and array type parameters
OBJECT = CommonParameterType.OBJECT.value
ARRAY = CommonParameterType.ARRAY.value
DYNAMIC_SELECT = CommonParameterType.DYNAMIC_SELECT.value
@docs(
description="The form of the parameter",
)
class ToolParameterForm(Enum):
SCHEMA = "schema" # should be set while adding tool
FORM = "form" # should be set before invoking tool
LLM = "llm" # will be set by LLM
name: str = Field(..., description="The name of the parameter")
label: I18nObject = Field(..., description="The label presented to the user")
human_description: I18nObject = Field(..., description="The description presented to the user")
type: ToolParameterType = Field(..., description="The type of the parameter")
auto_generate: ParameterAutoGenerate | None = Field(default=None, description="The auto generate of the parameter")
template: ParameterTemplate | None = Field(default=None, description="The template of the parameter")
scope: str | None = None
form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
llm_description: str | None = None
required: bool | None = False
default: Union[int, float, str] | None = None
min: Union[float, int] | None = None
max: Union[float, int] | None = None
precision: int | None = None
options: list[ToolParameterOption] | None = None
# MCP object and array type parameters use this field to store the schema
input_schema: Mapping[str, Any] | None = None
@docs(
description="The description of the tool",
)
class ToolDescription(BaseModel):
human: I18nObject = Field(..., description="The description presented to the user")
llm: str = Field(..., description="The description presented to the LLM")
@docs(
name="ToolExtra",
description="The extra of the tool",
)
class ToolConfigurationExtra(BaseModel):
class Python(BaseModel):
source: str
python: Python
@docs(
name="Tool",
description="The manifest of the tool",
)
class ToolConfiguration(BaseModel):
identity: ToolIdentity
parameters: list[ToolParameter] = Field(default=[], description="The parameters of the tool")
description: ToolDescription
extra: ToolConfigurationExtra
has_runtime_parameters: bool = Field(default=False, description="Whether the tool has runtime parameters")
output_schema: Mapping[str, Any] | None = None
@docs(
description="The label of the tool",
)
class ToolLabelEnum(Enum):
SEARCH = "search"
IMAGE = "image"
VIDEOS = "videos"
WEATHER = "weather"
FINANCE = "finance"
DESIGN = "design"
TRAVEL = "travel"
SOCIAL = "social"
NEWS = "news"
MEDICAL = "medical"
PRODUCTIVITY = "productivity"
EDUCATION = "education"
BUSINESS = "business"
ENTERTAINMENT = "entertainment"
UTILITIES = "utilities"
RAG = "rag"
OTHER = "other"
@docs(
description="The identity of the tool provider",
)
class ToolProviderIdentity(BaseModel):
author: str = Field(..., description="The author of the tool")
name: str = Field(..., description="The name of the tool")
description: I18nObject = Field(..., description="The description of the tool")
icon: str = Field(..., description="The icon of the tool")
icon_dark: str | None = Field(None, description="The dark mode icon of the tool")
label: I18nObject = Field(..., description="The label of the tool")
tags: list[ToolLabelEnum] = Field(
default=[],
description="The tags of the tool",
)
@docs(
name="ToolProviderExtra",
description="The extra of the tool provider",
)
class ToolProviderConfigurationExtra(BaseModel):
class Python(BaseModel):
source: str
python: Python
@docs(
name="ToolProvider",
description="The Manifest of the tool provider",
outside_reference_fields={"tools": ToolConfiguration},
)
class ToolProviderConfiguration(BaseModel):
identity: ToolProviderIdentity
credentials_schema: list[ProviderConfig] = Field(
default_factory=list,
alias="credentials_for_provider",
description="The credentials schema of the tool provider",
)
oauth_schema: OAuthSchema | None = Field(
default=None,
description="The OAuth schema of the tool provider if OAuth is supported",
)
tools: list[ToolConfiguration] = Field(default=[], description="The tools of the tool provider")
extra: ToolProviderConfigurationExtra
@model_validator(mode="before")
@classmethod
def validate_credentials_schema(cls, data: dict) -> dict:
original_credentials_for_provider: dict[str, dict] = data.get("credentials_for_provider", {})
credentials_for_provider: list[dict[str, Any]] = []
for name, credential in original_credentials_for_provider.items():
credential["name"] = name
credentials_for_provider.append(credential)
data["credentials_for_provider"] = credentials_for_provider
return data
@field_validator("tools", mode="before")
@classmethod
def validate_tools(cls, value) -> list[ToolConfiguration]:
if not isinstance(value, list):
raise ValueError("tools should be a list")
tools: list[ToolConfiguration] = []
for tool in value:
# read from yaml
if not isinstance(tool, str):
raise ValueError("tool path should be a string")
try:
file = load_yaml_file(tool)
tools.append(
ToolConfiguration(
identity=ToolIdentity(**file["identity"]),
parameters=[ToolParameter(**param) for param in file.get("parameters", []) or []],
description=ToolDescription(**file["description"]),
extra=ToolConfigurationExtra(**file.get("extra", {})),
output_schema=file.get("output_schema", None),
)
)
except Exception as e:
raise ValueError(f"Error loading tool configuration: {e!s}") from e
return tools
class ToolProviderType(Enum):
"""
Enum class for tool provider
"""
BUILT_IN = "builtin"
WORKFLOW = "workflow"
API = "api"
APP = "app"
DATASET_RETRIEVAL = "dataset-retrieval"
MCP = "mcp"
@classmethod
def value_of(cls, value: str) -> "ToolProviderType":
"""
Get value of given mode.
:param value: mode value
:return: mode
"""
for mode in cls:
if mode.value == value:
return mode
raise ValueError(f"invalid mode value {value}")
class ToolSelector(BaseModel):
class Parameter(BaseModel):
name: str = Field(..., description="The name of the parameter")
type: ToolParameter.ToolParameterType = Field(..., description="The type of the parameter")
required: bool = Field(..., description="Whether the parameter is required")
description: str = Field(..., description="The description of the parameter")
default: Union[int, float, str] | None = None
options: list[ToolParameterOption] | None = None
provider_id: str = Field(..., description="The id of the provider")
tool_name: str = Field(..., description="The name of the tool")
tool_description: str = Field(..., description="The description of the tool")
tool_configuration: Mapping[str, Any] = Field(..., description="Configuration, type form")
tool_parameters: Mapping[str, Parameter] = Field(..., description="Parameters, type llm")
def to_prompt_message(self) -> PromptMessageTool:
"""
Convert tool selector to prompt message tool, based on openai function calling schema.
"""
tool = PromptMessageTool(
name=self.tool_name,
description=self.tool_description,
parameters={
"type": "object",
"properties": {},
"required": [],
},
)
for name, parameter in self.tool_parameters.items():
tool.parameters[name] = {
"type": parameter.type.value,
"description": parameter.description,
}
if parameter.required:
tool.parameters["required"].append(name)
if parameter.options:
tool.parameters[name]["enum"] = [option.value for option in parameter.options]
return tool