Deprecated. The Assistants API (
dashscope.assistantsanddashscope.threads) is deprecated and will be removed in a future release. Please migrate toGenerationorMultiModalConversationwith themessagesparameter. See the migration reference for details.
This guide documents the existing surface for maintaining code that still depends on it.
Create an assistant, start a thread, run it, and wait for completion:
from dashscope import Assistants, Threads, Runs, Messages
assistant = Assistants.create(
model="qwen-max",
name="smart helper",
description="A tool helper.",
instructions="You are a helpful assistant.",
)
thread = Threads.create(
messages=[{"role": "user", "content": "How do I make beef stew?"}],
)
run = Runs.create(thread.id, assistant_id=assistant.id)
run_status = Runs.wait(run.id, thread_id=thread.id)
print(run_status.status)
messages = Messages.list(thread.id)
print(messages)from dashscope import Assistants
assistant = Assistants.get(assistant_id=assistant.id)
assistants = Assistants.list(limit=20)
for a in assistants.data:
print(a.id, a.name)
assistant = Assistants.update(assistant.id, name="renamed helper")
Assistants.delete(assistant.id)from dashscope import Threads
thread = Threads.get(thread_id=thread.id)
thread = Threads.update(thread.id, metadata={"topic": "cooking"})
Threads.delete(thread.id)from dashscope import Messages
message = Messages.create(thread.id, content="What about a vegetarian version?")
message = Messages.get(message.id, thread_id=thread.id)
messages = Messages.list(thread.id, limit=20)from dashscope import Runs
run = Runs.get(run.id, thread_id=thread.id)
run = Runs.wait(run.id, thread_id=thread.id, timeout_seconds=60)
Runs.cancel(run.id, thread_id=thread.id)List the individual execution steps of a run (tool calls, message creations, etc.):
from dashscope import Steps
steps = Steps.list(run.id, thread_id=thread.id, limit=20)
for step in steps.data:
print(step.id, step.type, step.status)Attaching a file uses a separate Files class under dashscope.assistants (distinct from the top-level dashscope.Files used for fine-tuning uploads):
from dashscope.assistants.files import Files
attached = Files.create(assistant_id=assistant.id, file_id="file-id-xxxx")
print(attached)
info = Files.get(file_id="file-id-xxxx", assistant_id=assistant.id)
files = Files.list(assistant.id, limit=10, order="asc")
for f in files.data:
print(f.id, f.assistant_id)
deleted = Files.delete(file_id="file-id-xxxx", assistant_id=assistant.id)
print(deleted.deleted)