Skip to content

Commit 5ca69de

Browse files
committed
[Bug] Fixed the issue where large language model information was not written to the database when importing agent configuration.
1 parent 9e9253c commit 5ca69de

File tree

3 files changed

+1250
-4
lines changed

3 files changed

+1250
-4
lines changed

backend/consts/model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ class ExportAndImportAgentInfo(BaseModel):
274274
enabled: bool
275275
tools: List[ToolConfig]
276276
managed_agents: List[int]
277+
model_id: Optional[int] = None
278+
model_name: Optional[str] = None
279+
business_logic_model_id: Optional[int] = None
280+
business_logic_model_name: Optional[str] = None
277281

278282
class Config:
279283
arbitrary_types_allowed = True

backend/services/agent_service.py

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from agents.agent_run_manager import agent_run_manager
1414
from agents.create_agent_info import create_agent_run_info, create_tool_config_list
1515
from agents.preprocess_manager import preprocess_manager
16-
from consts.const import MEMORY_SEARCH_START_MSG, MEMORY_SEARCH_DONE_MSG, MEMORY_SEARCH_FAIL_MSG, TOOL_TYPE_MAPPING, LANGUAGE, MESSAGE_ROLE
16+
from consts.const import MEMORY_SEARCH_START_MSG, MEMORY_SEARCH_DONE_MSG, MEMORY_SEARCH_FAIL_MSG, TOOL_TYPE_MAPPING, LANGUAGE, MESSAGE_ROLE, MODEL_CONFIG_MAPPING
1717
from consts.exceptions import MemoryPreparationException
1818
from consts.model import (
1919
AgentInfoRequest,
@@ -37,7 +37,7 @@
3737
search_blank_sub_agent_by_main_agent_id,
3838
update_agent
3939
)
40-
from database.model_management_db import get_model_by_model_id
40+
from database.model_management_db import get_model_by_model_id, get_model_id_by_display_name
4141
from database.remote_mcp_db import check_mcp_name_exists, get_mcp_server_by_name_and_tenant
4242
from database.tool_db import (
4343
check_tool_is_available,
@@ -52,6 +52,7 @@
5252
from services.remote_mcp_service import add_remote_mcp_server_list
5353
from services.tool_configuration_service import update_tool_list
5454
from utils.auth_utils import get_current_user_info, get_user_language
55+
from utils.config_utils import tenant_config_manager
5556
from utils.memory_utils import build_memory_config
5657
from utils.thread_utils import submit
5758

@@ -429,6 +430,20 @@ async def export_agent_by_agent_id(agent_id: int, tenant_id: str, user_id: str)
429430
if tool.class_name == "KnowledgeBaseSearchTool":
430431
tool.metadata = {}
431432

433+
# Get model_id and model display name from agent_info
434+
model_id = agent_info.get("model_id")
435+
model_name = None
436+
if model_id is not None:
437+
model_info = get_model_by_model_id(model_id)
438+
model_name = model_info.get("display_name") if model_info is not None else None
439+
440+
# Get business_logic_model_id and business logic model display name
441+
business_logic_model_id = agent_info.get("business_logic_model_id")
442+
business_logic_model_name = None
443+
if business_logic_model_id is not None:
444+
business_logic_model_info = get_model_by_model_id(business_logic_model_id)
445+
business_logic_model_name = business_logic_model_info.get("display_name") if business_logic_model_info is not None else None
446+
432447
agent_info = ExportAndImportAgentInfo(agent_id=agent_id,
433448
name=agent_info["name"],
434449
display_name=agent_info["display_name"],
@@ -444,7 +459,11 @@ async def export_agent_by_agent_id(agent_id: int, tenant_id: str, user_id: str)
444459
"few_shots_prompt"),
445460
enabled=agent_info["enabled"],
446461
tools=tool_list,
447-
managed_agents=agent_relation_in_db)
462+
managed_agents=agent_relation_in_db,
463+
model_id=model_id,
464+
model_name=model_name,
465+
business_logic_model_id=business_logic_model_id,
466+
business_logic_model_name=business_logic_model_name)
448467
return agent_info
449468

