Skip to content

Commit a3e8ceb

Browse files
docs: add fine-tuning, assistants, configuration, error-handling, and response-types guides
1 parent 6755c23 commit a3e8ceb

16 files changed

Lines changed: 1458 additions & 0 deletions

‎README.md‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ else:
118118
print(response.output.choices[0].message.content)
119119
```
120120

121+
For the full list of exception classes and when each is raised, see the [Error Handling Reference](docs/guides/error-handling.md).
122+
121123
## API Key Authentication
122124

123125
The SDK uses API key for authentication. To obtain an API Key, see [How to get an API Key](https://help.aliyun.com/en/model-studio/get-api-key). Please refer to [official documentation for alibabacloud china](https://www.alibabacloud.com/help/en/model-studio/) and [official documentation for alibabacloud international](https://www.alibabacloud.com/help/en/model-studio/) regarding how to obtain your api-key.
@@ -252,6 +254,8 @@ print(response)
252254

253255
A complete runnable example is available in [`samples/set_region_example.py`](samples/set_region_example.py).
254256

257+
For request timeouts, custom headers, proxy support, and closing shared connection pools, see the [Advanced Configuration guide](docs/guides/configuration.md).
258+
255259
## AI Assistant: DashScope SDK Expert
256260

257261
The SDK ships with an interactive AI assistant, **DashScope SDK Expert**, built on the bundled Agentic CLI (`dashscope/acli`) framework. For DashScope SDK/CLI users it is the recommended way to get development consultation and AI coding help — answering SDK/API questions, generating runnable examples, showing CLI usage, and diagnosing errors, right in your terminal.
@@ -297,6 +301,26 @@ response = MultiModalConversation.call(model="qwen-vl-max", messages=messages)
297301
print(response.output.choices[0].message.content[0]["text"])
298302
```
299303

304+
Use `AioMultiModalConversation` for the `async`/`await` form:
305+
306+
```python
307+
import asyncio
308+
from dashscope import AioMultiModalConversation
309+
310+
async def main():
311+
messages = [{
312+
"role": "user",
313+
"content": [
314+
{"image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"},
315+
{"text": "What does this picture describe?"},
316+
],
317+
}]
318+
response = await AioMultiModalConversation.call(model="qwen-vl-max", messages=messages)
319+
print(response.output.choices[0].message.content[0]["text"])
320+
321+
asyncio.run(main())
322+
```
323+
300324
### Using Local Files
301325

