全部產品
Search
文件中心

Alibaba Cloud Model Studio:知識檢索

更新時間:Jun 18, 2026

大模型無法回答私人領域問題。知識檢索工具可從知識庫中檢索內容並提供給大模型,使其產生更準確、專業的回答。

使用方式

知識檢索功能通過 Responses API 呼叫。在 tools 參數中添加 file_search 工具,並通過 vector_store_ids 參數指定要檢索的知識庫 ID。

使用前需先建立和使用知識庫並擷取知識庫 ID。當前 vector_store_ids 僅支援傳入一個知識庫 ID。
# 匯入依賴與建立用戶端...
response = client.responses.create(
    model="qwen3.7-plus",
    input="介紹一下阿里雲百鍊X1手機",
    tools=[
        {
            "type": "file_search",
            # 替換為您的知識庫 ID,當前僅支援一個
            "vector_store_ids": ["your_knowledge_base_id"]
        }
    ]
)

print(response.output_text)

支援的模型

  • 千問Plus:Qwen3.7-Plus系列、Qwen3.6-Plus系列、Qwen3.5-Plus系列

  • 千問Flash:Qwen3.6-Flash系列、Qwen3.5-Flash系列

  • Qwen3.6開源系列(qwen3.6-27b除外)

  • Qwen3.5開源系列

僅支援通過 Responses API 呼叫。

前提條件

  1. 擷取API Key配置API Key到環境變數

  2. 已建立知識庫並擷取知識庫 ID。可通過以下方式建立知識庫:

    • 控制台建立:在百鍊控制台的知識庫頁面建立知識庫。詳細步驟請參考建立和使用知識庫

    • API 建立:通過阿里雲百鍊 SDK 調用 API 建立知識庫。詳細步驟請參考知識庫API指南

      通過 API 建立知識庫前,需要先在百鍊控制台擷取業務空間 ID(workspace_id)。僅支援兩類知識庫:文檔搜尋和資料查詢;適用於基礎文檔問答,不支援圖文並茂回複。

    知識庫 ID 可在百鍊控制台的知識庫詳情頁中查看。

快速開始

運行以下代碼,通過 Responses API 呼叫知識檢索工具,在指定的知識庫中檢索相關內容並產生回答。

請將範例程式碼中的 vector_store_ids 替換為您實際的知識庫 ID。
import os
from openai import OpenAI

client = OpenAI(
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"(不建議),
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 以下為新加坡地區配置,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的配置不同。
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)

response = client.responses.create(
    model="qwen3.7-plus",
    input="介紹一下阿里雲百鍊X1手機",
    tools=[
        {
            "type": "file_search",
            # 替換為您的知識庫 ID,當前僅支援一個
            "vector_store_ids": ["your_knowledge_base_id"]
        }
    ]
)

print("[模型回複]")
print(response.output_text)
print(f"\n[Token 用量] 輸入: {response.usage.input_tokens}, 輸出: {response.usage.output_tokens}, 合計: {response.usage.total_tokens}")
import OpenAI from "openai";
import process from 'process';

const openai = new OpenAI({
    // 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey: "sk-xxx",
    apiKey: process.env.DASHSCOPE_API_KEY,
    // 以下為新加坡地區配置,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的配置不同。
    baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});

async function main() {
    const response = await openai.responses.create({
        model: "qwen3.7-plus",
        input: "介紹一下阿里雲百鍊X1手機",
        tools: [
            {
                type: "file_search",
                // 替換為您的知識庫 ID,當前僅支援一個
                vector_store_ids: ["your_knowledge_base_id"]
            }
        ]
    });

    console.log("[模型回複]");
    console.log(response.output_text);

    const usage = response.usage;
    console.log(`\n[Token 用量] 輸入: ${usage.input_tokens}, 輸出: ${usage.output_tokens}, 合計: ${usage.total_tokens}`);
}

