Skip to content

Latest commit

 

History

History
111 lines (75 loc) · 3.06 KB

assistants.md

File metadata and controls

111 lines (75 loc) · 3.06 KB

Assistants API (Deprecated)

English | 中文 | 日本語 | Español | 한국어

Deprecated. The Assistants API (dashscope.assistants and dashscope.threads) is deprecated and will be removed in a future release. Please migrate to Generation or MultiModalConversation with the messages parameter. See the migration reference for details.

This guide documents the existing surface for maintaining code that still depends on it.

Basic Flow

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)

Assistants

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)

Threads

from dashscope import Threads

thread = Threads.get(thread_id=thread.id)
thread = Threads.update(thread.id, metadata={"topic": "cooking"})
Threads.delete(thread.id)

Messages

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)

Runs

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)

Run Steps

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 Files to an Assistant

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)