全部產品
Search
文件中心

Alibaba Cloud Model Studio:Threads

更新時間:Dec 16, 2025

本文詳細闡述了Assistant API中Thread類的各項功能,涵蓋了線程的建立、檢索、修改以及刪除操作。

功能介紹:如果您想瞭解 Assistant API 的功能和基本用法,請參考Assistant API功能概覽
有效期間限:所有 Thread 執行個體均儲存在阿里雲百鍊伺服器上,目前沒有失效日期,您可通過 thread.id 檢索上下文資訊。

函數名

類型

create

建立 Thread 類

retrieve

檢索 Thread 類

update

修改 Thread 類

delete

刪除 Thread 類

建立線程

HTTP

程式碼範例

curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
    "messages": [
        {
            "role": "user",
            "content": "hello"
        }
    ]
}'

輸入參數

輸入參數名字

輸入參數的描述

參數類型

是否必須

messages

thread 所傳入的message

Message 類

metadata

thread使用的名字

str

返回結果

{
    "id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
    "object": "thread",
    "created_at": 1711448377850,
    "metadata": {},
    "request_id": "dd9489ec-dbdb-95d4-9ff8-cfe29b61db27"
}

輸出參數

輸出thread類,並包含除了使用者輸入以外參數的其他額外欄位:

  • id :thread id

  • request_id :請求id

SDK

程式碼範例

import json
from dashscope import Threads
import dashscope
import os

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

thread = Threads.create(
    # 建議您優先配置環境變數。若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    messages=[{"role": "user", "content": "How does AI work? Explain it in simple terms."}]
)
print(json.dumps(thread, default=lambda o: o.__dict__, sort_keys=True, indent=4))
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.AssistantThread;
import com.alibaba.dashscope.threads.ThreadParam;
import com.alibaba.dashscope.threads.Threads;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Threads threads = new Threads();
        // 建議您優先配置環境變數。若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey("sk-xxx")
        AssistantThread assistantThread = threads.create(ThreadParam.builder()
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build());
    }
}

輸入參數

參數

類型

預設值

說明

messages

List[Dict]

None

Thread初始messages

metadata

Dict

None

與該Thread關聯的key/value資訊

workspace

str

None

阿里雲百鍊的Workspace ID,僅當 api_key 為子業務空間 API Key 時才需要傳入。

api_key

str

None

阿里雲百鍊的API Key,建議您配置API Key到環境變數(準備下線,併入配置 API Key)

返回結果

結果為Thread對象,json化內容為:

{
    "account_id": "sk-6bddfc116de744c3aa1d66893cc87b20",
    "created_at": 1711338305031,
    "gmt_create": "2024-03-25 11:45:05",
    "gmt_update": "2024-03-25 11:45:05",
    "id": "thread_97934051-2c15-44bf-97de-310039d873f9",
    "is_deleted": false,
    "metadata": {},
    "object": "thread",
    "request_id": "982d4b9a-b982-9d53-9c79-a75b32f7168a",
    "status_code": 200
}

輸出參數

欄位名

欄位類型

欄位描述

status_code

int

為調用http status code,200表示調用成功,其他表示調用出錯

id

str

thread id,為uuid字串

metadata

Dict

和這個Thread關聯的key/value資訊

created_at

timestamp

thread 建立時間

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-intl.aliyuncs.com/api/v1/threads/thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

輸入參數

輸入參數名字

輸入參數的描述

參數類型

是否必須

thread_id

待檢索的thread_id

str

返回結果

{
    "id": "thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7",
    "object": "thread",
    "created_at": 1711507920700,
    "metadata": {},
    "request_id": "4d4e73ad-15fb-96ac-9262-0643a0fdb5ca"
}

輸出參數

輸出檢索到的thread類,並包含除了使用者輸入以外參數的其他額外欄位:

  • id :thread_id

  • request_id :請求id

SDK

程式碼範例

from dashscope import Threads
import dashscope
import os

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

