消息
试用版

在话题中创建消息source

相关指南: 助手source

创建消息
试用版

发布 https://api.openai.com/v1/threads/{thread_id}/messages

创建消息。source

路径参数

要为其创建消息的线程的 ID。source

请求正文

创建消息的实体的角色。允许的值包括:source

  • user:表示消息是由实际用户发送的,在大多数情况下应该用于表示用户生成的消息。
  • assistant:表示消息由助手生成。使用此值可将来自助手的消息插入到对话中。

附加到邮件的文件列表,以及应将它们添加到的工具。source

一组 16 个可附加到对象的键值对。这对于以结构化格式存储有关对象的其他信息非常有用。键的长度最大为 64 个字符,值的长度最大为 512 个字符。source

返回

消息对象。source

示例请求
1
2
3
4
5
6
7
8
curl https://api.openai.com/v1/threads/thread_abc123/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v2" \
  -d '{
      "role": "user",
      "content": "How does AI work? Explain it in simple terms."
    }'
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "id": "msg_abc123",
  "object": "thread.message",
  "created_at": 1713226573,
  "assistant_id": null,
  "thread_id": "thread_abc123",
  "run_id": null,
  "role": "user",
  "content": [
    {
      "type": "text",
      "text": {
        "value": "How does AI work? Explain it in simple terms.",
        "annotations": []
      }
    }
  ],
  "attachments": [],
  "metadata": {}
}

列出消息
试用版

获取 https://api.openai.com/v1/threads/{thread_id}/messages

返回给定线程的消息列表。source

路径参数

消息所属线程的 ID。source

查询参数

要返回的对象数量限制。Limit 的范围可以介于 1 和 100 之间,默认值为 20。source

created_at对象的时间戳。asc对于升序和desc以降序排列。source

用于分页的游标。after是定义您在列表中的位置的对象 ID。例如,如果您发出列表请求并收到 100 个对象,以 obj_foo 结尾,则您的后续调用可以包含 after=obj_foo 以便获取列表的下一页。source

用于分页的游标。before是定义您在列表中的位置的对象 ID。例如,如果您发出一个列表请求并收到 100 个对象,以 obj_foo 开头,则您的后续调用可以包含 before=obj_foo 以便获取列表的上一页。source

按生成消息的运行 ID 筛选消息。source

返回

消息对象列表。source

示例请求
1
2
3
4
curl https://api.openai.com/v1/threads/thread_abc123/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v2"
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
  "object": "list",
  "data": [
    {
      "id": "msg_abc123",
      "object": "thread.message",
      "created_at": 1699016383,
      "assistant_id": null,
      "thread_id": "thread_abc123",
      "run_id": null,
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "How does AI work? Explain it in simple terms.",
            "annotations": []
          }
        }
      ],
      "attachments": [],
      "metadata": {}
    },
    {
      "id": "msg_abc456",
      "object": "thread.message",
      "created_at": 1699016383,
      "assistant_id": null,
      "thread_id": "thread_abc123",
      "run_id": null,
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "Hello, what is AI?",
            "annotations": []
          }
        }
      ],
      "attachments": [],
      "metadata": {}
    }
  ],
  "first_id": "msg_abc123",
  "last_id": "msg_abc456",
  "has_more": false
}

检索消息
试用版

获取 https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}

检索消息。source

路径参数

此消息所属的线程的 ID。source

要检索的消息的 ID。source

返回

与指定 ID 匹配的 message 对象。source

示例请求
1
2
3
4
curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v2"
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "id": "msg_abc123",
  "object": "thread.message",
  "created_at": 1699017614,
  "assistant_id": null,
  "thread_id": "thread_abc123",
  "run_id": null,
  "role": "user",
  "content": [
    {
      "type": "text",
      "text": {
        "value": "How does AI work? Explain it in simple terms.",
        "annotations": []
      }
    }
  ],
  "attachments": [],
  "metadata": {}
}

修改消息
试用版

POST https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}

修改消息。source

路径参数

此消息所属的线程的 ID。source

要修改的消息的 ID。source

请求正文

一组 16 个可附加到对象的键值对。这对于以结构化格式存储有关对象的其他信息非常有用。键的长度最大为 64 个字符,值的长度最大为 512 个字符。source

返回

修改后的 message 对象。source

示例请求
1
2
3
4
5
6
7
8
9
10
curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v2" \
  -d '{
      "metadata": {
        "modified": "true",
        "user": "abc123"
      }
    }'
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "id": "msg_abc123",
  "object": "thread.message",
  "created_at": 1699017614,
  "assistant_id": null,
  "thread_id": "thread_abc123",
  "run_id": null,
  "role": "user",
  "content": [
    {
      "type": "text",
      "text": {
        "value": "How does AI work? Explain it in simple terms.",
        "annotations": []
      }
    }
  ],
  "file_ids": [],
  "metadata": {
    "modified": "true",
    "user": "abc123"
  }
}

删除消息
试用版

删除 https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}

删除消息。source

路径参数

此消息所属的线程的 ID。source

要删除的消息的 ID。source

返回

删除状态source

示例请求
1
2
3
4
curl -X DELETE https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v2"
响应
1
2
3
4
5
{
  "id": "msg_abc123",
  "object": "thread.message.deleted",
  "deleted": true
}

message 对象
试用版

表示线程中的消息。source

标识符,可在 API 终端节点中引用。source

对象类型,始终为thread.message.source

创建消息时的 Unix 时间戳(以秒为单位)。source

此消息所属的线程 ID。source

消息的状态,可以是in_progress,incompletecompleted.source

在未完成的消息中,有关消息未完成原因的详细信息。source

消息完成时的 Unix 时间戳(以秒为单位)。source

消息标记为未完成时的 Unix 时间戳(以秒为单位)。source

生成消息的实体。其中之一userassistant.source

文本和/或图像数组中的消息内容。source

如果适用,则为创作此消息的助手的 ID。source

与创建此消息关联的运行的 ID。值为null使用 Create Message 或 Create Thread 端点手动创建消息时。source

附加到消息的文件列表,以及它们被添加到的工具。source

一组 16 个可附加到对象的键值对。这对于以结构化格式存储有关对象的其他信息非常有用。键的长度最大为 64 个字符,值的长度最大为 512 个字符。source

OBJECT 消息对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "id": "msg_abc123",
  "object": "thread.message",
  "created_at": 1698983503,
  "thread_id": "thread_abc123",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": {
        "value": "Hi! How can I help you today?",
        "annotations": []
      }
    }
  ],
  "assistant_id": "asst_abc123",
  "run_id": "run_abc123",
  "attachments": [],
  "metadata": {}
}