在跨设备或长时间中断的对话中,手动维护消息列表容易丢失上下文。阿里云百炼提供兼容 OpenAI 的 Conversations API。配合 Responses API,可自动注入历史上下文,无需手动同步消息,实现跨场景、跨设备的对话延续。
Create conversation
创建一个新会话,可同时添加初始消息项。
华北2(北京):POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/conversations
新加坡:POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/conversations
itemsarray(可选)初始消息项列表,最多20条。
属性 type string(必选)消息类型,仅支持 message。rolestring(必选)消息的角色。system 与developer 角色的指令优先级高于 user 角色,assistant 角色表示模型在之前交互中生成的消息。取值:user 、assistant 、system 、developer 。contentstring or array(必选)消息内容。支持纯文本字符串或结构化内容列表(如 ResponseInputText 对象数组),列表格式可包含文本等多种内容类型。object(可选)会话元数据,用于以结构化格式存储会话的附加信息。最多16对键值对,key最大长度64字符,value最大长度512字符。 | Python |
响应参数
created_atinteger会话创建的 Unix 时间戳(毫秒)。idstring会话唯一标识符。metadataobject会话元数据,以键值对形式存储的附加信息。最多16对,key最大长度64字符,value最大长度512字符。objectstring对象类型,固定为 conversation。 |
Retrieve conversation
获取指定会话的信息。
华北2(北京):GET https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}
新加坡:GET https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}
conversation_idstring(必选, Path)会话ID。 | Python |
响应参数
created_atinteger会话创建的 Unix 时间戳(毫秒)。idstring会话唯一标识符。metadataobject会话元数据,以键值对形式存储的附加信息。最多16对,key最大长度64字符,value最大长度512字符。objectstring对象类型,固定为 conversation。 |
Update conversation
更新会话的元数据信息。
华北2(北京):POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}
新加坡:POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}
conversation_idstring(必选, Path)会话ID。metadataobject(必选)会话元数据,会完全覆盖原有元数据。最多16对键值对,key最大长度64字符,value最大长度512字符。 | Python |
响应参数
created_atinteger会话创建的 Unix 时间戳(毫秒)。idstring会话唯一标识符。metadataobject会话元数据,以键值对形式存储的附加信息。最多16对,key最大长度64字符,value最大长度512字符。objectstring对象类型,固定为 conversation。 |
Delete conversation
删除指定会话。会话中的消息项不会被删除。
华北2(北京):DELETE https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}
新加坡:DELETE https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}
conversation_idstring(必选, Path)会话ID。 | Python |
响应参数
deletedboolean是否删除成功。idstring被删除的会话ID。objectstring对象类型,固定为 conversation.deleted。 |
Create Items
向指定会话添加消息项。
华北2(北京):POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}/items
新加坡:POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}/items
conversation_idstring(必选, Path)会话ID。itemsarray(必选)消息项列表,每次最多添加20条。
属性 type string(必选)消息类型,仅支持 message。rolestring(必选)消息的角色。system 、developer角色的指令优先级高于 user 角色,assistant 角色表示模型在之前交互中生成的消息。取值:user、assistant、system、developer。contentstring or array(必选)消息内容。支持纯文本字符串或结构化内容列表(如 ResponseInputText 对象数组),列表格式可包含文本等多种内容类型。 | Python |
响应参数
dataarray[object]创建的消息项列表。
属性 id string消息项唯一标识符。contentstring or array消息内容。纯文本字符串或结构化内容列表(如 ResponseInputText 对象数组)。rolestring消息的角色类型,取值:user、assistant、system、developer。statusstring消息的处理状态,取值:in_progress(处理中)、completed(已完成)、incomplete(未完成)。typestring消息项的类型,固定为 message。string列表中第一条消息项的ID。has_moreboolean是否还有更多数据。last_idstring列表中最后一条消息项的ID。 |
List Items
列出会话中的所有消息项。
华北2(北京):GET https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}/items
新加坡:GET https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}/items
conversation_idstring(必选, Path)会话ID。afterstring(可选)分页游标,返回指定消息ID之后的消息项。orderstring(可选)排序方式,asc(升序)或 desc(降序),默认 desc。limitinteger(可选)返回数量,范围1-100,默认20。 | Python |
响应参数
dataarray[object]消息项列表。
属性 id string消息项唯一标识符。contentstring or array消息内容。纯文本字符串或结构化内容列表(如 ResponseInputText 对象数组)。rolestring消息的角色类型,取值:user、assistant、system、developer。statusstring消息的处理状态,取值:in_progress(处理中)、completed(已完成)、incomplete(未完成)。typestring消息项的类型,固定为 message。string列表中第一条消息项的ID。has_moreboolean是否还有更多数据。last_idstring列表中最后一条消息项的ID。objectstring对象类型,固定为 list。 |
Retrieve Item
获取指定消息项的详情。
华北2(北京):GET https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}/items/{item_id}
新加坡:GET https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}/items/{item_id}
conversation_idstring(必选, Path)会话ID。item_idstring(必选, Path)消息项ID。 | Python |
响应参数
contentarray[object]消息内容列表,包含一个或多个内容对象。
属性 type string内容类型,如 input_text(用户输入文本)或 output_text(模型输出文本)。textstring文本内容。string消息项唯一标识符。rolestring消息的角色类型,取值:user、assistant、system、developer。statusstring消息的处理状态,取值:in_progress(处理中)、completed(已完成)、incomplete(未完成)。typestring消息项的类型,固定为 message。 |
Delete Item
删除指定的消息项。
华北2(北京):DELETE https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}/items/{item_id}
新加坡:DELETE https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/conversations/{conversation_id}/items/{item_id}
conversation_idstring(必选, Path)会话ID。item_idstring(必选, Path)消息项ID。 | Python |
响应参数
deletedboolean是否删除成功。idstring被删除的消息项ID。objectstring对象类型,固定为 conversation.item.deleted。 |
Response API 使用 conversation 示例
通过 Responses API 的 conversation 参数,可以实现多轮对话的上下文保持。
请勿同时传入previous_response_id和conversation,否则会报错:[400] INVALID_REQUEST: Mutually exclusive parameters: Ensure you are only providing one of: previous_response_id or conversation.
Python
使用限制
- 创建会话或添加消息项时,
items最多包含20条。 metadata最多16对键值对,key最大长度64字符,value最大长度512字符。- 会话信息保留最近7天内的最新100条,超出时间或数量限制的内容将自动清理。