文档

Message

更新时间:

本文详细阐述了Assistant API中Message类的各项功能,涵盖了消息的创建、列举、检索、修改等操作。

相关指南:关于Assistant API的快速使用方法,请参考快速入门

函数名

类型

create

创建message 类

retrieve

检索message类

modify

修改message类

list

列出message类

创建消息

HTTP

代码示例

curl --location 'https://dashscope.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
    "role": "user",
    "content": "你是谁",
    "metadata": {}
}'

输入参数

输入参数名字

输入参数的描述

参数类型

是否必须

thread id

message 所传入的thread id

str

content

message的具体信息

str

role

传入信息的角色。目前只支持role = “user"

str

metadata

其他相关信息

str

返回结果

{
    "id": "message_f1933671-19e1-4162-ad25-7326165123e1",
    "object": "thread.message",
    "created_at": 1711508433283,
    "thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
    "assistant_id": "",
    "run_id": "",
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": {
                "value": "你是谁",
                "annotations": []
            }
        }
    ],
    "metadata": {},
    "from": "",
    "name": "",
    "plugin_call": {},
    "tool_calls": [],
    "status": {},
    "request_id": "b3ad40b9-f052-9665-a064-dab11c34625f"
}

输出参数

输出message类,并包含除了用户输入以外参数的其他额外字段:

  • id:message id

  • request_id:请求id

SDK

代码示例

from dashscope import Messages


msg = Messages.create('the_thread_id',
                      content='The message content.',
                      role='user',
                      metadata={'key': 'value'})
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.threads.messages.TextMessageParam;
import com.alibaba.dashscope.threads.messages.ThreadMessage;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        // create a message to thread
        Messages messages = new Messages();
        ThreadMessage message = messages.create("threadId", TextMessageParam.builder().role("user").content("如何做出美味的牛肉炖土豆?").build());
    }
}

输入参数

参数

类型

默认值

说明

thread_id

str

-

Thread id

content

str

-

消息内容

role

str

'user'

Message的role,默认user

metadata

Dict

None

与该Message关联的key/value信息

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope API key,可以通过环境变量等方法设置。

返回结果

结果为Message对象,json化内容为:

{
    "account_id": "sk-6bddfc116de744c3aa1d66893cc87b20",
    "assistant_id": "",
    "content": [
        {
            "text": {
                "value": "sdhafjdasf"
            },
            "type": "text"
        }
    ],
    "created_at": 1711345341301,
    "display": true,
    "from": "",
    "gmt_crete": "2024-03-25 13:42:21",
    "gmt_update": "2024-03-25 13:42:21",
    "id": "message_05494921-a646-484e-85fc-76329acba842",
    "metadata": {
        "key": "value"
    },
    "name": "",
    "object": "thread.message",
    "plugin_call": {},
    "request_id": "631de0b3-7e50-9c9e-8444-0924d1b7e7a5",
    "role": "user",
    "run_id": "",
    "status": {},
    "status_code": 200,
    "thread_id": "thread_f1e7737e-b045-479f-99d1-510db49d535b",
    "tool_calls": []
}

输出参数

字段名

字段类型

字段描述

status_code

int

为调用http status code,200表示调用成功,其他表示调用出错

id

str

Message id,为uuid字符串

content

List[dict]

消息内容

content.type

str

content内容类型,如text

content.text

dict

content内容

content.text.value

str

content text value

metadata

Dict

和这个Message关联的key/value信息

tool_calls

Dcit

调用tool信息

plugin_call

Dict

调用plugin 信息

created_at

timestamp

assistant创建时间

gmt_created

datetime

2024-03-22 17:12:31

gmt_modified

datetime

2024-03-22 17:12:31

code

str

表示请求失败,表示错误码,成功忽略。

python only

message

str

失败,表示失败详细信息,成功忽略。

python only

消息列表

HTTP

代码示例

curl --location 'https://dashscope.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages?limit=2&order=desc' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

输入参数

输入参数名字

输入参数的描述

参数类型

是否必须

thread id

message 所传入的thread id

str

limit

创建message的数量

int

order

按照创建时间升序或降序

str

否(默认为“desc")

返回结果

{
    "object": "list",
    "data": [
        {
            "id": "message_f1933671-19e1-4162-ad25-7326165123e1",
            "object": "thread.message",
            "created_at": 1711508433283,
            "thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
            "assistant_id": "",
            "run_id": "",
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": {
                        "value": "你是谁",
                        "annotations": []
                    }
                }
            ],
            "metadata": {},
            "from": "",
            "name": "",
            "plugin_call": {},
            "tool_calls": [],
            "status": {}
        }
    ],
    "first_id": "message_f1933671-19e1-4162-ad25-7326165123e1",
    "last_id": "message_f1933671-19e1-4162-ad25-7326165123e1",
    "has_more": false,
    "request_id": "78f7d607-4a9a-90c6-8040-d3f81c84d60a"
}

