消息 (v1)
Legacy
在线程中创建消息。
相关指南:助手概述
创建消息 (v1)
POSThttps://api.openai.com/v1/threads/{thread_id}/messages
功能说明
创建一条消息。
路径参数
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| thread_id | string | 必需 | 要为其创建消息的 线程 的 ID。 | 
请求体
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| role | string | 必需 | 创建消息的实体的角色。允许的值包括: 
 | 
| content | string | 必需 | 消息的内容。 | 
| file_ids | array | 可选 | 消息应使用的 文件 ID 列表。每条消息最多可以附加 10 个文件。对于像 retrieval和code_interpreter这样可以访问和使用文件的工具很有用。 | 
| metadata | map | 可选 | 一组最多 16 个键值对,可以附加到对象上。这对于以结构化格式存储有关对象的附加信息很有用。键最大长度为 64 个字符,值最大长度为 512 个字符。 | 
返回值
一个 消息 对象。
示例请求
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=v1" \
  -d '{
      "role": "user",
      "content": "How does AI work? Explain it in simple terms."
    }'
示例响应
{
  "id": "msg_abc123",
  "object": "thread.message",
  "created_at": 1699017614,
  "thread_id": "thread_abc123",
  "role": "user",
  "content": [
    {
      "type": "text",
      "text": {
        "value": "How does AI work? Explain it in simple terms.",
        "annotations": []
      }
    }
  ],
  "file_ids": [],
  "assistant_id": null,
  "run_id": null,
  "metadata": {}
}
列出消息 (v1)
GEThttps://api.openai.com/v1/threads/{thread_id}/messages
功能说明
返回一个指定线程的消息列表。
路径参数
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| thread_id | string | 必需 | 消息所属的 线程 的 ID。 | 
查询参数
| 参数名称 | 类型 | 是否必需 | 默认值 | 描述 | 
|---|---|---|---|---|
| limit | integer | 可选 | 20 | 要返回的对象数量限制。限制范围是 1 到 100,默认值为 20。 | 
| order | string | 可选 | desc | 按 created_at时间戳对对象进行排序。可选值为asc(升序)和desc(降序)。 | 
| after | string | 可选 | - | 用于分页的游标。 after是一个对象 ID,定义了您在列表中的位置。例如,如果您发出一个列表请求并收到 100 个对象,以 obj_foo 结尾,则后续调用可以包含after=obj_foo以获取列表的下一页。 | 
| before | string | 可选 | - | 用于分页的游标。 before是一个对象 ID,定义了您在列表中的位置。例如,如果您发出一个列表请求并收到 100 个对象,以 obj_foo 结尾,则后续调用可以包含before=obj_foo以获取列表的上一页。 | 
| run_id | string | 可选 | - | 按生成它们的运行 ID 过滤消息。 | 
返回值
一个 消息 对象列表。
示例请求
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=v1"
示例响应
{
  "object": "list",
  "data": [
    {
      "id": "msg_abc123",
      "object": "thread.message",
      "created_at": 1699016383,
      "thread_id": "thread_abc123",
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "How does AI work? Explain it in simple terms.",
            "annotations": []
          }
        }
      ],
      "file_ids": [],
      "assistant_id": null,
      "run_id": null,
      "metadata": {}
    },
    {
      "id": "msg_abc456",
      "object": "thread.message",
      "created_at": 1699016383,
      "thread_id": "thread_abc123",
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "Hello, what is AI?",
            "annotations": []
          }
        }
      ],
      "file_ids": [
        "file-abc123"
      ],
      "assistant_id": null,
      "run_id": null,
      "metadata": {}
    }
  ],
  "first_id": "msg_abc123",
  "last_id": "msg_abc456",
  "has_more": false
}
列出消息文件 (v1)
GEThttps://api.openai.com/v1/threads/{thread_id}/messages/{message_id}/files
功能说明
返回一个消息文件列表。
路径参数
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| thread_id | string | 必需 | 消息和文件所属的线程的 ID。 | 
| message_id | string | 必需 | 文件所属的消息的 ID。 | 
查询参数
| 参数名称 | 类型 | 是否必需 | 默认值 | 描述 | 
|---|---|---|---|---|
| limit | integer | 可选 | 20 | 要返回的对象数量限制。限制范围是 1 到 100,默认值为 20。 | 
| order | string | 可选 | desc | 按 created_at时间戳对对象进行排序。可选值为asc(升序)和desc(降序)。 | 
| after | string | 可选 | - | 用于分页的游标。 after是一个对象 ID,定义了您在列表中的位置。例如,如果您发出一个列表请求并收到 100 个对象,以 obj_foo 结尾,则后续调用可以包含after=obj_foo以获取列表的下一页。 | 
| before | string | 可选 | - | 用于分页的游标。 before是一个对象 ID,定义了您在列表中的位置。例如,如果您发出一个列表请求并收到 100 个对象,以 obj_foo 结尾,则后续调用可以包含before=obj_foo以获取列表的上一页。 | 
返回值
一个 消息文件 对象列表。
示例请求
curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123/files \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "OpenAI-Beta: assistants=v1"
示例响应
{
  "object": "list",
  "data": [
    {
      "id": "file-abc123",
      "object": "thread.message.file",
      "created_at": 1699061776,
      "message_id": "msg_abc123"
    },
    {
      "id": "file-abc123",
      "object": "thread.message.file",
      "created_at": 1699061776,
      "message_id": "msg_abc123"
    }
  ],
  "first_id": "file-abc123",
  "last_id": "file-abc123",
  "has_more": false
}
获取消息 (v1)
GEThttps://api.openai.com/v1/threads/{thread_id}/messages/{message_id}
功能说明
获取一个指定 ID 的消息。
路径参数
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| thread_id | string | 必需 | 此消息所属的 线程 的 ID。 | 
| message_id | string | 必需 | 要获取的消息的 ID。 | 
返回值
与指定 ID 匹配的 消息 对象。
示例请求
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=v1"
示例响应
{
  "id": "msg_abc123",
  "object": "thread.message",
  "created_at": 1699017614,
  "thread_id": "thread_abc123",
  "role": "user",
  "content": [
    {
      "type": "text",
      "text": {
        "value": "How does AI work? Explain it in simple terms.",
        "annotations": []
      }
    }
  ],
  "file_ids": [],
  "assistant_id": null,
  "run_id": null,
  "metadata": {}
}
获取消息文件 (v1)
GEThttps://api.openai.com/v1/threads/{thread_id}/messages/{message_id}/files/{file_id}
功能说明
获取一个指定 ID 的消息文件。
路径参数
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| thread_id | string | 必需 | 消息和文件所属的线程的 ID。 | 
| message_id | string | 必需 | 文件所属的消息的 ID。 | 
| file_id | string | 必需 | 要获取的文件的 ID。 | 
返回值
与指定 ID 匹配的 消息文件 对象。
示例请求
curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123/files/file-abc123 \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "OpenAI-Beta: assistants=v1"
示例响应
{
  "id": "file-abc123",
  "object": "thread.message.file",
  "created_at": 1699061776,
  "message_id": "msg_abc123"
}
修改消息 (v1)
POST https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}功能说明
修改一条消息。
路径参数
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| thread_id | string | 必需 | 此消息所属的线程的 ID。 | 
| message_id | string | 必需 | 要修改的消息的 ID。 | 
请求体
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| metadata | map | 可选 | 一组最多 16 个键值对,可以附加到对象上。这对于以结构化格式存储有关对象的附加信息很有用。键最大长度为 64 个字符,值最大长度为 512 个字符。 | 
返回值
修改后的 消息 对象。
示例请求
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=v1" \
  -d '{
      "metadata": {
        "modified": "true",
        "user": "abc123"
      }
    }'
