Skip to main content

Retrieve

Retrieves relevant text chunks from a knowledge base using vector and keyword search.

Operation description

  • How to call: This page describes the OpenAPI Retrieve operation POST /{WorkspaceId}/index/retrieve. You can call it through the latest Alibaba Cloud Model Studio SDK with an Alibaba Cloud AccessKey. Spring AI Alibaba uses an Alibaba Cloud Model Studio API Key and a separate DashScope Pipeline Retrieve path, and does not use the OpenAPI request syntax described on this page.
  • Required permissions:
    • RAM user (sub-account): A RAM user must first obtain the API permissions for Alibaba Cloud Model Studio (you can use the AliyunBailianDataFullAccess policy, which includes the sfm:Retrieve permission required by this operation) and join a workspace before calling this operation.
    • Alibaba Cloud account (primary account): This account has the required permissions by default and can call the operation directly.
  • Response latency: Because the operation involves complex retrieval and matching, the response time can be long. We recommend that you set appropriate timeout and retry strategies for requests.
  • Idempotency: This operation is idempotent.

Try it now

You can run this operation directly in OpenAPI Explorer, which saves you the trouble of calculating signatures. After the operation runs successfully, OpenAPI Explorer can automatically generate SDK code examples.

RAM authorization

The following table lists the authorization information for the API. You can use it in the Action element of a RAM permission policy statement to grant RAM users or RAM roles the permission to call this API. Details:
  • Action: the specific permission point.
  • Access level: the access level of each action. Valid values: Write, Read, or List.
  • Resource type: the resource types that the operation supports for authorization. Details:
    • Required resource types are marked with a leading *.
    • For operations that do not support resource-level authorization, All Resources is used.
  • Condition key: the condition keys defined by the cloud service itself.
  • Dependent action: the other permissions required to successfully run the operation. The caller must also have the permissions for the dependent actions for the operation to succeed.

Action

Access level

Resource type

Condition key

Dependent action

sfm:Retrieve

none

*All Resources

*

NoneNone

Request syntax

POST /{WorkspaceId}/index/retrieve HTTP/1.1

Path parameters

Name

Type

Required

Description

Example value

WorkspaceId

string

Yes

The ID of the workspace to which the knowledge base belongs. For more information, see how to use workspaces.

llm-3shx2gu255oqxxxx

Request parameters

Name

Type

Required

Description

Example value

Query

string

No

The input text (the original input prompt). The length and characters of Query are not limited.

Introduction to Alibaba Cloud Model Studio

DenseSimilarityTopK

integer

No

Vector retrieval Top K. The system generates a vector for the input text and retrieves the K text chunks whose vector representations are most similar to it from the knowledge base. Valid range of K: [0-100]. The sum of DenseSimilarityTopK and SparseSimilarityTopK must be less than or equal to 200.

Default value: 100.

100

EnableReranking

boolean

No

Specifies whether to enable reranking. For more information, see Knowledge base. Valid values:

  • true: Enabled.

  • false: Disabled.

Default value: true.

Valid values:

  • true :

    Enabled

  • false :

    Disabled

true

EnableRewrite

boolean

No

Specifies whether to enable conversational query rewriting. Valid values:

  • true: Enabled.

  • false: Disabled.

Default value: false.

Valid values:

  • true :

    Enabled

  • false :

    Disabled

false

Rerank

array<object>

No

The reranking configuration.

object

No

The reranking configuration object.

ModelName

string

No

The reranking model to use. The specified value overrides the reranking model selected when the knowledge base was created. Valid values:

  • qwen3-rerank-hybrid: Reranks by using qwen3-rerank (hybrid).

  • qwen3-rerank: Reranks by using qwen3-rerank.

  • gte-rerank-hybrid: Reranks by using gte-rerank (hybrid).

  • gte-rerank: Reranks by using gte-rerank.

Default value: empty, which means the reranking model selected when the knowledge base was created is used.

If you only need semantic ranking, you can use qwen3-rerank. If you need both semantic ranking and text matching features to ensure relevance, we recommend qwen3-rerank-hybrid.

gte-rerank-hybrid and gte-rerank will no longer be updated and are not recommended.

Valid values:

  • gte-rerank-hybrid :

    Reranks by using gte-rerank (hybrid).

  • gte-rerank :

    Reranks by using gte-rerank.

gte-rerank-hybrid

RerankMode

string

No

Specifies the instruction mode of the reranking model, which determines its scoring preference.

