app.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import json
  2. from flask import current_app
  3. from flask_restful import fields, marshal_with, Resource
  4. from controllers.service_api import api
  5. from controllers.service_api.wraps import validate_app_token
  6. from extensions.ext_database import db
  7. from models.model import App, AppModelConfig
  8. from models.tools import ApiToolProvider
  9. class AppParameterApi(Resource):
  10. """Resource for app variables."""
  11. variable_fields = {
  12. 'key': fields.String,
  13. 'name': fields.String,
  14. 'description': fields.String,
  15. 'type': fields.String,
  16. 'default': fields.String,
  17. 'max_length': fields.Integer,
  18. 'options': fields.List(fields.String)
  19. }
  20. system_parameters_fields = {
  21. 'image_file_size_limit': fields.String
  22. }
  23. parameters_fields = {
  24. 'opening_statement': fields.String,
  25. 'suggested_questions': fields.Raw,
  26. 'suggested_questions_after_answer': fields.Raw,
  27. 'speech_to_text': fields.Raw,
  28. 'text_to_speech': fields.Raw,
  29. 'retriever_resource': fields.Raw,
  30. 'annotation_reply': fields.Raw,
  31. 'more_like_this': fields.Raw,
  32. 'user_input_form': fields.Raw,
  33. 'sensitive_word_avoidance': fields.Raw,
  34. 'file_upload': fields.Raw,
  35. 'system_parameters': fields.Nested(system_parameters_fields)
  36. }
  37. @validate_app_token
  38. @marshal_with(parameters_fields)
  39. def get(self, app_model: App):
  40. """Retrieve app parameters."""
  41. app_model_config = app_model.app_model_config
  42. return {
  43. 'opening_statement': app_model_config.opening_statement,
  44. 'suggested_questions': app_model_config.suggested_questions_list,
  45. 'suggested_questions_after_answer': app_model_config.suggested_questions_after_answer_dict,
  46. 'speech_to_text': app_model_config.speech_to_text_dict,
  47. 'text_to_speech': app_model_config.text_to_speech_dict,
  48. 'retriever_resource': app_model_config.retriever_resource_dict,
  49. 'annotation_reply': app_model_config.annotation_reply_dict,
  50. 'more_like_this': app_model_config.more_like_this_dict,
  51. 'user_input_form': app_model_config.user_input_form_list,
  52. 'sensitive_word_avoidance': app_model_config.sensitive_word_avoidance_dict,
  53. 'file_upload': app_model_config.file_upload_dict,
  54. 'system_parameters': {
  55. 'image_file_size_limit': current_app.config.get('UPLOAD_IMAGE_FILE_SIZE_LIMIT')
  56. }
  57. }
  58. class AppMetaApi(Resource):
  59. @validate_app_token
  60. def get(self, app_model: App):
  61. """Get app meta"""
  62. app_model_config: AppModelConfig = app_model.app_model_config
  63. agent_config = app_model_config.agent_mode_dict or {}
  64. meta = {
  65. 'tool_icons': {}
  66. }
  67. # get all tools
  68. tools = agent_config.get('tools', [])
  69. url_prefix = (current_app.config.get("CONSOLE_API_URL")
  70. + "/console/api/workspaces/current/tool-provider/builtin/")
  71. for tool in tools:
  72. keys = list(tool.keys())
  73. if len(keys) >= 4:
  74. # current tool standard
  75. provider_type = tool.get('provider_type')
  76. provider_id = tool.get('provider_id')
  77. tool_name = tool.get('tool_name')
  78. if provider_type == 'builtin':
  79. meta['tool_icons'][tool_name] = url_prefix + provider_id + '/icon'
  80. elif provider_type == 'api':
  81. try:
  82. provider: ApiToolProvider = db.session.query(ApiToolProvider).filter(
  83. ApiToolProvider.id == provider_id
  84. )
  85. meta['tool_icons'][tool_name] = json.loads(provider.icon)
  86. except:
  87. meta['tool_icons'][tool_name] = {
  88. "background": "#252525",
  89. "content": "\ud83d\ude01"
  90. }
  91. return meta
  92. api.add_resource(AppParameterApi, '/parameters')
  93. api.add_resource(AppMetaApi, '/meta')