bailian-cli 1.0.0-beta.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1008 +0,0 @@
1
- ---
2
- name: bailian-api-doc
3
- description: "DashScope API Documentation Reference — MUST READ when writing ANY code that needs AI/LLM capabilities: text generation, image generation, video generation, speech synthesis, speech recognition, vision understanding, web search, RAG knowledge retrieval, memory, or app orchestration. This is the canonical API reference for integrating Aliyun Model Studio (DashScope) AI services into applications, scripts, backends, or tools. Contains complete API endpoints, request/response schemas, curl examples, authentication (auto-detect local API Key), streaming/async patterns. WHEN TO USE: user is writing code / building an app / creating a service / developing a tool / implementing a feature that involves calling any AI model (LLM, VLM, TTS, ASR, image gen, video gen, etc.). Trigger keywords: 'write code', 'build app', 'integrate API', 'call model', 'HTTP request', 'curl example', 'backend service', 'generate text/image/video/audio programmatically', '接入大模型', '调用API', '写代码调用', '开发应用'."
4
- ---
5
-
6
- # DashScope API Reference — Integration Guide
7
-
8
- > **When to use this document**: Whenever you are writing code (in any language) that needs AI capabilities — text generation, image creation, video production, speech synthesis/recognition, vision understanding, web search, RAG, or memory — refer to this document for the correct API endpoints, request formats, and authentication.
9
- >
10
- > This is the single source of truth for integrating Aliyun Model Studio (DashScope) AI services into your applications, scripts, backends, and tools.
11
-
12
- ## Base URL
13
-
14
- | Region | Base URL |
15
- | ------------ | ------------------------------------- |
16
- | cn (default) | `https://dashscope.aliyuncs.com` |
17
- | us | `https://dashscope-us.aliyuncs.com` |
18
- | intl | `https://dashscope-intl.aliyuncs.com` |
19
-
20
- ## Authentication
21
-
22
- ### Method 1: API Key (Bearer Token)
23
-
24
- ```
25
- Authorization: Bearer sk-xxxxxxxxxxxxxxxx
26
- ```
27
-
28
- #### Obtaining API Key (Priority Order)
29
-
30
- Before requesting a new key, check if one already exists locally:
31
-
32
- **1. Environment Variable (Highest Priority)**
33
-
34
- ```bash
35
- echo $DASHSCOPE_API_KEY
36
- ```
37
-
38
- If set, use it directly.
39
-
40
- **2. Aliyun Model Studio CLI Config File**
41
-
42
- ```bash
43
- # Check if Aliyun Model Studio CLI has saved credentials
44
- cat ~/.bailian/config.json | grep api_key
45
-
46
- # Or use CLI command
47
- bl auth status
48
- ```
49
-
50
- If `api_key` exists in `~/.bailian/config.json`, use it directly.
51
-
52
- **3. Apply for a New Key**
53
-
54
- Only if neither of the above is available, get a new API Key at:
55
- https://bailian.console.aliyun.com/cn-beijing/?tab=app#/api-key
56
-
57
- #### Quick Integration Snippet
58
-
59
- ```bash
60
- # Auto-detect: env var → config file → prompt user
61
- API_KEY="${DASHSCOPE_API_KEY:-$(cat ~/.bailian/config.json 2>/dev/null | grep -o '"api_key":\s*"[^"]*"' | cut -d'"' -f4)}"
62
-
63
- if [ -z "$API_KEY" ]; then
64
- echo "No API Key found. Get one at: https://bailian.console.aliyun.com/cn-beijing/?tab=app#/api-key"
65
- exit 1
66
- fi
67
-
68
- # Use in requests
69
- curl -H "Authorization: Bearer $API_KEY" ...
70
- ```
71
-
72
- ### Method 2: AK/SK Signing (Alibaba Cloud V3 ROA)
73
-
74
- Required for Knowledge Retrieve API. Uses `Authorization` header with HMAC-SHA256 signature:
75
-
76
- ```
77
- Authorization: ACS3-HMAC-SHA256 Credential={AccessKeyId},SignedHeaders=host;x-acs-action;x-acs-content-sha256;x-acs-date;x-acs-signature-nonce;x-acs-version,Signature={signature}
78
- ```
79
-
80
- ## Important: Server-Side Calls Required
81
-
82
- > **DashScope API does NOT support direct calls from browser-side (frontend) JavaScript.** The API does not provide CORS headers, so `fetch()` or `XMLHttpRequest` from a web page will be blocked by the browser's same-origin policy.
83
- >
84
- > You MUST build a **server-side** backend (Node.js, Python, Java, Go, etc.) to call DashScope API, then expose your own endpoints for the frontend to consume.
85
-
86
- ```
87
- ┌──────────┐ ┌──────────────┐ ┌───────────────┐
88
- │ Frontend │ ──► │ Your Backend │ ──► │ DashScope API │
89
- │ (Browser)│ ◄── │ (Server) │ ◄── │ │
90
- └──────────┘ └──────────────┘ └───────────────┘
91
- ```
92
-
93
- Typical architecture:
94
-
95
- - **Frontend**: Calls your own backend API (e.g., `POST /api/chat`)
96
- - **Backend**: Forwards request to DashScope API with API Key, returns response to frontend
97
- - **API Key**: Stored on server only — NEVER expose API Key in frontend code
98
-
99
- ## Common Headers
100
-
101
- | Header | Value | Description |
102
- | -------------------------------- | ---------------------- | ---------------------------------------- |
103
- | `Content-Type` | `application/json` | Required for all JSON requests |
104
- | `Authorization` | `Bearer sk-xxx` | API Key authentication |
105
- | `x-dashscope-source-config` | JSON array (see below) | Source tracking / channel identification |
106
- | `X-DashScope-Async` | `enable` | Enable async mode (returns task_id) |
107
- | `X-DashScope-OssResourceResolve` | `enable` | Required when using `oss://` URLs |
108
-
109
- ### x-dashscope-source-config (Required)
110
-
111
- All requests MUST include this header for source tracking:
112
-
113
- ```
114
- x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]
115
- ```
116
-
117
- Formatted value:
118
-
119
- ```json
120
- [
121
- {
122
- "channel": "bailian-cli",
123
- "tags": {
124
- "t1": "",
125
- "t2": "public",
126
- "t3": "skill-doc"
127
- }
128
- }
129
- ]
130
- ```
131
-
132
- | Field | Description |
133
- | --------- | -------------------------------------------------------- |
134
- | `channel` | Source channel identifier, fixed as `bailian-cli` |
135
- | `tags.t1` | Reserved tag |
136
- | `tags.t2` | Access scope: `public` for external integration |
137
- | `tags.t3` | Integration source: `skill-doc` indicates API doc driven |
138
-
139
- ## Error Response Format
140
-
141
- ```json
142
- {
143
- "code": "ErrorCode",
144
- "message": "Human-readable error message",
145
- "request_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
146
- }
147
- ```
148
-
149
- Common error codes: `InvalidApiKey`, `AccessDenied`, `Throttling`, `InvalidParameter`, `ModelNotFound`.
150
-
151
- ---
152
-
153
- ## 1. Chat Completions (OpenAI Compatible)
154
-
155
- **Endpoint**: `POST {baseUrl}/compatible-mode/v1/chat/completions`
156
-
157
- Fully compatible with OpenAI Chat Completions API format.
158
-
159
- ### Request Body
160
-
161
- ```json
162
- {
163
- "model": "qwen3.6-plus",
164
- "messages": [
165
- { "role": "system", "content": "You are a helpful assistant." },
166
- { "role": "user", "content": "Hello" }
167
- ],
168
- "max_tokens": 2048,
169
- "temperature": 0.7,
170
- "top_p": 0.9,
171
- "stream": false,
172
- "tools": [],
173
- "enable_thinking": false,
174
- "thinking_budget": 4096
175
- }
176
- ```
177
-
178
- ### Multimodal Messages (Vision / Audio / Video)
179
-
180
- ```json
181
- {
182
- "model": "qwen-vl-max",
183
- "messages": [
184
- {
185
- "role": "user",
186
- "content": [
187
- { "type": "text", "text": "Describe this image" },
188
- { "type": "image_url", "image_url": { "url": "https://example.com/photo.jpg" } }
189
- ]
190
- }
191
- ]
192
- }
193
- ```
194
-
195
- Content types: `text`, `image_url`, `audio_url`, `input_audio` (base64), `video` (frame URLs array).
196
-
197
- ### Omni Multimodal (Text + Audio Output)
198
-
199
- ```json
200
- {
201
- "model": "qwen3.5-omni-plus",
202
- "messages": [{ "role": "user", "content": "你好" }],
203
- "modalities": ["text", "audio"],
204
- "audio": { "voice": "Cherry", "format": "wav" },
205
- "stream": true,
206
- "stream_options": { "include_usage": true }
207
- }
208
- ```
209
-
210
- Voices: `Cherry`, `Serena`, `Ethan`, `Chelsie`, `Tina`.
211
-
212
- ### Response (Non-streaming)
213
-
214
- ```json
215
- {
216
- "id": "chatcmpl-xxx",
217
- "object": "chat.completion",
218
- "model": "qwen3.6-plus",
219
- "choices": [
220
- {
221
- "index": 0,
222
- "message": {
223
- "role": "assistant",
224
- "content": "Hello! How can I help you?",
225
- "reasoning_content": null,
226
- "tool_calls": null
227
- },
228
- "finish_reason": "stop"
229
- }
230
- ],
231
- "usage": { "prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30 }
232
- }
233
- ```
234
-
235
- ### Streaming (SSE)
236
-
237
- ```
238
- data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
239
-
240
- data: [DONE]
241
- ```
242
-
243
- Audio chunks in streaming: `choices[0].delta.audio.data` (base64 PCM).
244
-
245
- ### curl Example
246
-
247
- ```bash
248
- curl -X POST "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions" \
249
- -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
250
- -H "Content-Type: application/json" \
251
- -H 'x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]' \
252
- -d '{
253
- "model": "qwen3.6-plus",
254
- "messages": [{"role": "user", "content": "Hello"}],
255
- "stream": false
256
- }'
257
- ```
258
-
259
- ### Available Models
260
-
261
- - Text: `qwen3.6-plus`, `qwen-plus`, `qwen-turbo`, `qwen-max`, `qwq-plus`
262
- - Vision: `qwen-vl-max`, `qwen-vl-plus`
263
- - Omni: `qwen3.5-omni-plus`
264
-
265
- ---
266
-
267
- ## 2. Image Generation (Sync)
268
-
269
- **Endpoint**: `POST {baseUrl}/api/v1/services/aigc/multimodal-generation/generation`
270
-
271
- Synchronous generation via `qwen-image-2.0` series.
272
-
273
- ### Request Body
274
-
275
- ```json
276
- {
277
- "model": "qwen-image-2.0",
278
- "input": {
279
- "messages": [
280
- {
281
- "role": "user",
282
- "content": [{ "text": "A cat in space" }]
283
- }
284
- ]
285
- },
286
- "parameters": {
287
- "size": "1024*1024",
288
- "n": 1,
289
- "seed": 42,
290
- "negative_prompt": "blurry, low quality",
291
- "prompt_extend": true,
292
- "watermark": false
293
- }
294
- }
295
- ```
296
-
297
- Size options: `1024*1024`, `720*1280`, `1280*720`, or ratios `1:1`, `3:4`, `4:3`, `9:16`, `16:9`.
298
-
299
- ### Image Edit (Multi-image Input)
300
-
301
- ```json
302
- {
303
- "model": "qwen-image-2.0",
304
- "input": {
305
- "messages": [
306
- {
307
- "role": "user",
308
- "content": [
309
- { "image": "https://example.com/photo.png" },
310
- { "image": "https://example.com/bg.png" },
311
- { "text": "Merge these two images" }
312
- ]
313
- }
314
- ]
315
- },
316
- "parameters": { "size": "1024*1024", "n": 1 }
317
- }
318
- ```
319
-
320
- ### Response
321
-
322
- ```json
323
- {
324
- "output": {
325
- "choices": [
326
- {
327
- "finish_reason": "stop",
328
- "message": {
329
- "role": "assistant",
330
- "content": [
331
- { "image": "https://dashscope-result.oss.aliyuncs.com/xxx.png", "type": "image" }
332
- ]
333
- }
334
- }
335
- ],
336
- "finished": true
337
- },
338
- "usage": { "image_count": 1 },
339
- "request_id": "xxx"
340
- }
341
- ```
342
-
343
- ### curl Example
344
-
345
- ```bash
346
- curl -X POST "https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation" \
347
- -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
348
- -H "Content-Type: application/json" \
349
- -H "X-DashScope-OssResourceResolve: enable" \
350
- -H 'x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]' \
351
- -d '{
352
- "model": "qwen-image-2.0",
353
- "input": { "messages": [{ "role": "user", "content": [{ "text": "A sunset over mountains" }] }] },
354
- "parameters": { "size": "1024*1024", "n": 1 }
355
- }'
356
- ```
357
-
358
- ---
359
-
360
- ## 3. Video Generation (Async)
361
-
362
- **Endpoint**: `POST {baseUrl}/api/v1/services/aigc/video-generation/video-synthesis`
363
-
364
- Async operation — returns `task_id`, requires polling.
365
-
366
- ### Request Headers
367
-
368
- ```
369
- Authorization: Bearer sk-xxx
370
- Content-Type: application/json
371
- X-DashScope-Async: enable
372
- ```
373
-
374
- ### Request Body (Text-to-Video)
375
-
376
- ```json
377
- {
378
- "model": "happyhorse-1.0-t2v",
379
- "input": {
380
- "prompt": "Sunset on the beach, cinematic",
381
- "negative_prompt": "blurry, low quality"
382
- },
383
- "parameters": {
384
- "resolution": "720P",
385
- "duration": 5,
386
- "prompt_extend": true,
387
- "seed": 42
388
- }
389
- }
390
- ```
391
-
392
- ### Request Body (Image-to-Video)
393
-
394
- ```json
395
- {
396
- "model": "happyhorse-1.0-i2v",
397
- "input": {
398
- "prompt": "The girl smiles and blinks",
399
- "img_url": "https://example.com/girl.png"
400
- },
401
- "parameters": { "resolution": "720P", "duration": 5 }
402
- }
403
- ```
404
-
405
- ### Video Edit Request
406
-
407
- ```json
408
- {
409
- "model": "happyhorse-1.0-video-edit",
410
- "input": {
411
- "prompt": "Convert to clay style",
412
- "media": [
413
- { "type": "video", "url": "https://example.com/input.mp4" },
414
- { "type": "reference_image", "url": "https://example.com/ref.png" }
415
- ]
416
- },
417
- "parameters": {
418
- "resolution": "1080P",
419
- "duration": 5,
420
- "audio_setting": "auto",
421
- "prompt_extend": true
422
- }
423
- }
424
- ```
425
-
426
- ### Video Reference Request (Reference-to-Video)
427
-
428
- Multi-subject reference-to-video generation with optional voice cloning. Use `图N` / `视频N` markers in prompt to reference specific media inputs.
429
-
430
- ```json
431
- {
432
- "model": "happyhorse-1.0-r2v",
433
- "input": {
434
- "prompt": "视频1抱着图2,在图3的椅子上弹吉他",
435
- "media": [
436
- {
437
- "type": "reference_image",
438
- "url": "https://example.com/person.jpg",
439
- "reference_voice": "https://example.com/voice.mp3"
440
- },
441
- {
442
- "type": "reference_video",
443
- "url": "https://example.com/scene.mp4",
444
- "reference_voice": "https://example.com/voice2.mp3"
445
- },
446
- {
447
- "type": "reference_image",
448
- "url": "https://example.com/guitar.png"
449
- },
450
- {
451
- "type": "reference_image",
452
- "url": "https://example.com/bg.png"
453
- }
454
- ]
455
- },
456
- "parameters": {
457
- "resolution": "720P",
458
- "ratio": "16:9",
459
- "duration": 10,
460
- "prompt_extend": false,
461
- "watermark": true
462
- }
463
- }
464
- ```
465
-
466
- **Media types**:
467
-
468
- - `reference_image` — reference person/object/background image
469
- - `reference_video` — reference video clip
470
- - `reference_voice` (optional per media) — voice audio for the subject
471
-
472
- ### Async Response
473
-
474
- ```json
475
- {
476
- "output": { "task_id": "abc-123-def", "task_status": "PENDING" },
477
- "request_id": "xxx"
478
- }
479
- ```
480
-
481
- ### Available Models
482
-
483
- - Text-to-Video: `happyhorse-1.0-t2v`
484
- - Image-to-Video: `happyhorse-1.0-i2v`
485
- - Video Edit: `happyhorse-1.0-video-edit`
486
- - Reference-to-Video: `happyhorse-1.0-r2v` (recommended), `wan2.6-r2v`, `wan2.6-r2v-flash`
487
-
488
- ---
489
-
490
- ## 4. Async Task Polling
491
-
492
- **Endpoint**: `GET {baseUrl}/api/v1/tasks/{task_id}`
493
-
494
- Poll until `task_status` becomes `SUCCEEDED` or `FAILED`. Recommended interval: 5-15 seconds.
495
-
496
- ### Response
497
-
498
- ```json
499
- {
500
- "output": {
501
- "task_id": "abc-123-def",
502
- "task_status": "SUCCEEDED",
503
- "video_url": "https://dashscope-result.oss.aliyuncs.com/xxx.mp4",
504
- "task_metrics": { "TOTAL": 1, "SUCCEEDED": 1, "FAILED": 0 },
505
- "submit_time": "2024-01-01T00:00:00Z",
506
- "end_time": "2024-01-01T00:01:00Z"
507
- },
508
- "usage": {},
509
- "request_id": "xxx"
510
- }
511
- ```
512
-
513
- Task statuses: `PENDING` → `RUNNING` → `SUCCEEDED` | `FAILED`.
514
-
515
- Result fields vary by task type:
516
-
517
- - Video: `output.video_url`
518
- - Image (async): `output.choices[].message.content[].image`
519
- - ASR: `output.results[].transcription_url`
520
-
521
- ### curl Example
522
-
523
- ```bash
524
- curl "https://dashscope.aliyuncs.com/api/v1/tasks/abc-123-def" \
525
- -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
526
- -H 'x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]'
527
- ```
528
-
529
- ---
530
-
531
- ## 5. Speech Synthesis (TTS)
532
-
533
- **Endpoint**: `POST {baseUrl}/api/v1/services/aigc/multimodal-generation/generation`
534
-
535
- ### Request Body
536
-
537
- ```json
538
- {
539
- "model": "qwen3-tts-flash",
540
- "input": {
541
- "text": "你好,我是千问",
542
- "voice": "Cherry",
543
- "language_type": "Chinese",
544
- "instructions": "语速较慢,温柔的语调",
545
- "optimize_instructions": true
546
- }
547
- }
548
- ```
549
-
550
- Voices: `Cherry`, `Serena`, `Ethan`, `Chelsie`.
551
-
552
- ### Response (Non-streaming)
553
-
554
- ```json
555
- {
556
- "output": {
557
- "audio": {
558
- "url": "https://dashscope-result.oss.aliyuncs.com/xxx.wav",
559
- "expires_at": "2024-01-02T00:00:00Z"
560
- },
561
- "finish_reason": "stop"
562
- },
563
- "usage": {},
564
- "request_id": "xxx"
565
- }
566
- ```
567
-
568
- ### Streaming Mode
569
-
570
- Add header `X-DashScope-SSE: enable`. Each SSE chunk contains base64 audio data:
571
-
572
- ```json
573
- {
574
- "output": {
575
- "audio": { "data": "base64_pcm_data..." },
576
- "finish_reason": null
577
- }
578
- }
579
- ```
580
-
581
- ### curl Example
582
-
583
- ```bash
584
- curl -X POST "https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation" \
585
- -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
586
- -H "Content-Type: application/json" \
587
- -H 'x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]' \
588
- -d '{
589
- "model": "qwen3-tts-flash",
590
- "input": { "text": "Hello world", "voice": "Cherry" }
591
- }'
592
- ```
593
-
594
- ### Available Models
595
-
596
- - `qwen3-tts-flash` — Fast TTS
597
- - `qwen3-tts-instruct-flash` — Supports instruction-based style control
598
-
599
- ---
600
-
601
- ## 6. Speech Recognition (ASR)
602
-
603
- **Endpoint**: `POST {baseUrl}/api/v1/services/audio/asr/transcription`
604
-
605
- Always uses async mode. Add header `X-DashScope-Async: enable`.
606
-
607
- ### Request Body
608
-
609
- ```json
610
- {
611
- "model": "fun-asr",
612
- "input": { "file_urls": ["https://example.com/audio.wav"] },
613
- "parameters": {
614
- "channel_id": [0],
615
- "language_hints": ["zh"],
616
- "diarization_enabled": false,
617
- "speaker_count": 2,
618
- "vocabulary_id": "vocab-abc123"
619
- }
620
- }
621
- ```
622
-
623
- Supports up to 100 URLs per request (`file_urls` array).
624
-
625
- ### Response (Task Submission)
626
-
627
- ```json
628
- {
629
- "output": { "task_id": "xxx", "task_status": "PENDING" },
630
- "request_id": "xxx"
631
- }
632
- ```
633
-
634
- ### Response (Task Poll — SUCCEEDED)
635
-
636
- ```json
637
- {
638
- "output": {
639
- "task_id": "xxx",
640
- "task_status": "SUCCEEDED",
641
- "results": [
642
- {
643
- "file_url": "https://example.com/audio.wav",
644
- "transcription_url": "https://...",
645
- "subtask_status": "SUCCEEDED"
646
- }
647
- ],
648
- "task_metrics": { "TOTAL": 1, "SUCCEEDED": 1, "FAILED": 0 }
649
- },
650
- "request_id": "xxx"
651
- }
652
- ```
653
-
654
- ### curl Example
655
-
656
- ```bash
657
- curl -X POST "https://dashscope.aliyuncs.com/api/v1/services/audio/asr/transcription" \
658
- -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
659
- -H "Content-Type: application/json" \
660
- -H "X-DashScope-Async: enable" \
661
- -H 'x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]' \
662
- -d '{
663
- "model": "fun-asr",
664
- "input": { "file_urls": ["https://example.com/audio.wav"] },
665
- "parameters": { "language_hints": ["zh"] }
666
- }'
667
- ```
668
-
669
- ---
670
-
671
- ## 7. Application Call (Agent / Workflow)
672
-
673
- **Endpoint**: `POST {baseUrl}/api/v1/apps/{app_id}/completion`
674
-
675
- ### Request Body
676
-
677
- ```json
678
- {
679
- "input": {
680
- "prompt": "你好",
681
- "session_id": "session_xxx",
682
- "image_list": ["https://example.com/photo.png"],
683
- "file_ids": ["file_xxx"],
684
- "biz_params": { "key": "value" }
685
- },
686
- "parameters": {
687
- "has_thoughts": true,
688
- "incremental_output": true,
689
- "rag_options": {
690
- "pipeline_ids": ["pipe_1", "pipe_2"]
691
- },
692
- "memory_id": "mem_xxx"
693
- }
694
- }
695
- ```
696
-
697
- ### Response
698
-
699
- ```json
700
- {
701
- "output": {
702
- "text": "Response text",
703
- "finish_reason": "stop",
704
- "session_id": "session_xxx",
705
- "thoughts": [
706
- {
707
- "thought": "I need to search...",
708
- "action_type": "api",
709
- "action_name": "search",
710
- "action_input": "{}",
711
- "response": "",
712
- "observation": "Search results..."
713
- }
714
- ],
715
- "doc_references": [
716
- {
717
- "index_id": "idx_xxx",
718
- "title": "Doc Title",
719
- "doc_id": "doc_xxx",
720
- "text": "Relevant text snippet"
721
- }
722
- ]
723
- },
724
- "usage": { "models": [{ "model_id": "qwen-plus", "input_tokens": 100, "output_tokens": 50 }] },
725
- "request_id": "xxx"
726
- }
727
- ```
728
-
729
- ### Streaming
730
-
731
- Use SSE. Each chunk has the same structure with incremental `output.text`.
732
-
733
- ### curl Example
734
-
735
- ```bash
736
- curl -X POST "https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion" \
737
- -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
738
- -H "Content-Type: application/json" \
739
- -H 'x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]' \
740
- -d '{
741
- "input": { "prompt": "Hello" },
742
- "parameters": { "incremental_output": true }
743
- }'
744
- ```
745
-
746
- ---
747
-
748
- ## 8. Memory API
749
-
750
- ### 8.1 Add Memory
751
-
752
- **Endpoint**: `POST {baseUrl}/api/v2/apps/memory/add`
753
-
754
- ```json
755
- {
756
- "user_id": "user_001",
757
- "messages": [
758
- { "role": "user", "content": "我喜欢Python编程" },
759
- { "role": "assistant", "content": "好的,已记录" }
760
- ],
761
- "custom_content": "用户喜欢Python编程",
762
- "profile_schema": "schema_xxx",
763
- "memory_library_id": "lib_xxx"
764
- }
765
- ```
766
-
767
- Response: `{ "request_id": "xxx", "memory_ids": ["mem_001"] }`
768
-
769
- ### 8.2 Search Memory
770
-
771
- **Endpoint**: `POST {baseUrl}/api/v2/apps/memory/memory_nodes/search`
772
-
773
- ```json
774
- {
775
- "user_id": "user_001",
776
- "query": "编程偏好",
777
- "top_k": 10,
778
- "memory_library_id": "lib_xxx"
779
- }
780
- ```
781
-
782
- Response: `{ "request_id": "xxx", "memory_nodes": [{ "memory_node_id": "xxx", "content": "...", "created_at": "..." }] }`
783
-
784
- ### 8.3 List Memory Nodes
785
-
786
- **Endpoint**: `GET {baseUrl}/api/v2/apps/memory/memory_nodes?user_id=user_001&page_size=10&page_num=1`
787
-
788
- ### 8.4 Update Memory Node
789
-
790
- **Endpoint**: `PUT {baseUrl}/api/v2/apps/memory/memory_nodes/{node_id}`
791
-
792
- ```json
793
- { "user_id": "user_001", "custom_content": "Updated content" }
794
- ```
795
-
796
- ### 8.5 Delete Memory Node
797
-
798
- **Endpoint**: `DELETE {baseUrl}/api/v2/apps/memory/memory_nodes/{node_id}?user_id=user_001`
799
-
800
- ### 8.6 Profile Schema — Create
801
-
802
- **Endpoint**: `POST {baseUrl}/api/v2/apps/memory/profile_schemas`
803
-
804
- ```json
805
- {
806
- "name": "user_basic",
807
- "description": "Basic user profile",
808
- "attributes": [
809
- { "name": "age", "description": "年龄" },
810
- { "name": "hobby", "description": "爱好" }
811
- ]
812
- }
813
- ```
814
-
815
- ### 8.7 Profile — Get User Profile
816
-
817
- **Endpoint**: `GET {baseUrl}/api/v2/apps/memory/profile_schemas/{schema_id}/profiles?user_id=user_001`
818
-
819
- ---
820
-
821
- ## 9. Knowledge Retrieve (AK/SK Required)
822
-
823
- **Endpoint**: `POST https://bailian.cn-beijing.aliyuncs.com` (Aliyun Model Studio Cloud API, POP style)
824
-
825
- Requires AK/SK V3 ROA signing, NOT Bearer token.
826
-
827
- ### Required Headers
828
-
829
- ```
830
- x-acs-action: Retrieve
831
- x-acs-version: 2023-12-29
832
- Content-Type: application/json
833
- Authorization: ACS3-HMAC-SHA256 Credential=...
834
- x-acs-date: 2024-01-01T00:00:00Z
835
- x-acs-signature-nonce: <uuid>
836
- x-acs-content-sha256: <sha256 of body>
837
- ```
838
-
839
- ### Request Body
840
-
841
- ```json
842
- {
843
- "IndexId": "idx_xxx",
844
- "Query": "如何使用阿里云百炼",
845
- "DenseSimilarityTopK": 100,
846
- "SparseSimilarityTopK": 100,
847
- "RerankTopN": 5,
848
- "Rerank": true,
849
- "TopK": 10
850
- }
851
- ```
852
-
853
- Query parameter: `?WorkspaceId=ws_xxx`
854
-
855
- ### Response
856
-
857
- ```json
858
- {
859
- "Success": true,
860
- "RequestId": "xxx",
861
- "Data": {
862
- "Nodes": [
863
- {
864
- "Text": "Relevant knowledge text...",
865
- "Score": 0.95,
866
- "Metadata": { "doc_name": "guide.pdf", "page": 3 }
867
- }
868
- ]
869
- }
870
- }
871
- ```
872
-
873
- ---
874
-
875
- ## 10. MCP WebSearch
876
-
877
- **Endpoint**: `POST {baseUrl}/api/v1/mcps/WebSearch/mcp`
878
-
879
- Uses JSON-RPC 2.0 over Streamable HTTP (MCP protocol).
880
-
881
- ### Step 1: Initialize
882
-
883
- ```json
884
- {
885
- "jsonrpc": "2.0",
886
- "id": 1,
887
- "method": "initialize",
888
- "params": { "protocolVersion": "2025-03-26", "capabilities": {} }
889
- }
890
- ```
891
-
892
- ### Step 2: Call Tool
893
-
894
- ```json
895
- {
896
- "jsonrpc": "2.0",
897
- "id": 2,
898
- "method": "tools/call",
899
- "params": {
900
- "name": "web-search_search-news",
901
- "arguments": { "query": "阿里云百炼最新功能", "count": 10 }
902
- }
903
- }
904
- ```
905
-
906
- ### Response
907
-
908
- ```json
909
- {
910
- "jsonrpc": "2.0",
911
- "id": 2,
912
- "result": {
913
- "content": [
914
- {
915
- "type": "text",
916
- "text": "{ \"results\": [{ \"title\": \"...\", \"url\": \"...\", \"snippet\": \"...\" }] }"
917
- }
918
- ]
919
- }
920
- }
921
- ```
922
-
923
- Note: Each request must include the `Mcp-Session-Id` header returned from the initialize response.
924
-
925
- ### curl Example
926
-
927
- ```bash
928
- # Initialize
929
- curl -X POST "https://dashscope.aliyuncs.com/api/v1/mcps/WebSearch/mcp" \
930
- -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
931
- -H "Content-Type: application/json" \
932
- -H 'x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]' \
933
- -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{}}}'
934
-
935
- # Search (use Mcp-Session-Id from init response)
936
- curl -X POST "https://dashscope.aliyuncs.com/api/v1/mcps/WebSearch/mcp" \
937
- -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
938
- -H "Content-Type: application/json" \
939
- -H "Mcp-Session-Id: SESSION_ID" \
940
- -H 'x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]' \
941
- -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"web-search_search-news","arguments":{"query":"latest AI news","count":5}}}'
942
- ```
943
-
944
- ---
945
-
946
- ## 11. File Upload (Two-step OSS)
947
-
948
- Upload local files to DashScope temporary storage. Returns `oss://` URL valid for 48 hours.
949
-
950
- ### Step 1: Get Upload Policy
951
-
952
- ```bash
953
- GET https://dashscope.aliyuncs.com/api/v1/uploads?action=getPolicy&model={model}
954
- Authorization: Bearer sk-xxx
955
- x-dashscope-source-config: [{"channel":"bailian-cli","tags":{"t1":"","t2":"public","t3":"skill-doc"}}]
956
- ```
957
-
958
- Response:
959
-
960
- ```json
961
- {
962
- "data": {
963
- "upload_host": "https://xxx.oss-cn-beijing.aliyuncs.com",
964
- "upload_dir": "tmp/xxx",
965
- "oss_access_key_id": "STS.xxx",
966
- "signature": "xxx",
967
- "policy": "base64...",
968
- "x_oss_object_acl": "private",
969
- "x_oss_forbid_overwrite": "true"
970
- }
971
- }
972
- ```
973
-
974
- ### Step 2: Upload to OSS (multipart/form-data)
975
-
976
- ```bash
977
- curl -X POST "{upload_host}" \
978
- -F "OSSAccessKeyId={oss_access_key_id}" \
979
- -F "Signature={signature}" \
980
- -F "policy={policy}" \
981
- -F "x-oss-object-acl={x_oss_object_acl}" \
982
- -F "x-oss-forbid-overwrite={x_oss_forbid_overwrite}" \
983
- -F "key={upload_dir}/{filename}" \
984
- -F "success_action_status=200" \
985
- -F "file=@/path/to/local/file.png"
986
- ```
987
-
988
- Result: `oss://{upload_dir}/{filename}`
989
-
990
- **Important**: When using `oss://` URLs in subsequent API calls, you MUST include the header:
991
-
992
- ```
993
- X-DashScope-OssResourceResolve: enable
994
- ```
995
-
996
- ---
997
-
998
- ## Integration Patterns Summary
999
-
1000
- | Pattern | APIs | Header |
1001
- | ------------------- | ------------------------------------------- | ---------------------------------------- |
1002
- | **Sync Request** | Chat, Image Gen/Edit, TTS, App Call, Memory | Standard |
1003
- | **Async Task** | Video Gen/Edit, ASR (long), Image (legacy) | `X-DashScope-Async: enable` |
1004
- | **Streaming (SSE)** | Chat, TTS, App Call | `stream: true` in body |
1005
- | **Polling** | All async tasks | `GET /api/v1/tasks/{id}` |
1006
- | **File Upload** | Two-step OSS | `X-DashScope-OssResourceResolve: enable` |
1007
- | **MCP Protocol** | WebSearch | JSON-RPC 2.0 + `Mcp-Session-Id` |
1008
- | **AK/SK Signing** | Knowledge Retrieve | ACS3-HMAC-SHA256 |