cURL
A knowledge-based intelligent Q&A API. The response is delivered via Server-Sent Events (SSE) in a streaming fashion, decomposing the answer generation process into three sequential phases: planning (reasoning about what to search and which knowledge base to query), tool calling (executing retrieval operations), and generation (producing the final answer). Before calling this API, you must first create and publish a Q&A service in the console.
During the tool-calling phase, the model selects from the following tools. The tool name appears in
When File Pre-parsing is enabled in the console, users can upload temporary files during a conversation. File IDs are obtained via the
The platform does not retain conversation state. Each request must include the full
Responses are delivered as SSE events with the following lifecycle:
plan_start → plan_end → tool_calling → tool_return → generation_start → generation_end. The entire sequence plan → tool_calling → tool_return may repeat multiple times (for multi-round retrieval).Basic Information
| Item | Description |
|---|---|
| Invocation method | HTTP API call with SSE streaming output |
| Request URL | POST https://{workspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/knowledge/chat |
| Content-Type | application/json |
| Accept | Recommended: text/event-stream |
| Authentication | Authorization: Bearer <API-Key> |
| Rate limiting | Default per-user limit: 25 QPS |
Execution Phases
Execution phase (group) | Description |
|---|---|
planning | Planning phase — includes both start and end events |
generating | Generation phase — includes both start and end events |
Current step (step) | Description |
|---|---|
planning | In planning phase |
tool_calling | Invoking tools (the group remains planning) |
generating | In generation phase |
Due to model behavior, the
step_change field may be absent. Prioritize using the persistent step field instead. In empty payloads, step, step_change, and group fields may all be missing. A tool_call consists of two distinct events: tool_calling (emitting tool_calls) and tool_return (receiving tool results).Step Transition Events
step | step_change | Event | Description |
|---|---|---|---|
planning | plan_start | Planning starts | step transitions to planning; subsequent content contains planning text |
planning | — (empty) | Planning in progress | content contains streamed planning text |
planning | plan_end | Planning ends | step is about to change; at event emission, step is still planning |
tool_calling | tool_calling | Tool invocation | Emits full tool_calls (including tool name and arguments); includes usage for this round |
tool_calling | tool_return | Tool returns | role is "tool"; content contains summary of returned data; structured result resides in additional_kwargs.extra_json.docs (e.g., retrieved documents for search tools) |
generating | generation_start | Generation starts | Subsequent content contains streamed final answer text |
generating | — (empty) | Generation in progress | content contains streamed answer text |
generating | generation_end | Generation ends | finish_reason is "stop"; includes final usage |
Event Lifecycle
Available Tools
During the tool-calling phase, the model selects from the following tools. The tool name appears in tool_calls[].function.name. The arguments field is a JSON string requiring secondary parsing to extract actual parameters. Tool return values appear in additional_kwargs.extra_json.docs within tool_return frames.
function.name | Tool | arguments | Returns docs[] |
|---|---|---|---|
semantic_search | Knowledge base search | {"query", "target_ids"} | Array of matched document chunks, including full text and multi-dimensional scores |
obtain_file | Retrieve full file content | {"file_id", "max_tokens"} | File-level metadata (including Markdown presigned URL); full content in message.content |
execute_sql | Execute SQL query (NL2SQL) | {"sql", "knowledge_base_id"} | SQL result rows; each row contains _citation_index plus queried column values |
section_browse | Section-level search | {"knowledge_base_id", "file_id", "section_path"} | Section preview (first 300 characters) |
section_peruse | Section deep-read | {"knowledge_base_id", "file_id", "section_path", "max_tokens"} | Full section content |
max_tokens(forobtain_file/section_peruse) is a numeric string, e.g.,"989482".target_ids/knowledge_base_idrefer to knowledge base (pipeline) IDs;section_pathuses>as hierarchical delimiter, e.g.,Open API Documentation>API Invocation Guide.- Multiple tools may be chained in one request (e.g.,
semantic_searchlocates a file →obtain_fileretrieves its full content).
Temporary Files (session_files)
When File Pre-parsing is enabled in the console, users can upload temporary files during a conversation. File IDs are obtained via the addFile API. Full workflow:
- Call
applyFileUploadLeaseto obtain an upload lease and OSS presigned URL - Upload the file to OSS using the provided PUT URL
- Call
addFileto register the file and retrieve itsfileId - Pass that
fileIdinparameters.agent_options.session_filesof this API (up to 10 files allowed)
Client-Side Response Assembly Guidance
| Goal | Assembly Strategy |
|---|---|
| Complete planning text | Concatenate message.content from all frames where step == "planning" |
| Final answer text | Concatenate message.content from all frames where step == "generating" |
| Citation sources | Extract from additional_kwargs.extra_json.docs in tool_return frames; match _citation_index to corresponding positions in answer text |
| Usage statistics | Use top-level usage in generation_end frame (final answer usage); retrieval usage appears in respective tool_calling frames |
Multi-turn Conversations
The platform does not retain conversation state. Each request must include the full messages history. We recommend limiting history length (e.g., last 10 turns) to avoid exceeding the model’s context window.Authorizations
string
header
required
DashScope API Key,在控制台 API Key 页面 获取。