All Products
Search
Document Center

Alibaba Cloud Model Studio:Threads (Deprecated)

Last Updated:Sep 02, 2026

Topik ini menjelaskan kelas Thread dalam Assistant API, termasuk cara membuat, mengambil, memodifikasi, dan menghapus thread.

PentingAssistant API akan segera dihentikan. Migrasikan ke Responses API sebagai alternatif. Responses API menyertakan beberapa alat bawaan dan mendukung manajemen konteks multi-turn.

Overview: Untuk informasi lebih lanjut mengenai fitur dan penggunaan dasar Assistant API, lihat Assistant API overview.

Retention period: Semua instans Thread disimpan di server Alibaba Cloud Model Studio dan tidak kedaluwarsa. Anda dapat menggunakan ID thread untuk mengambil informasi konteks.

Nama fungsi

Tipe

create

Membuat kelas Thread.

retrieve

Mengambil kelas Thread.

update

Memodifikasi kelas Thread.

delete

Menghapus kelas Thread.

Create a thread

HTTP

Kode contoh
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"
        }
    ]
}'
Parameter permintaan

Nama parameter input

Deskripsi parameter input

Tipe

Wajib

messages

Pesan yang diteruskan ke thread.

Message class

Tidak

metadata

Nama thread

string

Tidak

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

Respons mengembalikan objek thread dengan bidang tambahan berikut:

  • id: ID thread.
  • request_id: ID permintaan.

SDK

Kode contoh
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(
    # Kami menyarankan agar Anda menyetel kunci API sebagai variabel lingkungan. Jika tidak, ganti baris berikut dengan api_key="sk-xxx" menggunakan Kunci API Model Studio Anda.
    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();
        // Kami menyarankan agar Anda menyetel kunci API sebagai variabel lingkungan. Jika tidak, ganti baris berikut dengan apiKey("sk-xxx") menggunakan Kunci API Model Studio Anda.
        AssistantThread assistantThread = threads.create(ThreadParam.builder()
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build());
    }
}
Parameter permintaan

Parameter

Jenis

Nilai default

Deskripsi

messages

List[Dict]

None

Pesan awal dari thread.

metadata

Dict

None

Pasangan kunci-nilai yang dikaitkan dengan thread.

workspace

str

None

ID ruang kerja Alibaba Cloud Model Studio. Parameter ini hanya diperlukan ketika `api_key` adalah kunci API sub-ruang kerja.

api_key

str

None

Kunci API untuk Alibaba Cloud Model Studio. Kami menyarankan agar Anda mengonfigurasi kunci API sebagai variabel lingkungan.

Hasil

Hasilnya adalah objek Thread. Kode berikut menampilkan objek tersebut dalam format JSON:

{
    "created_at": 1711338305031,
    "id": "thread_97934051-2c15-44bf-97de-310039d873f9",
    "metadata": {},
    "object": "thread",
    "request_id": "982d4b9a-b982-9d53-9c79-a75b32f7168a",
    "status_code": 200
}
Parameter output

Nama bidang

Jenis

Deskripsi

status_code

int

Kode status HTTP. Nilai 200 menunjukkan bahwa pemanggilan berhasil. Nilai lain menunjukkan kegagalan pemanggilan.

id

str

ID thread, berupa string UUID.

metadata

Dict

Pasangan kunci-nilai yang dikaitkan dengan thread.

created_at

timestamp

Waktu pembuatan thread.

code

str

Kode kesalahan yang dikembalikan jika permintaan gagal. Parameter ini tidak dikembalikan jika permintaan berhasil.

Hanya Python

message

str

Pesan kesalahan. Bidang ini hanya dikembalikan saat permintaan gagal.

Hanya Python

Retrieve a thread

HTTP

Kode contoh
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"
Parameter permintaan

Nama Parameter

Deskripsi parameter

Jenis

Wajib

thread_id

ID thread yang akan diambil.

str

Ya

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

Respons berisi objek thread yang diambil, yang mencakup bidang tambahan berikut:

  • ID: ID Thread
  • request_id: ID permintaan.

SDK

