Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
2acb7426
Commit
2acb7426
authored
Feb 29, 2024
by
takatost
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add features structure validate
parent
286f7b3e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
49 additions
and
58 deletions
+49
-58
model_config.py
api/controllers/console/app/model_config.py
+1
-35
config_validator.py
api/core/app/advanced_chat/config_validator.py
+7
-2
moderation.py
api/core/app/validators/moderation.py
+10
-8
config_validator.py
api/core/app/workflow/config_validator.py
+7
-2
app_model_config_service.py
api/services/app_model_config_service.py
+0
-9
workflow_service.py
api/services/workflow_service.py
+24
-2
No files found.
api/controllers/console/app/model_config.py
View file @
2acb7426
from
flask
import
request
from
flask_login
import
current_user
from
flask_restful
import
Resource
,
reqparse
from
flask_restful
import
Resource
from
controllers.console
import
api
from
controllers.console.app.wraps
import
get_app_model
...
...
@@ -48,38 +48,4 @@ class ModelConfigResource(Resource):
return
{
'result'
:
'success'
}
class
FeaturesResource
(
Resource
):
@
setup_required
@
login_required
@
account_initialization_required
@
get_app_model
(
mode
=
[
AppMode
.
ADVANCED_CHAT
,
AppMode
.
WORKFLOW
])
def
put
(
self
,
app_model
):
"""Get app features"""
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'features'
,
type
=
dict
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
args
=
parser
.
parse_args
()
model_configuration
=
AppModelConfigService
.
validate_features
(
tenant_id
=
current_user
.
current_tenant_id
,
config
=
args
.
get
(
'features'
),
app_mode
=
AppMode
.
value_of
(
app_model
.
mode
)
)
# update config
app_model_config
=
app_model
.
app_model_config
app_model_config
.
from_model_config_dict
(
model_configuration
)
db
.
session
.
commit
()
app_model_config_was_updated
.
send
(
app_model
,
app_model_config
=
app_model_config
)
return
{
'result'
:
'success'
}
api
.
add_resource
(
ModelConfigResource
,
'/apps/<uuid:app_id>/model-config'
)
api
.
add_resource
(
FeaturesResource
,
'/apps/<uuid:app_id>/features'
)
api/core/app/advanced_chat/config_validator.py
View file @
2acb7426
...
...
@@ -9,12 +9,13 @@ from core.app.validators.text_to_speech import TextToSpeechValidator
class
AdvancedChatAppConfigValidator
:
@
classmethod
def
config_validate
(
cls
,
tenant_id
:
str
,
config
:
dict
)
->
dict
:
def
config_validate
(
cls
,
tenant_id
:
str
,
config
:
dict
,
only_structure_validate
:
bool
=
False
)
->
dict
:
"""
Validate for advanced chat app model config
:param tenant_id: tenant id
:param config: app model config args
:param only_structure_validate: if True, only structure validation will be performed
"""
related_config_keys
=
[]
...
...
@@ -43,7 +44,11 @@ class AdvancedChatAppConfigValidator:
related_config_keys
.
extend
(
current_related_config_keys
)
# moderation validation
config
,
current_related_config_keys
=
ModerationValidator
.
validate_and_set_defaults
(
tenant_id
,
config
)
config
,
current_related_config_keys
=
ModerationValidator
.
validate_and_set_defaults
(
tenant_id
=
tenant_id
,
config
=
config
,
only_structure_validate
=
only_structure_validate
)
related_config_keys
.
extend
(
current_related_config_keys
)
related_config_keys
=
list
(
set
(
related_config_keys
))
...
...
api/core/app/validators/moderation.py
View file @
2acb7426
...
...
@@ -7,7 +7,8 @@ logger = logging.getLogger(__name__)
class
ModerationValidator
:
@
classmethod
def
validate_and_set_defaults
(
cls
,
tenant_id
,
config
:
dict
)
->
tuple
[
dict
,
list
[
str
]]:
def
validate_and_set_defaults
(
cls
,
tenant_id
,
config
:
dict
,
only_structure_validate
:
bool
=
False
)
\
->
tuple
[
dict
,
list
[
str
]]:
if
not
config
.
get
(
"sensitive_word_avoidance"
):
config
[
"sensitive_word_avoidance"
]
=
{
"enabled"
:
False
...
...
@@ -23,13 +24,14 @@ class ModerationValidator:
if
not
config
[
"sensitive_word_avoidance"
]
.
get
(
"type"
):
raise
ValueError
(
"sensitive_word_avoidance.type is required"
)
typ
=
config
[
"sensitive_word_avoidance"
][
"type"
]
config
=
config
[
"sensitive_word_avoidance"
][
"config"
]
if
not
only_structure_validate
:
typ
=
config
[
"sensitive_word_avoidance"
][
"type"
]
config
=
config
[
"sensitive_word_avoidance"
][
"config"
]
ModerationFactory
.
validate_config
(
name
=
typ
,
tenant_id
=
tenant_id
,
config
=
config
)
ModerationFactory
.
validate_config
(
name
=
typ
,
tenant_id
=
tenant_id
,
config
=
config
)
return
config
,
[
"sensitive_word_avoidance"
]
api/core/app/workflow/config_validator.py
View file @
2acb7426
...
...
@@ -5,12 +5,13 @@ from core.app.validators.text_to_speech import TextToSpeechValidator
class
WorkflowAppConfigValidator
:
@
classmethod
def
config_validate
(
cls
,
tenant_id
:
str
,
config
:
dict
)
->
dict
:
def
config_validate
(
cls
,
tenant_id
:
str
,
config
:
dict
,
only_structure_validate
:
bool
=
False
)
->
dict
:
"""
Validate for workflow app model config
:param tenant_id: tenant id
:param config: app model config args
:param only_structure_validate: only validate the structure of the config
"""
related_config_keys
=
[]
...
...
@@ -23,7 +24,11 @@ class WorkflowAppConfigValidator:
related_config_keys
.
extend
(
current_related_config_keys
)
# moderation validation
config
,
current_related_config_keys
=
ModerationValidator
.
validate_and_set_defaults
(
tenant_id
,
config
)
config
,
current_related_config_keys
=
ModerationValidator
.
validate_and_set_defaults
(
tenant_id
=
tenant_id
,
config
=
config
,
only_structure_validate
=
only_structure_validate
)
related_config_keys
.
extend
(
current_related_config_keys
)
related_config_keys
=
list
(
set
(
related_config_keys
))
...
...
api/services/app_model_config_service.py
View file @
2acb7426
...
...
@@ -18,12 +18,3 @@ class AppModelConfigService:
return
CompletionAppConfigValidator
.
config_validate
(
tenant_id
,
config
)
else
:
raise
ValueError
(
f
"Invalid app mode: {app_mode}"
)
@
classmethod
def
validate_features
(
cls
,
tenant_id
:
str
,
config
:
dict
,
app_mode
:
AppMode
)
->
dict
:
if
app_mode
==
AppMode
.
ADVANCED_CHAT
:
return
AdvancedChatAppConfigValidator
.
config_validate
(
tenant_id
,
config
)
elif
app_mode
==
AppMode
.
WORKFLOW
:
return
WorkflowAppConfigValidator
.
config_validate
(
tenant_id
,
config
)
else
:
raise
ValueError
(
f
"Invalid app mode: {app_mode}"
)
api/services/workflow_service.py
View file @
2acb7426
...
...
@@ -2,6 +2,8 @@ import json
from
datetime
import
datetime
from
typing
import
Optional
from
core.app.advanced_chat.config_validator
import
AdvancedChatAppConfigValidator
from
core.app.workflow.config_validator
import
WorkflowAppConfigValidator
from
extensions.ext_database
import
db
from
models.account
import
Account
from
models.model
import
App
,
AppMode
...
...
@@ -56,7 +58,11 @@ class WorkflowService:
# fetch draft workflow by app_model
workflow
=
self
.
get_draft_workflow
(
app_model
=
app_model
)
# TODO validate features
# validate features structure
self
.
validate_features_structure
(
app_model
=
app_model
,
features
=
features
)
# create draft workflow if not found
if
not
workflow
:
...
...
@@ -100,7 +106,7 @@ class WorkflowService:
if
not
draft_workflow
:
raise
ValueError
(
'No valid workflow found.'
)
# TODO check if the workflow
is valid, basic check
# TODO check if the workflow
structure is valid
# create new workflow
workflow
=
Workflow
(
...
...
@@ -153,3 +159,19 @@ class WorkflowService:
)
return
new_app
def
validate_features_structure
(
self
,
app_model
:
App
,
features
:
dict
)
->
dict
:
if
app_model
.
mode
==
AppMode
.
ADVANCED_CHAT
.
value
:
return
AdvancedChatAppConfigValidator
.
config_validate
(
tenant_id
=
app_model
.
tenant_id
,
config
=
features
,
only_structure_validate
=
True
)
elif
app_model
.
mode
==
AppMode
.
WORKFLOW
.
value
:
return
WorkflowAppConfigValidator
.
config_validate
(
tenant_id
=
app_model
.
tenant_id
,
config
=
features
,
only_structure_validate
=
True
)
else
:
raise
ValueError
(
f
"Invalid app mode: {app_model.mode}"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment