Skip to content

Commit 69082a1

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

File tree

2 files changed

+361
-58
lines changed

2 files changed

+361
-58
lines changed

backend/services/agent_service.py

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,57 @@ def _resolve_user_tenant_language(
8383
return user_id, tenant_id, get_user_language(http_request)
8484

8585

86+
def _resolve_model_with_fallback(
87+
model_display_name: str | None,
88+
exported_model_id: str | None,
89+
model_label: str,
90+
tenant_id: str
91+
) -> str | None:
92+
"""
93+
Resolve model_id from model_display_name with fallback to quick config LLM model.
94+
95+
Args:
96+
model_display_name: Display name of the model to lookup
97+
exported_model_id: Original model_id from export (for logging only)
98+
model_label: Label for logging (e.g., "Model", "Business logic model")
99+
tenant_id: Tenant ID for model lookup
100+
101+
Returns:
102+
Resolved model_id or None if not found and no fallback available
103+
"""
104+
if not model_display_name:
105+
return None
106+
107+
# Try to find model by display name in current tenant
108+
resolved_id = get_model_id_by_display_name(model_display_name, tenant_id)
109+
110+
if resolved_id:
111+
logger.info(
112+
f"{model_label} '{model_display_name}' found in tenant {tenant_id}, "
113+
f"mapped to model_id: {resolved_id} (exported model_id was: {exported_model_id})")
114+
return resolved_id
115+
116+
# Model not found, try fallback to quick config LLM model
117+
logger.warning(
118+
f"{model_label} '{model_display_name}' (exported model_id: {exported_model_id}) "
119+
f"not found in tenant {tenant_id}, falling back to quick config LLM model.")
120+
121+
quick_config_model = tenant_config_manager.get_model_config(
122+
key=MODEL_CONFIG_MAPPING["llm"],
123+
tenant_id=tenant_id
124+
)
125+
126+
if quick_config_model:
127+
fallback_id = quick_config_model.get("model_id")
128+
logger.info(
129+
f"Using quick config LLM model for {model_label.lower()}: "
130+
f"{quick_config_model.get('display_name')} (model_id: {fallback_id})")
131+
return fallback_id
132+
133+
logger.warning(f"No quick config LLM model found for tenant {tenant_id}")
134+
return None
135+
136+
86137
async def _stream_agent_chunks(
87138
agent_request: "AgentRequest",
88139
user_id: str,
@@ -432,17 +483,17 @@ async def export_agent_by_agent_id(agent_id: int, tenant_id: str, user_id: str)
432483

433484
# Get model_id and model display name from agent_info
434485
model_id = agent_info.get("model_id")
435-
model_name = None
486+
model_display_name = None
436487
if model_id is not None:
437488
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
489+
model_display_name = model_info.get("display_name") if model_info is not None else None
439490

440491
# Get business_logic_model_id and business logic model display name
441492
business_logic_model_id = agent_info.get("business_logic_model_id")
442-
business_logic_model_name = None
493+
business_logic_model_display_name = None
443494
if business_logic_model_id is not None:
444495
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
496+
business_logic_model_display_name = business_logic_model_info.get("display_name") if business_logic_model_info is not None else None
446497

447498
agent_info = ExportAndImportAgentInfo(agent_id=agent_id,
448499
name=agent_info["name"],
@@ -461,9 +512,9 @@ async def export_agent_by_agent_id(agent_id: int, tenant_id: str, user_id: str)
461512
tools=tool_list,
462513
managed_agents=agent_relation_in_db,
463514
model_id=model_id,
464-
model_name=model_name,
515+
model_name=model_display_name,
465516
business_logic_model_id=business_logic_model_id,
466-
business_logic_model_name=business_logic_model_name)
517+
business_logic_model_name=business_logic_model_display_name)
467518
return agent_info
468519

469520

@@ -585,59 +636,22 @@ async def import_agent_by_agent_id(import_agent_info: ExportAndImportAgentInfo,
585636
raise ValueError(
586637
f"Invalid agent name: {import_agent_info.name}. agent name must be a valid python variable name.")
587638

588-
# Get model_id from model_name if provided
589-
# Note: We use model_name (display_name) for cross-tenant compatibility
639+
# Resolve model IDs with fallback
640+
# Note: We use model_display_name for cross-tenant compatibility
590641
# 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})")
642+
model_id = _resolve_model_with_fallback(
643+
model_display_name=import_agent_info.model_name,
644+
exported_model_id=import_agent_info.model_id,
645+
model_label="Model",
646+
tenant_id=tenant_id
647+
)
648+
649+
business_logic_model_id = _resolve_model_with_fallback(
650+
model_display_name=import_agent_info.business_logic_model_name,
651+
exported_model_id=import_agent_info.business_logic_model_id,
652+
model_label="Business logic model",
653+
tenant_id=tenant_id
654+
)
641655

642656
# create a new agent
643657
new_agent = create_agent(agent_info={"name": import_agent_info.name,

0 commit comments

Comments
 (0)