Large Language Models (LLMs) cannot answer questions about private data. The knowledge retrieval tool retrieves content from a knowledge base and provides it to the LLM. This enables the model to generate more accurate and relevant answers.
Prerequisites
- Obtain an API key and configure it as an environment variable.
-
Create a knowledge base and obtain its ID. You can create a knowledge base in one of the following ways:
- Create in the console: Create a knowledge base on the knowledge base page in the Model Studio console. For more information, see Create and use a knowledge base.
-
Create using the API: You can call the API through the Alibaba Cloud Model Studio software development kit (SDK) to create a knowledge base. For more information, see Knowledge Base API Guide.
Before you create a knowledge base using the API, obtain the workspace ID (
workspace_id) from the Model Studio console. Only two types of knowledge bases are supported: document search and data query. These types are suitable for basic document Q&A pairs and do not support responses with rich text or images.
How to use
You can call the knowledge retrieval feature through the Responses API by adding the file_search tool to the tools parameter and specifying the knowledge base ID in the vector_store_ids parameter.
Before you start, create and use a knowledge base and obtain its ID. Currently, the vector_store_ids parameter supports only one knowledge base ID.
Supported models
- Qwen-Max: Qwen3.8-Max series and Qwen3.7-Max series
- Qwen-Plus: Qwen3.7-Plus series, Qwen3.6-Plus series, and Qwen3.5-Plus series
- Qwen-Flash: Qwen3.7-Flash series, Qwen3.6-Flash series and Qwen3.5-Flash series
- Qwen3.8 open-source series
- Qwen3.6 open-source series (except qwen3.6-27b)
- Qwen3.5 open source series
Getting started
Run the following code to call the knowledge retrieval tool through the Responses API. The tool retrieves relevant content from the specified knowledge base to generate a response.
Replace vector_store_ids in the sample code with your knowledge base ID.
Streaming output
The knowledge retrieval tool performs a semantic search in the knowledge base, which can take some time to process. You can enable streaming output to obtain intermediate results in real time.
Parameters
The file_search tool supports the following parameters:
| Parameter | Required | Description |
|---|---|---|
type | Yes | Must be set to "file_search". |
vector_store_ids | Yes | A list of knowledge base IDs. Currently, you can pass only one knowledge base ID. You can find the knowledge base ID on the knowledge base details page in the Model Studio console or get it when you create a knowledge base using the API.Make sure to pass a valid knowledge base ID. If you pass an empty array or an invalid ID, the knowledge retrieval tool does not work. The model then uses its own knowledge to generate a response and does not return an error. |
Billing
Billing involves the following aspects:
- Model call fees: Content retrieved from the knowledge base is added to the prompt. This increases the number of input tokens for the model. You are charged based on the model's standard pricing. For pricing details, see the Model Studio console.
- Tool call fees: The knowledge base feature is currently free of charge.