model_config.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import json
  2. from typing import cast
  3. from flask import request
  4. from flask_login import current_user # type: ignore
  5. from flask_restful import Resource # type: ignore
  6. from controllers.console import api
  7. from controllers.console.app.wraps import get_app_model
  8. from controllers.console.wraps import account_initialization_required, setup_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=cast(dict, 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. created_by=current_user.id,
  33. updated_by=current_user.id,
  34. )
  35. new_app_model_config = new_app_model_config.from_model_config_dict(model_configuration)
  36. if app_model.mode == AppMode.AGENT_CHAT.value or app_model.is_agent:
  37. # get original app model config
  38. original_app_model_config = (
  39. db.session.query(AppModelConfig).filter(AppModelConfig.id == app_model.app_model_config_id).first()
  40. )
  41. if original_app_model_config is None:
  42. raise ValueError("Original app model config not found")
  43. agent_mode = original_app_model_config.agent_mode_dict
  44. # decrypt agent tool parameters if it's secret-input
  45. parameter_map = {}
  46. masked_parameter_map = {}
  47. tool_map = {}
  48. for tool in agent_mode.get("tools") or []:
  49. if not isinstance(tool, dict) or len(tool.keys()) <= 3:
  50. continue
  51. agent_tool_entity = AgentToolEntity(**tool)
  52. # get tool
  53. try:
  54. tool_runtime = ToolManager.get_agent_tool_runtime(
  55. tenant_id=current_user.current_tenant_id,
  56. app_id=app_model.id,
  57. agent_tool=agent_tool_entity,
  58. )
  59. manager = ToolParameterConfigurationManager(
  60. tenant_id=current_user.current_tenant_id,
  61. tool_runtime=tool_runtime,
  62. provider_name=agent_tool_entity.provider_id,
  63. provider_type=agent_tool_entity.provider_type,
  64. identity_id=f"AGENT.{app_model.id}",
  65. )
  66. except Exception:
  67. continue
  68. # get decrypted parameters
  69. if agent_tool_entity.tool_parameters:
  70. parameters = manager.decrypt_tool_parameters(agent_tool_entity.tool_parameters or {})
  71. masked_parameter = manager.mask_tool_parameters(parameters or {})
  72. else:
  73. parameters = {}
  74. masked_parameter = {}
  75. key = f"{agent_tool_entity.provider_id}.{agent_tool_entity.provider_type}.{agent_tool_entity.tool_name}"
  76. masked_parameter_map[key] = masked_parameter
  77. parameter_map[key] = parameters
  78. tool_map[key] = tool_runtime
  79. # encrypt agent tool parameters if it's secret-input
  80. agent_mode = new_app_model_config.agent_mode_dict
  81. for tool in agent_mode.get("tools") or []:
  82. agent_tool_entity = AgentToolEntity(**tool)
  83. # get tool
  84. key = f"{agent_tool_entity.provider_id}.{agent_tool_entity.provider_type}.{agent_tool_entity.tool_name}"
  85. if key in tool_map:
  86. tool_runtime = tool_map[key]
  87. else:
  88. try:
  89. tool_runtime = ToolManager.get_agent_tool_runtime(
  90. tenant_id=current_user.current_tenant_id,
  91. app_id=app_model.id,
  92. agent_tool=agent_tool_entity,
  93. )
  94. except Exception:
  95. continue
  96. manager = ToolParameterConfigurationManager(
  97. tenant_id=current_user.current_tenant_id,
  98. tool_runtime=tool_runtime,
  99. provider_name=agent_tool_entity.provider_id,
  100. provider_type=agent_tool_entity.provider_type,
  101. identity_id=f"AGENT.{app_model.id}",
  102. )
  103. manager.delete_tool_parameters_cache()
  104. # override parameters if it equals to masked parameters
  105. if agent_tool_entity.tool_parameters:
  106. if key not in masked_parameter_map:
  107. continue
  108. for masked_key, masked_value in masked_parameter_map[key].items():
  109. if (
  110. masked_key in agent_tool_entity.tool_parameters
  111. and agent_tool_entity.tool_parameters[masked_key] == masked_value
  112. ):
  113. agent_tool_entity.tool_parameters[masked_key] = parameter_map[key].get(masked_key)
  114. # encrypt parameters
  115. if agent_tool_entity.tool_parameters:
  116. tool["tool_parameters"] = manager.encrypt_tool_parameters(agent_tool_entity.tool_parameters or {})
  117. # update app model config
  118. new_app_model_config.agent_mode = json.dumps(agent_mode)
  119. db.session.add(new_app_model_config)
  120. db.session.flush()
  121. app_model.app_model_config_id = new_app_model_config.id
  122. db.session.commit()
  123. app_model_config_was_updated.send(app_model, app_model_config=new_app_model_config)
  124. return {"result": "success"}
  125. api.add_resource(ModelConfigResource, "/apps/<uuid:app_id>/model-config")