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
8bd3b807
Unverified
Commit
8bd3b807
authored
Feb 26, 2024
by
Yeuoly
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: invoke app
parent
34c96aa1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
132 additions
and
28 deletions
+132
-28
app.py
api/controllers/inner_api/app.py
+81
-3
model_runtime.py
api/controllers/inner_api/model_runtime.py
+51
-25
No files found.
api/controllers/inner_api/app.py
View file @
8bd3b807
import
json
import
logging
from
flask_login
import
current_user
from
flask_restful
import
Resource
from
flask_restful
import
Resource
,
reqparse
from
flask
import
Response
from
flask.helpers
import
stream_with_context
from
controllers.console.setup
import
setup_required
from
controllers.inner_api
import
api
from
controllers.inner_api.wraps
import
inner_api_only
from
services.completion_service
import
CompletionService
from
core.entities.application_entities
import
InvokeFrom
from
extensions.ext_database
import
db
from
models.model
import
App
from
typing
import
Union
,
Generator
from
werkzeug.exceptions
import
InternalServerError
,
NotFound
import
services
from
controllers.service_api.app.error
import
(
AppUnavailableError
,
CompletionRequestError
,
ConversationCompletedError
,
ProviderModelCurrentlyNotSupportError
,
ProviderNotInitializeError
,
ProviderQuotaExceededError
,
)
from
core.entities.application_entities
import
InvokeFrom
from
core.errors.error
import
ModelCurrentlyNotSupportError
,
ProviderTokenNotInitError
,
QuotaExceededError
from
core.model_runtime.errors.invoke
import
InvokeError
class
EnterpriseAppInvokeApi
(
Resource
):
"""App invoke API for enterprise edition"""
...
...
@@ -12,6 +36,60 @@ class EnterpriseAppInvokeApi(Resource):
@
setup_required
@
inner_api_only
def
post
(
self
):
pass
request_parser
=
reqparse
.
RequestParser
()
request_parser
.
add_argument
(
'app_id'
,
type
=
str
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'query'
,
type
=
str
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'inputs'
,
type
=
dict
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'stream'
,
type
=
bool
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'conversation_id'
,
type
=
str
,
required
=
False
,
nullable
=
True
,
location
=
'json'
)
args
=
request_parser
.
parse_args
()
app_id
=
args
[
'app_id'
]
app_model
:
App
=
db
.
session
.
query
(
App
)
.
filter
(
App
.
id
==
app_id
)
.
first
()
if
app_model
is
None
:
raise
NotFound
(
"App Not Exists."
)
try
:
response
=
CompletionService
.
completion
(
app_model
=
app_model
,
user
=
current_user
,
args
=
args
,
invoke_from
=
InvokeFrom
.
INNER_API
,
streaming
=
args
[
'stream'
],
)
return
compact_response
(
response
)
except
services
.
errors
.
conversation
.
ConversationNotExistsError
:
raise
NotFound
(
"Conversation Not Exists."
)
except
services
.
errors
.
conversation
.
ConversationCompletedError
:
raise
ConversationCompletedError
()
except
services
.
errors
.
app_model_config
.
AppModelConfigBrokenError
:
logging
.
exception
(
"App model config broken."
)
raise
AppUnavailableError
()
except
ProviderTokenNotInitError
as
ex
:
raise
ProviderNotInitializeError
(
ex
.
description
)
except
QuotaExceededError
:
raise
ProviderQuotaExceededError
()
except
ModelCurrentlyNotSupportError
:
raise
ProviderModelCurrentlyNotSupportError
()
except
InvokeError
as
e
:
raise
CompletionRequestError
(
e
.
description
)
except
ValueError
as
e
:
raise
e
except
Exception
as
e
:
logging
.
exception
(
"internal server error."
)
raise
InternalServerError
()
def
compact_response
(
response
:
Union
[
dict
,
Generator
])
->
Response
:
if
isinstance
(
response
,
dict
):
return
Response
(
response
=
json
.
dumps
(
response
),
status
=
200
,
mimetype
=
'application/json'
)
else
:
def
generate
()
->
Generator
:
yield
from
response
return
Response
(
stream_with_context
(
generate
()),
status
=
200
,
mimetype
=
'text/event-stream'
)
api
.
add_resource
(
EnterpriseAppInvokeApi
,
'/app/invoke'
)
\ No newline at end of file
api/controllers/inner_api/model_runtime.py
View file @
8bd3b807
...
...
@@ -10,6 +10,17 @@ from controllers.console.setup import setup_required
from
controllers.inner_api
import
api
from
controllers.inner_api.wraps
import
inner_api_only
from
services.completion_service
import
CompletionService
from
core.errors.error
import
ModelCurrentlyNotSupportError
,
ProviderTokenNotInitError
,
QuotaExceededError
from
core.model_runtime.errors.invoke
import
InvokeError
from
libs.helper
import
uuid_value
from
services.completion_service
import
CompletionService
from
controllers.service_api.app.error
import
(
CompletionRequestError
,
ProviderModelCurrentlyNotSupportError
,
ProviderNotInitializeError
,
ProviderQuotaExceededError
,
)
from
werkzeug.exceptions
import
InternalServerError
class
EnterpriseModelInvokeLLMApi
(
Resource
):
...
...
@@ -19,40 +30,55 @@ class EnterpriseModelInvokeLLMApi(Resource):
@
inner_api_only
def
post
(
self
):
request_parser
=
reqparse
.
RequestParser
()
request_parser
.
add_argument
(
'tenant_id'
,
type
=
str
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'provider'
,
type
=
str
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'model'
,
type
=
str
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'completion_params'
,
type
=
dict
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'prompt_messages'
,
type
=
list
,
required
=
True
,
nullable
=
False
,
location
=
'json'
)
request_parser
.
add_argument
(
'tools'
,
type
=
list
,
required
=
False
,
nullable
=
True
,
location
=
'json'
)
request_parser
.
add_argument
(
'stop'
,
type
=
list
,
required
=
False
,
nullable
=
True
,
location
=
'json'
)
request_parser
.
add_argument
(
'stream'
,
type
=
bool
,
required
=
False
,
nullable
=
True
,
location
=
'json'
)
request_parser
.
add_argument
(
'user'
,
type
=
str
,
required
=
False
,
nullable
=
True
,
location
=
'json'
)
request_parser
.
add_argument
(
"tenant_id"
,
type
=
str
,
required
=
True
,
nullable
=
False
,
location
=
"json"
)
request_parser
.
add_argument
(
"provider"
,
type
=
str
,
required
=
True
,
nullable
=
False
,
location
=
"json"
)
request_parser
.
add_argument
(
"model"
,
type
=
str
,
required
=
True
,
nullable
=
False
,
location
=
"json"
)
request_parser
.
add_argument
(
"completion_params"
,
type
=
dict
,
required
=
True
,
nullable
=
False
,
location
=
"json"
)
request_parser
.
add_argument
(
"prompt_messages"
,
type
=
list
,
required
=
True
,
nullable
=
False
,
location
=
"json"
)
request_parser
.
add_argument
(
"tools"
,
type
=
list
,
required
=
False
,
nullable
=
True
,
location
=
"json"
)
request_parser
.
add_argument
(
"stop"
,
type
=
list
,
required
=
False
,
nullable
=
True
,
location
=
"json"
)
request_parser
.
add_argument
(
"stream"
,
type
=
bool
,
required
=
False
,
nullable
=
True
,
location
=
"json"
)
request_parser
.
add_argument
(
"user"
,
type
=
str
,
required
=
False
,
nullable
=
True
,
location
=
"json"
)
args
=
request_parser
.
parse_args
()
response
=
CompletionService
.
invoke_model
(
tenant_id
=
args
[
'tenant_id'
],
provider
=
args
[
'provider'
],
model
=
args
[
'model'
],
completion_params
=
args
[
'completion_params'
],
prompt_messages
=
args
[
'prompt_messages'
],
tools
=
args
[
'tools'
],
stop
=
args
[
'stop'
],
stream
=
args
[
'stream'
],
user
=
args
[
'user'
],
)
try
:
response
=
CompletionService
.
invoke_model
(
tenant_id
=
args
[
"tenant_id"
],
provider
=
args
[
"provider"
],
model
=
args
[
"model"
],
completion_params
=
args
[
"completion_params"
],
prompt_messages
=
args
[
"prompt_messages"
],
tools
=
args
[
"tools"
],
stop
=
args
[
"stop"
],
stream
=
args
[
"stream"
],
user
=
args
[
"user"
],
)
return
compact_response
(
response
)
except
ProviderTokenNotInitError
as
ex
:
raise
ProviderNotInitializeError
(
ex
.
description
)
except
QuotaExceededError
:
raise
ProviderQuotaExceededError
()
except
ModelCurrentlyNotSupportError
:
raise
ProviderModelCurrentlyNotSupportError
()
except
InvokeError
as
e
:
raise
CompletionRequestError
(
e
.
description
)
except
ValueError
as
e
:
raise
e
except
Exception
as
e
:
raise
InternalServerError
()
return
compact_response
(
response
)
def
compact_response
(
response
:
Union
[
dict
,
Generator
])
->
Response
:
if
isinstance
(
response
,
dict
):
return
Response
(
response
=
json
.
dumps
(
response
),
status
=
200
,
mimetype
=
'application/json'
)
return
Response
(
response
=
json
.
dumps
(
response
),
status
=
200
,
mimetype
=
"application/json"
)
else
:
def
generate
()
->
Generator
:
yield
from
response
return
Response
(
stream_with_context
(
generate
()),
status
=
200
,
mimetype
=
'text/event-stream'
)
return
Response
(
stream_with_context
(
generate
()),
status
=
200
,
mimetype
=
"text/event-stream"
)
api
.
add_resource
(
EnterpriseModelInvokeLLMApi
,
'/model/invoke/llm'
)
api
.
add_resource
(
EnterpriseModelInvokeLLMApi
,
"/model/invoke/llm"
)
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