Kode contoh
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',
    # Kami menyarankan agar Anda menyetel kunci API sebagai variabel lingkungan. Jika tidak, ganti baris berikut dengan api_key="sk-xxx" menggunakan Kunci API Model Studio Anda.
    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();
        // Masukkan thread_id dan apiKey secara langsung
        // Kami menyarankan agar Anda menyetel kunci API sebagai variabel lingkungan. Jika tidak, ganti baris berikut dengan apiKey("sk-xxx") menggunakan Kunci API Model Studio Anda.
        AssistantThread assistantThread = threads.retrieve(
            "thread_id",
            System.getenv("DASHSCOPE_API_KEY")
        );
    }
}
Parameter permintaan

Parameter

Tipe

Nilai default

Deskripsi

thread_id

str

-

ID thread yang akan dikueri.

workspace

str

None

ID ruang kerja Alibaba Cloud Model Studio. Parameter ini hanya diperlukan ketika `api_key` adalah kunci API sub-ruang kerja.

api_key

str

None

Kunci API untuk Alibaba Cloud Model Studio. Kami menyarankan agar Anda mengonfigurasi kunci API sebagai variabel lingkungan.

Parameter respons

Lihat hasil pembuatan.

Modify a thread

HTTP

Kode contoh
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"
    }
}'
Parameter permintaan

Nama Parameter Input

Deskripsi Parameter Input

Tipe

Wajib

thread_id

ID thread yang akan dimodifikasi.

str

Ya

metadata

Nama thread

dict

Tidak

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

Output berisi kelas thread yang diambil dan bidang tambahan yang tidak ditentukan dalam parameter yang diberikan pengguna:

  • id: thread_id
  • request_id: ID permintaan.

SDK

Kode contoh
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',
    # Kami menyarankan agar Anda menyetel kunci API sebagai variabel lingkungan. Jika tidak, ganti baris berikut dengan api_key="sk-xxx" menggunakan Kunci API Model Studio Anda.
    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"))
                // Kami menyarankan agar Anda menyetel kunci API sebagai variabel lingkungan. Jika tidak, ganti baris berikut dengan apiKey("sk-xxx") dan gunakan Kunci API Model Studio Anda.
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build();
        threads.update("thread_id", updateMetadataParam);
    }
}
Parameter permintaan

Parameter

Tipe

Nilai default

Deskripsi

thread_id

str

-

ID thread yang akan diperbarui.

metadata

Dict

None

Informasi yang dikaitkan dengan thread.

workspace

str

None

ID ruang kerja Alibaba Cloud Model Studio. Parameter ini hanya diperlukan ketika `api_key` adalah kunci API sub-ruang kerja.

api_key

str

None

Kunci API untuk Alibaba Cloud Model Studio. Kami menyarankan agar Anda mengonfigurasi kunci API sebagai variabel lingkungan.

Parameter respons

Operasi ini sama dengan operasi create.

Delete a thread

HTTP

Kode contoh
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"
Parameter permintaan

Nama Parameter Input

Deskripsi

Jenis

Wajib

id

ID thread yang akan diambil

str

Ya

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

Status thread setelah penghapusan.

SDK

Kode contoh
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',
    # Kami menyarankan agar Anda menyetel kunci API sebagai variabel lingkungan. Jika tidak, ganti baris berikut dengan api_key="sk-xxx" menggunakan Kunci API Model Studio Anda.
    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();
        // Kami menyarankan agar Anda menyetel kunci API sebagai variabel lingkungan. Jika tidak, ganti baris berikut dengan apiKey("sk-xxx") menggunakan Kunci API Model Studio Anda.
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        DeletionStatus assistantThread = threads.delete("thread_id", apiKey);
    }
}
Parameter permintaan

Parameter

Tipe

Nilai default

Deskripsi

thread_id

str

-

ID thread yang akan dihapus.

workspace

str

None

ID ruang kerja Alibaba Cloud Model Studio. Parameter ini hanya diperlukan ketika `api_key` adalah kunci API sub-ruang kerja.

api_key

str

None

Kunci API untuk Alibaba Cloud Model Studio. Kami menyarankan agar Anda mengonfigurasi kunci API sebagai variabel lingkungan.

Parameter respons

Nama bidang

Tipe

Deskripsi

id

str

ID objek yang dihapus.

deleted

bool

Konfirmasi Penghapusan

Kode kesalahan

Jika pemanggilan model gagal dan pesan kesalahan dikembalikan, lihat Pesan kesalahan untuk troubleshooting.