大模型無法回答私人領域問題。知識檢索工具可從知識庫中檢索內容並提供給大模型,使其產生更準確、專業的回答。
使用方式
知識檢索功能通過 Responses API 呼叫。在 tools 參數中添加 file_search 工具,並通過 vector_store_ids 參數指定要檢索的知識庫 ID。
使用前需先建立和使用知識庫並擷取知識庫 ID。當前 vector_store_ids 僅支援傳入一個知識庫 ID。# 匯入依賴與建立用戶端...
response = client.responses.create(
model="qwen3.5-plus",
input="介紹一下阿里雲百鍊X1手機",
tools=[
{
"type": "file_search",
# 替換為您的知識庫 ID,當前僅支援一個
"vector_store_ids": ["your_knowledge_base_id"]
}
]
)
print(response.output_text)支援的模型
千問Plus:
qwen3.5-plus、qwen3.5-plus-2026-02-15千問Flash:
qwen3.5-flash、qwen3.5-flash-2026-02-23千問開源:
qwen3.5-397b-a17b、qwen3.5-122b-a10b、qwen3.5-27b、qwen3.5-35b-a3b。僅支援通過 Responses API 呼叫。
前提條件
快速開始
運行以下代碼,通過 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"),
base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
)
response = client.responses.create(
model="qwen3.5-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,
baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});
async function main() {
const response = await openai.responses.create({
model: "qwen3.5-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();curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-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"),
base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
)
stream = client.responses.create(
model="qwen3.5-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,
baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});
async function main() {
const stream = await openai.responses.create({
model: "qwen3.5-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();curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-plus",
"input": "介紹一下阿里雲百鍊X1手機",
"tools": [
{
"type": "file_search",
"vector_store_ids": ["your_knowledge_base_id"]
}
],
"stream": true
}'參數說明
file_search 工具支援以下參數:
參數 | 必填 | 說明 |
| 是 | 固定為 |
| 是 | 知識庫 ID 列表。當前僅支援傳入一個知識庫 ID。知識庫 ID 可在百鍊控制台的知識庫詳情頁查看,也可通過 API 建立知識庫時擷取。 |
計費說明
計費涉及以下方面:
模型調用費用:知識庫檢索到的內容會拼接到提示詞中,增加模型的輸入 Token,按照模型的標準價格計費。價格詳情請參考模型列表。
工具調用費用:知識庫功能目前免費。