Feature/memory redis (#152)

* [feature]Emotional memory cache

* [feature]Implicit memory cache

* [changes]Modify the expiration time of implicit memory to 24 hours.

* [feature]Emotional memory cache

* [feature]Implicit memory cache

* [changes]Modify the expiration time of implicit memory to 24 hours.

* [changes]Modify the code based on the AI review

* [feature]Emotional memory cache

* [feature]Implicit memory cache

* [changes]Modify the expiration time of implicit memory to 24 hours.

* [feature]Implicit memory cache

* [changes]Modify the code based on the AI review

* [changes]Modify the generated emotion cache to be "end_user_id"

* [feature]Emotional memory cache

* [feature]Implicit memory cache

* [changes]Modify the code based on the AI review

* [feature]Emotional memory cache

* [changes]Modify the code based on the AI review

* [changes]Modify the generated emotion cache to be "end_user_id"
This commit is contained in:
乐力齐
2026-01-19 17:56:52 +08:00
committed by GitHub
parent 9d25b08641
commit 12a27dbcf7
2 changed files with 7 additions and 33 deletions

View File

@@ -267,7 +267,7 @@ async def generate_emotion_suggestions(
"""生成个性化情绪建议调用LLM并缓存 """生成个性化情绪建议调用LLM并缓存
Args: Args:
request: 包含 group_id、可选的 config_id 和 force_refresh request: 包含 end_user_id
db: 数据库会话 db: 数据库会话
current_user: 当前用户 current_user: 当前用户
@@ -275,47 +275,22 @@ async def generate_emotion_suggestions(
新生成的个性化情绪建议响应 新生成的个性化情绪建议响应
""" """
try: try:
# 验证 config_id如果提供
# 获取终端用户关联的配置
config_id = request.config_id
if config_id is None:
# 如果没有提供 config_id尝试获取用户关联的配置
try:
from app.services.memory_agent_service import (
get_end_user_connected_config,
)
connected_config = get_end_user_connected_config(request.group_id, db)
config_id = connected_config.get("memory_config_id")
except ValueError as e:
return fail(BizCode.INVALID_PARAMETER, "无法获取用户关联的配置", str(e))
else:
# 如果提供了 config_id验证其有效性
from app.services.memory_config_service import MemoryConfigService
try:
config_service = MemoryConfigService(db)
config = config_service.get_config_by_id(config_id)
if not config:
return fail(BizCode.INVALID_PARAMETER, "配置ID无效", f"配置 {config_id} 不存在")
except Exception as e:
return fail(BizCode.INVALID_PARAMETER, "配置ID验证失败", str(e))
api_logger.info( api_logger.info(
f"用户 {current_user.username} 请求生成个性化情绪建议", f"用户 {current_user.username} 请求生成个性化情绪建议",
extra={ extra={
"group_id": request.group_id, "end_user_id": request.end_user_id
"config_id": config_id
} }
) )
# 调用服务层生成建议 # 调用服务层生成建议
data = await emotion_service.generate_emotion_suggestions( data = await emotion_service.generate_emotion_suggestions(
end_user_id=request.group_id, end_user_id=request.end_user_id,
db=db db=db
) )
# 保存到缓存 # 保存到缓存
await emotion_service.save_suggestions_cache( await emotion_service.save_suggestions_cache(
end_user_id=request.group_id, end_user_id=request.end_user_id,
suggestions_data=data, suggestions_data=data,
db=db, db=db,
expires_hours=24 expires_hours=24
@@ -324,7 +299,7 @@ async def generate_emotion_suggestions(
api_logger.info( api_logger.info(
"个性化建议生成成功", "个性化建议生成成功",
extra={ extra={
"group_id": request.group_id, "end_user_id": request.end_user_id,
"suggestions_count": len(data.get("suggestions", [])) "suggestions_count": len(data.get("suggestions", []))
} }
) )
@@ -334,7 +309,7 @@ async def generate_emotion_suggestions(
except Exception as e: except Exception as e:
api_logger.error( api_logger.error(
f"生成个性化建议失败: {str(e)}", f"生成个性化建议失败: {str(e)}",
extra={"group_id": request.group_id}, extra={"end_user_id": request.end_user_id},
exc_info=True exc_info=True
) )
raise HTTPException( raise HTTPException(

View File

@@ -34,5 +34,4 @@ class EmotionSuggestionsRequest(BaseModel):
class EmotionGenerateSuggestionsRequest(BaseModel): class EmotionGenerateSuggestionsRequest(BaseModel):
"""生成个性化情绪建议请求""" """生成个性化情绪建议请求"""
group_id: str = Field(..., description="ID") end_user_id: str = Field(..., description="终端用户ID")
config_id: Optional[int] = Field(None, description="配置ID用于指定LLM模型")