+40









lyzno1
GitHub
-LAN-
GuanMu
Davide Delbianco
NeatGuyCoding
kenwoodjw
Yongtao Huang
Yongtao Huang
Qiang Lee
李强04
autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Asuka Minato
Matri Qi
huayaoyue6
Bowen Liang
znn
crazywoola
crazywoola
Copilot
yihong
Muke Wang
wangmuke
Wu Tianwei
quicksand
非法操作
zxhlyh
Eric Guo
Zhedong Cen
jiangbo721
github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
hjlarry
lxsummer
湛露先生
Guangdong Liu
QuantumGhost
Claude
Yessenia-d
huangzhuo1949
huangzhuo
17hz
Amy
Joel
Nite Knite
Yeuoly
Petrus Han
iamjoel
Kalo Chin
Ujjwal Maurya
Maries
5bbf685035
Signed-off-by: -LAN- <[email protected]> Signed-off-by: kenwoodjw <[email protected]> Signed-off-by: Yongtao Huang <[email protected]> Signed-off-by: yihong0618 <[email protected]> Signed-off-by: zhanluxianshen <[email protected]> Co-authored-by: -LAN- <[email protected]> Co-authored-by: GuanMu <[email protected]> Co-authored-by: Davide Delbianco <[email protected]> Co-authored-by: NeatGuyCoding <[email protected]> Co-authored-by: kenwoodjw <[email protected]> Co-authored-by: Yongtao Huang <[email protected]> Co-authored-by: Yongtao Huang <[email protected]> Co-authored-by: Qiang Lee <[email protected]> Co-authored-by: 李强04 <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <[email protected]> Co-authored-by: Matri Qi <[email protected]> Co-authored-by: huayaoyue6 <[email protected]> Co-authored-by: Bowen Liang <[email protected]> Co-authored-by: znn <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: yihong <[email protected]> Co-authored-by: Muke Wang <[email protected]> Co-authored-by: wangmuke <[email protected]> Co-authored-by: Wu Tianwei <[email protected]> Co-authored-by: quicksand <[email protected]> Co-authored-by: 非法操作 <[email protected]> Co-authored-by: zxhlyh <[email protected]> Co-authored-by: Eric Guo <[email protected]> Co-authored-by: Zhedong Cen <[email protected]> Co-authored-by: jiangbo721 <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <[email protected]> Co-authored-by: lxsummer <[email protected]> Co-authored-by: 湛露先生 <[email protected]> Co-authored-by: Guangdong Liu <[email protected]> Co-authored-by: QuantumGhost <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: Yessenia-d <[email protected]> Co-authored-by: huangzhuo1949 <[email protected]> Co-authored-by: huangzhuo <[email protected]> Co-authored-by: 17hz <[email protected]> Co-authored-by: Amy <[email protected]> Co-authored-by: Joel <[email protected]> Co-authored-by: Nite Knite <[email protected]> Co-authored-by: Yeuoly <[email protected]> Co-authored-by: Petrus Han <[email protected]> Co-authored-by: iamjoel <[email protected]> Co-authored-by: Kalo Chin <[email protected]> Co-authored-by: Ujjwal Maurya <[email protected]> Co-authored-by: Maries <[email protected]>
148 lines
6.3 KiB
Python
148 lines
6.3 KiB
Python
import json
|
|
from typing import cast
|
|
|
|
from flask import request
|
|
from flask_login import current_user
|
|
from flask_restx import Resource
|
|
|
|
from controllers.console import api
|
|
from controllers.console.app.wraps import get_app_model
|
|
from controllers.console.wraps import account_initialization_required, setup_required
|
|
from core.agent.entities import AgentToolEntity
|
|
from core.tools.tool_manager import ToolManager
|
|
from core.tools.utils.configuration import ToolParameterConfigurationManager
|
|
from events.app_event import app_model_config_was_updated
|
|
from extensions.ext_database import db
|
|
from libs.login import login_required
|
|
from models.model import AppMode, AppModelConfig
|
|
from services.app_model_config_service import AppModelConfigService
|
|
|
|
|
|
class ModelConfigResource(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model(mode=[AppMode.AGENT_CHAT, AppMode.CHAT, AppMode.COMPLETION])
|
|
def post(self, app_model):
|
|
"""Modify app model config"""
|
|
# validate config
|
|
model_configuration = AppModelConfigService.validate_configuration(
|
|
tenant_id=current_user.current_tenant_id,
|
|
config=cast(dict, request.json),
|
|
app_mode=AppMode.value_of(app_model.mode),
|
|
)
|
|
|
|
new_app_model_config = AppModelConfig(
|
|
app_id=app_model.id,
|
|
created_by=current_user.id,
|
|
updated_by=current_user.id,
|
|
)
|
|
new_app_model_config = new_app_model_config.from_model_config_dict(model_configuration)
|
|
|
|
if app_model.mode == AppMode.AGENT_CHAT.value or app_model.is_agent:
|
|
# get original app model config
|
|
original_app_model_config = (
|
|
db.session.query(AppModelConfig).where(AppModelConfig.id == app_model.app_model_config_id).first()
|
|
)
|
|
if original_app_model_config is None:
|
|
raise ValueError("Original app model config not found")
|
|
agent_mode = original_app_model_config.agent_mode_dict
|
|
# decrypt agent tool parameters if it's secret-input
|
|
parameter_map = {}
|
|
masked_parameter_map = {}
|
|
tool_map = {}
|
|
for tool in agent_mode.get("tools") or []:
|
|
if not isinstance(tool, dict) or len(tool.keys()) <= 3:
|
|
continue
|
|
|
|
agent_tool_entity = AgentToolEntity(**tool)
|
|
# get tool
|
|
try:
|
|
tool_runtime = ToolManager.get_agent_tool_runtime(
|
|
tenant_id=current_user.current_tenant_id,
|
|
app_id=app_model.id,
|
|
agent_tool=agent_tool_entity,
|
|
)
|
|
manager = ToolParameterConfigurationManager(
|
|
tenant_id=current_user.current_tenant_id,
|
|
tool_runtime=tool_runtime,
|
|
provider_name=agent_tool_entity.provider_id,
|
|
provider_type=agent_tool_entity.provider_type,
|
|
identity_id=f"AGENT.{app_model.id}",
|
|
)
|
|
except Exception:
|
|
continue
|
|
|
|
# get decrypted parameters
|
|
if agent_tool_entity.tool_parameters:
|
|
parameters = manager.decrypt_tool_parameters(agent_tool_entity.tool_parameters or {})
|
|
masked_parameter = manager.mask_tool_parameters(parameters or {})
|
|
else:
|
|
parameters = {}
|
|
masked_parameter = {}
|
|
|
|
key = f"{agent_tool_entity.provider_id}.{agent_tool_entity.provider_type}.{agent_tool_entity.tool_name}"
|
|
masked_parameter_map[key] = masked_parameter
|
|
parameter_map[key] = parameters
|
|
tool_map[key] = tool_runtime
|
|
|
|
# encrypt agent tool parameters if it's secret-input
|
|
agent_mode = new_app_model_config.agent_mode_dict
|
|
for tool in agent_mode.get("tools") or []:
|
|
agent_tool_entity = AgentToolEntity(**tool)
|
|
|
|
# get tool
|
|
key = f"{agent_tool_entity.provider_id}.{agent_tool_entity.provider_type}.{agent_tool_entity.tool_name}"
|
|
if key in tool_map:
|
|
tool_runtime = tool_map[key]
|
|
else:
|
|
try:
|
|
tool_runtime = ToolManager.get_agent_tool_runtime(
|
|
tenant_id=current_user.current_tenant_id,
|
|
app_id=app_model.id,
|
|
agent_tool=agent_tool_entity,
|
|
)
|
|
except Exception:
|
|
continue
|
|
|
|
manager = ToolParameterConfigurationManager(
|
|
tenant_id=current_user.current_tenant_id,
|
|
tool_runtime=tool_runtime,
|
|
provider_name=agent_tool_entity.provider_id,
|
|
provider_type=agent_tool_entity.provider_type,
|
|
identity_id=f"AGENT.{app_model.id}",
|
|
)
|
|
manager.delete_tool_parameters_cache()
|
|
|
|
# override parameters if it equals to masked parameters
|
|
if agent_tool_entity.tool_parameters:
|
|
if key not in masked_parameter_map:
|
|
continue
|
|
|
|
for masked_key, masked_value in masked_parameter_map[key].items():
|
|
if (
|
|
masked_key in agent_tool_entity.tool_parameters
|
|
and agent_tool_entity.tool_parameters[masked_key] == masked_value
|
|
):
|
|
agent_tool_entity.tool_parameters[masked_key] = parameter_map[key].get(masked_key)
|
|
|
|
# encrypt parameters
|
|
if agent_tool_entity.tool_parameters:
|
|
tool["tool_parameters"] = manager.encrypt_tool_parameters(agent_tool_entity.tool_parameters or {})
|
|
|
|
# update app model config
|
|
new_app_model_config.agent_mode = json.dumps(agent_mode)
|
|
|
|
db.session.add(new_app_model_config)
|
|
db.session.flush()
|
|
|
|
app_model.app_model_config_id = new_app_model_config.id
|
|
db.session.commit()
|
|
|
|
app_model_config_was_updated.send(app_model, app_model_config=new_app_model_config)
|
|
|
|
return {"result": "success"}
|
|
|
|
|
|
api.add_resource(ModelConfigResource, "/apps/<uuid:app_id>/model-config")
|