示例响应
{
  "id": "msg_abc123",
  "object": "thread.message",
  "created_at": 1699017614,
  "thread_id": "thread_abc123",
  "role": "user",
  "content": [
    {
      "type": "text",
      "text": {
        "value": "How does AI work? Explain it in simple terms.",
        "annotations": []
      }
    }
  ],
  "file_ids": [],
  "assistant_id": null,
  "run_id": null,
  "metadata": {
    "modified": "true",
    "user": "abc123"
  }
}
消息对象 (v1)
表示 线程中的一条消息。
| 参数名称 | 类型 | 描述 | 
|---|---|---|
| id | string | 可在 API 端点中引用的标识符。 | 
| object | string | 对象类型,始终为 thread.message。 | 
| created_at | integer | 创建消息的 Unix 时间戳(秒)。 | 
| thread_id | string | 此消息所属的 线程 ID。 | 
| status | string | 消息的状态,可以是 in_progress、incomplete或completed。 | 
| incomplete_details | object or null | 对于不完整的消息,关于消息不完整的原因的详细信息。 | 
| completed_at | integer or null | 完成消息的 Unix 时间戳(秒)。 | 
| incomplete_at | integer or null | 将消息标记为不完整的 Unix 时间戳(秒)。 | 
| role | string | 生成此消息的实体。可以是 user或assistant。 | 
| content | array | 消息的内容,由文本和/或图像组成。 | 
| assistant_id | string or null | 如果适用,撰写此消息的 助手 的 ID。 | 
| run_id | string or null | 与此消息的创建关联的 run ID。当使用创建消息或创建线程端点手动创建消息时,该值为 null。 | 
| file_ids | array | 助手应使用的 文件 ID 列表。每条消息最多可以附加 10 个文件。 | 
| metadata | map | 一组最多 16 个键值对,可以附加到对象上。这对于以结构化格式存储有关对象的附加信息很有用。键最大长度为 64 个字符,值最大长度为 512 个字符。 | 
示例:
{
  "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": []
      }
    }
  ],
  "file_ids": [],
  "assistant_id": "asst_abc123",
  "run_id": "run_abc123",
  "metadata": {}
}
消息文件对象 (v1)
附加到 message 的文件列表。
| 参数名称 | 类型 | 描述 | 
|---|---|---|
| id | string | 可在 API 端点中引用的标识符。 | 
| object | string | 对象类型,始终为 thread.message.file。 | 
| created_at | integer | 创建消息文件的 Unix 时间戳(秒)。 | 
| message_id | string | 此 文件 附加到的 消息 的 ID。 | 
示例:
{
  "id": "file-abc123",
  "object": "thread.message.file",
  "created_at": 1698107661,
  "message_id": "message_QLoItBbqwyAJEzlTy4y9kOMM"
}