main();
# 以下為新加坡地區配置,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的配置不同。
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.7-plus",
    "input": "介紹一下阿里雲百鍊X1手機",
    "tools": [
        {
            "type": "file_search",
            "vector_store_ids": ["your_knowledge_base_id"]
        }
    ]
}'

運行以上代碼可擷取如下回複:

[模型回複]
根據知識庫中的內容,該產品要點包括以下幾個方面:

1. **核心功能**:產品提供了...
2. **適用情境**:適用於...
3. **技術特性**:基於...

...

[Token 用量] 輸入: 1568, 輸出: 1724, 合計: 3292

流式輸出

知識檢索工具需要在知識庫中進行語義搜尋,可能需要一定的處理時間,建議啟用流式輸出,即時擷取中間過程輸出結果。

import os
from openai import OpenAI

client = OpenAI(
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 以下為新加坡地區配置,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的配置不同。
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)

stream = client.responses.create(
    model="qwen3.7-plus",
    input="介紹一下阿里雲百鍊X1手機",
    tools=[
        {
            "type": "file_search",
            # 替換為您的知識庫 ID,當前僅支援一個
            "vector_store_ids": ["your_knowledge_base_id"]
        }
    ],
    stream=True
)

for event in stream:
    # 模型回複開始
    if event.type == "response.content_part.added":
        print("[模型回複]")
    # 流式列印模型回複
    elif event.type == "response.output_text.delta":
        print(event.delta, end="", flush=True)
    # 響應完成,列印 Token 用量
    elif event.type == "response.completed":
        usage = event.response.usage
        print(f"\n\n[Token 用量] 輸入: {usage.input_tokens}, 輸出: {usage.output_tokens}, 合計: {usage.total_tokens}")
import OpenAI from "openai";
import process from 'process';

const openai = new OpenAI({
    // 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey: "sk-xxx",
    apiKey: process.env.DASHSCOPE_API_KEY,
    // 以下為新加坡地區配置,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的配置不同。
    baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});

async function main() {
    const stream = await openai.responses.create({
        model: "qwen3.7-plus",
        input: "介紹一下阿里雲百鍊X1手機",
        tools: [
            {
                type: "file_search",
                // 替換為您的知識庫 ID,當前僅支援一個
                vector_store_ids: ["your_knowledge_base_id"]
            }
        ],
        stream: true
    });

    for await (const event of stream) {
        // 模型回複開始
        if (event.type === "response.content_part.added") {
            console.log("[模型回複]");
        }
        // 流式列印模型回複
        else if (event.type === "response.output_text.delta") {
            process.stdout.write(event.delta);
        }
        // 響應完成,列印 Token 用量
        else if (event.type === "response.completed") {
            const usage = event.response.usage;
            console.log(`\n\n[Token 用量] 輸入: ${usage.input_tokens}, 輸出: ${usage.output_tokens}, 合計: ${usage.total_tokens}`);
        }
    }
}

main();
# 以下為新加坡地區配置,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的配置不同。
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.7-plus",
    "input": "介紹一下阿里雲百鍊X1手機",
    "tools": [
        {
            "type": "file_search",
            "vector_store_ids": ["your_knowledge_base_id"]
        }
    ],
    "stream": true
}'

參數說明

file_search 工具支援以下參數:

參數

必填

說明

type

固定為 "file_search"

vector_store_ids

知識庫 ID 列表。當前僅支援傳入一個知識庫 ID。知識庫 ID 可在百鍊控制台的知識庫詳情頁查看,也可通過 API 建立知識庫時擷取。

請確保傳入有效知識庫 ID。如果傳入空數組或無效的知識庫 ID,知識檢索工具將不會生效,模型會直接使用自身知識回答,且不會返回錯誤提示。

計費說明

計費涉及以下方面:

  • 模型調用費用:知識庫檢索到的內容會拼接到提示詞中,增加模型的輸入 Token,按照模型的標準價格計費。價格詳情請參考百鍊控制台。

  • 工具調用費用知識庫功能目前免費。