450469

@@ -565,12 +584,70 @@ async def import_agent_by_agent_id(import_agent_info: ExportAndImportAgentInfo,
565584
if not import_agent_info.name.isidentifier():
566585
raise ValueError(
567586
f"Invalid agent name: {import_agent_info.name}. agent name must be a valid python variable name.")
587+
588+
# Get model_id from model_name if provided
589+
# Note: We use model_name (display_name) for cross-tenant compatibility
590+
# The exported model_id is kept for reference/debugging only
591+
model_id = None
592+
if import_agent_info.model_name:
593+
model_id = get_model_id_by_display_name(import_agent_info.model_name, tenant_id)
594+
if model_id is None:
595+
logger.warning(
596+
f"Model '{import_agent_info.model_name}' (exported model_id: {import_agent_info.model_id}) "
597+
f"not found in tenant {tenant_id}, falling back to quick config LLM model.")
598+
# Fallback to quick config LLM model
599+
quick_config_model = tenant_config_manager.get_model_config(
600+
key=MODEL_CONFIG_MAPPING["llm"],
601+
tenant_id=tenant_id
602+
)
603+
if quick_config_model:
604+
model_id = quick_config_model.get("model_id")
605+
logger.info(
606+
f"Using quick config LLM model: {quick_config_model.get('display_name')} "
607+
f"(model_id: {model_id})")
608+
else:
609+
logger.warning(f"No quick config LLM model found for tenant {tenant_id}")
610+
else:
611+
logger.info(
612+
f"Model '{import_agent_info.model_name}' found in tenant {tenant_id}, "
613+
f"mapped to model_id: {model_id} (exported model_id was: {import_agent_info.model_id})")
614+
615+
# Get business_logic_model_id from business_logic_model_name if provided
616+
business_logic_model_id = None
617+
if import_agent_info.business_logic_model_name:
618+
business_logic_model_id = get_model_id_by_display_name(import_agent_info.business_logic_model_name, tenant_id)
619+
if business_logic_model_id is None:
620+
logger.warning(
621+
f"Business logic model '{import_agent_info.business_logic_model_name}' "
622+
f"(exported model_id: {import_agent_info.business_logic_model_id}) "
623+
f"not found in tenant {tenant_id}, falling back to quick config LLM model.")
624+
# Fallback to quick config LLM model
625+
quick_config_model = tenant_config_manager.get_model_config(
626+
key=MODEL_CONFIG_MAPPING["llm"],
627+
tenant_id=tenant_id
628+
)
629+
if quick_config_model:
630+
business_logic_model_id = quick_config_model.get("model_id")
631+
logger.info(
632+
f"Using quick config LLM model for business logic: {quick_config_model.get('display_name')} "
633+
f"(model_id: {business_logic_model_id})")
634+
else:
635+
logger.warning(f"No quick config LLM model found for tenant {tenant_id}")
636+
else:
637+
logger.info(
638+
f"Business logic model '{import_agent_info.business_logic_model_name}' found in tenant {tenant_id}, "
639+
f"mapped to model_id: {business_logic_model_id} "
640+
f"(exported model_id was: {import_agent_info.business_logic_model_id})")
641+
568642
# create a new agent
569643
new_agent = create_agent(agent_info={"name": import_agent_info.name,
570644
"display_name": import_agent_info.display_name,
571645
"description": import_agent_info.description,
572646
"business_description": import_agent_info.business_description,
573-
"model_id": None,
647+
"model_id": model_id,
648+
"model_name": import_agent_info.model_name,
649+
"business_logic_model_id": business_logic_model_id,
650+
"business_logic_model_name": import_agent_info.business_logic_model_name,
574651
"max_steps": import_agent_info.max_steps,
575652
"provide_run_summary": import_agent_info.provide_run_summary,
576653
"duty_prompt": import_agent_info.duty_prompt,

0 commit comments

Comments
 (0)