Valid values:

  • qa: (Default) Q&A mode. The model tends to assign higher scores to candidates that can directly answer the query. We recommend this mode for Q&A scenarios.

  • similar: Similarity mode. The model tends to assign higher scores to candidates highly consistent with the query content. We recommend this mode for matching retrieval scenarios.

  • custom: Custom mode. The ranking behavior of the model is determined by the instructions in the rerank_instruct parameter.

Valid values:

  • similar: Similarity mode. :

    similar: Similarity mode.

  • custom: Custom mode. :

    custom: Custom mode.

  • qa: (Default) Q&A mode. :

    qa: (Default) Q&A mode.

qa

RerankInstruct

string

No

Provides a natural language instruction to fine-tune the behavior of the reranking model.

This parameter takes effect only when rerank_mode is set to "custom".

RerankMinScore

number

No

The similarity threshold. It specifies the minimum similarity score of text chunks that can be recalled, and is used to filter the text chunks returned by the Rank model. That is, only text chunks with a score higher than this value are recalled. Valid range: [0.01-1.00]. This parameter takes precedence over the similarity threshold configured for the knowledge base.

If no specific value is specified, the similarity threshold configured for the knowledge base is used by default.

0.20

RerankTopN

integer

No

The top N results returned after reranking. Valid range: [1-20]. Default value: 5.

5

Rewrite

array<object>

No

The configuration for conversational query rewriting.

object

No

The configuration object for conversational query rewriting.

ModelName

string

No

The name of the model for conversational query rewriting. It automatically adjusts the original input prompt (user question) based on the conversation context to improve retrieval. Valid values:

  • conv-rewrite-qwen-1.8b: the conv-rewrite-qwen-1.8b model (currently the only supported model)

Default value: empty, in which case the conv-rewrite-qwen-1.8b model is used.

Valid values:

  • conv-rewrite-qwen-1.8b :

    The conv-rewrite-qwen-1.8b model

conv-rewrite-qwen-1.8b

SparseSimilarityTopK

integer

No

Keyword retrieval Top K, that is, retrieving chunks from the knowledge base whose keywords exactly match those of the input text. It helps you filter out irrelevant text chunks and provides more accurate results. Valid range: [0-100]. The sum of DenseSimilarityTopK and SparseSimilarityTopK must be less than or equal to 200.

Default value: 100.

100

IndexId

string

Yes

The knowledge base ID, that is, the Data.Id returned by the CreateIndex operation.

  • Make sure that the corresponding knowledge base has been created and not deleted.

5pwe0mxxxx

SaveRetrieverHistory

boolean

No

Specifies whether to save the test data of historical text chunk recall. Valid values:

  • true: Saved.

  • false: Not saved.

Default value: false.

false

SearchFilters

array<object>

No

You can use SearchFilters to set personalized retrieval conditions (such as tags) to filter the semantic retrieval results and exclude information irrelevant to the query. Condition objects in the array are combined with AND. Multiple retrieval fields within the same object use AND by default; place _operator in the same object as the retrieval fields and set it to OR (or is also supported) to match any of the field conditions, or set it to AND to require all field conditions. The filtering logic takes effect when the is_displayed_chunk_content parameter is set to true. For more information, see SearchFilters for a knowledge base.

object

No

The search condition object.

string

No

Images

array

No

You can pass image URLs in the query.

string

No

When you retrieve from an image Q&A knowledge base, you can pass image URLs. If the knowledge base contains an image index, the system converts the input image into a vector and retrieves relevant records; if no image index exists, the input image is not used for retrieval.

This field is not supported for document search, data query, or audio/video search knowledge bases (passing it in has no effect).

Make sure that the link is publicly accessible and points to a valid image file. Example: https://example.com/downloads/pic.jpg

https://example.com/downloads/pic.jpg

QueryHistory

array<object>

No

Conversational query rewriting lets you pass in the conversation history that you manage. It takes effect only when EnableRewrite=true (otherwise it has no effect even if passed in).

object

No

role

string

No

The role.

Valid values:

  • user: With this role, content represents the text that you enter for the Alibaba Cloud Model Studio application.

  • assistant: With this role, content represents the reply from the Alibaba Cloud Model Studio application.

user

content

string

No

The question or answer content corresponding to the role.

Represents a piece of text.

Extra

object

No

uniqueId

string

No

