Skip to content

Commit

Permalink
auto fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenliang123 committed Mar 26, 2024
1 parent f1356cd commit 756c6a6
Show file tree
Hide file tree
Showing 296 changed files with 1,035 additions and 679 deletions.
9 changes: 5 additions & 4 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

from commands import register_commands
from config import CloudEditionConfig, Config

# DO NOT REMOVE BELOW
from events import event_handlers
from extensions import (
ext_celery,
ext_code_based_extension,
Expand All @@ -39,19 +42,17 @@
from extensions.ext_database import db
from extensions.ext_login import login_manager
from libs.passport import PassportService
from models import account, dataset, model, source, task, tool, tools, web
from services.account_service import AccountService

# DO NOT REMOVE BELOW
from events import event_handlers
from models import account, dataset, model, source, task, tool, tools, web
# DO NOT REMOVE ABOVE


warnings.simplefilter("ignore", ResourceWarning)

# fix windows platform
if os.name == "nt":
os.system('tzutil /s "UTC"')
os.system('tzutil /s "UTC"')
else:
os.environ['TZ'] = 'UTC'
time.tzset()
Expand Down
2 changes: 0 additions & 2 deletions api/constants/model_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,3 @@
}
},
}


3 changes: 0 additions & 3 deletions api/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
# -*- coding:utf-8 -*-



22 changes: 20 additions & 2 deletions api/controllers/console/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
from flask import Blueprint

from libs.external_api import ExternalApi

bp = Blueprint('console', __name__, url_prefix='/console/api')
api = ExternalApi(bp)

# Import other controllers
from . import admin, apikey, extension, feature, setup, version

# Import app controllers
from .app import (advanced_prompt_template, annotation, app, audio, completion, conversation, generator, message,
model_config, site, statistic)
from .app import (
advanced_prompt_template,
annotation,
app,
audio,
completion,
conversation,
generator,
message,
model_config,
site,
statistic,
)

# Import auth controllers
from .auth import activate, data_source_oauth, login, oauth

# Import billing controllers
from .billing import billing

# Import datasets controllers
from .datasets import data_source, datasets, datasets_document, datasets_segments, file, hit_testing

# Import explore controllers
from .explore import audio, completion, conversation, installed_app, message, parameter, recommended_app, saved_message

# Import workspace controllers
from .workspace import account, members, model_providers, models, tool_providers, workspace
2 changes: 1 addition & 1 deletion api/controllers/console/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def post(self):
copy_right = site.copyright if site.copyright else \
args['copyright'] if args['copyright'] else ''
privacy_policy = site.privacy_policy if site.privacy_policy else \
args['privacy_policy'] if args['privacy_policy'] else ''
args['privacy_policy'] if args['privacy_policy'] else ''

recommended_app = RecommendedApp.query.filter(RecommendedApp.app_id == args['app_id']).first()

Expand Down
5 changes: 3 additions & 2 deletions api/controllers/console/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from flask_login import current_user
from werkzeug.exceptions import NotFound

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):
Expand Down
3 changes: 2 additions & 1 deletion api/controllers/console/app/advanced_prompt_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ def get(self):

return AdvancedPromptTemplateService.get_prompt(args)

api.add_resource(AdvancedPromptTemplateList, '/app/prompt-templates')

