Skip to content

Commit

Permalink
Merge branch 'feat/model-runtime' into deploy/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Dec 28, 2023
2 parents 18828ff + 0b77449 commit 2644503
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions api/core/model_runtime/docs/en_US/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
- `secret-input` Password input component
- `select` Single-choice dropdown
- `radio` Radio component
- `switch` Switch component, only supports `true` and `false` values

### FormOption

Expand Down
1 change: 1 addition & 0 deletions api/core/model_runtime/docs/zh_Hans/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
- `secret-input` 密码输入组件
- `select` 单选下拉
- `radio` Radio 组件
- `switch` 开关组件,仅支持 `true``false`

### FormOption

Expand Down
1 change: 1 addition & 0 deletions api/core/model_runtime/entities/provider_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FormType(Enum):
SECRET_INPUT = "secret-input"
SELECT = "select"
RADIO = "radio"
SWITCH = "switch"


class FormShowOnObject(BaseModel):
Expand Down
4 changes: 2 additions & 2 deletions api/core/model_runtime/model_providers/localai/localai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ model_credential_schema:
credential_form_schemas:
- variable: completion_type
show_on:
- variable: model_type
value: text-generation
- variable: __model_type
value: llm
label:
en_US: Completion type
type: select
Expand Down
26 changes: 16 additions & 10 deletions api/core/model_runtime/schema_validators/common_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,31 @@ def _validate_and_filter_credential_form_schemas(self,
show_on_relations[variable].append(credential_form_schema)

# If the variable does not exist in credentials, it is considered that the cascading form item is not displayed
need_validate_credential_form_schemas = []
need_validate_credential_form_schema_map = {}
for variable, sub_credential_form_schemas in show_on_relations.items():
if variable in credentials and credentials[variable] is not None:
for sub_credential_form_schema in sub_credential_form_schemas:
show_on = sub_credential_form_schema.show_on
for show_on_object in show_on:
if show_on_object.variable == variable and show_on_object.value == credentials[variable]:
need_validate_credential_form_schemas.append(sub_credential_form_schema)
break

# remove duplicate
need_validate_credential_form_schemas = list(set(need_validate_credential_form_schemas))
if sub_credential_form_schema.variable not in need_validate_credential_form_schema_map:
need_validate_credential_form_schema_map[sub_credential_form_schema.variable] \
= sub_credential_form_schema
break

# get all credential_form_schemas where show_on is empty
for credential_form_schema in credential_form_schemas:
if credential_form_schema in need_validate_credential_form_schemas:
if credential_form_schema.variable in need_validate_credential_form_schema_map:
continue

if credential_form_schema.show_on:
continue

need_validate_credential_form_schemas.append(credential_form_schema)
need_validate_credential_form_schema_map[credential_form_schema.variable] = credential_form_schema

# Iterate over the remaining credential_form_schemas, verify each credential_form_schema
validated_credentials = {}
for credential_form_schema in need_validate_credential_form_schemas:
for credential_form_schema in need_validate_credential_form_schema_map.values():
# add the value of the credential_form_schema corresponding to it to validated_credentials
result = self._validate_credential_form_schema(credential_form_schema, credentials)
if result:
Expand Down Expand Up @@ -93,4 +92,11 @@ def _validate_credential_form_schema(self, credential_form_schema: CredentialFor
if value not in [option.value for option in credential_form_schema.options]:
raise ValueError(f'Variable {credential_form_schema.variable} is not in options')

return value
if credential_form_schema.type == FormType.SWITCH:
# If the value is not in ['true', 'false'], an exception is thrown
if value.lower() not in ['true', 'false']:
raise ValueError(f'Variable {credential_form_schema.variable} should be true or false')

value = True if value.lower() == 'true' else False

return value

0 comments on commit 2644503

Please sign in to comment.