-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathprovider.py
More file actions
296 lines (236 loc) · 7.73 KB
/
Copy pathprovider.py
File metadata and controls
296 lines (236 loc) · 7.73 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
import glob
import os
from collections.abc import Sequence
from enum import Enum
from pydantic import BaseModel, ConfigDict, Field, 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
from dify_plugin.entities.model import AIModelEntity, ModelType
@docs(
description="Configurate method",
name="ModelConfigurateMethod",
)
class ConfigurateMethod(Enum):
"""
Enum class for configurate method of provider model.
"""
PREDEFINED_MODEL = "predefined-model"
CUSTOMIZABLE_MODEL = "customizable-model"
@docs(
description="Model form type",
name="ModelFormType",
)
class FormType(Enum):
"""
Enum class for form type.
"""
TEXT_INPUT = "text-input"
SECRET_INPUT = "secret-input"
SELECT = "select"
RADIO = "radio"
SWITCH = "switch"
@docs(
description="Form show on",
name="ModelFormShowOnObject",
)
class FormShowOnObject(BaseModel):
"""
Model class for form show on.
"""
variable: str
value: str
@docs(
description="Form option",
name="ModelFormOption",
)
class FormOption(BaseModel):
"""
Model class for form option.
"""
label: I18nObject
value: str
show_on: list[FormShowOnObject] = Field(default_factory=list)
def __init__(self, **data):
super().__init__(**data)
if not self.label:
self.label = I18nObject(en_US=self.value)
@docs(
description="Credential form schema",
name="ModelCredentialFormSchema",
)
class CredentialFormSchema(BaseModel):
"""
Model class for credential form schema.
"""
variable: str
label: I18nObject
type: FormType
required: bool = True
default: str | None = None
options: list[FormOption] | None = None
placeholder: I18nObject | None = None
max_length: int = 0
show_on: list[FormShowOnObject] = Field(default_factory=list)
@docs(
description="Model provider credential schema",
name="ModelProviderCredentialSchema",
)
class ProviderCredentialSchema(BaseModel):
"""
Model class for provider credential schema.
"""
credential_form_schemas: list[CredentialFormSchema]
@docs(
description="Field model schema",
name="ModelFieldModelSchema",
)
class FieldModelSchema(BaseModel):
label: I18nObject
placeholder: I18nObject | None = None
class ModelCredentialSchema(BaseModel):
"""
Model class for model credential schema.
"""
model: FieldModelSchema
credential_form_schemas: list[CredentialFormSchema]
class SimpleProviderEntity(BaseModel):
"""
Simple model class for provider.
"""
provider: str
label: I18nObject
icon_small: I18nObject | None = None
icon_large: I18nObject | None = None
icon_small_dark: I18nObject | None = None
icon_large_dark: I18nObject | None = None
supported_model_types: Sequence[ModelType]
models: list[AIModelEntity] = []
@docs(
description="Model provider help",
name="ModelProviderHelp",
)
class ProviderHelpEntity(BaseModel):
"""
Model class for provider help.
"""
title: I18nObject
url: I18nObject
@docs(
description="Model position",
name="ModelPosition",
)
class ModelPosition(BaseModel):
"""
Model class for ai models
"""
llm: list[str] | None = Field(
default_factory=list, description="Sorts of llm model in ascending order, fill model name here"
)
text_embedding: list[str] | None = Field(
default_factory=list, description="Sorts of text embedding model in ascending order, fill model name here"
)
rerank: list[str] | None = Field(
default_factory=list, description="Sorts of rerank model in ascending order, fill model name here"
)
tts: list[str] | None = Field(
default_factory=list, description="Sorts of tts model in ascending order, fill model name here"
)
speech2text: list[str] | None = Field(
default_factory=list, description="Sorts of speech2text model in ascending order, fill model name here"
)
moderation: list[str] | None = Field(
default_factory=list, description="Sorts of moderation model in ascending order, fill model name here"
)
class ProviderEntity(BaseModel):
"""
Model class for provider.
"""
provider: str
label: I18nObject
description: I18nObject | None = None
icon_small: I18nObject | None = None
icon_large: I18nObject | None = None
icon_small_dark: I18nObject | None = None
icon_large_dark: I18nObject | None = None
background: str | None = None
help: ProviderHelpEntity | None = None
supported_model_types: Sequence[ModelType]
configurate_methods: list[ConfigurateMethod]
models: list[AIModelEntity] = Field(default_factory=list)
provider_credential_schema: ProviderCredentialSchema | None = None
model_credential_schema: ModelCredentialSchema | None = None
position: ModelPosition | None = None
# pydantic configs
model_config = ConfigDict(protected_namespaces=())
def to_simple_provider(self) -> SimpleProviderEntity:
"""
Convert to simple provider.
:return: simple provider
"""
return SimpleProviderEntity(
provider=self.provider,
label=self.label,
icon_small=self.icon_small,
icon_large=self.icon_large,
icon_small_dark=self.icon_small_dark,
icon_large_dark=self.icon_large_dark,
supported_model_types=self.supported_model_types,
models=self.models,
)
@model_validator(mode="before")
@classmethod
def validate_models(cls, values) -> dict:
value = values.get("models", {})
if not isinstance(value, dict):
raise ValueError("models should be a glob path list")
cwd = os.getcwd()
model_entities = []
def load_models(model_type: str):
if model_type not in value:
return
for path in value[model_type].get("predefined", []):
yaml_paths = glob.glob(os.path.join(cwd, path))
for yaml_path in yaml_paths:
if yaml_path.endswith("_position.yaml"):
if "position" not in values:
values["position"] = {}
position = load_yaml_file(yaml_path)
values["position"][model_type] = position
else:
model_entity = load_yaml_file(yaml_path)
if not model_entity:
raise ValueError(f"Error loading model entity: {yaml_path}")
provider_model = AIModelEntity(**model_entity)
model_entities.append(provider_model)
load_models("llm")
load_models("text_embedding")
load_models("rerank")
load_models("tts")
load_models("speech2text")
load_models("moderation")
values["models"] = model_entities
return values
@docs(
description="Model provider configuration extra",
name="ModelProviderExtra",
)
class ModelProviderConfigurationExtra(BaseModel):
class Python(BaseModel):
provider_source: str
model_sources: list[str] = Field(default_factory=list)
model_config = ConfigDict(protected_namespaces=())
python: Python
@docs(
name="ModelProvider",
description="Model provider configuration",
outside_reference_fields={"models": AIModelEntity},
)
class ModelProviderConfiguration(ProviderEntity):
extra: ModelProviderConfigurationExtra
# class ProviderConfig(BaseModel):
# """
# Model class for provider config.
# """
# provider: str
# credentials: dict