diff --git a/api/app/core/memory/utils/prompt/prompt_utils.py b/api/app/core/memory/utils/prompt/prompt_utils.py index 50d31f2a..d88f50cf 100644 --- a/api/app/core/memory/utils/prompt/prompt_utils.py +++ b/api/app/core/memory/utils/prompt/prompt_utils.py @@ -400,7 +400,8 @@ async def render_user_summary_prompt( user_id: str, entities: str, statements: str, - language: str = "zh" + language: str = "zh", + user_display_name: str = None ) -> str: """ Renders the user summary prompt using the user_summary.jinja2 template. @@ -410,16 +411,22 @@ async def render_user_summary_prompt( entities: Core entities with frequency information statements: Representative statement samples language: The language to use for summary generation ("zh" for Chinese, "en" for English) + user_display_name: Display name for the user (e.g., other_name or "该用户"/"the user") Returns: Rendered prompt content as string """ + # 如果没有提供 user_display_name,使用默认值 + if user_display_name is None: + user_display_name = "该用户" if language == "zh" else "the user" + template = prompt_env.get_template("user_summary.jinja2") rendered_prompt = template.render( user_id=user_id, entities=entities, statements=statements, - language=language + language=language, + user_display_name=user_display_name ) # 记录渲染结果到提示日志 @@ -429,7 +436,8 @@ async def render_user_summary_prompt( 'user_id': user_id, 'entities_len': len(entities), 'statements_len': len(statements), - 'language': language + 'language': language, + 'user_display_name': user_display_name }) return rendered_prompt diff --git a/api/app/core/memory/utils/prompt/prompts/user_summary.jinja2 b/api/app/core/memory/utils/prompt/prompts/user_summary.jinja2 index 35619112..30b48719 100644 --- a/api/app/core/memory/utils/prompt/prompts/user_summary.jinja2 +++ b/api/app/core/memory/utils/prompt/prompts/user_summary.jinja2 @@ -14,8 +14,8 @@ Your task is to generate a comprehensive user profile based on the provided enti {% endif %} ===Inputs=== -{% if user_id %} -- User ID: {{ user_id }} +{% if user_display_name %} +- User Display Name: {{ user_display_name }} {% endif %} {% if entities %} - Core Entities & Frequency: {{ entities }} @@ -33,6 +33,20 @@ Your task is to generate a comprehensive user profile based on the provided enti 3. Avoid excessive adjectives and empty phrases 4. Strictly follow the output format specified below +{% if language == "zh" %} +**【严格人称规定】** +- 在描述用户时,必须使用"{{ user_display_name }}"作为人称 +- 绝对禁止使用用户ID(如 {{ user_id }})来称呼用户 +- 绝对禁止在摘要中出现任何形式的UUID或ID字符串 +- 如果需要指代用户,只能使用"{{ user_display_name }}"或相应的代词(他/她/TA) +{% else %} +**【STRICT PRONOUN RULES】** +- When describing the user, you MUST use "{{ user_display_name }}" as the reference +- It is ABSOLUTELY FORBIDDEN to use the user ID (such as {{ user_id }}) to refer to the user +- It is ABSOLUTELY FORBIDDEN to include any form of UUID or ID string in the summary +- If you need to refer to the user, you can ONLY use "{{ user_display_name }}" or appropriate pronouns (he/she/they) +{% endif %} + **Section-Specific Requirements:** {% if language == "zh" %} @@ -103,13 +117,13 @@ Your task is to generate a comprehensive user profile based on the provided enti {% if language == "zh" %} Example Input: -- User ID: user_12345 +- User Display Name: 张三 - Core Entities & Frequency: 产品经理 (15), AI (12), 深圳 (10), 数据分析 (8), 团队协作 (7) - Representative Statement Samples: 我在深圳从事产品经理工作已经5年了 | 我相信好的产品源于对用户需求的深刻理解 | 我喜欢在团队中起到协调作用 | 数据驱动决策是我的工作原则 Example Output: 【基本介绍】 -我是张三,一名充满热情的高级产品经理。在过去的5年里,我专注于AI和数据驱动的产品设计,致力于创造能够真正改善用户生活的产品。我相信好的产品源于对用户需求的深刻理解和对技术可能性的不断探索。 +张三是一名充满热情的高级产品经理,在深圳工作。在过去的5年里,张三专注于AI和数据驱动的产品设计,致力于创造能够真正改善用户生活的产品。张三相信好的产品源于对用户需求的深刻理解和对技术可能性的不断探索。 【性格特点】 性格开朗,善于沟通,注重细节。喜欢在团队中起到协调作用,帮助大家达成共识。面对挑战时保持乐观,相信每个问题都有解决方案。 @@ -121,13 +135,13 @@ Example Output: "让每一个产品决策都充满温度。" {% else %} Example Input: -- User ID: user_12345 +- User Display Name: John - Core Entities & Frequency: Product Manager (15), AI (12), San Francisco (10), Data Analysis (8), Team Collaboration (7) - Representative Statement Samples: I have been working as a product manager in San Francisco for 5 years | I believe good products come from deep understanding of user needs | I enjoy playing a coordinating role in teams | Data-driven decision making is my work principle Example Output: 【Basic Introduction】 -This is a passionate senior product manager based in San Francisco. Over the past 5 years, they have focused on AI and data-driven product design, dedicated to creating products that truly improve users' lives. They believe good products stem from deep understanding of user needs and continuous exploration of technological possibilities. +John is a passionate senior product manager based in San Francisco. Over the past 5 years, John has focused on AI and data-driven product design, dedicated to creating products that truly improve users' lives. John believes good products stem from deep understanding of user needs and continuous exploration of technological possibilities. 【Personality Traits】 Outgoing personality with excellent communication skills and attention to detail. Enjoys playing a coordinating role in teams, helping everyone reach consensus. Maintains optimism when facing challenges, believing every problem has a solution. diff --git a/api/app/services/user_memory_service.py b/api/app/services/user_memory_service.py index 80413c12..e34756b9 100644 --- a/api/app/services/user_memory_service.py +++ b/api/app/services/user_memory_service.py @@ -1163,11 +1163,32 @@ async def analytics_user_summary(end_user_id: Optional[str] = None, language: st """ from app.core.memory.utils.prompt.prompt_utils import render_user_summary_prompt from app.core.language_utils import validate_language + from app.repositories.end_user_repository import EndUserRepository + from app.db import get_db import re # 验证语言参数 language = validate_language(language) + # 获取用户的 other_name 字段 + user_display_name = "该用户" if language == "zh" else "the user" + if end_user_id: + try: + # 获取数据库会话并查询用户信息 + db = next(get_db()) + try: + repo = EndUserRepository(db) + end_user = repo.get_by_id(uuid.UUID(end_user_id)) + if end_user and end_user.other_name: + user_display_name = end_user.other_name + logger.info(f"使用 other_name 作为用户显示名称: {user_display_name}") + else: + logger.info(f"用户 {end_user_id} 的 other_name 为空,使用默认称呼: {user_display_name}") + finally: + db.close() + except Exception as e: + logger.warning(f"获取用户 other_name 失败,使用默认称呼: {str(e)}") + # 创建 UserSummaryHelper 实例 user_summary_tool = UserSummaryHelper(end_user_id or os.getenv("SELECTED_end_user_id", "group_123")) @@ -1184,7 +1205,8 @@ async def analytics_user_summary(end_user_id: Optional[str] = None, language: st user_id=user_summary_tool.user_id, entities=", ".join(entity_lines) if entity_lines else "(空)" if language == "zh" else "(empty)", statements=" | ".join(statement_samples) if statement_samples else "(空)" if language == "zh" else "(empty)", - language=language + language=language, + user_display_name=user_display_name ) messages = [