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
5968cab1
Commit
5968cab1
authored
Feb 19, 2024
by
takatost
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimize get app model to wraps
parent
57ffecd0
Changes
17
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
231 additions
and
264 deletions
+231
-264
__init__.py
api/controllers/console/__init__.py
+1
-1
__init__.py
api/controllers/console/app/__init__.py
+0
-21
app.py
api/controllers/console/app/app.py
+38
-60
audio.py
api/controllers/console/app/audio.py
+11
-12
completion.py
api/controllers/console/app/completion.py
+10
-26
conversation.py
api/controllers/console/app/conversation.py
+22
-37
message.py
api/controllers/console/app/message.py
+21
-43
model_config.py
api/controllers/console/app/model_config.py
+7
-10
site.py
api/controllers/console/app/site.py
+5
-9
statistic.py
api/controllers/console/app/statistic.py
+16
-22
workflow.py
api/controllers/console/app/workflow.py
+20
-0
wraps.py
api/controllers/console/app/wraps.py
+55
-0
basic_app_runner.py
api/core/app_runner/basic_app_runner.py
+2
-2
application_entities.py
api/core/entities/application_entities.py
+20
-0
prompt_transform.py
api/core/prompt/prompt_transform.py
+1
-19
advanced_prompt_template_service.py
api/services/advanced_prompt_template_service.py
+1
-1
app_model_config_service.py
api/services/app_model_config_service.py
+1
-1
No files found.
api/controllers/console/__init__.py
View file @
5968cab1
...
@@ -8,7 +8,7 @@ api = ExternalApi(bp)
...
@@ -8,7 +8,7 @@ api = ExternalApi(bp)
from
.
import
admin
,
apikey
,
extension
,
feature
,
setup
,
version
from
.
import
admin
,
apikey
,
extension
,
feature
,
setup
,
version
# Import app controllers
# Import app controllers
from
.app
import
(
advanced_prompt_template
,
annotation
,
app
,
audio
,
completion
,
conversation
,
generator
,
message
,
from
.app
import
(
advanced_prompt_template
,
annotation
,
app
,
audio
,
completion
,
conversation
,
generator
,
message
,
model_config
,
site
,
statistic
)
model_config
,
site
,
statistic
,
workflow
)
# Import auth controllers
# Import auth controllers
from
.auth
import
activate
,
data_source_oauth
,
login
,
oauth
from
.auth
import
activate
,
data_source_oauth
,
login
,
oauth
# Import billing controllers
# Import billing controllers
...
...
api/controllers/console/app/__init__.py
View file @
5968cab1
from
controllers.console.app.error
import
AppUnavailableError
from
extensions.ext_database
import
db
from
flask_login
import
current_user
from
models.model
import
App
from
werkzeug.exceptions
import
NotFound
def
_get_app
(
app_id
,
mode
=
None
):
app
=
db
.
session
.
query
(
App
)
.
filter
(
App
.
id
==
app_id
,
App
.
tenant_id
==
current_user
.
current_tenant_id
,
App
.
status
==
'normal'
)
.
first
()
if
not
app
:
raise
NotFound
(
"App not found"
)
if
mode
and
app
.
mode
!=
mode
:
raise
NotFound
(
"The {} app not found"
.
format
(
mode
))
return
app
api/controllers/console/app/app.py
View file @
5968cab1
...
@@ -9,7 +9,8 @@ from werkzeug.exceptions import Forbidden
...
@@ -9,7 +9,8 @@ from werkzeug.exceptions import Forbidden
from
constants.languages
import
demo_model_templates
,
languages
from
constants.languages
import
demo_model_templates
,
languages
from
constants.model_template
import
model_templates
from
constants.model_template
import
model_templates
from
controllers.console
import
api
from
controllers.console
import
api
from
controllers.console.app.error
import
AppNotFoundError
,
ProviderNotInitializeError
from
controllers.console.app.error
import
ProviderNotInitializeError
from
controllers.console.app.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
,
cloud_edition_billing_resource_check
from
controllers.console.wraps
import
account_initialization_required
,
cloud_edition_billing_resource_check
from
core.errors.error
import
LLMBadRequestError
,
ProviderTokenNotInitError
from
core.errors.error
import
LLMBadRequestError
,
ProviderTokenNotInitError
...
@@ -29,13 +30,6 @@ from models.model import App, AppModelConfig, Site
...
@@ -29,13 +30,6 @@ from models.model import App, AppModelConfig, Site
from
services.app_model_config_service
import
AppModelConfigService
from
services.app_model_config_service
import
AppModelConfigService
def
_get_app
(
app_id
,
tenant_id
):
app
=
db
.
session
.
query
(
App
)
.
filter
(
App
.
id
==
app_id
,
App
.
tenant_id
==
tenant_id
)
.
first
()
if
not
app
:
raise
AppNotFoundError
return
app
class
AppListApi
(
Resource
):
class
AppListApi
(
Resource
):
@
setup_required
@
setup_required
...
@@ -232,33 +226,28 @@ class AppApi(Resource):
...
@@ -232,33 +226,28 @@ class AppApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
app_detail_fields_with_site
)
@
marshal_with
(
app_detail_fields_with_site
)
def
get
(
self
,
app_
id
):
def
get
(
self
,
app_
model
):
"""Get app detail"""
"""Get app detail"""
app_id
=
str
(
app_id
)
return
app_model
app
=
_get_app
(
app_id
,
current_user
.
current_tenant_id
)
return
app
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
delete
(
self
,
app_id
):
@
get_app_model
def
delete
(
self
,
app_model
):
"""Delete app"""
"""Delete app"""
app_id
=
str
(
app_id
)
if
not
current_user
.
is_admin_or_owner
:
if
not
current_user
.
is_admin_or_owner
:
raise
Forbidden
()
raise
Forbidden
()
app
=
_get_app
(
app_id
,
current_user
.
current_tenant_id
)
db
.
session
.
delete
(
app_model
)
db
.
session
.
delete
(
app
)
db
.
session
.
commit
()
db
.
session
.
commit
()
# todo delete related data??
# todo delete related data??
# model_config, site, api_token, conversation, message, message_feedback, message_annotation
# model_config, site, api_token, conversation, message, message_feedback, message_annotation
app_was_deleted
.
send
(
app
)
app_was_deleted
.
send
(
app
_model
)
return
{
'result'
:
'success'
},
204
return
{
'result'
:
'success'
},
204
...
@@ -267,86 +256,77 @@ class AppNameApi(Resource):
...
@@ -267,86 +256,77 @@ class AppNameApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
app_detail_fields
)
@
marshal_with
(
app_detail_fields
)
def
post
(
self
,
app_id
):
def
post
(
self
,
app_model
):
app_id
=
str
(
app_id
)
app
=
_get_app
(
app_id
,
current_user
.
current_tenant_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'name'
,
type
=
str
,
required
=
True
,
location
=
'json'
)
parser
.
add_argument
(
'name'
,
type
=
str
,
required
=
True
,
location
=
'json'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
app
.
name
=
args
.
get
(
'name'
)
app
_model
.
name
=
args
.
get
(
'name'
)
app
.
updated_at
=
datetime
.
utcnow
()
app
_model
.
updated_at
=
datetime
.
utcnow
()
db
.
session
.
commit
()
db
.
session
.
commit
()
return
app
return
app
_model
class
AppIconApi
(
Resource
):
class
AppIconApi
(
Resource
):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
app_detail_fields
)
@
marshal_with
(
app_detail_fields
)
def
post
(
self
,
app_id
):
def
post
(
self
,
app_model
):
app_id
=
str
(
app_id
)
app
=
_get_app
(
app_id
,
current_user
.
current_tenant_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'icon'
,
type
=
str
,
location
=
'json'
)
parser
.
add_argument
(
'icon'
,
type
=
str
,
location
=
'json'
)
parser
.
add_argument
(
'icon_background'
,
type
=
str
,
location
=
'json'
)
parser
.
add_argument
(
'icon_background'
,
type
=
str
,
location
=
'json'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
app
.
icon
=
args
.
get
(
'icon'
)
app
_model
.
icon
=
args
.
get
(
'icon'
)
app
.
icon_background
=
args
.
get
(
'icon_background'
)
app
_model
.
icon_background
=
args
.
get
(
'icon_background'
)
app
.
updated_at
=
datetime
.
utcnow
()
app
_model
.
updated_at
=
datetime
.
utcnow
()
db
.
session
.
commit
()
db
.
session
.
commit
()
return
app
return
app
_model
class
AppSiteStatus
(
Resource
):
class
AppSiteStatus
(
Resource
):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
app_detail_fields
)
@
marshal_with
(
app_detail_fields
)
def
post
(
self
,
app_
id
):
def
post
(
self
,
app_
model
):
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'enable_site'
,
type
=
bool
,
required
=
True
,
location
=
'json'
)
parser
.
add_argument
(
'enable_site'
,
type
=
bool
,
required
=
True
,
location
=
'json'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
app_id
=
str
(
app_id
)
app
=
db
.
session
.
query
(
App
)
.
filter
(
App
.
id
==
app_id
,
App
.
tenant_id
==
current_user
.
current_tenant_id
)
.
first
()
if
not
app
:
raise
AppNotFoundError
if
args
.
get
(
'enable_site'
)
==
app
.
enable_site
:
if
args
.
get
(
'enable_site'
)
==
app
_model
.
enable_site
:
return
app
return
app
_model
app
.
enable_site
=
args
.
get
(
'enable_site'
)
app
_model
.
enable_site
=
args
.
get
(
'enable_site'
)
app
.
updated_at
=
datetime
.
utcnow
()
app
_model
.
updated_at
=
datetime
.
utcnow
()
db
.
session
.
commit
()
db
.
session
.
commit
()
return
app
return
app
_model
class
AppApiStatus
(
Resource
):
class
AppApiStatus
(
Resource
):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
app_detail_fields
)
@
marshal_with
(
app_detail_fields
)
def
post
(
self
,
app_
id
):
def
post
(
self
,
app_
model
):
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'enable_api'
,
type
=
bool
,
required
=
True
,
location
=
'json'
)
parser
.
add_argument
(
'enable_api'
,
type
=
bool
,
required
=
True
,
location
=
'json'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
app_id
=
str
(
app_id
)
if
args
.
get
(
'enable_api'
)
==
app_model
.
enable_api
:
app
=
_get_app
(
app_id
,
current_user
.
current_tenant_id
)
return
app_model
if
args
.
get
(
'enable_api'
)
==
app
.
enable_api
:
app_model
.
enable_api
=
args
.
get
(
'enable_api'
)
return
app
app_model
.
updated_at
=
datetime
.
utcnow
()
app
.
enable_api
=
args
.
get
(
'enable_api'
)
app
.
updated_at
=
datetime
.
utcnow
()
db
.
session
.
commit
()
db
.
session
.
commit
()
return
app
return
app
_model
class
AppCopy
(
Resource
):
class
AppCopy
(
Resource
):
...
@@ -376,16 +356,14 @@ class AppCopy(Resource):
...
@@ -376,16 +356,14 @@ class AppCopy(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
app_detail_fields
)
@
marshal_with
(
app_detail_fields
)
def
post
(
self
,
app_id
):
def
post
(
self
,
app_model
):
app_id
=
str
(
app_id
)
copy_app
=
self
.
create_app_copy
(
app_model
)
app
=
_get_app
(
app_id
,
current_user
.
current_tenant_id
)
copy_app
=
self
.
create_app_copy
(
app
)
db
.
session
.
add
(
copy_app
)
db
.
session
.
add
(
copy_app
)
app_config
=
db
.
session
.
query
(
AppModelConfig
)
.
\
app_config
=
db
.
session
.
query
(
AppModelConfig
)
.
\
filter
(
AppModelConfig
.
app_id
==
app_id
)
.
\
filter
(
AppModelConfig
.
app_id
==
app_
model
.
id
)
.
\
one_or_none
()
one_or_none
()
if
app_config
:
if
app_config
:
...
...
api/controllers/console/app/audio.py
View file @
5968cab1
...
@@ -6,7 +6,6 @@ from werkzeug.exceptions import InternalServerError
...
@@ -6,7 +6,6 @@ from werkzeug.exceptions import InternalServerError
import
services
import
services
from
controllers.console
import
api
from
controllers.console
import
api
from
controllers.console.app
import
_get_app
from
controllers.console.app.error
import
(
from
controllers.console.app.error
import
(
AppUnavailableError
,
AppUnavailableError
,
AudioTooLargeError
,
AudioTooLargeError
,
...
@@ -18,8 +17,10 @@ from controllers.console.app.error import (
...
@@ -18,8 +17,10 @@ from controllers.console.app.error import (
ProviderQuotaExceededError
,
ProviderQuotaExceededError
,
UnsupportedAudioTypeError
,
UnsupportedAudioTypeError
,
)
)
from
controllers.console.app.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
from
controllers.console.wraps
import
account_initialization_required
from
core.entities.application_entities
import
AppMode
from
core.errors.error
import
ModelCurrentlyNotSupportError
,
ProviderTokenNotInitError
,
QuotaExceededError
from
core.errors.error
import
ModelCurrentlyNotSupportError
,
ProviderTokenNotInitError
,
QuotaExceededError
from
core.model_runtime.errors.invoke
import
InvokeError
from
core.model_runtime.errors.invoke
import
InvokeError
from
libs.login
import
login_required
from
libs.login
import
login_required
...
@@ -36,10 +37,8 @@ class ChatMessageAudioApi(Resource):
...
@@ -36,10 +37,8 @@ class ChatMessageAudioApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
post
(
self
,
app_id
):
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
app_id
=
str
(
app_id
)
def
post
(
self
,
app_model
):
app_model
=
_get_app
(
app_id
,
'chat'
)
file
=
request
.
files
[
'file'
]
file
=
request
.
files
[
'file'
]
try
:
try
:
...
@@ -80,10 +79,8 @@ class ChatMessageTextApi(Resource):
...
@@ -80,10 +79,8 @@ class ChatMessageTextApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
post
(
self
,
app_id
):
@
get_app_model
app_id
=
str
(
app_id
)
def
post
(
self
,
app_model
):
app_model
=
_get_app
(
app_id
,
None
)
try
:
try
:
response
=
AudioService
.
transcript_tts
(
response
=
AudioService
.
transcript_tts
(
tenant_id
=
app_model
.
tenant_id
,
tenant_id
=
app_model
.
tenant_id
,
...
@@ -120,9 +117,11 @@ class ChatMessageTextApi(Resource):
...
@@ -120,9 +117,11 @@ class ChatMessageTextApi(Resource):
class
TextModesApi
(
Resource
):
class
TextModesApi
(
Resource
):
def
get
(
self
,
app_id
:
str
):
@
setup_required
app_model
=
_get_app
(
str
(
app_id
))
@
login_required
@
account_initialization_required
@
get_app_model
def
get
(
self
,
app_model
):
try
:
try
:
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'language'
,
type
=
str
,
required
=
True
,
location
=
'args'
)
parser
.
add_argument
(
'language'
,
type
=
str
,
required
=
True
,
location
=
'args'
)
...
...
api/controllers/console/app/completion.py
View file @
5968cab1
...
@@ -10,7 +10,6 @@ from werkzeug.exceptions import InternalServerError, NotFound
...
@@ -10,7 +10,6 @@ from werkzeug.exceptions import InternalServerError, NotFound
import
services
import
services
from
controllers.console
import
api
from
controllers.console
import
api
from
controllers.console.app
import
_get_app
from
controllers.console.app.error
import
(
from
controllers.console.app.error
import
(
AppUnavailableError
,
AppUnavailableError
,
CompletionRequestError
,
CompletionRequestError
,
...
@@ -19,10 +18,11 @@ from controllers.console.app.error import (
...
@@ -19,10 +18,11 @@ from controllers.console.app.error import (
ProviderNotInitializeError
,
ProviderNotInitializeError
,
ProviderQuotaExceededError
,
ProviderQuotaExceededError
,
)
)
from
controllers.console.app.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
from
controllers.console.wraps
import
account_initialization_required
from
core.application_queue_manager
import
ApplicationQueueManager
from
core.application_queue_manager
import
ApplicationQueueManager
from
core.entities.application_entities
import
InvokeFrom
from
core.entities.application_entities
import
InvokeFrom
,
AppMode
from
core.errors.error
import
ModelCurrentlyNotSupportError
,
ProviderTokenNotInitError
,
QuotaExceededError
from
core.errors.error
import
ModelCurrentlyNotSupportError
,
ProviderTokenNotInitError
,
QuotaExceededError
from
core.model_runtime.errors.invoke
import
InvokeError
from
core.model_runtime.errors.invoke
import
InvokeError
from
libs.helper
import
uuid_value
from
libs.helper
import
uuid_value
...
@@ -36,12 +36,8 @@ class CompletionMessageApi(Resource):
...
@@ -36,12 +36,8 @@ class CompletionMessageApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
post
(
self
,
app_id
):
@
get_app_model
(
mode
=
AppMode
.
WORKFLOW
)
app_id
=
str
(
app_id
)
def
post
(
self
,
app_model
):
# get app info
app_model
=
_get_app
(
app_id
,
'completion'
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'inputs'
,
type
=
dict
,
required
=
True
,
location
=
'json'
)
parser
.
add_argument
(
'inputs'
,
type
=
dict
,
required
=
True
,
location
=
'json'
)
parser
.
add_argument
(
'query'
,
type
=
str
,
location
=
'json'
,
default
=
''
)
parser
.
add_argument
(
'query'
,
type
=
str
,
location
=
'json'
,
default
=
''
)
...
@@ -93,12 +89,8 @@ class CompletionMessageStopApi(Resource):
...
@@ -93,12 +89,8 @@ class CompletionMessageStopApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
post
(
self
,
app_id
,
task_id
):
@
get_app_model
(
mode
=
AppMode
.
WORKFLOW
)
app_id
=
str
(
app_id
)
def
post
(
self
,
app_model
,
task_id
):
# get app info
_get_app
(
app_id
,
'completion'
)
account
=
flask_login
.
current_user
account
=
flask_login
.
current_user
ApplicationQueueManager
.
set_stop_flag
(
task_id
,
InvokeFrom
.
DEBUGGER
,
account
.
id
)
ApplicationQueueManager
.
set_stop_flag
(
task_id
,
InvokeFrom
.
DEBUGGER
,
account
.
id
)
...
@@ -110,12 +102,8 @@ class ChatMessageApi(Resource):
...
@@ -110,12 +102,8 @@ class ChatMessageApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
post
(
self
,
app_id
):
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
app_id
=
str
(
app_id
)
def
post
(
self
,
app_model
):
# get app info
app_model
=
_get_app
(
app_id
,
'chat'
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'inputs'
,
type
=
dict
,
required
=
True
,
location
=
'json'
)
parser
.
add_argument
(
'inputs'
,
type
=
dict
,
required
=
True
,
location
=
'json'
)
parser
.
add_argument
(
'query'
,
type
=
str
,
required
=
True
,
location
=
'json'
)
parser
.
add_argument
(
'query'
,
type
=
str
,
required
=
True
,
location
=
'json'
)
...
@@ -179,12 +167,8 @@ class ChatMessageStopApi(Resource):
...
@@ -179,12 +167,8 @@ class ChatMessageStopApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
post
(
self
,
app_id
,
task_id
):
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
app_id
=
str
(
app_id
)
def
post
(
self
,
app_model
,
task_id
):
# get app info
_get_app
(
app_id
,
'chat'
)
account
=
flask_login
.
current_user
account
=
flask_login
.
current_user
ApplicationQueueManager
.
set_stop_flag
(
task_id
,
InvokeFrom
.
DEBUGGER
,
account
.
id
)
ApplicationQueueManager
.
set_stop_flag
(
task_id
,
InvokeFrom
.
DEBUGGER
,
account
.
id
)
...
...
api/controllers/console/app/conversation.py
View file @
5968cab1
...
@@ -9,9 +9,10 @@ from sqlalchemy.orm import joinedload
...
@@ -9,9 +9,10 @@ from sqlalchemy.orm import joinedload
from
werkzeug.exceptions
import
NotFound
from
werkzeug.exceptions
import
NotFound
from
controllers.console
import
api
from
controllers.console
import
api
from
controllers.console.app
import
_get_app
from
controllers.console.app
.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
from
controllers.console.wraps
import
account_initialization_required
from
core.entities.application_entities
import
AppMode
from
extensions.ext_database
import
db
from
extensions.ext_database
import
db
from
fields.conversation_fields
import
(
from
fields.conversation_fields
import
(
conversation_detail_fields
,
conversation_detail_fields
,
...
@@ -29,10 +30,9 @@ class CompletionConversationApi(Resource):
...
@@ -29,10 +30,9 @@ class CompletionConversationApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
(
mode
=
AppMode
.
WORKFLOW
)
@
marshal_with
(
conversation_pagination_fields
)
@
marshal_with
(
conversation_pagination_fields
)
def
get
(
self
,
app_id
):
def
get
(
self
,
app_model
):
app_id
=
str
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'keyword'
,
type
=
str
,
location
=
'args'
)
parser
.
add_argument
(
'keyword'
,
type
=
str
,
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
@@ -43,10 +43,7 @@ class CompletionConversationApi(Resource):
...
@@ -43,10 +43,7 @@ class CompletionConversationApi(Resource):
parser
.
add_argument
(
'limit'
,
type
=
int_range
(
1
,
100
),
default
=
20
,
location
=
'args'
)
parser
.
add_argument
(
'limit'
,
type
=
int_range
(
1
,
100
),
default
=
20
,
location
=
'args'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
# get app info
query
=
db
.
select
(
Conversation
)
.
where
(
Conversation
.
app_id
==
app_model
.
id
,
Conversation
.
mode
==
'completion'
)
app
=
_get_app
(
app_id
,
'completion'
)
query
=
db
.
select
(
Conversation
)
.
where
(
Conversation
.
app_id
==
app
.
id
,
Conversation
.
mode
==
'completion'
)
if
args
[
'keyword'
]:
if
args
[
'keyword'
]:
query
=
query
.
join
(
query
=
query
.
join
(
...
@@ -106,24 +103,22 @@ class CompletionConversationDetailApi(Resource):
...
@@ -106,24 +103,22 @@ class CompletionConversationDetailApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
(
mode
=
AppMode
.
WORKFLOW
)
@
marshal_with
(
conversation_message_detail_fields
)
@
marshal_with
(
conversation_message_detail_fields
)
def
get
(
self
,
app_id
,
conversation_id
):
def
get
(
self
,
app_model
,
conversation_id
):
app_id
=
str
(
app_id
)
conversation_id
=
str
(
conversation_id
)
conversation_id
=
str
(
conversation_id
)
return
_get_conversation
(
app_
id
,
conversation_id
,
'completion'
)
return
_get_conversation
(
app_
model
,
conversation_id
)
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
delete
(
self
,
app_id
,
conversation_id
):
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
app_id
=
str
(
app_id
)
def
delete
(
self
,
app_model
,
conversation_id
):
conversation_id
=
str
(
conversation_id
)
conversation_id
=
str
(
conversation_id
)
app
=
_get_app
(
app_id
,
'chat'
)
conversation
=
db
.
session
.
query
(
Conversation
)
\
conversation
=
db
.
session
.
query
(
Conversation
)
\
.
filter
(
Conversation
.
id
==
conversation_id
,
Conversation
.
app_id
==
app
.
id
)
.
first
()
.
filter
(
Conversation
.
id
==
conversation_id
,
Conversation
.
app_id
==
app
_model
.
id
)
.
first
()
if
not
conversation
:
if
not
conversation
:
raise
NotFound
(
"Conversation Not Exists."
)
raise
NotFound
(
"Conversation Not Exists."
)
...
@@ -139,10 +134,9 @@ class ChatConversationApi(Resource):
...
@@ -139,10 +134,9 @@ class ChatConversationApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
@
marshal_with
(
conversation_with_summary_pagination_fields
)
@
marshal_with
(
conversation_with_summary_pagination_fields
)
def
get
(
self
,
app_id
):
def
get
(
self
,
app_model
):
app_id
=
str
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'keyword'
,
type
=
str
,
location
=
'args'
)
parser
.
add_argument
(
'keyword'
,
type
=
str
,
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
@@ -154,10 +148,7 @@ class ChatConversationApi(Resource):
...
@@ -154,10 +148,7 @@ class ChatConversationApi(Resource):
parser
.
add_argument
(
'limit'
,
type
=
int_range
(
1
,
100
),
required
=
False
,
default
=
20
,
location
=
'args'
)
parser
.
add_argument
(
'limit'
,
type
=
int_range
(
1
,
100
),
required
=
False
,
default
=
20
,
location
=
'args'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
# get app info
query
=
db
.
select
(
Conversation
)
.
where
(
Conversation
.
app_id
==
app_model
.
id
,
Conversation
.
mode
==
'chat'
)
app
=
_get_app
(
app_id
,
'chat'
)
query
=
db
.
select
(
Conversation
)
.
where
(
Conversation
.
app_id
==
app
.
id
,
Conversation
.
mode
==
'chat'
)
if
args
[
'keyword'
]:
if
args
[
'keyword'
]:
query
=
query
.
join
(
query
=
query
.
join
(
...
@@ -228,25 +219,22 @@ class ChatConversationDetailApi(Resource):
...
@@ -228,25 +219,22 @@ class ChatConversationDetailApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
@
marshal_with
(
conversation_detail_fields
)
@
marshal_with
(
conversation_detail_fields
)
def
get
(
self
,
app_id
,
conversation_id
):
def
get
(
self
,
app_model
,
conversation_id
):
app_id
=
str
(
app_id
)
conversation_id
=
str
(
conversation_id
)
conversation_id
=
str
(
conversation_id
)
return
_get_conversation
(
app_
id
,
conversation_id
,
'chat'
)
return
_get_conversation
(
app_
model
,
conversation_id
)
@
setup_required
@
setup_required
@
login_required
@
login_required
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
@
account_initialization_required
@
account_initialization_required
def
delete
(
self
,
app_id
,
conversation_id
):
def
delete
(
self
,
app_model
,
conversation_id
):
app_id
=
str
(
app_id
)
conversation_id
=
str
(
conversation_id
)
conversation_id
=
str
(
conversation_id
)
# get app info
app
=
_get_app
(
app_id
,
'chat'
)
conversation
=
db
.
session
.
query
(
Conversation
)
\
conversation
=
db
.
session
.
query
(
Conversation
)
\
.
filter
(
Conversation
.
id
==
conversation_id
,
Conversation
.
app_id
==
app
.
id
)
.
first
()
.
filter
(
Conversation
.
id
==
conversation_id
,
Conversation
.
app_id
==
app
_model
.
id
)
.
first
()
if
not
conversation
:
if
not
conversation
:
raise
NotFound
(
"Conversation Not Exists."
)
raise
NotFound
(
"Conversation Not Exists."
)
...
@@ -263,12 +251,9 @@ api.add_resource(ChatConversationApi, '/apps/<uuid:app_id>/chat-conversations')
...
@@ -263,12 +251,9 @@ api.add_resource(ChatConversationApi, '/apps/<uuid:app_id>/chat-conversations')
api
.
add_resource
(
ChatConversationDetailApi
,
'/apps/<uuid:app_id>/chat-conversations/<uuid:conversation_id>'
)
api
.
add_resource
(
ChatConversationDetailApi
,
'/apps/<uuid:app_id>/chat-conversations/<uuid:conversation_id>'
)
def
_get_conversation
(
app_id
,
conversation_id
,
mode
):
def
_get_conversation
(
app_model
,
conversation_id
):
# get app info
app
=
_get_app
(
app_id
,
mode
)
conversation
=
db
.
session
.
query
(
Conversation
)
\
conversation
=
db
.
session
.
query
(
Conversation
)
\
.
filter
(
Conversation
.
id
==
conversation_id
,
Conversation
.
app_id
==
app
.
id
)
.
first
()
.
filter
(
Conversation
.
id
==
conversation_id
,
Conversation
.
app_id
==
app
_model
.
id
)
.
first
()
if
not
conversation
:
if
not
conversation
:
raise
NotFound
(
"Conversation Not Exists."
)
raise
NotFound
(
"Conversation Not Exists."
)
...
...
api/controllers/console/app/message.py
View file @
5968cab1
...
@@ -10,7 +10,6 @@ from flask_restful.inputs import int_range
...
@@ -10,7 +10,6 @@ from flask_restful.inputs import int_range
from
werkzeug.exceptions
import
Forbidden
,
InternalServerError
,
NotFound
from
werkzeug.exceptions
import
Forbidden
,
InternalServerError
,
NotFound
from
controllers.console
import
api
from
controllers.console
import
api
from
controllers.console.app
import
_get_app
from
controllers.console.app.error
import
(
from
controllers.console.app.error
import
(
AppMoreLikeThisDisabledError
,
AppMoreLikeThisDisabledError
,
CompletionRequestError
,
CompletionRequestError
,
...
@@ -18,9 +17,10 @@ from controllers.console.app.error import (
...
@@ -18,9 +17,10 @@ from controllers.console.app.error import (
ProviderNotInitializeError
,
ProviderNotInitializeError
,
ProviderQuotaExceededError
,
ProviderQuotaExceededError
,
)
)
from
controllers.console.app.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
,
cloud_edition_billing_resource_check
from
controllers.console.wraps
import
account_initialization_required
,
cloud_edition_billing_resource_check
from
core.entities.application_entities
import
InvokeFrom
from
core.entities.application_entities
import
InvokeFrom
,
AppMode
from
core.errors.error
import
ModelCurrentlyNotSupportError
,
ProviderTokenNotInitError
,
QuotaExceededError
from
core.errors.error
import
ModelCurrentlyNotSupportError
,
ProviderTokenNotInitError
,
QuotaExceededError
from
core.model_runtime.errors.invoke
import
InvokeError
from
core.model_runtime.errors.invoke
import
InvokeError
from
extensions.ext_database
import
db
from
extensions.ext_database
import
db
...
@@ -46,14 +46,10 @@ class ChatMessageListApi(Resource):
...
@@ -46,14 +46,10 @@ class ChatMessageListApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
@
account_initialization_required
@
account_initialization_required
@
marshal_with
(
message_infinite_scroll_pagination_fields
)
@
marshal_with
(
message_infinite_scroll_pagination_fields
)
def
get
(
self
,
app_id
):
def
get
(
self
,
app_model
):
app_id
=
str
(
app_id
)
# get app info
app
=
_get_app
(
app_id
,
'chat'
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'conversation_id'
,
required
=
True
,
type
=
uuid_value
,
location
=
'args'
)
parser
.
add_argument
(
'conversation_id'
,
required
=
True
,
type
=
uuid_value
,
location
=
'args'
)
parser
.
add_argument
(
'first_id'
,
type
=
uuid_value
,
location
=
'args'
)
parser
.
add_argument
(
'first_id'
,
type
=
uuid_value
,
location
=
'args'
)
...
@@ -62,7 +58,7 @@ class ChatMessageListApi(Resource):
...
@@ -62,7 +58,7 @@ class ChatMessageListApi(Resource):
conversation
=
db
.
session
.
query
(
Conversation
)
.
filter
(
conversation
=
db
.
session
.
query
(
Conversation
)
.
filter
(
Conversation
.
id
==
args
[
'conversation_id'
],
Conversation
.
id
==
args
[
'conversation_id'
],
Conversation
.
app_id
==
app
.
id
Conversation
.
app_id
==
app
_model
.
id
)
.
first
()
)
.
first
()
if
not
conversation
:
if
not
conversation
:
...
@@ -110,12 +106,8 @@ class MessageFeedbackApi(Resource):
...
@@ -110,12 +106,8 @@ class MessageFeedbackApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
post
(
self
,
app_id
):
@
get_app_model
app_id
=
str
(
app_id
)
def
post
(
self
,
app_model
):
# get app info
app
=
_get_app
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'message_id'
,
required
=
True
,
type
=
uuid_value
,
location
=
'json'
)
parser
.
add_argument
(
'message_id'
,
required
=
True
,
type
=
uuid_value
,
location
=
'json'
)
parser
.
add_argument
(
'rating'
,
type
=
str
,
choices
=
[
'like'
,
'dislike'
,
None
],
location
=
'json'
)
parser
.
add_argument
(
'rating'
,
type
=
str
,
choices
=
[
'like'
,
'dislike'
,
None
],
location
=
'json'
)
...
@@ -125,7 +117,7 @@ class MessageFeedbackApi(Resource):
...
@@ -125,7 +117,7 @@ class MessageFeedbackApi(Resource):
message
=
db
.
session
.
query
(
Message
)
.
filter
(
message
=
db
.
session
.
query
(
Message
)
.
filter
(
Message
.
id
==
message_id
,
Message
.
id
==
message_id
,
Message
.
app_id
==
app
.
id
Message
.
app_id
==
app
_model
.
id
)
.
first
()
)
.
first
()
if
not
message
:
if
not
message
:
...
@@ -141,7 +133,7 @@ class MessageFeedbackApi(Resource):
...
@@ -141,7 +133,7 @@ class MessageFeedbackApi(Resource):
raise
ValueError
(
'rating cannot be None when feedback not exists'
)
raise
ValueError
(
'rating cannot be None when feedback not exists'
)
else
:
else
:
feedback
=
MessageFeedback
(
feedback
=
MessageFeedback
(
app_id
=
app
.
id
,
app_id
=
app
_model
.
id
,
conversation_id
=
message
.
conversation_id
,
conversation_id
=
message
.
conversation_id
,
message_id
=
message
.
id
,
message_id
=
message
.
id
,
rating
=
args
[
'rating'
],
rating
=
args
[
'rating'
],
...
@@ -160,21 +152,20 @@ class MessageAnnotationApi(Resource):
...
@@ -160,21 +152,20 @@ class MessageAnnotationApi(Resource):
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
cloud_edition_billing_resource_check
(
'annotation'
)
@
cloud_edition_billing_resource_check
(
'annotation'
)
@
get_app_model
@
marshal_with
(
annotation_fields
)
@
marshal_with
(
annotation_fields
)
def
post
(
self
,
app_
id
):
def
post
(
self
,
app_
model
):
# The role of the current user in the ta table must be admin or owner
# The role of the current user in the ta table must be admin or owner
if
not
current_user
.
is_admin_or_owner
:
if
not
current_user
.
is_admin_or_owner
:
raise
Forbidden
()
raise
Forbidden
()
app_id
=
str
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'message_id'
,
required
=
False
,
type
=
uuid_value
,
location
=
'json'
)
parser
.
add_argument
(
'message_id'
,
required
=
False
,
type
=
uuid_value
,
location
=
'json'
)
parser
.
add_argument
(
'question'
,
required
=
True
,
type
=
str
,
location
=
'json'
)
parser
.
add_argument
(
'question'
,
required
=
True
,
type
=
str
,
location
=
'json'
)
parser
.
add_argument
(
'answer'
,
required
=
True
,
type
=
str
,
location
=
'json'
)
parser
.
add_argument
(
'answer'
,
required
=
True
,
type
=
str
,
location
=
'json'
)
parser
.
add_argument
(
'annotation_reply'
,
required
=
False
,
type
=
dict
,
location
=
'json'
)
parser
.
add_argument
(
'annotation_reply'
,
required
=
False
,
type
=
dict
,
location
=
'json'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
annotation
=
AppAnnotationService
.
up_insert_app_annotation_from_message
(
args
,
app_id
)
annotation
=
AppAnnotationService
.
up_insert_app_annotation_from_message
(
args
,
app_
model
.
id
)
return
annotation
return
annotation
...
@@ -183,14 +174,10 @@ class MessageAnnotationCountApi(Resource):
...
@@ -183,14 +174,10 @@ class MessageAnnotationCountApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
):
@
get_app_model
app_id
=
str
(
app_id
)
def
get
(
self
,
app_model
):
# get app info
app
=
_get_app
(
app_id
)
count
=
db
.
session
.
query
(
MessageAnnotation
)
.
filter
(
count
=
db
.
session
.
query
(
MessageAnnotation
)
.
filter
(
MessageAnnotation
.
app_id
==
app
.
id
MessageAnnotation
.
app_id
==
app
_model
.
id
)
.
count
()
)
.
count
()
return
{
'count'
:
count
}
return
{
'count'
:
count
}
...
@@ -200,8 +187,8 @@ class MessageMoreLikeThisApi(Resource):
...
@@ -200,8 +187,8 @@ class MessageMoreLikeThisApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
,
message_id
):
@
get_app_model
(
mode
=
AppMode
.
COMPLETION
)
app_id
=
str
(
app_id
)
def
get
(
self
,
app_model
,
message_id
):
message_id
=
str
(
message_id
)
message_id
=
str
(
message_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
...
@@ -211,9 +198,6 @@ class MessageMoreLikeThisApi(Resource):
...
@@ -211,9 +198,6 @@ class MessageMoreLikeThisApi(Resource):
streaming
=
args
[
'response_mode'
]
==
'streaming'
streaming
=
args
[
'response_mode'
]
==
'streaming'
# get app info
app_model
=
_get_app
(
app_id
,
'completion'
)
try
:
try
:
response
=
CompletionService
.
generate_more_like_this
(
response
=
CompletionService
.
generate_more_like_this
(
app_model
=
app_model
,
app_model
=
app_model
,
...
@@ -257,13 +241,10 @@ class MessageSuggestedQuestionApi(Resource):
...
@@ -257,13 +241,10 @@ class MessageSuggestedQuestionApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
,
message_id
):
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
app_id
=
str
(
app_id
)
def
get
(
self
,
app_model
,
message_id
):
message_id
=
str
(
message_id
)
message_id
=
str
(
message_id
)
# get app info
app_model
=
_get_app
(
app_id
,
'chat'
)
try
:
try
:
questions
=
MessageService
.
get_suggested_questions_after_answer
(
questions
=
MessageService
.
get_suggested_questions_after_answer
(
app_model
=
app_model
,
app_model
=
app_model
,
...
@@ -294,14 +275,11 @@ class MessageApi(Resource):
...
@@ -294,14 +275,11 @@ class MessageApi(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
message_detail_fields
)
@
marshal_with
(
message_detail_fields
)
def
get
(
self
,
app_id
,
message_id
):
def
get
(
self
,
app_model
,
message_id
):
app_id
=
str
(
app_id
)
message_id
=
str
(
message_id
)
message_id
=
str
(
message_id
)
# get app info
app_model
=
_get_app
(
app_id
)
message
=
db
.
session
.
query
(
Message
)
.
filter
(
message
=
db
.
session
.
query
(
Message
)
.
filter
(
Message
.
id
==
message_id
,
Message
.
id
==
message_id
,
Message
.
app_id
==
app_model
.
id
Message
.
app_id
==
app_model
.
id
...
...
api/controllers/console/app/model_config.py
View file @
5968cab1
...
@@ -4,7 +4,7 @@ from flask_login import current_user
...
@@ -4,7 +4,7 @@ from flask_login import current_user
from
flask_restful
import
Resource
from
flask_restful
import
Resource
from
controllers.console
import
api
from
controllers.console
import
api
from
controllers.console.app
import
_get_app
from
controllers.console.app
.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
from
controllers.console.wraps
import
account_initialization_required
from
events.app_event
import
app_model_config_was_updated
from
events.app_event
import
app_model_config_was_updated
...
@@ -19,33 +19,30 @@ class ModelConfigResource(Resource):
...
@@ -19,33 +19,30 @@ class ModelConfigResource(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
post
(
self
,
app_id
):
@
get_app_model
def
post
(
self
,
app_model
):
"""Modify app model config"""
"""Modify app model config"""
app_id
=
str
(
app_id
)
app
=
_get_app
(
app_id
)
# validate config
# validate config
model_configuration
=
AppModelConfigService
.
validate_configuration
(
model_configuration
=
AppModelConfigService
.
validate_configuration
(
tenant_id
=
current_user
.
current_tenant_id
,
tenant_id
=
current_user
.
current_tenant_id
,
account
=
current_user
,
account
=
current_user
,
config
=
request
.
json
,
config
=
request
.
json
,
app_mode
=
app
.
mode
app_mode
=
app
_model
.
mode
)
)
new_app_model_config
=
AppModelConfig
(
new_app_model_config
=
AppModelConfig
(
app_id
=
app
.
id
,
app_id
=
app
_model
.
id
,
)
)
new_app_model_config
=
new_app_model_config
.
from_model_config_dict
(
model_configuration
)
new_app_model_config
=
new_app_model_config
.
from_model_config_dict
(
model_configuration
)
db
.
session
.
add
(
new_app_model_config
)
db
.
session
.
add
(
new_app_model_config
)
db
.
session
.
flush
()
db
.
session
.
flush
()
app
.
app_model_config_id
=
new_app_model_config
.
id
app
_model
.
app_model_config_id
=
new_app_model_config
.
id
db
.
session
.
commit
()
db
.
session
.
commit
()
app_model_config_was_updated
.
send
(
app_model_config_was_updated
.
send
(
app
,
app
_model
,
app_model_config
=
new_app_model_config
app_model_config
=
new_app_model_config
)
)
...
...
api/controllers/console/app/site.py
View file @
5968cab1
...
@@ -4,7 +4,7 @@ from werkzeug.exceptions import Forbidden, NotFound
...
@@ -4,7 +4,7 @@ from werkzeug.exceptions import Forbidden, NotFound
from
constants.languages
import
supported_language
from
constants.languages
import
supported_language
from
controllers.console
import
api
from
controllers.console
import
api
from
controllers.console.app
import
_get_app
from
controllers.console.app
.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
from
controllers.console.wraps
import
account_initialization_required
from
extensions.ext_database
import
db
from
extensions.ext_database
import
db
...
@@ -34,13 +34,11 @@ class AppSite(Resource):
...
@@ -34,13 +34,11 @@ class AppSite(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
app_site_fields
)
@
marshal_with
(
app_site_fields
)
def
post
(
self
,
app_
id
):
def
post
(
self
,
app_
model
):
args
=
parse_app_site_args
()
args
=
parse_app_site_args
()
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
)
# The role of the current user in the ta table must be admin or owner
# The role of the current user in the ta table must be admin or owner
if
not
current_user
.
is_admin_or_owner
:
if
not
current_user
.
is_admin_or_owner
:
raise
Forbidden
()
raise
Forbidden
()
...
@@ -82,11 +80,9 @@ class AppSiteAccessTokenReset(Resource):
...
@@ -82,11 +80,9 @@ class AppSiteAccessTokenReset(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
@
get_app_model
@
marshal_with
(
app_site_fields
)
@
marshal_with
(
app_site_fields
)
def
post
(
self
,
app_id
):
def
post
(
self
,
app_model
):
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
)
# The role of the current user in the ta table must be admin or owner
# The role of the current user in the ta table must be admin or owner
if
not
current_user
.
is_admin_or_owner
:
if
not
current_user
.
is_admin_or_owner
:
raise
Forbidden
()
raise
Forbidden
()
...
...
api/controllers/console/app/statistic.py
View file @
5968cab1
...
@@ -7,9 +7,10 @@ from flask_login import current_user
...
@@ -7,9 +7,10 @@ from flask_login import current_user
from
flask_restful
import
Resource
,
reqparse
from
flask_restful
import
Resource
,
reqparse
from
controllers.console
import
api
from
controllers.console
import
api
from
controllers.console.app
import
_get_app
from
controllers.console.app
.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
from
controllers.console.wraps
import
account_initialization_required
from
core.entities.application_entities
import
AppMode
from
extensions.ext_database
import
db
from
extensions.ext_database
import
db
from
libs.helper
import
datetime_string
from
libs.helper
import
datetime_string
from
libs.login
import
login_required
from
libs.login
import
login_required
...
@@ -20,10 +21,9 @@ class DailyConversationStatistic(Resource):
...
@@ -20,10 +21,9 @@ class DailyConversationStatistic(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
):
@
get_app_model
def
get
(
self
,
app_model
):
account
=
current_user
account
=
current_user
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
@@ -81,10 +81,9 @@ class DailyTerminalsStatistic(Resource):
...
@@ -81,10 +81,9 @@ class DailyTerminalsStatistic(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
):
@
get_app_model
def
get
(
self
,
app_model
):
account
=
current_user
account
=
current_user
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
@@ -141,10 +140,9 @@ class DailyTokenCostStatistic(Resource):
...
@@ -141,10 +140,9 @@ class DailyTokenCostStatistic(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
):
@
get_app_model
def
get
(
self
,
app_model
):
account
=
current_user
account
=
current_user
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
@@ -205,10 +203,9 @@ class AverageSessionInteractionStatistic(Resource):
...
@@ -205,10 +203,9 @@ class AverageSessionInteractionStatistic(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
):
@
get_app_model
(
mode
=
AppMode
.
CHAT
)
def
get
(
self
,
app_model
):
account
=
current_user
account
=
current_user
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
,
'chat'
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
@@ -271,10 +268,9 @@ class UserSatisfactionRateStatistic(Resource):
...
@@ -271,10 +268,9 @@ class UserSatisfactionRateStatistic(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
):
@
get_app_model
def
get
(
self
,
app_model
):
account
=
current_user
account
=
current_user
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
@@ -334,10 +330,9 @@ class AverageResponseTimeStatistic(Resource):
...
@@ -334,10 +330,9 @@ class AverageResponseTimeStatistic(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
):
@
get_app_model
(
mode
=
AppMode
.
WORKFLOW
)
def
get
(
self
,
app_model
):
account
=
current_user
account
=
current_user
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
,
'completion'
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
@@ -396,10 +391,9 @@ class TokensPerSecondStatistic(Resource):
...
@@ -396,10 +391,9 @@ class TokensPerSecondStatistic(Resource):
@
setup_required
@
setup_required
@
login_required
@
login_required
@
account_initialization_required
@
account_initialization_required
def
get
(
self
,
app_id
):
@
get_app_model
def
get
(
self
,
app_model
):
account
=
current_user
account
=
current_user
app_id
=
str
(
app_id
)
app_model
=
_get_app
(
app_id
)
parser
=
reqparse
.
RequestParser
()
parser
=
reqparse
.
RequestParser
()
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
parser
.
add_argument
(
'start'
,
type
=
datetime_string
(
'
%
Y-
%
m-
%
d
%
H:
%
M'
),
location
=
'args'
)
...
...
api/controllers/console/app/workflow.py
0 → 100644
View file @
5968cab1
from
flask_restful
import
Resource
from
controllers.console
import
api
from
controllers.console.app.wraps
import
get_app_model
from
controllers.console.setup
import
setup_required
from
controllers.console.wraps
import
account_initialization_required
from
core.entities.application_entities
import
AppMode
from
libs.login
import
login_required
class
DefaultBlockConfigApi
(
Resource
):
@
setup_required
@
login_required
@
account_initialization_required
@
get_app_model
(
mode
=
[
AppMode
.
CHAT
,
AppMode
.
WORKFLOW
])
def
post
(
self
,
app_model
):
return
'success'
,
200
api
.
add_resource
(
DefaultBlockConfigApi
,
'/apps/<uuid:app_id>/default-workflow-block-configs'
)
api/controllers/console/app/wraps.py
0 → 100644
View file @
5968cab1
from
functools
import
wraps
from
typing
import
Union
,
Optional
,
Callable
from
controllers.console.app.error
import
AppNotFoundError
from
core.entities.application_entities
import
AppMode
from
extensions.ext_database
import
db
from
libs.login
import
current_user
from
models.model
import
App
def
get_app_model
(
view
:
Optional
[
Callable
]
=
None
,
*
,
mode
:
Union
[
AppMode
,
list
[
AppMode
]]
=
None
):
def
decorator
(
view_func
):
@
wraps
(
view_func
)
def
decorated_view
(
*
args
,
**
kwargs
):
if
not
kwargs
.
get
(
'app_id'
):
raise
ValueError
(
'missing app_id in path parameters'
)
app_id
=
kwargs
.
get
(
'app_id'
)
app_id
=
str
(
app_id
)
del
kwargs
[
'app_id'
]
app_model
=
db
.
session
.
query
(
App
)
.
filter
(
App
.
id
==
app_id
,
App
.
tenant_id
==
current_user
.
current_tenant_id
,
App
.
status
==
'normal'
)
.
first
()
if
not
app_model
:
raise
AppNotFoundError
()
app_mode
=
AppMode
.
value_of
(
app_model
.
mode
)
if
mode
is
not
None
:
if
isinstance
(
mode
,
list
):
modes
=
mode
else
:
modes
=
[
mode
]
# [temp] if workflow is in the mode list, then completion should be in the mode list
if
AppMode
.
WORKFLOW
in
modes
:
modes
.
append
(
AppMode
.
COMPLETION
)
if
app_mode
not
in
modes
:
mode_values
=
{
m
.
value
for
m
in
modes
}
raise
AppNotFoundError
(
f
"App mode is not in the supported list: {mode_values}"
)
kwargs
[
'app_model'
]
=
app_model
return
view_func
(
*
args
,
**
kwargs
)
return
decorated_view
if
view
is
None
:
return
decorator
else
:
return
decorator
(
view
)
api/core/app_runner/basic_app_runner.py
View file @
5968cab1
...
@@ -4,12 +4,12 @@ from typing import Optional
...
@@ -4,12 +4,12 @@ from typing import Optional
from
core.app_runner.app_runner
import
AppRunner
from
core.app_runner.app_runner
import
AppRunner
from
core.application_queue_manager
import
ApplicationQueueManager
,
PublishFrom
from
core.application_queue_manager
import
ApplicationQueueManager
,
PublishFrom
from
core.callback_handler.index_tool_callback_handler
import
DatasetIndexToolCallbackHandler
from
core.callback_handler.index_tool_callback_handler
import
DatasetIndexToolCallbackHandler
from
core.entities.application_entities
import
ApplicationGenerateEntity
,
DatasetEntity
,
InvokeFrom
,
ModelConfigEntity
from
core.entities.application_entities
import
ApplicationGenerateEntity
,
DatasetEntity
,
InvokeFrom
,
ModelConfigEntity
,
\
AppMode
from
core.features.dataset_retrieval.dataset_retrieval
import
DatasetRetrievalFeature
from
core.features.dataset_retrieval.dataset_retrieval
import
DatasetRetrievalFeature
from
core.memory.token_buffer_memory
import
TokenBufferMemory
from
core.memory.token_buffer_memory
import
TokenBufferMemory
from
core.model_manager
import
ModelInstance
from
core.model_manager
import
ModelInstance
from
core.moderation.base
import
ModerationException
from
core.moderation.base
import
ModerationException
from
core.prompt.prompt_transform
import
AppMode
from
extensions.ext_database
import
db
from
extensions.ext_database
import
db
from
models.model
import
App
,
Conversation
,
Message
from
models.model
import
App
,
Conversation
,
Message
...
...
api/core/entities/application_entities.py
View file @
5968cab1
...
@@ -9,6 +9,26 @@ from core.model_runtime.entities.message_entities import PromptMessageRole
...
@@ -9,6 +9,26 @@ from core.model_runtime.entities.message_entities import PromptMessageRole
from
core.model_runtime.entities.model_entities
import
AIModelEntity
from
core.model_runtime.entities.model_entities
import
AIModelEntity
class
AppMode
(
Enum
):
COMPLETION
=
'completion'
# will be deprecated in the future
WORKFLOW
=
'workflow'
# instead of 'completion'
CHAT
=
'chat'
AGENT
=
'agent'
@
classmethod
def
value_of
(
cls
,
value
:
str
)
->
'AppMode'
:
"""
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
ModelConfigEntity
(
BaseModel
):
class
ModelConfigEntity
(
BaseModel
):
"""
"""
Model Config Entity.
Model Config Entity.
...
...
api/core/prompt/prompt_transform.py
View file @
5968cab1
...
@@ -7,7 +7,7 @@ from typing import Optional, cast
...
@@ -7,7 +7,7 @@ from typing import Optional, cast
from
core.entities.application_entities
import
(
from
core.entities.application_entities
import
(
AdvancedCompletionPromptTemplateEntity
,
AdvancedCompletionPromptTemplateEntity
,
ModelConfigEntity
,
ModelConfigEntity
,
PromptTemplateEntity
,
PromptTemplateEntity
,
AppMode
,
)
)
from
core.file.file_obj
import
FileObj
from
core.file.file_obj
import
FileObj
from
core.memory.token_buffer_memory
import
TokenBufferMemory
from
core.memory.token_buffer_memory
import
TokenBufferMemory
...
@@ -25,24 +25,6 @@ from core.prompt.prompt_builder import PromptBuilder
...
@@ -25,24 +25,6 @@ from core.prompt.prompt_builder import PromptBuilder
from
core.prompt.prompt_template
import
PromptTemplateParser
from
core.prompt.prompt_template
import
PromptTemplateParser
class
AppMode
(
enum
.
Enum
):
COMPLETION
=
'completion'
CHAT
=
'chat'
@
classmethod
def
value_of
(
cls
,
value
:
str
)
->
'AppMode'
:
"""
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
ModelMode
(
enum
.
Enum
):
class
ModelMode
(
enum
.
Enum
):
COMPLETION
=
'completion'
COMPLETION
=
'completion'
CHAT
=
'chat'
CHAT
=
'chat'
...
...
api/services/advanced_prompt_template_service.py
View file @
5968cab1
import
copy
import
copy
from
core.entities.application_entities
import
AppMode
from
core.prompt.advanced_prompt_templates
import
(
from
core.prompt.advanced_prompt_templates
import
(
BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG
,
BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG
,
BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG
,
BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG
,
...
@@ -13,7 +14,6 @@ from core.prompt.advanced_prompt_templates import (
...
@@ -13,7 +14,6 @@ from core.prompt.advanced_prompt_templates import (
COMPLETION_APP_COMPLETION_PROMPT_CONFIG
,
COMPLETION_APP_COMPLETION_PROMPT_CONFIG
,
CONTEXT
,
CONTEXT
,
)
)
from
core.prompt.prompt_transform
import
AppMode
class
AdvancedPromptTemplateService
:
class
AdvancedPromptTemplateService
:
...
...
api/services/app_model_config_service.py
View file @
5968cab1
...
@@ -2,11 +2,11 @@ import re
...
@@ -2,11 +2,11 @@ import re
import
uuid
import
uuid
from
core.entities.agent_entities
import
PlanningStrategy
from
core.entities.agent_entities
import
PlanningStrategy
from
core.entities.application_entities
import
AppMode
from
core.external_data_tool.factory
import
ExternalDataToolFactory
from
core.external_data_tool.factory
import
ExternalDataToolFactory
from
core.model_runtime.entities.model_entities
import
ModelPropertyKey
,
ModelType
from
core.model_runtime.entities.model_entities
import
ModelPropertyKey
,
ModelType
from
core.model_runtime.model_providers
import
model_provider_factory
from
core.model_runtime.model_providers
import
model_provider_factory
from
core.moderation.factory
import
ModerationFactory
from
core.moderation.factory
import
ModerationFactory
from
core.prompt.prompt_transform
import
AppMode
from
core.provider_manager
import
ProviderManager
from
core.provider_manager
import
ProviderManager
from
models.account
import
Account
from
models.account
import
Account
from
services.dataset_service
import
DatasetService
from
services.dataset_service
import
DatasetService
...
...
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