输出参数

输出List message类,并包含除了用户输入以外参数的其他额外字段:

  • 多个message 组成的列表

SDK

代码示例

from dashscope import Messages


messages = Messages.list('thread_id',
                         limit=1,
                         order='desc',
                         after='',
                         before='')
import com.alibaba.dashscope.common.GeneralListParam;
import com.alibaba.dashscope.common.ListResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.threads.messages.ThreadMessage;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        // create a message to thread
        Messages messages = new Messages();
        GeneralListParam listThreadMessages = GeneralListParam.builder().build();
        ListResult<ThreadMessage> message = messages.list("threadId", listThreadMessages);
    }
}

输入参数

参数

类型

默认值

说明

thread_id

str

-

指定要查询的thread id

limit

str

-

order

after

before

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope API key,可以通过环境变量等方法设置。

输出参数

字段名

字段类型

字段描述

has_more

bool

last_id

str

first_id

str

data

list[Message]

Message对象列表

检索消息

HTTP

代码示例

curl --location 'https://dashscope.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages/message_ea26d29d-4509-490e-98e9-9f6238bd821b' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

输入参数

输入参数名字

输入参数的描述

参数类型

是否必须

thread id

待检索的message 所传入的thread id

str

message_id

待检索的message_id

str

返回结果

{
    "id": "message_ea26d29d-4509-490e-98e9-9f6238bd821b",
    "object": "thread.message",
    "created_at": 1711508622598,
    "thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
    "assistant_id": "",
    "run_id": "",
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": {
                "value": "你好",
                "annotations": []
            }
        }
    ],
    "metadata": {},
    "from": "",
    "name": "",
    "plugin_call": {},
    "tool_calls": [],
    "status": {},
    "request_id": "4d5ce962-91c3-9edb-87f7-00bbf985135e"
}

输出参数

输出检索到的message类,并包含除了用户输入以外参数的其他额外字段:

  • id:message_id

  • request_id:请求id

SDK

代码示例

from dashscope import Messages


message = Messages.retrieve('message_id',
                            thread_id='thread_id')
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.threads.messages.ThreadMessage;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        // create a message to thread
        Messages messages = new Messages();
        ThreadMessage message = messages.retrieve("threadId", "messageId");
    }
}

输入参数

参数

类型

默认值

说明

message_id

str

-

指定要查询的Message id

thread_id

str

-

指定要查询的Message所属Thread id

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope API key,可以通过环境变量等方法设置。

输出参数

参考create结果

修改消息

HTTP

代码示例

curl --location 'https://dashscope.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages/message_ea26d29d-4509-490e-98e9-9f6238bd821b' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
    "metadata": {
        "modified": "true",
        "user": "abc123"
    }
}'

输入参数

输入参数名字

输入参数的描述

参数类型

是否必须

thread_id

待修改的thread_id

str

message_id

待修改的message_id

str

metadata

元数据

dict

返回结果

{
    "id": "message_ea26d29d-4509-490e-98e9-9f6238bd821b",
    "object": "thread.message",
    "created_at": 1711508622598,
    "thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
    "assistant_id": "",
    "run_id": "",
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": {
                "value": "你好",
                "annotations": []
            }
        }
    ],
    "metadata": {
        "modified": "true",
        "user": "abc123"
    },
    "from": "",
    "name": "",
    "plugin_call": {},
    "tool_calls": [],
    "status": {},
    "request_id": "7877b011-cb94-9df1-9add-dc42b7d611f6"
}

输出参数

输出修改后的message类,并包含除了用户输入以外参数的其他额外字段:

  • id :message_id

  • request_id :请求id

SDK

代码示例

from dashscope import Messages


thread = Messages.update('message_id',
                    thread_id='the_message_thread_id',
                    metadata={'key': 'value'})
import java.util.Collections;
import com.alibaba.dashscope.common.UpdateMetadataParam;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.messages.Messages;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Messages messages = new Messages();
        UpdateMetadataParam updateMetadataParam = UpdateMetadataParam.builder().metadata(Collections.singletonMap("key", "value")).build();
        messages.update("thread_id", "message_Id", updateMetadataParam);
    }
}

输入参数

参数

类型

默认值

说明

message_id

str

-

指定要更新的Message id

thread_id

str

-

指定要更新的Message所属Thread id

metadata

Dict

None

Thread关联信息

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope api key,可以通过环境变量等方法设置。

输出参数

同create,参考create