Cette rubrique décrit la classe Thread de l'API Assistant, ainsi que les méthodes pour créer, récupérer, modifier et supprimer des threads.
ImportantL'API Assistant est en cours de dépréciation. Migrez vers l'API Responses comme alternative. L'API Responses intègre plusieurs outils natifs et prend en charge la gestion du contexte sur plusieurs tours.
Vue d'ensemble : Pour plus d'informations sur les fonctionnalités et l'utilisation de base de l'API Assistant, reportez-vous à la Vue d'ensemble de l'API Assistant .
Durée de conservation : Toutes les instances Thread sont stockées sur le serveur Alibaba Cloud Model Studio et n'expirent pas. Utilisez l'ID du thread pour récupérer les informations de contexte.
|
|
|---|---|
|
|
|
|
|
|
|
|
Créer un thread
HTTP
Exemple de codecurl --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"
}
]
}'
Paramètres de la requête
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
{
"id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
"object": "thread",
"created_at": 1711448377850,
"metadata": {},
"request_id": "dd9489ec-dbdb-95d4-9ff8-cfe29b61db27"
}
Paramètres de la réponseLa réponse renvoie un objet thread contenant les champs supplémentaires suivants :
- id : ID du thread.
- request_id : ID de la requête.
SDK
Exemple de codeimport json
from dashscope import Threads
import dashscope
import os
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
thread = Threads.create(
# We recommend that you set the API key as an environment variable. If not, replace the following line with api_key="sk-xxx" using your Model Studio API key.
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();
// We recommend that you set the API key as an environment variable. If not, replace the following line with apiKey("sk-xxx") using your Model Studio API key.
AssistantThread assistantThread = threads.create(ThreadParam.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.build());
}
}
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Le résultat est un objet Thread. Le code suivant présente cet objet au 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
}
Paramètres de sortie
|
|
|
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Code d'erreur retourné en cas d'échec de la requête. Ce paramètre n'est pas présent si la requête aboutit. Python uniquement |
|
| Message d'erreur. Ce champ n'apparaît qu'en cas d'échec de la requête. Python uniquement |
Récupérer un thread
HTTP
Exemple de codecurl --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"
Paramètres de la requête
|
|
|
|
|---|---|---|---|
|
|
|
|
{
"id": "thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7",
"object": "thread",
"created_at": 1711507920700,
"metadata": {},
"request_id": "4d4e73ad-15fb-96ac-9262-0643a0fdb5ca"
}
Paramètres de la réponseLa réponse contient l'objet thread récupéré, qui inclut les champs supplémentaires suivants :
- ID : ID du thread
- request_id : ID de la requête.
SDK
Exemple de codefrom dashscope import Threads
import dashscope
import os
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
thread = Threads.retrieve(
'thread_id',
# We recommend that you set the API key as an environment variable. If not, replace the following line with api_key="sk-xxx" using your Model Studio API key.
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();
// Pass the thread_id and apiKey directly
// We recommend that you set the API key as an environment variable. If not, replace the following line with apiKey("sk-xxx") using your Model Studio API key.
AssistantThread assistantThread = threads.retrieve(
"thread_id",
System.getenv("DASHSCOPE_API_KEY")
);
}
}
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Reportez-vous au résultat de création.
Modifier un thread
HTTP
Exemple de codecurl --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"
}
}'
Paramètres de la requête
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
{
"id": "thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7",
"object": "thread",
"created_at": 1711507920700,
"metadata": {
"modified": "true",
"user": "abc123"
},
"request_id": "a9ad63fa-b884-94be-9ec6-5000882de3c4"
}
Paramètres de la réponseLa sortie contient la classe thread récupérée ainsi que des champs supplémentaires non spécifiés dans les paramètres fournis par l'utilisateur :
- id : thread_id
- request_id : ID de la requête.
SDK
Exemple de codefrom dashscope import Threads
import dashscope
import os
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
thread = Threads.update(
'thread_id',
# We recommend that you set the API key as an environment variable. If not, replace the following line with api_key="sk-xxx" using your Model Studio API key.
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"))
// We recommend that you set the API key as an environment variable. If not, replace the following line with apiKey("sk-xxx") and use your Model Studio API key.
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.build();
threads.update("thread_id", updateMetadataParam);
}
}
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Cette opération est identique à l'opération de création.
Supprimer un thread
HTTP
Exemple de codecurl --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"
Paramètres de la requête
|
|
|
|
|---|---|---|---|
|
|
|
|
{
"id": "thread_c7ebb0ca-2e4f-43e5-b223-6e1f8c6fccc7",
"object": "thread.deleted",
"deleted": true,
"request_id": "b4edb7b8-5855-9787-b5c3-0374ee2b3b2c"
}
Paramètres de la réponseÉtat du thread après suppression.
SDK
Exemple de codefrom dashscope import Threads
import dashscope
import os
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
thread = Threads.delete(
'thread_id',
# We recommend that you set the API key as an environment variable. If not, replace the following line with api_key="sk-xxx" using your Model Studio API key.
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();
// We recommend that you set the API key as an environment variable. If not, replace the following line with apiKey("sk-xxx") using your Model Studio API key.
String apiKey = System.getenv("DASHSCOPE_API_KEY");
DeletionStatus assistantThread = threads.delete("thread_id", apiKey);
}
}
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|---|---|---|
|
|
|
|
|
|
Codes d'erreur
Si un appel de modèle échoue et qu'un message d'erreur est retourné, consultez la rubrique Codes d'erreur pour le dépannage.