向文档添加分段
在文档中创建一个或多个分段。
1 min read
post/datasets/{dataset_id}/documents/{document_id}/segments
Request example
curl --request POST \
--url https://api.dify.ai/v1/datasets/{dataset_id}/documents/{document_id}/segments \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"segments": [
{
"answer": "<string>",
"attachment_ids": [
"<string>"
],
"content": "<string>",
"keywords": [
"<string>"
]
}
]
}'import requests
url = "https://api.dify.ai/v1/datasets/{dataset_id}/documents/{document_id}/segments"
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json",
}
payload = {
"segments": [
{
"answer": "<string>",
"attachment_ids": [
"<string>"
],
"content": "<string>",
"keywords": [
"<string>"
]
}
]
}
response = requests.request("POST", url, headers=headers, json=payload)
print(response.text)const response = await fetch('https://api.dify.ai/v1/datasets/{dataset_id}/documents/{document_id}/segments', {
method: 'POST',
headers: {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"segments": [
{
"answer": "<string>",
"attachment_ids": [
"<string>"
],
"content": "<string>",
"keywords": [
"<string>"
]
}
]
})
});
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}/documents/{document_id}/segments',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"segments": [
{
"answer": "<string>",
"attachment_ids": [
"<string>"
],
"content": "<string>",
"keywords": [
"<string>"
]
}
]
}',
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}/documents/{document_id}/segments"
payload := strings.NewReader("{\n \"segments\": [\n {\n \"answer\": \"<string>\",\n \"attachment_ids\": [\n \"<string>\"\n ],\n \"content\": \"<string>\",\n \"keywords\": [\n \"<string>\"\n ]\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}/documents/{document_id}/segments"))
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\n \"segments\": [\n {\n \"answer\": \"<string>\",\n \"attachment_ids\": [\n \"<string>\"\n ],\n \"content\": \"<string>\",\n \"keywords\": [\n \"<string>\"\n ]\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}/documents/{document_id}/segments')
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 = '{
"segments": [
{
"answer": "<string>",
"attachment_ids": [
"<string>"
],
"content": "<string>",
"keywords": [
"<string>"
]
}
]
}'
response = http.request(request)
puts response.read_bodyResponse
{
"data": [
{
"answer": "",
"attachments": [],
"child_chunks": [],
"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_id": "a8e0e5b5-78c6-4130-a5ce-25feb0e0b4ac",
"enabled": true,
"error": null,
"hit_count": 0,
"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,
"summary": null,
"tokens": 12,
"updated_at": 1741267200,
"updated_by": "ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4",
"word_count": 9
}
],
"doc_form": "text_model"
}{
"code": "invalid_param",
"message": "Exceeded maximum segments limit of 1000.",
"status": 400
}{
"code": "provider_not_initialize",
"message": "No Embedding Model available. Please configure a valid provider in the Settings -> Model Provider.",
"status": 400
}{
"error": "1 validation error for SegmentCreatePayload\nsegments.0.content\n String should have at least 1 character [type=string_too_short, input_value='', input_type=str]"
}{
"code": "forbidden",
"message": "Dataset api access is not enabled.",
"status": 403
}{
"code": "forbidden",
"message": "The capacity of the vector space has reached the limit of your subscription.",
"status": 403
}{
"code": "forbidden",
"message": "Sorry, you have reached the knowledge base request rate limit of your subscription.",
"status": 403
}{
"code": "forbidden",
"message": "To unlock this feature and elevate your Dify experience, please upgrade to a paid plan.",
"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": "not_found",
"message": "Document not found.",
"status": 404
}{
"code": "not_found",
"message": "Document is not completed.",
"status": 404
}{
"code": "not_found",
"message": "Document is disabled.",
"status": 404
}{
"code": "service_unavailable",
"message": "Unable to verify vector space usage right now. Please try again later.",
"status": 503
}post/datasets/{dataset_id}/documents/{document_id}/segments
Authorization
Path parameters
知识库 ID,通过 获取知识库列表 获取。使用限定范围的密钥时,从 Dify 页面 URL 复制 ID。
文档 ID。从 获取知识库的文档列表 获取。
Body
application/jsonResponses
application/json
分段创建成功。
已创建的分段列表。
Show child attributesHide child attributes
回答内容,用于问答模式文档。
附加到该分段的文件。
属于该分段的子分段。仅在分层模式文档中存在。
Show child attributesHide child attributes
索引完成的时间戳。尚未完成时为
null。分段的文本内容。
创建时间戳(Unix 纪元,单位为秒)。
创建该分段的用户 ID。
分段被禁用的时间戳。启用时为
null。禁用该分段的用户 ID。启用时为
null。该分段所属文档的 ID。
该分段是否启用检索。
索引失败时的错误消息。无错误时为
null。该分段在检索查询中被匹配的次数。
分段的唯一标识符。
索引内容的哈希值,用于检测变更。
向量存储中索引节点的 ID。
索引开始的时间戳。尚未开始时为
null。与该分段关联的关键词,用于基于关键词的检索。
分段在文档中的位置。
用于完整性验证的签名内容哈希。
分段的当前索引状态,例如
completed、indexing、error。索引停止的时间戳。未停止时为
null。AI 生成的分段内容摘要。未启用摘要索引时为
null。分段内容的令牌数。
最后更新时间戳(Unix 纪元,单位为秒)。
最后更新该分段的用户 ID。
分段内容的字数。
该文档使用的分块模式。