mirror of
https://github.com/langgenius/dify.git
synced 2026-04-08 01:06:51 +08:00
refactor recommend app api
This commit is contained in:
parent
c709e339b1
commit
65ed4dc91f
@ -49,7 +49,8 @@ DEFAULTS = {
|
||||
'HOSTED_ANTHROPIC_PAID_ENABLED': 'False',
|
||||
'HOSTED_MODERATION_ENABLED': 'False',
|
||||
'HOSTED_MODERATION_PROVIDERS': '',
|
||||
'HOSTED_EXPOSE_APP_TEMPLATES': 'False',
|
||||
'HOSTED_FETCH_APP_TEMPLATES_MODE': 'remote',
|
||||
'HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN': 'https://tmpl.dify.ai',
|
||||
'CLEAN_DAY_SETTING': 30,
|
||||
'UPLOAD_FILE_SIZE_LIMIT': 15,
|
||||
'UPLOAD_FILE_BATCH_LIMIT': 5,
|
||||
@ -291,7 +292,9 @@ class Config:
|
||||
self.HOSTED_MODERATION_ENABLED = get_bool_env('HOSTED_MODERATION_ENABLED')
|
||||
self.HOSTED_MODERATION_PROVIDERS = get_env('HOSTED_MODERATION_PROVIDERS')
|
||||
|
||||
self.HOSTED_EXPOSE_APP_TEMPLATES = get_bool_env('HOSTED_EXPOSE_APP_TEMPLATES')
|
||||
# fetch app templates mode, remote, builtin, db(only for dify SaaS), default: remote
|
||||
self.HOSTED_FETCH_APP_TEMPLATES_MODE = get_env('HOSTED_FETCH_APP_TEMPLATES_MODE')
|
||||
self.HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN = get_env('HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN')
|
||||
|
||||
self.ETL_TYPE = get_env('ETL_TYPE')
|
||||
self.UNSTRUCTURED_API_URL = get_env('UNSTRUCTURED_API_URL')
|
||||
|
||||
@ -25,67 +25,3 @@ def supported_language(lang):
|
||||
error = ('{lang} is not a valid language.'
|
||||
.format(lang=lang))
|
||||
raise ValueError(error)
|
||||
|
||||
|
||||
user_input_form_template = {
|
||||
"en-US": [
|
||||
{
|
||||
"paragraph": {
|
||||
"label": "Query",
|
||||
"variable": "default_input",
|
||||
"required": False,
|
||||
"default": ""
|
||||
}
|
||||
}
|
||||
],
|
||||
"zh-Hans": [
|
||||
{
|
||||
"paragraph": {
|
||||
"label": "查询内容",
|
||||
"variable": "default_input",
|
||||
"required": False,
|
||||
"default": ""
|
||||
}
|
||||
}
|
||||
],
|
||||
"pt-BR": [
|
||||
{
|
||||
"paragraph": {
|
||||
"label": "Consulta",
|
||||
"variable": "default_input",
|
||||
"required": False,
|
||||
"default": ""
|
||||
}
|
||||
}
|
||||
],
|
||||
"es-ES": [
|
||||
{
|
||||
"paragraph": {
|
||||
"label": "Consulta",
|
||||
"variable": "default_input",
|
||||
"required": False,
|
||||
"default": ""
|
||||
}
|
||||
}
|
||||
],
|
||||
"ua-UK": [
|
||||
{
|
||||
"paragraph": {
|
||||
"label": "Запит",
|
||||
"variable": "default_input",
|
||||
"required": False,
|
||||
"default": ""
|
||||
}
|
||||
}
|
||||
],
|
||||
"vi-VN": [
|
||||
{
|
||||
"paragraph": {
|
||||
"label": "Nội dung truy vấn",
|
||||
"variable": "default_input",
|
||||
"required": False,
|
||||
"default": ""
|
||||
}
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
767
api/constants/recommended_apps.json
Normal file
767
api/constants/recommended_apps.json
Normal file
File diff suppressed because one or more lines are too long
@ -3,12 +3,9 @@ from flask_restful import Resource, fields, marshal_with, reqparse
|
||||
|
||||
from constants.languages import languages
|
||||
from controllers.console import api
|
||||
from controllers.console.app.error import AppNotFoundError
|
||||
from controllers.console.wraps import account_initialization_required
|
||||
from extensions.ext_database import db
|
||||
from libs.login import login_required
|
||||
from models.model import App, RecommendedApp
|
||||
from services.app_service import AppService
|
||||
from services.recommended_app_service import RecommendedAppService
|
||||
|
||||
app_fields = {
|
||||
'id': fields.String,
|
||||
@ -52,38 +49,7 @@ class RecommendedAppListApi(Resource):
|
||||
else:
|
||||
language_prefix = languages[0]
|
||||
|
||||
recommended_apps = db.session.query(RecommendedApp).filter(
|
||||
RecommendedApp.is_listed == True,
|
||||
RecommendedApp.language == language_prefix
|
||||
).all()
|
||||
|
||||
categories = set()
|
||||
recommended_apps_result = []
|
||||
for recommended_app in recommended_apps:
|
||||
app = recommended_app.app
|
||||
if not app or not app.is_public:
|
||||
continue
|
||||
|
||||
site = app.site
|
||||
if not site:
|
||||
continue
|
||||
|
||||
recommended_app_result = {
|
||||
'id': recommended_app.id,
|
||||
'app': app,
|
||||
'app_id': recommended_app.app_id,
|
||||
'description': site.description,
|
||||
'copyright': site.copyright,
|
||||
'privacy_policy': site.privacy_policy,
|
||||
'category': recommended_app.category,
|
||||
'position': recommended_app.position,
|
||||
'is_listed': recommended_app.is_listed
|
||||
}
|
||||
recommended_apps_result.append(recommended_app_result)
|
||||
|
||||
categories.add(recommended_app.category) # add category to categories
|
||||
|
||||
return {'recommended_apps': recommended_apps_result, 'categories': list(categories)}
|
||||
return RecommendedAppService.get_recommended_apps_and_categories(language_prefix)
|
||||
|
||||
|
||||
class RecommendedAppApi(Resource):
|
||||
@ -91,32 +57,7 @@ class RecommendedAppApi(Resource):
|
||||
@account_initialization_required
|
||||
def get(self, app_id):
|
||||
app_id = str(app_id)
|
||||
|
||||
# is in public recommended list
|
||||
recommended_app = db.session.query(RecommendedApp).filter(
|
||||
RecommendedApp.is_listed == True,
|
||||
RecommendedApp.app_id == app_id
|
||||
).first()
|
||||
|
||||
if not recommended_app:
|
||||
raise AppNotFoundError
|
||||
|
||||
# get app detail
|
||||
app_model = db.session.query(App).filter(App.id == app_id).first()
|
||||
if not app_model or not app_model.is_public:
|
||||
raise AppNotFoundError
|
||||
|
||||
app_service = AppService()
|
||||
export_str = app_service.export_app(app_model)
|
||||
|
||||
return {
|
||||
'id': app_model.id,
|
||||
'name': app_model.name,
|
||||
'icon': app_model.icon,
|
||||
'icon_background': app_model.icon_background,
|
||||
'mode': app_model.mode,
|
||||
'export_data': export_str
|
||||
}
|
||||
return RecommendedAppService.get_recommend_app_detail(app_id)
|
||||
|
||||
|
||||
api.add_resource(RecommendedAppListApi, '/explore/apps')
|
||||
|
||||
@ -97,6 +97,7 @@ class AppService:
|
||||
else:
|
||||
default_model_dict = default_model_config['model']
|
||||
|
||||
default_model_dict = default_model_dict.copy()
|
||||
default_model_config['model'] = json.dumps(default_model_dict)
|
||||
|
||||
app = App(**app_template['app'])
|
||||
|
||||
245
api/services/recommended_app_service.py
Normal file
245
api/services/recommended_app_service.py
Normal file
@ -0,0 +1,245 @@
|
||||
import json
|
||||
import logging
|
||||
from os import path
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
from flask import current_app
|
||||
|
||||
from constants.languages import languages
|
||||
from extensions.ext_database import db
|
||||
from models.model import App, RecommendedApp
|
||||
from services.app_service import AppService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RecommendedAppService:
|
||||
|
||||
builtin_data: Optional[dict] = None
|
||||
|
||||
@classmethod
|
||||
def get_recommended_apps_and_categories(cls, language: str) -> dict:
|
||||
"""
|
||||
Get recommended apps and categories.
|
||||
:param language: language
|
||||
:return:
|
||||
"""
|
||||
mode = current_app.config.get('HOSTED_FETCH_APP_TEMPLATES_MODE', 'remote')
|
||||
if mode == 'remote':
|
||||
try:
|
||||
result = cls._fetch_recommended_apps_from_dify_official(language)
|
||||
except Exception as e:
|
||||
logger.warning(f'fetch recommended apps from dify official failed: {e}, switch to built-in.')
|
||||
result = cls._fetch_recommended_apps_from_builtin(language)
|
||||
elif mode == 'db':
|
||||
result = cls._fetch_recommended_apps_from_db(language)
|
||||
elif mode == 'builtin':
|
||||
result = cls._fetch_recommended_apps_from_builtin(language)
|
||||
else:
|
||||
raise ValueError(f'invalid fetch recommended apps mode: {mode}')
|
||||
|
||||
if not result.get('recommended_apps') and language != 'en-US':
|
||||
result = cls._fetch_recommended_apps_from_builtin('en-US')
|
||||
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def _fetch_recommended_apps_from_db(cls, language: str) -> dict:
|
||||
"""
|
||||
Fetch recommended apps from db.
|
||||
:param language: language
|
||||
:return:
|
||||
"""
|
||||
recommended_apps = db.session.query(RecommendedApp).filter(
|
||||
RecommendedApp.is_listed == True,
|
||||
RecommendedApp.language == language
|
||||
).all()
|
||||
|
||||
categories = set()
|
||||
recommended_apps_result = []
|
||||
for recommended_app in recommended_apps:
|
||||
app = recommended_app.app
|
||||
if not app or not app.is_public:
|
||||
continue
|
||||
|
||||
site = app.site
|
||||
if not site:
|
||||
continue
|
||||
|
||||
recommended_app_result = {
|
||||
'id': recommended_app.id,
|
||||
'app': {
|
||||
'id': app.id,
|
||||
'name': app.name,
|
||||
'mode': app.mode,
|
||||
'icon': app.icon,
|
||||
'icon_background': app.icon_background
|
||||
},
|
||||
'app_id': recommended_app.app_id,
|
||||
'description': site.description,
|
||||
'copyright': site.copyright,
|
||||
'privacy_policy': site.privacy_policy,
|
||||
'category': recommended_app.category,
|
||||
'position': recommended_app.position,
|
||||
'is_listed': recommended_app.is_listed
|
||||
}
|
||||
recommended_apps_result.append(recommended_app_result)
|
||||
|
||||
categories.add(recommended_app.category) # add category to categories
|
||||
|
||||
return {'recommended_apps': recommended_apps_result, 'categories': list(categories)}
|
||||
|
||||
@classmethod
|
||||
def _fetch_recommended_apps_from_dify_official(cls, language: str) -> dict:
|
||||
"""
|
||||
Fetch recommended apps from dify official.
|
||||
:param language: language
|
||||
:return:
|
||||
"""
|
||||
domain = current_app.config.get('HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN', 'https://tmpl.dify.ai')
|
||||
url = f'{domain}/apps?language={language}'
|
||||
response = requests.get(url, timeout=(3, 10))
|
||||
if response.status_code != 200:
|
||||
raise ValueError(f'fetch recommended apps failed, status code: {response.status_code}')
|
||||
|
||||
return response.json()
|
||||
|
||||
@classmethod
|
||||
def _fetch_recommended_apps_from_builtin(cls, language: str) -> dict:
|
||||
"""
|
||||
Fetch recommended apps from builtin.
|
||||
:param language: language
|
||||
:return:
|
||||
"""
|
||||
builtin_data = cls._get_builtin_data()
|
||||
return builtin_data.get('recommended_apps', {}).get(language)
|
||||
|
||||
@classmethod
|
||||
def get_recommend_app_detail(cls, app_id: str) -> Optional[dict]:
|
||||
"""
|
||||
Get recommend app detail.
|
||||
:param app_id: app id
|
||||
:return:
|
||||
"""
|
||||
mode = current_app.config.get('HOSTED_FETCH_APP_TEMPLATES_MODE', 'remote')
|
||||
if mode == 'remote':
|
||||
try:
|
||||
result = cls._fetch_recommended_app_detail_from_dify_official(app_id)
|
||||
except Exception as e:
|
||||
logger.warning(f'fetch recommended app detail from dify official failed: {e}, switch to built-in.')
|
||||
result = cls._fetch_recommended_app_detail_from_builtin(app_id)
|
||||
elif mode == 'db':
|
||||
result = cls._fetch_recommended_app_detail_from_db(app_id)
|
||||
elif mode == 'builtin':
|
||||
result = cls._fetch_recommended_app_detail_from_builtin(app_id)
|
||||
else:
|
||||
raise ValueError(f'invalid fetch recommended app detail mode: {mode}')
|
||||
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def _fetch_recommended_app_detail_from_dify_official(cls, app_id: str) -> Optional[dict]:
|
||||
"""
|
||||
Fetch recommended app detail from dify official.
|
||||
:param app_id: App ID
|
||||
:return:
|
||||
"""
|
||||
domain = current_app.config.get('HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN', 'https://tmpl.dify.ai')
|
||||
url = f'{domain}/apps/{app_id}'
|
||||
response = requests.get(url, timeout=(3, 10))
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
return response.json()
|
||||
|
||||
@classmethod
|
||||
def _fetch_recommended_app_detail_from_db(cls, app_id: str) -> Optional[dict]:
|
||||
"""
|
||||
Fetch recommended app detail from db.
|
||||
:param app_id: App ID
|
||||
:return:
|
||||
"""
|
||||
# is in public recommended list
|
||||
recommended_app = db.session.query(RecommendedApp).filter(
|
||||
RecommendedApp.is_listed == True,
|
||||
RecommendedApp.app_id == app_id
|
||||
).first()
|
||||
|
||||
if not recommended_app:
|
||||
return None
|
||||
|
||||
# get app detail
|
||||
app_model = db.session.query(App).filter(App.id == app_id).first()
|
||||
if not app_model or not app_model.is_public:
|
||||
return None
|
||||
|
||||
app_service = AppService()
|
||||
export_str = app_service.export_app(app_model)
|
||||
|
||||
return {
|
||||
'id': app_model.id,
|
||||
'name': app_model.name,
|
||||
'icon': app_model.icon,
|
||||
'icon_background': app_model.icon_background,
|
||||
'mode': app_model.mode,
|
||||
'export_data': export_str
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _fetch_recommended_app_detail_from_builtin(cls, app_id: str) -> Optional[dict]:
|
||||
"""
|
||||
Fetch recommended app detail from builtin.
|
||||
:param app_id: App ID
|
||||
:return:
|
||||
"""
|
||||
builtin_data = cls._get_builtin_data()
|
||||
return builtin_data.get('app_details', {}).get(app_id)
|
||||
|
||||
@classmethod
|
||||
def _get_builtin_data(cls) -> dict:
|
||||
"""
|
||||
Get builtin data.
|
||||
:return:
|
||||
"""
|
||||
if cls.builtin_data:
|
||||
return cls.builtin_data
|
||||
|
||||
root_path = current_app.root_path
|
||||
with open(path.join(root_path, 'constants', 'recommended_apps.json'), encoding='utf-8') as f:
|
||||
json_data = f.read()
|
||||
data = json.loads(json_data)
|
||||
cls.builtin_data = data
|
||||
|
||||
return cls.builtin_data
|
||||
|
||||
@classmethod
|
||||
def fetch_all_recommended_apps_and_export_datas(cls):
|
||||
"""
|
||||
Fetch all recommended apps and export datas
|
||||
:return:
|
||||
"""
|
||||
templates = {
|
||||
"recommended_apps": {},
|
||||
"app_details": {}
|
||||
}
|
||||
for language in languages:
|
||||
try:
|
||||
result = cls._fetch_recommended_apps_from_dify_official(language)
|
||||
except Exception as e:
|
||||
logger.warning(f'fetch recommended apps from dify official failed: {e}, skip.')
|
||||
continue
|
||||
|
||||
templates['recommended_apps'][language] = result
|
||||
|
||||
for recommended_app in result.get('recommended_apps'):
|
||||
app_id = recommended_app.get('app_id')
|
||||
|
||||
# get app detail
|
||||
app_detail = cls._fetch_recommended_app_detail_from_dify_official(app_id)
|
||||
if not app_detail:
|
||||
continue
|
||||
|
||||
templates['app_details'][app_id] = app_detail
|
||||
|
||||
return templates
|
||||
Loading…
Reference in New Issue
Block a user