model_config.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import json
  2. from flask import request
  3. from flask_login import current_user
  4. from flask_restful import Resource
  5. from controllers.console import api
  6. from controllers.console.app.wraps import get_app_model
  7. from controllers.console.setup import setup_required
  8. from controllers.console.wraps import account_initialization_required
  9. from core.agent.entities import AgentToolEntity
  10. from core.tools.tool_manager import ToolManager
  11. from core.tools.utils.configuration import ToolParameterConfigurationManager
  12. from events.app_event import app_model_config_was_updated
  13. from extensions.ext_database import db
  14. from libs.login import login_required
  15. from models.model import AppMode, AppModelConfig
  16. from services.app_model_config_service import AppModelConfigService
  17. class ModelConfigResource(Resource):
  18. @setup_required
  19. @login_required
  20. @account_initialization_required
  21. @get_app_model(mode=[AppMode.AGENT_CHAT, AppMode.CHAT, AppMode.COMPLETION])
  22. def post(self, app_model):
  23. """Modify app model config"""
  24. # validate config
  25. model_configuration = AppModelConfigService.validate_configuration(
  26. tenant_id=current_user.current_tenant_id,
  27. config=request.json,
  28. app_mode=AppMode.value_of(app_model.mode)
  29. )
  30. new_app_model_config = AppModelConfig(
  31. app_id=app_model.id,
  32. )
  33. new_app_model_config = new_app_model_config.from_model_config_dict(model_configuration)
  34. if app_model.mode == AppMode.AGENT_CHAT.value or app_model.is_agent:
  35. # get original app model config
  36. original_app_model_config: AppModelConfig = db.session.query(AppModelConfig).filter(
  37. AppModelConfig.id == app_model.app_model_config_id
  38. ).first()
  39. agent_mode = original_app_model_config.agent_mode_dict
  40. # decrypt agent tool parameters if it's secret-input
  41. parameter_map = {}
  42. masked_parameter_map = {}
  43. tool_map = {}
  44. for tool in agent_mode.get('tools') or []:
  45. if not isinstance(tool, dict) or len(tool.keys()) <= 3:
  46. continue
  47. agent_tool_entity = AgentToolEntity(**tool)
  48. # get tool
  49. try:
  50. tool_runtime = ToolManager.get_agent_tool_runtime(
  51. tenant_id=current_user.current_tenant_id,
  52. app_id=app_model.id,
  53. agent_tool=agent_tool_entity,
  54. )
  55. manager = ToolParameterConfigurationManager(
  56. tenant_id=current_user.current_tenant_id,
  57. tool_runtime=tool_runtime,
  58. provider_name=agent_tool_entity.provider_id,
  59. provider_type=agent_tool_entity.provider_type,
  60. identity_id=f'AGENT.{app_model.id}'
  61. )
  62. except Exception as e:
  63. continue
  64. # get decrypted parameters
  65. if agent_tool_entity.tool_parameters:
  66. parameters = manager.decrypt_tool_parameters(agent_tool_entity.tool_parameters or {})
  67. masked_parameter = manager.mask_tool_parameters(parameters or {})
  68. else:
  69. parameters = {}
  70. masked_parameter = {}
  71. key = f'{agent_tool_entity.provider_id}.{agent_tool_entity.provider_type}.{agent_tool_entity.tool_name}'
  72. masked_parameter_map[key] = masked_parameter
  73. parameter_map[key] = parameters
  74. tool_map[key] = tool_runtime
  75. # encrypt agent tool parameters if it's secret-input
  76. agent_mode = new_app_model_config.agent_mode_dict
  77. for tool in agent_mode.get('tools') or []:
  78. agent_tool_entity = AgentToolEntity(**tool)
  79. # get tool
  80. key = f'{agent_tool_entity.provider_id}.{agent_tool_entity.provider_type}.{agent_tool_entity.tool_name}'
  81. if key in tool_map:
  82. tool_runtime = tool_map[key]
  83. else:
  84. try:
  85. tool_runtime = ToolManager.get_agent_tool_runtime(
  86. tenant_id=current_user.current_tenant_id,
  87. app_id=app_model.id,
  88. agent_tool=agent_tool_entity,
  89. )
  90. except Exception as e:
  91. continue
  92. manager = ToolParameterConfigurationManager(
  93. tenant_id=current_user.current_tenant_id,
  94. tool_runtime=tool_runtime,
  95. provider_name=agent_tool_entity.provider_id,
  96. provider_type=agent_tool_entity.provider_type,
  97. identity_id=f'AGENT.{app_model.id}'
  98. )
  99. manager.delete_tool_parameters_cache()
  100. # override parameters if it equals to masked parameters
  101. if agent_tool_entity.tool_parameters:
  102. if key not in masked_parameter_map:
  103. continue
  104. for masked_key, masked_value in masked_parameter_map[key].items():
  105. if masked_key in agent_tool_entity.tool_parameters and \
  106. agent_tool_entity.tool_parameters[masked_key] == masked_value:
  107. agent_tool_entity.tool_parameters[masked_key] = parameter_map[key].get(masked_key)
  108. # encrypt parameters
  109. if agent_tool_entity.tool_parameters:
  110. tool['tool_parameters'] = manager.encrypt_tool_parameters(agent_tool_entity.tool_parameters or {})
  111. # update app model config
  112. new_app_model_config.agent_mode = json.dumps(agent_mode)
  113. db.session.add(new_app_model_config)
  114. db.session.flush()
  115. app_model.app_model_config_id = new_app_model_config.id
  116. db.session.commit()
  117. app_model_config_was_updated.send(
  118. app_model,
  119. app_model_config=new_app_model_config
  120. )
  121. return {'result': 'success'}
  122. api.add_resource(ModelConfigResource, '/apps/<uuid:app_id>/model-config')