在跨裝置或長時間中斷的對話中,手動維護訊息列表容易丟失上下文。阿里雲百鍊提供相容 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條,超出時間或數量限制的內容將自動清理。