资讯 文档
技术能力
语音技术
文字识别
人脸与人体
图像技术
语言与知识
视频技术

写入用户记忆内容

功能介绍

本接口用于在用户记忆中写入多条记忆内容,即更新一个appId 下某userMemoryId 的记忆内容。支持以下:

  • 全量写入/更新用户记忆内容,新存入的用户记忆信息会覆盖用户过往所有的历史记忆信息。
  • 写入用户记忆内容。

使用说明

本文API支持通过Python SDK、Go SDK、Java SDK 和 Node.js SDK调用,调用流程请参考SDK安装及使用流程

SDK调用

调用示例

import os
from qianfan  import resources

# 通过环境变量初始化认证信息
# 使用安全认证AK/SK调用,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"



resp = resources.console.utils.call_action(
    # 调用本文API,该参数值为固定值,无需修改;对应API调用文档-请求结构-请求地址的后缀
    "/v2/memory", 
    # 调用本文API,该参数值为固定值,无需修改;对应API调用文档-请求参数-Query参数的Action 
    "ModifyUserMemory", 
    # 请查看本文请求参数说明,根据实际使用选择参数;对应API调用文档-请求参数-Body参数
    {
        "appId":"112xxx706", 
        "userMemoryId": "um-58a8xxxxa5",
        "operator":"cover",
        "memories":[
            [
                {   "role": "user",
                    "content": "你好,我叫小A,我的口味偏清淡,喜欢喝绿茶,很注重饮食健康。"
                },
                {
                    "role": "system",
                    "content": "好的小A,我记住你的口味清淡,喜欢健康饮食,而且喜欢喝绿茶"
                }
             ],
             [
                {
                    "role": "user",
                    "content": "记住我的点单偏好:不另外加糖、少冰、不加小料。"
                },
                {
                    "role": "system",
                    "content": "好的小A,我已经记住了你的偏好。"
                }
             ]
        ]
    }
)

print(resp.body)
package main

import (
	"context"
	"fmt"
    "os"

	"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
)