For example, the following SearchFilters filters chunks whose document name is 长上下文Qwen-Long or whose chunk title is 返回结果. Replace the fields and values with the fields and values that are actually searchable in the knowledge base:
[
  {
    "_operator": "OR",
    "doc_name": "长上下文Qwen-Long",
    "title": "返回结果"
  }
]
This OpenAPI Retrieve operation and its SDKs use maps with string values for SearchFilters and do not support nested _conditions. If you pass in a nested structure, the request may still return a successful response, but it does not filter by the nested conditions. To set multiple OR conditions for the same field, use a RAG API JSON request body, or call through the /api/v1/indices/pipeline/{pipeline_id}/retrieve path of the Spring AI Alibaba DashScope SDK. Both invocation methods support _operator and nested _conditions. For syntax and examples, see SearchFilters for a knowledge base.

Response elements

Name

Type

Description

Example value

object

Code

string

The error code.

Index.InvalidParameter

Data

object

The business data fields of the operation.

Nodes

array<object>

The list of matched text chunks.

object

The text chunk object.

Metadata

any

The metadata map of the text chunk.

For document search knowledge bases, the file_path field in the metadata map is meaningless. Do not use it in your business code.

When you retrieve from a document search knowledge base, if a text chunk contains an image, the image is exposed through the image_url field in the metadata map, with an expiration time.

When you retrieve from an audio/video search knowledge base, if a text chunk contains audio, the audio is exposed through the audio_url field in the metadata map, with an expiration time.

When you retrieve from an audio/video search knowledge base, if a text chunk contains video, the video is exposed through the video_url field in the metadata map, with an expiration time.

{ "parent": "", "file_path": "https://***", "image_url": [ "http://***" ], "nid": "***", "title": "Alibaba Cloud Model Studio documentation", "doc_id": "doc_***", "content": "Alibaba Cloud Model Studio is a one-stop large model development platform built on Tongyi large models, industry large models, and third-party large models. It provides enterprise customers and individual developers with complete model service tools and full-link application development suites, pre-configured rich capability plugins, and convenient integration methods such as APIs and SDKs to efficiently build large model applications", "workspace_id": "ws_***", "hier_title": "Alibaba Cloud Model Studio documentation", "doc_name": "Introduction to Alibaba Cloud Model Studio documentation.pdf", "pipeline_id": "rhd***", "_id": "ws_***" }

Score

number

The similarity score of the text chunk. Valid range: [0-1].

0.3

Text

string

The content of the text chunk.

Alibaba Cloud Model Studio is a one-stop large model development platform built on Tongyi large models, industry large models, and third-party large models. It provides enterprise customers and individual developers with complete model service tools and full-link application development suites, pre-configured rich capability plugins, and convenient integration methods such as APIs and SDKs to efficiently build large model applications.

Message

string

The error message.

Required parameter(%s) missing or invalid, please check the request parameters.

RequestId

string

The request ID.

17204B98-7734-4F9A-8464-2446A84821CA

Status

string

The status code returned by the operation.

200

Success

boolean

Indicates whether the operation call is successful. Valid values:

  • true: Succeeded.

  • false: Failed.

true

Examples

Success response JSON format
{
  "Code": "Index.InvalidParameter",
  "Data": {
    "Nodes": [
      {
        "Metadata": "{\n  \"parent\": \"\",\n  \"file_path\": \"https://***\",\n  \"image_url\": [\n    \"http://***\"\n  ],\n  \"nid\": \"***\",\n  \"title\": \"阿里云百炼文档\",\n  \"doc_id\": \"doc_***\",\n  \"content\": \"阿里云百炼是基于通义大模型、行业大模型以及三方大模型的一站式大模型开发平台。面向企业客户和个人开发者,提供完整的模型服务工具和全链路应用开发套件,预置丰富的能力插件,提供API及SDK等便捷的集成方式,高效完成大模型应用构建\",\n  \"workspace_id\": \"ws_***\",\n  \"hier_title\": \"阿里云百炼文档\",\n  \"doc_name\": \"阿里云百炼文档介绍.pdpf\",\n  \"pipeline_id\": \"rhd***\",\n  \"_id\": \"ws_***\"\n}",
        "Score": 0.3,
        "Text": "阿里云百炼是基于通义大模型、行业大模型以及三方大模型的一站式大模型开发平台。面向企业客户和个人开发者,提供完整的模型服务工具和全链路应用开发套件,预置丰富的能力插件,提供API及SDK等便捷的集成方式,高效完成大模型应用构建。"
      }
    ]
  },
  "Message": "Required parameter(%s) missing or invalid, please check the request parameters.",
  "RequestId": "17204B98-7734-4F9A-8464-2446A84821CA",
  "Status": "200",
  "Success": true
}

Error codes

Visit the Error Center to view more error codes.

Release notes

For more information, see change details.