api.add_resource(AdvancedPromptTemplateList, '/app/prompt-templates')
7 changes: 4 additions & 3 deletions api/controllers/console/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
from controllers.console.app.error import AppNotFoundError, ProviderNotInitializeError
from controllers.console.setup import setup_required
from controllers.console.wraps import account_initialization_required, cloud_edition_billing_resource_check
from core.entities.application_entities import AgentToolEntity
from core.errors.error import LLMBadRequestError, ProviderTokenNotInitError
from core.model_manager import ModelManager
from core.model_runtime.entities.model_entities import ModelType
from core.provider_manager import ProviderManager
from core.tools.tool_manager import ToolManager
from core.tools.utils.configuration import ToolParameterConfigurationManager
from events.app_event import app_was_created, app_was_deleted
from extensions.ext_database import db
from fields.app_fields import (
Expand All @@ -27,9 +30,7 @@
from libs.login import login_required
from models.model import App, AppModelConfig, Site
from services.app_model_config_service import AppModelConfigService
from core.tools.utils.configuration import ToolParameterConfigurationManager
from core.tools.tool_manager import ToolManager
from core.entities.application_entities import AgentToolEntity


def _get_app(app_id, tenant_id):
app = db.session.query(App).filter(App.id == app_id, App.tenant_id == tenant_id).first()
Expand Down
26 changes: 13 additions & 13 deletions api/controllers/console/app/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get(self, app_id):

sql_query = '''
SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date, count(distinct messages.conversation_id) AS conversation_count
FROM messages where app_id = :app_id
FROM messages where app_id = :app_id
'''
arg_dict = {'tz': account.timezone, 'app_id': app_model.id}

Expand Down Expand Up @@ -93,7 +93,7 @@ def get(self, app_id):

sql_query = '''
SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date, count(distinct messages.from_end_user_id) AS terminal_count
FROM messages where app_id = :app_id
FROM messages where app_id = :app_id
'''
arg_dict = {'tz': account.timezone, 'app_id': app_model.id}

Expand Down Expand Up @@ -125,7 +125,7 @@ def get(self, app_id):
response_data = []

with db.engine.begin() as conn:
rs = conn.execute(db.text(sql_query), arg_dict)
rs = conn.execute(db.text(sql_query), arg_dict)
for i in rs:
response_data.append({
'date': str(i.date),
Expand All @@ -152,10 +152,10 @@ def get(self, app_id):
args = parser.parse_args()

sql_query = '''
SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
(sum(messages.message_tokens) + sum(messages.answer_tokens)) as token_count,
sum(total_price) as total_price
FROM messages where app_id = :app_id
FROM messages where app_id = :app_id
'''
arg_dict = {'tz': account.timezone, 'app_id': app_model.id}

Expand Down Expand Up @@ -215,7 +215,7 @@ def get(self, app_id):
parser.add_argument('end', type=datetime_string('%Y-%m-%d %H:%M'), location='args')
args = parser.parse_args()

sql_query = """SELECT date(DATE_TRUNC('day', c.created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
sql_query = """SELECT date(DATE_TRUNC('day', c.created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
AVG(subquery.message_count) AS interactions
FROM (SELECT m.conversation_id, COUNT(m.id) AS message_count
FROM conversations c
Expand Down Expand Up @@ -282,11 +282,11 @@ def get(self, app_id):
args = parser.parse_args()

sql_query = '''
SELECT date(DATE_TRUNC('day', m.created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
COUNT(m.id) as message_count, COUNT(mf.id) as feedback_count
SELECT date(DATE_TRUNC('day', m.created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
COUNT(m.id) as message_count, COUNT(mf.id) as feedback_count
FROM messages m
LEFT JOIN message_feedbacks mf on mf.message_id=m.id
WHERE m.app_id = :app_id
WHERE m.app_id = :app_id
'''
arg_dict = {'tz': account.timezone, 'app_id': app_model.id}

Expand Down Expand Up @@ -345,7 +345,7 @@ def get(self, app_id):
args = parser.parse_args()

sql_query = '''
SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
AVG(provider_response_latency) as latency
FROM messages
WHERE app_id = :app_id
Expand Down Expand Up @@ -380,7 +380,7 @@ def get(self, app_id):
response_data = []

with db.engine.begin() as conn:
rs = conn.execute(db.text(sql_query), arg_dict)
rs = conn.execute(db.text(sql_query), arg_dict)
for i in rs:
response_data.append({
'date': str(i.date),
Expand All @@ -406,8 +406,8 @@ def get(self, app_id):
parser.add_argument('end', type=datetime_string('%Y-%m-%d %H:%M'), location='args')
args = parser.parse_args()

sql_query = '''SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
CASE
sql_query = '''SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
CASE
WHEN SUM(provider_response_latency) = 0 THEN 0
ELSE (SUM(answer_tokens) / SUM(provider_response_latency))
END as tokens_per_second
Expand Down
6 changes: 2 additions & 4 deletions api/controllers/console/auth/data_source_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ def get(self, provider: str):
if current_app.config.get('NOTION_INTEGRATION_TYPE') == 'internal':
internal_secret = current_app.config.get('NOTION_INTERNAL_SECRET')
oauth_provider.save_internal_access_token(internal_secret)
return { 'data': '' }
return {'data': ''}
else:
auth_url = oauth_provider.get_authorization_url()
return { 'data': auth_url }, 200


return {'data': auth_url}, 200


class OAuthDataSourceCallback(Resource):
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/console/auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ def get(self):
'subject': 'Reset your Dify password',
'html': """
<p>Dear User,</p>
<p>The Dify team has generated a new password for you, details as follows:</p>
<p>The Dify team has generated a new password for you, details as follows:</p>
<p><strong>{new_password}</strong></p>
<p>Please change your password to log in as soon as possible.</p>
<p>Regards,</p>
<p>The Dify Team</p>
<p>The Dify Team</p>
"""
}

Expand Down
3 changes: 3 additions & 0 deletions api/controllers/console/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ class NotSetupError(BaseHTTPException):
"Please proceed with the initialization and installation process first."
code = 401


class NotInitValidateError(BaseHTTPException):
error_code = 'not_init_validated'
description = "Init validation has not been completed yet. " \
"Please proceed with the init validation process first."
code = 401


class InitValidateFailedError(BaseHTTPException):
error_code = 'init_validate_failed'
description = "Init validation failed. Please check the password and try again."
code = 401


class AccountNotLinkTenantError(BaseHTTPException):
error_code = 'account_not_link_tenant'
description = "Account not link tenant."
Expand Down
1 change: 1 addition & 0 deletions api/controllers/console/explore/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def get(self, installed_app):
except services.errors.message.FirstMessageNotExistsError:
raise NotFound("First Message Not Exists.")


class MessageFeedbackApi(InstalledAppResource):
def post(self, installed_app, message_id):
app_model = installed_app.app
Expand Down
4 changes: 3 additions & 1 deletion api/controllers/console/explore/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def get(self, installed_app: InstalledApp):
}
}


class ExploreAppMetaApi(InstalledAppResource):
def get(self, installed_app: InstalledApp):
"""Get app meta"""
Expand Down Expand Up @@ -94,12 +95,13 @@ def get(self, installed_app: InstalledApp):
)
meta['tool_icons'][tool_name] = json.loads(provider.icon)
except:
meta['tool_icons'][tool_name] = {
meta['tool_icons'][tool_name] = {
"background": "#252525",
"content": "\ud83d\ude01"
}

return meta


api.add_resource(AppParameterApi, '/installed-apps/<uuid:installed_app_id>/parameters', endpoint='installed_app_parameters')
api.add_resource(ExploreAppMetaApi, '/installed-apps/<uuid:installed_app_id>/meta', endpoint='installed_app_meta')
6 changes: 4 additions & 2 deletions api/controllers/console/init_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class InitValidateAPI(Resource):
def get(self):
init_status = get_init_validate_status()
if init_status:
return { 'status': 'finished' }
return {'status': 'not_started' }
return {'status': 'finished'}
return {'status': 'not_started'}

@only_edition_self_hosted
def post(self):
Expand All @@ -39,11 +39,13 @@ def post(self):
session['is_init_validated'] = True
return {'result': 'success'}, 201


def get_init_validate_status():
if current_app.config['EDITION'] == 'SELF_HOSTED':
if os.environ.get('INIT_PASSWORD'):
return session.get('is_init_validated') or DifySetup.query.first()

return True


api.add_resource(InitValidateAPI, '/init')
1 change: 1 addition & 0 deletions api/controllers/console/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ def get_setup_status():
else:
return True


api.add_resource(SetupApi, '/setup')
Loading

0 comments on commit 756c6a6

Please sign in to comment.