使用用户画像

更新时间:
复制 MD 格式

通过 API 创建画像模板、提取画像并获取用户画像

用户画像用于从对话中提取结构化的用户属性(如年龄、职业、偏好),适用于需要持久化存储固定属性的场景。控制台配置方式参见配置记忆规则。

本页介绍通过 API 完成画像模板创建、对话提取和画像获取的完整流程。

前提条件

  • 已创建记忆库或使用默认记忆库
  • 配置 DASHSCOPE_API_KEY 环境变量
  • 将下文请求地址中的 {WorkspaceId} 替换为真实的业务空间 ID;使用 Python SDK 时设置 MEMORY_SERVICE_ENDPOINT,详见服务地址

完整流程

创建画像模板 → 添加对话并提取 → 获取用户画像。

# 1. 创建画像模板(CreateProfileSchema)
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/memory/profile_schemas \
  --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
    "name": "用户基础画像",
    "description": "包含年龄和兴趣的用户信息",
    "extract_scene": "efficient",
    "attributes": [
      {"name": "年龄", "description": "用户年龄"},
      {"name": "爱好", "description": "用户的兴趣爱好"},
      {"name": "职业", "description": "用户职业"}
    ]
  }'

# 2. 添加对话并提取画像(AddMemory,传入上面返回的 profile_schema_id)
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/memory/add \
  --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
    "user_id": "user_001",
    "messages": [
      {"role": "user", "content": "我今年28岁,是一名软件工程师。周末喜欢踢足球。"},
      {"role": "assistant", "content": "很高兴认识你!"}
    ],
    "profile_schema": "YOUR_SCHEMA_ID"
  }'

# 3. 获取用户画像(GetUserProfile,等待3秒后执行)
curl -X GET "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/memory/profile_schemas/{YOUR_SCHEMA_ID}/user_profile?user_id=user_001" \
  --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
  --header "Content-Type: application/json"
from agentscope_runtime.tools.modelstudio_memory import (
    CreateProfileSchema, GetUserProfile, AddMemory,
    ProfileAttribute, CreateProfileSchemaInput,
    GetUserProfileInput, AddMemoryInput, Message,
)
import asyncio

async def profile_example():
    create_schema = CreateProfileSchema()
    get_profile = GetUserProfile()
    add_memory = AddMemory()

    try:
        # 1. 创建画像模板
        schema_result = await create_schema.arun(CreateProfileSchemaInput(
            name="用户基础画像",
            description="包含年龄和兴趣的用户信息",
            attributes=[
                ProfileAttribute(name="年龄", description="用户年龄"),
                ProfileAttribute(name="爱好", description="用户的兴趣爱好"),
                ProfileAttribute(name="职业", description="用户职业"),
            ]
        ))
        schema_id = schema_result.profile_schema_id

        # 2. 添加对话并提取画像(必须传入 profile_schema,否则不会提取画像)
        await add_memory.arun(AddMemoryInput(
            user_id="user_001",
            profile_schema=schema_id,
            messages=[
                Message(role="user", content="我今年28岁,是一名软件工程师。周末喜欢踢足球。"),
                Message(role="assistant", content="很高兴认识你!"),
            ]
        ))

        await asyncio.sleep(3)  # 等待画像提取

        # 3. 获取用户画像
        profile = await get_profile.arun(GetUserProfileInput(
            schema_id=schema_id, user_id="user_001"
        ))
        for attr in profile.profile.attributes:
            print(f"{attr.name}: {attr.value or '未提取'}")

    finally:
        await create_schema.close()
        await get_profile.close()
        await add_memory.close()

asyncio.run(profile_example())

说明画像提取是异步过程。添加对话后,首次查询可能尚未返回属性值,请按业务重试策略再次查询。

重要画像配置完成后,继续管理记忆查看和检索已有记忆。