model_config.py 6.2 KB

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