func main() {
	 // 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
	os.Setenv("QIANFAN_ACCESS_KEY", "your_iam_ak")
	os.Setenv("QIANFAN_SECRET_KEY", "your_iam_sk")
    
	ca := qianfan.NewConsoleAction()
    
	res, err := ca.Call(context.TODO(),
    // 调用本文API,该参数值为固定值,无需修改;对应API调用文档-请求结构-请求地址的后缀
    "/v2/memory",
    // 调用本文API,该参数值为固定值,无需修改;对应API调用文档-请求参数-Query参数的Action 
    "ModifyUserMemory",
    // 请查看本文请求参数说明,根据实际使用选择参数;对应API调用文档-请求参数-Body参数
    map[string]interface{}{
		"appId":"112xxx706", 
        "userMemoryId": "um-58a8xxxxa5",
        "operator":"cover",
        "memories":[][]map[string]any{
            {
                {   "role": "user",
                    "content": "你好,我叫小A,我的口味偏清淡,喜欢喝绿茶,很注重饮食健康。",
                },
                {
                    "role": "system",
                    "content": "好的小A,我记住你的口味清淡,喜欢健康饮食,而且喜欢喝绿茶",
                },
             },
             {
                {
                    "role": "user",
                    "content": "记住我的点单偏好:不另外加糖、少冰、不加小料。",
                },
                {
                    "role": "system",
                    "content": "好的小A,我已经记住了你的偏好。",
                },
             },
        },
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(string(res.Body))
	
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.console.ConsoleResponse;
import com.baidubce.qianfan.util.CollUtils;
import com.baidubce.qianfan.util.Json;
import java.util.Map;

public class Dome {
    public static void main(String args[]){
        // 使用安全认证AK/SK鉴权,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
        Qianfan qianfan = new Qianfan("your_iam_ak", "your_iam_sk");
        
        ConsoleResponse<String> response = qianfan.console()
                // 调用本文API,该参数值为固定值,无需修改;对应API调用文档-请求结构-请求地址的后缀
                .route("/v2/memory")
                // 调用本文API,该参数值为固定值,无需修改;对应API调用文档-请求参数-Query参数的Action 
                .action("ModifyUserMemory")
                // 需要传入参数的场景,可以自行封装请求类,或者使用Map.of()来构建请求Body
                // Java 8可以使用SDK提供的CollUtils.mapOf()来替代Map.of()
                // 请查看本文请求参数说明,根据实际使用选择参数;对应API调用文档-请求参数-Body参数
                .body(CollUtils.mapOf(
                    "appId","112xxx706", 
                    "userMemoryId", "um-58a8xxxxa5",
                    "operator","cover",
                    "memories",new Map[][]{
                        {
                            CollUtils.mapOf(   
                                "role", "user",
                                "content", "你好,我叫小A,我的口味偏清淡,喜欢喝绿茶,很注重饮食健康。"
                            ),
                            CollUtils.mapOf(
                                "role", "system",
                                "content", "好的小A,我记住你的口味清淡,喜欢健康饮食,而且喜欢喝绿茶"
                            )
                         },
                         {
                            CollUtils.mapOf(
                                "role", "user",
                                "content", "记住我的点单偏好:不另外加糖、少冰、不加小料。"
                            ),
                            CollUtils.mapOf(
                                "role", "system",
                                "content", "好的小A,我已经记住了你的偏好。"
                            )
                         }
                    }
                ))
                .execute(String.class);

        System.out.println(response.getResult());
    }
}
import {consoleAction, setEnvVariable} from "@baiducloud/qianfan";

// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
setEnvVariable('QIANFAN_ACCESS_KEY','your_iam_ak');
setEnvVariable('QIANFAN_SECRET_KEY','your_iam_sk');

async function main() {
  //base_api_route:调用本文API,该参数值为固定值,无需修改;对应API调用文档-请求结构-请求地址的后缀
  //action:调用本文API,该参数值为固定值,无需修改;对应API调用文档-请求参数-Query参数的Action 
  //data:请查看本文请求参数说明,根据实际使用选择参数;对应API调用文档-请求参数-Body参数
  const res = await consoleAction({base_api_route: '/v2/memory', action: 'ModifyUserMemory', data: {
        "appId":"112xxx706", 
        "userMemoryId": "um-58a8xxxxa5",
        "operator":"cover",
        "memories":[
            [
                {   "role": "user",
                    "content": "你好,我叫小A,我的口味偏清淡,喜欢喝绿茶,很注重饮食健康。"
                },
                {
                    "role": "system",
                    "content": "好的小A,我记住你的口味清淡,喜欢健康饮食,而且喜欢喝绿茶"
                }
             ],
             [
                {
                    "role": "user",
                    "content": "记住我的点单偏好:不另外加糖、少冰、不加小料。"
                },
                {
                    "role": "system",
                    "content": "好的小A,我已经记住了你的偏好。"
                }
             ]
        ]
    }
  });    
    
  console.log(res);
}

main();

返回示例

{
    "requestId":"1bef3f87-c5b2-4419-936b-50f9884f10d4",
    "result": {
        "userMemoryId" : "um-9axxxe58"
      }
}
{
    "requestId":"1bef3f87-c5b2-4419-936b-50f9884f10d4",
    "result": {
        "userMemoryId" : "um-9axxxe58"
      }
}
{
    "requestId":"1bef3f87-c5b2-4419-936b-50f9884f10d4",
    "result": {
        "userMemoryId" : "um-9axxxe58"
      }
}
{
    requestId:'1bef3f87-c5b2-4419-936b-50f9884f10d4',
    result: {
        userMemoryId : 'um-9axxxe58'
      }
}

请求参数

名称 类型 必填 描述
appId string 千帆应用AppId,说明:
(1)在控制台-系统管理与统计-应用接入页面查看,如下图所示:
image.png
(2)如果应用列表无应用,请先创建应用,请查看创建应用操作步骤
operator string 操作,可选值:
· cover:全量覆盖,默认值为cover
· insert :插入一条记忆问答组
userMemoryId string 用户人设标识id
memories list[][object] 记忆数据的内容,说明:
(1)列表长度必须大于0,小于等于1000
(2)以数组为元素的列表,每个数组有两个Object,代表一轮对话

memories说明

两个角色一问一答为一组

名称 类型 描述
role string 角色,说明:
· user:用户问题
· system:系统回答
content string 问答内容

返回参数

名称 类型 描述
requestId string 请求ID
code string 错误码,错误时返回
message string 错误信息,请求失败时返回
result bool 请求结果,请求成功时返回
上一篇
创建用户记忆id
下一篇
查询用户记忆内容