从知识库检索分段 / 测试检索
搜索知识库,返回与查询最相关的分段,可用于生产环境检索和测试检索。
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
Path parameters
知识库 ID,通过 获取知识库列表 获取。使用限定范围的密钥时,从 Dify 页面 URL 复制 ID。
Body
application/json要包含在检索上下文中的附件 ID 列表。
外部知识库的检索设置。
Show child attributesHide child attributes
仅检索文档元数据匹配指定条件的分段。条件由服务端基于文档元数据字段评估。
Show child attributesHide child attributes
要评估的元数据条件列表。
Show child attributesHide child attributes
按元数据类型选择要应用的比较方式:
- 字符串或数组类型元数据:
contains、not contains、start with、end with、is、is not、empty、not empty、in、not in - 数值类型元数据:
=、≠、>、<、≥、≤ - 时间类型元数据:
before、after
Options:
containsnot containsstart withend withisis notemptynot emptyinnot in=≠><≥≤beforeafter用于比较的元数据字段名称。
用于比较的值。类型取决于
comparison_operator:大多数字符串运算符使用字符串;in 和 not in 使用字符串数组;数值运算符使用数字;empty 和 not empty 时省略。是否启用重排序。
重排序模式。当
reranking_enable 为 true 时必填。Options:
reranking_modelweighted_score重排序模型配置。
Show child attributesHide child attributes
结果的最低相关性分数。仅在
score_threshold_enabled 为 true 时生效。是否启用分数阈值过滤。
用于检索的搜索方法。
Options:
keyword_searchsemantic_searchfull_text_searchhybrid_search返回的最大结果数。
混合搜索的权重配置。
Show child attributesHide child attributes
关键词搜索权重设置。
Show child attributesHide child attributes
分配给关键词搜索结果的权重。
语义搜索权重设置。
Responses
application/json
检索结果。
匹配的检索记录列表。
Show child attributesHide child attributes
使用分层索引时,分段内匹配的子分段。
附加到该分段的文件。
相关性得分。
从知识库中匹配的分段。
Show child attributesHide child attributes
回答内容,用于问答模式文档。
索引完成的时间戳。尚未完成时为
null。分段的文本内容。
创建时间戳(Unix 纪元,单位为秒)。
创建该分段的用户 ID。
分段被禁用的时间戳。启用时为
null。禁用该分段的用户 ID。启用时为
null。匹配分段的父文档信息。
该分段所属文档的 ID。
该分段是否启用检索。
索引失败时的错误消息。无错误时为
null。该分段在检索查询中被匹配的次数。
分段的唯一标识符。
索引内容的哈希值,用于检测变更。
向量存储中索引节点的 ID。
索引开始的时间戳。尚未开始时为
null。与该分段关联的关键词,用于基于关键词的检索。
分段在文档中的位置。
用于完整性验证的签名内容哈希。
分段的索引状态。
索引停止的时间戳。未停止时为
null。分段内容的令牌数。
分段内容的字数。
通过摘要索引检索到的摘要内容。
t-SNE 可视化位置。