从指定记忆库中检索相关的画像记忆信息,可依据用户提问进行语义相似度检索,并根据查询条件划定查询范围。
collection.search_profile_memory(query=None, filter=None, limit=None, headers=None, timeout=None)
参数 | 类型 | 是否必须 | 描述 |
|---|---|---|---|
query | String | 否 | 用户的检索查询语句(最大长度 4000 字符)
|
filter | Object | 否 | 检索过滤条件和返回设置。 |
| String or Array of String | 否 | 用户 ID,支持单个 ID 或 ID 列表。 |
| String or Array of String | 否 | 助手 ID,支持单个 ID 或 ID 列表。 |
| String or Array of String | 否 | 用于过滤的画像主键表达式。可以是单个或列表,比如画像主键名为kb_id,想查询kb_id为1的数据,则 |
| String | 否 | 画像对应的记忆检索时间范围。日报用单日日期(如 "2025/12/01"),周报用起止区间(如 "2025/12/01~2025/12/07"),用于过滤并检索该周期内自动更新的画像。 |
| Integer | 否 | 检索记忆的起始时间,毫秒级时间戳。 |
| Integer | 否 | 检索记忆的终止时间,毫秒级时间戳。 |
| String or Array of String | 是 | 要检索的画像类型名称或它们的列表。 |
| String or Array of String | 否 | 用于过滤的群组ID。可以是单个ID或ID列表。 |
limit | Integer | 否 | 返回的检索结果条数,默认为10,取值范围[1, 5000]。 |
说明:
字段 | 类型 | 描述 |
|---|---|---|
code | Integer | 状态码,0 表示成功,其他表示错误。 |
message | String | 返回信息。 |
data | Object | 返回的详细检索结果。 |
| String | 被检索的记忆库名称。 |
| Integer | 返回的检索结果数量。 |
| Array of Object | 检索结果列表。 |
| String | 记忆条目(画像实例)的唯一ID。 |
| Float | 与查询的相关性得分。 |
| String | 记忆条目的类型(EventType 或 ProfileType)名称。 |
| Array of String | 关联的用户ID列表。 |
| Array of String | 关联的助手ID列表。 |
| String | 关联的群组ID。 |
| Integer | 记忆发生或最后更新的时间戳(毫秒)。 |
| String | 记忆条目的状态(预留字段)。 |
| String | 记忆条目的标签(预留字段)。 |
| Object | 额外的详细信息。其结构取决于memory_type。
|
| Integer | 本次检索消耗的token数量。 |
request_id | String | 标识每个请求的唯一ID。 |
import json import os import time from vikingdb import APIKey from vikingdb.memory import VikingMem def build_client() -> VikingMem: api_key = os.getenv("MEMORY_API_KEY") if not api_key: raise RuntimeError("Missing credentials: set MEMORY_API_KEY") auth = APIKey(api_key=api_key) return VikingMem( host="api-knowledgebase.mlp.cn-beijing.volces.com", region="cn-beijing", auth=auth, scheme="http", ) def main() -> None: client = build_client() collection = client.get_collection(collection_name="your_collection", project_name="default") now_ms = int(time.time() * 1000) user_id = f"sdk_example_user_{now_ms}" assistant_id = "sdk_example_assistant" profile_id = None try: created = collection.add_profile( profile_type="profile_v1", user_id=user_id, assistant_id=assistant_id, memory_info={"user_profile": "用户名字: 小明; 城市: 上海; 偏好: 手冲咖啡"}, ) profile_id = (created.get("data") or {}).get("profile_id") time.sleep(5) result = collection.search_profile_memory( query="小明", filter={"user_id": user_id, "assistant_id": assistant_id, "memory_type": ["profile_v1"]}, limit=10, ) print("search_profile_memory response:") print(json.dumps(result, indent=2, ensure_ascii=False)) print("user_id:", user_id) print("assistant_id:", assistant_id) finally: if profile_id: collection.delete_profile(profile_id=profile_id) if __name__ == "__main__": main()