thread = Threads.retrieve(
    'thread_id',
    # 建議您優先配置環境變數。若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY")
)
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.AssistantThread;
import com.alibaba.dashscope.threads.Threads;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Threads threads = new Threads();
        // 直接傳入 thread_id 和 apiKey
        // 建議您優先配置環境變數。若沒有配置環境變數,請用百鍊API Key將下行替換為:"sk-xxx"
        AssistantThread assistantThread = threads.retrieve(
            "thread_id",
            System.getenv("DASHSCOPE_API_KEY")
        );
    }
}

輸入參數

參數

類型

預設值

說明

thread_id

str

-

指定要查詢的thread id

workspace

str

None

阿里雲百鍊的Workspace ID,僅當 api_key 為子業務空間 API Key 時才需要傳入。

api_key

str

None

阿里雲百鍊的API Key,建議您配置API Key到環境變數(準備下線,併入配置 API Key)

輸出參數

參考create結果

修改線程

HTTP

程式碼範例

curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
    "metadata": {
        "modified": "true",
        "user": "abc123"
    }
}'

輸入參數

輸入參數名字

輸入參數的描述

參數類型

是否必須

thread_id

待檢索的thread_id

str

metadata

thread使用的名字

dict

返回結果

{
    "id": "thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7",
    "object": "thread",
    "created_at": 1711507920700,
    "metadata": {
        "modified": "true",
        "user": "abc123"
    },
    "request_id": "a9ad63fa-b884-94be-9ec6-5000882de3c4"
}

輸出參數

輸出檢索到的thread類,並包含除了使用者輸入以外參數的其他額外欄位:

  • id :thread_id

  • request_id :請求id

SDK

程式碼範例

from dashscope import Threads
import dashscope
import os

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

thread = Threads.update(
    'thread_id',
    # 建議您優先配置環境變數。若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    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.Threads;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Threads threads = new Threads();
        UpdateMetadataParam updateMetadataParam = UpdateMetadataParam.builder()
                .metadata(Collections.singletonMap("key", "value"))
                // 建議您優先配置環境變數。若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build();
        threads.update("assistant_id", updateMetadataParam);
    }
}

輸入參數

參數

類型

預設值

說明

thread_id

str

-

指定需要update的thread id

metadata

Dict

None

Thread關聯資訊

workspace

str

None

阿里雲百鍊的Workspace ID,僅當 api_key 為子業務空間 API Key 時才需要傳入。

api_key

str

None

阿里雲百鍊的API Key,建議您配置API Key到環境變數(準備下線,併入配置 API Key)

輸出參數

同create,參考create

刪除線程

HTTP

程式碼範例

curl --location --request DELETE 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

輸入參數

輸入參數名字

輸入參數的描述

參數類型

是否必須

id

待檢索的thread id

str

返回結果

{
    "id": "thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7",
    "object": "thread.deleted",
    "deleted": true,
    "request_id": "b4edb7b8-5855-9787-b5c3-0374ee2b3b2c"
}

輸出參數

輸出刪除thread後的狀態

SDK

程式碼範例

from dashscope import Threads
import dashscope
import os

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

thread = Threads.delete(
    'thread_id',
    # 建議您優先配置環境變數。若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY")
)
import com.alibaba.dashscope.common.DeletionStatus;
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.Threads;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Threads threads = new Threads();
        // 建議您優先配置環境變數。若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey("sk-xxx")
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        DeletionStatus assistantThread = threads.delete("thread_id", apiKey);
    }
}

輸入參數

參數

類型

預設值

說明

thread_id

str

-

指定要刪除的thread id

workspace

str

None

阿里雲百鍊的Workspace ID,僅當 api_key 為子業務空間 API Key 時才需要傳入。

api_key

str

None

阿里雲百鍊的API Key,建議您配置API Key到環境變數(準備下線,併入配置 API Key)

輸出參數

欄位名

欄位類型

欄位描述

id

str

刪除的對象的id

deleted

bool

是否刪除

錯誤碼

如果模型調用失敗並返回報錯資訊,請參見錯誤資訊進行解決。