302326
Every field that accepts a URL (`image`, `audio`, `video` in messages; `images` on `ImageSynthesis`, etc.) also accepts a local file path — the SDK uploads it to OSS automatically, no manual header needed:
@@ -341,6 +365,22 @@ resp = MultiModalEmbedding.call(
341365
print(resp.output)
342366
```
343367

368+
### Batch (Offline) Text Embedding
369+
370+
For large volumes of text, submit a file (one text per line) for asynchronous batch embedding instead of calling `TextEmbedding.call` per item:
371+
372+
```python
373+
from dashscope import BatchTextEmbedding
374+
375+
resp = BatchTextEmbedding.call(
376+
model=BatchTextEmbedding.Models.text_embedding_async_v2,
377+
url="https://example.com/texts.txt",
378+
)
379+
print(resp.output.task_id, resp.output.task_status)
380+
if resp.output.task_status == "SUCCEEDED":
381+
print(resp.output.url) # download the result file from here
382+
```
383+
344384
### Text ReRank
345385

346386
```python
@@ -360,6 +400,26 @@ for r in resp.output.results:
360400
print(r.index, r.relevance_score, r.document)
361401
```
362402

403+
Use `AioTextReRank` for the `async`/`await` form:
404+
405+
```python
406+
import asyncio
407+
from dashscope import AioTextReRank
408+
409+
async def main():
410+
resp = await AioTextReRank.call(
411+
model=AioTextReRank.Models.gte_rerank,
412+
query="What is the capital of China?",
413+
documents=["The capital of China is Beijing.", "China is a large country in East Asia."],
414+
return_documents=True,
415+
top_n=1,
416+
)
417+
for r in resp.output.results:
418+
print(r.index, r.relevance_score, r.document)
419+
420+
asyncio.run(main())
421+
```
422+
363423
### Code Generation
364424

365425
`CodeGeneration` powers task-specific coding scenarios (`Scenes`): natural-language-to-code, code explanation, comment generation, commit messages, unit tests, code Q&A, and natural-language-to-SQL.
@@ -443,6 +503,21 @@ with open("output.wav", "wb") as f:
443503
f.write(result.get_audio_data())
444504
```
445505

506+
`HttpSpeechSynthesizer` calls TTS over plain HTTP (no WebSocket), useful in environments that can't hold a persistent connection:
507+
508+
```python
509+
from dashscope.audio.http_tts import HttpSpeechSynthesizer
510+
511+
result = HttpSpeechSynthesizer.call(
512+
model="cosyvoice-v3-flash",
513+
text="Hello, Bailian.",
514+
voice="longxiaochun",
515+
audio_format="wav",
516+
)
517+
with open("output.wav", "wb") as f:
518+
f.write(result.audio_data)
519+
```
520+
446521
### Streaming Speech Synthesis (CosyVoice v2)
447522

448523
Stream text in incrementally and receive audio bytes as they're generated, via a callback:
@@ -611,6 +686,12 @@ status = Deployments.get(deployed_model).output.status
611686
response = Generation.call(model=deployed_model, messages=[{"role": "user", "content": "Hi"}])
612687
```
613688

689+
For the full job/deployment lifecycle (listing, canceling, streaming events, scaling), see the [Fine-tuning & Deployment Lifecycle guide](docs/guides/fine-tuning.md).
690+
691+
### Assistants API (Deprecated)
692+
693+
The legacy Assistants API (`Assistants`, `Threads`, `Runs`, `Messages`) still works but is deprecated — see the [Assistants API guide](docs/guides/assistants.md) for the full surface and migration notes. New code should use [`Generation`](#quick-start) or [`MultiModalConversation`](#multimodal-understanding-vision) instead.
694+
614695
## CLI Usage
615696

616697
Every SDK capability is also available as a `dashscope` sub-command (installed with the base package), for scripting or quick checks without writing Python:
@@ -676,5 +757,7 @@ response.output # Any: the request output (shape depends on the API calle
676757
response.usage # Any: token/quota usage information
677758
```
678759

760+
For how `output`/`usage` support both dict-style and attribute-style access, and the per-capability response subclasses, see the [Response Object Model guide](docs/guides/response-types.md).
761+
679762
## License
680763
This project is licensed under the Apache License (Version 2.0).

‎docs/guides/assistants.md‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Assistants API (Deprecated)
2+
3+
> **English** | [中文](assistants_zh.md) | [日本語](assistants_ja.md)
4+
5+
> **Deprecated.** The Assistants API (`dashscope.assistants` and `dashscope.threads`) is deprecated and will be removed in a future release. Please migrate to [`Generation`](../../README.md#quick-start) or [`MultiModalConversation`](../../README.md#multimodal-understanding-vision) with the `messages` parameter. See the [migration reference](https://help.aliyun.com/zh/model-studio/synchronous-call-api-reference) for details.
6+
7+
This guide documents the existing surface for maintaining code that still depends on it.
8+
9+
## Basic Flow
10+
11+
Create an assistant, start a thread, run it, and wait for completion:
12+
13+
```python
14+
from dashscope import Assistants, Threads, Runs, Messages
15+
16+
assistant = Assistants.create(
17+
model="qwen-max",
18+
name="smart helper",
19+
description="A tool helper.",
20+
instructions="You are a helpful assistant.",
21+
)
22+
23+
thread = Threads.create(
24+
messages=[{"role": "user", "content": "How do I make beef stew?"}],
25+
)
26+
27+
run = Runs.create(thread.id, assistant_id=assistant.id)
28+
run_status = Runs.wait(run.id, thread_id=thread.id)
29+
print(run_status.status)
30+
31+
messages = Messages.list(thread.id)
32+
print(messages)
33+
```
34+
35+
## Assistants
36+
37+
```python
38+
from dashscope import Assistants
39+
40+
assistant = Assistants.get(assistant_id=assistant.id)
41+
42+
assistants = Assistants.list(limit=20)
43+
for a in assistants.data:
44+
print(a.id, a.name)
45+
46+
assistant = Assistants.update(assistant.id, name="renamed helper")
47+
48+
Assistants.delete(assistant.id)
49+
```
50+
51+
## Threads
52+
53+
```python
54+
from dashscope import Threads
55+
56+
thread = Threads.get(thread_id=thread.id)
57+
thread = Threads.update(thread.id, metadata={"topic": "cooking"})
58+
Threads.delete(thread.id)
59+
```
60+
61+
## Messages
62+
63+
```python
64+
from dashscope import Messages
65+
66+
message = Messages.create(thread.id, content="What about a vegetarian version?")
67+
message = Messages.get(message.id, thread_id=thread.id)
68+
messages = Messages.list(thread.id, limit=20)
69+
```
70+
71+
## Runs
72+
73+
```python
74+
from dashscope import Runs
75+
76+
run = Runs.get(run.id, thread_id=thread.id)
77+
run = Runs.wait(run.id, thread_id=thread.id, timeout_seconds=60)
78+
Runs.cancel(run.id, thread_id=thread.id)
79+
```
80+
81+
## Run Steps
82+
83+
List the individual execution steps of a run (tool calls, message creations, etc.):
84+
85+
```python
86+
from dashscope import Steps
87+
88+
steps = Steps.list(run.id, thread_id=thread.id, limit=20)
89+
for step in steps.data:
90+
print(step.id, step.type, step.status)
91+
```
92+
93+
## Attaching Files to an Assistant
94+
95+
Attaching a file uses a separate `Files` class under `dashscope.assistants` (distinct from the top-level `dashscope.Files` used for fine-tuning uploads):
96+
97+
```python
98+
from dashscope.assistants.files import Files
99+
100+
attached = Files.create(assistant_id=assistant.id, file_id="file-id-xxxx")
101+
print(attached)
102+
103+
info = Files.get(assistant_id=assistant.id, file_id="file-id-xxxx")
104+
```

‎docs/guides/assistants_ja.md‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Assistants API(非推奨)
2+
3+
> [English](assistants.md) | [中文](assistants_zh.md) | **日本語**
4+
5+
> **非推奨。** Assistants API(`dashscope.assistants` および `dashscope.threads`)は非推奨であり、将来のリリースで削除される予定です。`messages` パラメータを使用する [`Generation`](../../README_ja.md#クイックスタート) または [`MultiModalConversation`](../../README_ja.md#マルチモーダル理解ビジョン) への移行をお願いします。詳細は[移行リファレンス](https://help.aliyun.com/zh/model-studio/synchronous-call-api-reference)を参照してください。
6+
7+
このガイドは、このAPIに依存するコードを保守するために、既存のインターフェースをまとめたものです。
8+
9+
## 基本的な流れ
10+
11+
アシスタントを作成し、スレッドを開始して実行し、完了を待ちます:
12+
13+
```python
14+
from dashscope import Assistants, Threads, Runs, Messages
15+
16+
assistant = Assistants.create(
17+
model="qwen-max",
18+
name="smart helper",
19+
description="A tool helper.",
20+
instructions="You are a helpful assistant.",
21+
)
22+
23+
thread = Threads.create(
24+
messages=[{"role": "user", "content": "How do I make beef stew?"}],
25+
)
26+
27+
run = Runs.create(thread.id, assistant_id=assistant.id)
28+
run_status = Runs.wait(run.id, thread_id=thread.id)
29+
print(run_status.status)
30+
31+
messages = Messages.list(thread.id)
32+
print(messages)
33+
```
34+
35+
## Assistants
36+
37+
```python
38+
from dashscope import Assistants
39+
40+
assistant = Assistants.get(assistant_id=assistant.id)
41+
42+
assistants = Assistants.list(limit=20)
43+
for a in assistants.data:
44+
print(a.id, a.name)
45+
46+
assistant = Assistants.update(assistant.id, name="renamed helper")
47+
48+
Assistants.delete(assistant.id)
49+
```
50+
51+
## Threads
52+
53+
```python
54+
from dashscope import Threads
55+
56+
thread = Threads.get(thread_id=thread.id)
57+
thread = Threads.update(thread.id, metadata={"topic": "cooking"})
58+
Threads.delete(thread.id)
59+
```
60+
61+
## Messages
62+
63+
```python
64+
from dashscope import Messages
65+
66+
message = Messages.create(thread.id, content="What about a vegetarian version?")
67+
message = Messages.get(message.id, thread_id=thread.id)
68+
messages = Messages.list(thread.id, limit=20)
69+
```
70+
71+
## Runs
72+
73+
```python
74+
from dashscope import Runs
75+
76+
run = Runs.get(run.id, thread_id=thread.id)
77+
run = Runs.wait(run.id, thread_id=thread.id, timeout_seconds=60)
78+
Runs.cancel(run.id, thread_id=thread.id)
79+
```
80+
81+
## Run Steps
82+
83+
run の個々の実行ステップ(ツール呼び出し、メッセージ作成など)を一覧表示します:
84+
85+
```python
86+
from dashscope import Steps
87+
88+
steps = Steps.list(run.id, thread_id=thread.id, limit=20)
89+
for step in steps.data:
90+
print(step.id, step.type, step.status)
91+
```
92+
93+
## アシスタントへのファイルの添付
94+
95+
ファイルの添付には、`dashscope.assistants` 配下の専用の `Files` クラスを使用します(微調整のアップロードに使うトップレベルの `dashscope.Files` とは別のものです):
96+
97+
```python
98+
from dashscope.assistants.files import Files
99+
100+
attached = Files.create(assistant_id=assistant.id, file_id="file-id-xxxx")
101+
print(attached)
102+
103+
info = Files.get(assistant_id=assistant.id, file_id="file-id-xxxx")
104+
```

0 commit comments

Comments
 (0)