refactor: streamline data serialization in tool implementations

- Replaced model_dump and to_dict methods with direct calls to dict for improved consistency and performance in JSON serialization across multiple tools.
- Updated ConversationMemoryManager, GetRecommendationsTool, QueryDownloadsTool, and QueryMediaLibraryTool to enhance data handling.
This commit is contained in:
jxxghp
2025-10-31 11:36:50 +08:00
parent 055117d83d
commit 3971c145df
5 changed files with 5 additions and 5 deletions

View File

@@ -235,7 +235,7 @@ class ConversationMemoryManager:
# 保存到Redis设置TTL自动过期
if settings.CACHE_BACKEND_TYPE == "redis":
try:
memory_dict = memory.model_dump()
memory_dict = memory.dict()
redis_key = f"agent_memory:{memory.user_id}:{memory.session_id}" if memory.user_id else f"agent_memory:{memory.session_id}"
ttl = int(timedelta(days=settings.LLM_REDIS_MEMORY_RETENTION_DAYS).total_seconds())
await self.redis_helper.set(

View File

@@ -33,7 +33,7 @@ class GetRecommendationsTool(MoviePilotTool):
if results:
# 使用 to_dict() 方法
return json.dumps([r.to_dict() if hasattr(r, 'to_dict') else r.dict() if hasattr(r, 'dict') else r.model_dump() if hasattr(r, 'model_dump') else r for r in results], ensure_ascii=False, indent=2)
return json.dumps(results)
return "未找到推荐内容。"
except Exception as e:
logger.error(f"获取推荐失败: {e}", exc_info=True)

View File

@@ -27,7 +27,7 @@ class QueryDownloadsTool(MoviePilotTool):
continue
filtered_downloads.append(dl)
if filtered_downloads:
return json.dumps([d.dict() if hasattr(d, 'dict') else d.model_dump() if hasattr(d, 'model_dump') else d for d in filtered_downloads], ensure_ascii=False, indent=2)
return json.dumps([d.dict() for d in filtered_downloads])
return "未找到相关下载任务。"
except Exception as e:
logger.error(f"查询下载失败: {e}", exc_info=True)

View File

@@ -26,7 +26,7 @@ class QueryMediaLibraryTool(MoviePilotTool):
continue
filtered_medias.append(media)
if filtered_medias:
return json.dumps([m.to_dict() if hasattr(m, 'to_dict') else m.dict() if hasattr(m, 'dict') else m.model_dump() if hasattr(m, 'model_dump') else m for m in filtered_medias], ensure_ascii=False, indent=2)
return json.dumps([m.to_dict() for m in filtered_medias])
return "媒体库中未找到相关媒体。"
except Exception as e:
logger.error(f"查询媒体库失败: {e}", exc_info=True)

View File

@@ -420,7 +420,7 @@ class ConfigModel(BaseModel):
# LLM温度参数
LLM_TEMPERATURE: float = 0.7
# LLM最大迭代次数
LLM_MAX_ITERATIONS: int = 5
LLM_MAX_ITERATIONS: int = 15
# LLM工具调用超时时间
LLM_TOOL_TIMEOUT: int = 300
# 是否启用详细日志