Retrieve Chunks from a Knowledge Base / Test Retrieval
Searches a knowledge base and returns the chunks most relevant to the query, for both production retrieval and test retrieval.
1 min read
post/datasets/{dataset_id}/retrieve
Request example
curl --request POST \
--url https://api.dify.ai/v1/datasets/{dataset_id}/retrieve \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"attachment_ids": [
"<string>"
],
"external_retrieval_model": {
"score_threshold": 1,
"score_threshold_enabled": true,
"top_k": 1
},
"query": "<string>",
"retrieval_model": {
"metadata_filtering_conditions": {
"conditions": [
{
"comparison_operator": "contains",
"name": "<string>",
"value": "<string>"
}
],
"logical_operator": "and"
},
"reranking_enable": true,
"reranking_mode": "reranking_model",
"reranking_model": {
"reranking_model_name": "<string>",
"reranking_provider_name": "<string>"
},
"score_threshold": 1,
"score_threshold_enabled": true,
"search_method": "keyword_search",
"top_k": 1,
"weights": {
"keyword_setting": {
"keyword_weight": 1
},
"vector_setting": {
"embedding_model_name": "<string>",
"embedding_provider_name": "<string>",
"vector_weight": 1
},
"weight_type": "semantic_first"
}
}
}'import requests
url = "https://api.dify.ai/v1/datasets/{dataset_id}/retrieve"
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json",
}
payload = {
"attachment_ids": [
"<string>"
],
"external_retrieval_model": {
"score_threshold": 1,
"score_threshold_enabled": True,
"top_k": 1
},
"query": "<string>",
"retrieval_model": {
"metadata_filtering_conditions": {
"conditions": [
{
"comparison_operator": "contains",
"name": "<string>",
"value": "<string>"
}
],
"logical_operator": "and"
},
"reranking_enable": True,
"reranking_mode": "reranking_model",
"reranking_model": {
"reranking_model_name": "<string>",
"reranking_provider_name": "<string>"
},
"score_threshold": 1,
"score_threshold_enabled": True,
"search_method": "keyword_search",
"top_k": 1,
"weights": {
"keyword_setting": {
"keyword_weight": 1
},
"vector_setting": {
"embedding_model_name": "<string>",
"embedding_provider_name": "<string>",
"vector_weight": 1
},
"weight_type": "semantic_first"
}
}
}
response = requests.request("POST", url, headers=headers, json=payload)
print(response.text)const response = await fetch('https://api.dify.ai/v1/datasets/{dataset_id}/retrieve', {
method: 'POST',
headers: {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"attachment_ids": [
"<string>"
],
"external_retrieval_model": {
"score_threshold": 1,
"score_threshold_enabled": true,
"top_k": 1
},
"query": "<string>",
"retrieval_model": {
"metadata_filtering_conditions": {
"conditions": [
{
"comparison_operator": "contains",
"name": "<string>",
"value": "<string>"
}
],
"logical_operator": "and"
},
"reranking_enable": true,
"reranking_mode": "reranking_model",
"reranking_model": {
"reranking_model_name": "<string>",
"reranking_provider_name": "<string>"
},
"score_threshold": 1,
"score_threshold_enabled": true,
"search_method": "keyword_search",
"top_k": 1,
"weights": {
"keyword_setting": {
"keyword_weight": 1
},
"vector_setting": {
"embedding_model_name": "<string>",
"embedding_provider_name": "<string>",
"vector_weight": 1
},
"weight_type": "semantic_first"
}
}
})
});
const data = await response.json();
console.log(data);<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.dify.ai/v1/datasets/{dataset_id}/retrieve',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"attachment_ids": [
"<string>"
],
"external_retrieval_model": {
"score_threshold": 1,
"score_threshold_enabled": true,
"top_k": 1
},
"query": "<string>",
"retrieval_model": {
"metadata_filtering_conditions": {
"conditions": [
{
"comparison_operator": "contains",
"name": "<string>",
"value": "<string>"
}
],
"logical_operator": "and"
},
"reranking_enable": true,
"reranking_mode": "reranking_model",
"reranking_model": {
"reranking_model_name": "<string>",
"reranking_provider_name": "<string>"
},
"score_threshold": 1,
"score_threshold_enabled": true,
"search_method": "keyword_search",
"top_k": 1,
"weights": {
"keyword_setting": {
"keyword_weight": 1
},
"vector_setting": {
"embedding_model_name": "<string>",
"embedding_provider_name": "<string>",
"vector_weight": 1
},
"weight_type": "semantic_first"
}
}
}',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer <token>',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
url := "https://api.dify.ai/v1/datasets/{dataset_id}/retrieve"
payload := strings.NewReader("{\n \"attachment_ids\": [\n \"<string>\"\n ],\n \"external_retrieval_model\": {\n \"score_threshold\": 1,\n \"score_threshold_enabled\": true,\n \"top_k\": 1\n },\n \"query\": \"<string>\",\n \"retrieval_model\": {\n \"metadata_filtering_conditions\": {\n \"conditions\": [\n {\n \"comparison_operator\": \"contains\",\n \"name\": \"<string>\",\n \"value\": \"<string>\"\n }\n ],\n \"logical_operator\": \"and\"\n },\n \"reranking_enable\": true,\n \"reranking_mode\": \"reranking_model\",\n \"reranking_model\": {\n \"reranking_model_name\": \"<string>\",\n \"reranking_provider_name\": \"<string>\"\n },\n \"score_threshold\": 1,\n \"score_threshold_enabled\": true,\n \"search_method\": \"keyword_search\",\n \"top_k\": 1,\n \"weights\": {\n \"keyword_setting\": {\n \"keyword_weight\": 1\n },\n \"vector_setting\": {\n \"embedding_model_name\": \"<string>\",\n \"embedding_provider_name\": \"<string>\",\n \"vector_weight\": 1\n },\n \"weight_type\": \"semantic_first\"\n }\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.dify.ai/v1/datasets/{dataset_id}/retrieve"))
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\n \"attachment_ids\": [\n \"<string>\"\n ],\n \"external_retrieval_model\": {\n \"score_threshold\": 1,\n \"score_threshold_enabled\": true,\n \"top_k\": 1\n },\n \"query\": \"<string>\",\n \"retrieval_model\": {\n \"metadata_filtering_conditions\": {\n \"conditions\": [\n {\n \"comparison_operator\": \"contains\",\n \"name\": \"<string>\",\n \"value\": \"<string>\"\n }\n ],\n \"logical_operator\": \"and\"\n },\n \"reranking_enable\": true,\n \"reranking_mode\": \"reranking_model\",\n \"reranking_model\": {\n \"reranking_model_name\": \"<string>\",\n \"reranking_provider_name\": \"<string>\"\n },\n \"score_threshold\": 1,\n \"score_threshold_enabled\": true,\n \"search_method\": \"keyword_search\",\n \"top_k\": 1,\n \"weights\": {\n \"keyword_setting\": {\n \"keyword_weight\": 1\n },\n \"vector_setting\": {\n \"embedding_model_name\": \"<string>\",\n \"embedding_provider_name\": \"<string>\",\n \"vector_weight\": 1\n },\n \"weight_type\": \"semantic_first\"\n }\n }\n}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());require 'uri'
require 'net/http'
url = URI('https://api.dify.ai/v1/datasets/{dataset_id}/retrieve')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = url.scheme == 'https'
request = Net::HTTP::Post.new(url)
request['Authorization'] = 'Bearer <token>'
request['Content-Type'] = 'application/json'
request.body = '{
"attachment_ids": [
"<string>"
],
"external_retrieval_model": {
"score_threshold": 1,
"score_threshold_enabled": true,
"top_k": 1
},
"query": "<string>",
"retrieval_model": {
"metadata_filtering_conditions": {
"conditions": [
{
"comparison_operator": "contains",
"name": "<string>",
"value": "<string>"
}
],
"logical_operator": "and"
},
"reranking_enable": true,
"reranking_mode": "reranking_model",
"reranking_model": {
"reranking_model_name": "<string>",
"reranking_provider_name": "<string>"
},
"score_threshold": 1,
"score_threshold_enabled": true,
"search_method": "keyword_search",
"top_k": 1,
"weights": {
"keyword_setting": {
"keyword_weight": 1
},
"vector_setting": {
"embedding_model_name": "<string>",
"embedding_provider_name": "<string>",
"vector_weight": 1
},
"weight_type": "semantic_first"
}
}
}'
response = http.request(request)
puts response.read_bodyResponse
{
"query": {
"content": "What is Dify?"
},
"records": [
{
"child_chunks": [],
"files": [],
"score": 0.92,
"segment": {
"answer": "",
"completed_at": 1741267200,
"content": "Dify is an open-source LLM app development platform.",
"created_at": 1741267200,
"created_by": "ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4",
"disabled_at": null,
"disabled_by": null,
"document": {
"data_source_type": "upload_file",
"doc_metadata": null,
"doc_type": null,
"id": "a8e0e5b5-78c6-4130-a5ce-25feb0e0b4ac",
"name": "guide.txt"
},
"document_id": "a8e0e5b5-78c6-4130-a5ce-25feb0e0b4ac",
"enabled": true,
"error": null,
"hit_count": 1,
"id": "f3d1c7be-9f3a-40d8-8eb8-3a1ef9c3f2c1",
"index_node_hash": "abc123def456",
"index_node_id": "a1b2c3d4-e5f6-7890-abcd-000000000001",
"indexing_at": 1741267200,
"keywords": [
"dify",
"platform",
"llm"
],
"position": 1,
"sign_content": "",
"status": "completed",
"stopped_at": null,
"tokens": 12,
"word_count": 9
},
"summary": null,
"tsne_position": null
}
]
}{
"code": "completion_request_error",
"message": "Completion request failed.",
"status": 400
}{
"code": "dataset_not_initialized",
"message": "The dataset is still being initialized or indexing. Please wait a moment.",
"status": 400
}{
"code": "invalid_param",
"message": "Invalid parameter value.",
"status": 400
}{
"code": "model_currently_not_support",
"message": "Dify Hosted OpenAI trial currently not support the GPT-4 model.",
"status": 400
}{
"code": "provider_not_initialize",
"message": "No valid model provider credentials found. Please go to Settings -> Model Provider to complete your provider credentials.",
"status": 400
}{
"code": "provider_quota_exceeded",
"message": "Your quota for Dify Hosted Model Provider has been exhausted. Please go to Settings -> Model Provider to complete your own provider credentials.",
"status": 400
}{
"code": "forbidden",
"message": "Dataset api access is not enabled.",
"status": 403
}{
"code": "forbidden",
"message": "Sorry, you have reached the knowledge base request rate limit of your subscription.",
"status": 403
}{
"code": "forbidden",
"message": "The API key is not authorized to access this knowledge base.",
"status": 403
}{
"code": "not_found",
"message": "Dataset not found.",
"status": 404
}{
"code": "internal_server_error",
"message": "An internal error occurred.",
"status": 500
}post/datasets/{dataset_id}/retrieve
Authorization
Every request authenticates with an API key: Authorization: Bearer {API_KEY}. App endpoints take an app API key; knowledge endpoints take a knowledge base API key (Get Started).
Keep keys server-side; never embed them in client code. Requests with a missing or invalid key fail with HTTP 401 (unauthorized).
Path parameters
Knowledge base ID, from List Knowledge Bases. For a scoped key, copy the ID from the Dify URL.
Body
application/jsonList of attachment IDs to include in the retrieval context.
Retrieval settings for external knowledge bases.
Show child attributesHide child attributes
Restrict retrieval to chunks whose document metadata matches the given conditions. Conditions are evaluated server-side against document metadata fields.
Show child attributesHide child attributes
List of metadata conditions to evaluate.
Show child attributesHide child attributes
Comparison to apply, by metadata type:
- String or array metadata:
contains,not contains,start with,end with,is,is not,empty,not empty,in,not in - Numeric metadata:
=,≠,>,<,≥,≤ - Time metadata:
before,after
Options:
containsnot containsstart withend withisis notemptynot emptyinnot in=≠><≥≤beforeafterMetadata field name to compare against.
Value to compare against. Type depends on
comparison_operator: string for most string operators, array of strings for in and not in, number for numeric operators, and omitted for empty and not empty.Whether reranking is enabled.
Reranking mode. Required when
reranking_enable is true.Options:
reranking_modelweighted_scoreReranking model configuration.
Show child attributesHide child attributes
Name of the reranking model.
Reranking model provider identifier, formatted as organization/plugin_name/provider_name (e.g. langgenius/cohere/cohere). A bare name like cohere expands to langgenius/<name>/<name> and works only for langgenius-published plugins.
Get valid values from the provider field of Get Available Models with model_type=rerank.
Minimum similarity score for results. Only effective when
score_threshold_enabled is true.Whether score threshold filtering is enabled.
Search method used for retrieval.
Options:
keyword_searchsemantic_searchfull_text_searchhybrid_searchMaximum number of results to return.
Weight configuration for hybrid search.
Show child attributesHide child attributes
Keyword search weight settings.
Show child attributesHide child attributes
Weight assigned to keyword search results.
Semantic search weight settings.
Strategy for balancing semantic and keyword search weights.
Options:
semantic_firstkeyword_firstcustomizedResponses
application/json
Retrieval results.
The original query object.
Show child attributesHide child attributes
The query text.
List of matched retrieval records.
Show child attributesHide child attributes
Matched child chunks within the chunk, if using hierarchical indexing.
Files attached to this chunk.
Similarity score.
Matched chunk from the knowledge base.
Show child attributesHide child attributes
Answer content, used in Q&A mode documents.
Timestamp when indexing completed.
null if not yet completed.Text content of the chunk.
Creation timestamp (Unix epoch in seconds).
ID of the user who created the chunk.
Timestamp when the chunk was disabled.
null if enabled.ID of the user who disabled the chunk.
null if enabled.Parent document information for the matched chunk.
Show child attributesHide child attributes
ID of the document this chunk belongs to.
Whether the chunk is enabled for retrieval.
Error message if indexing failed.
null when no error.Number of times this chunk has been matched in retrieval queries.
Unique identifier of the chunk.
Hash of the indexed content, used to detect changes.
ID of the index node in the vector store.
Timestamp when indexing started.
null if not yet started.Keywords associated with this chunk for keyword-based retrieval.
Position of the chunk within the document.
Signed content hash for integrity verification.
Indexing status of the chunk.
Timestamp when indexing was stopped.
null if not stopped.Token count of the chunk content.
Word count of the chunk content.
Summary content if retrieved via summary index.
t-SNE visualization position.