Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat: datasets openapi list segements support paged resp (#16603)

L8ng83cd14104df9
diff --git a/api/controllers/service_api/dataset/segment.py b/api/controllers/service_api/dataset/segment.py
index 25ae43f2ad3..ee5178862aa 100644
--- a/api/controllers/service_api/dataset/segment.py
+++ b/api/controllers/service_api/dataset/segment.py
@@ -1,3 +1,4 @@
+from flask import request
from flask_login import current_user # type: ignore
from flask_restful import marshal, reqparse # type: ignore
from werkzeug.exceptions import NotFound
@@ -74,6 +75,8 @@ class SegmentApi(DatasetApiResource):
# check dataset
dataset_id = str(dataset_id)
tenant_id = str(tenant_id)
+ page = request.args.get("page", default=1, type=int)
+ limit = request.args.get("limit", default=20, type=int)
dataset = db.session.query(Dataset).filter(Dataset.tenant_id == tenant_id, Dataset.id == dataset_id).first()
if not dataset:
raise NotFound("Dataset not found.")
@@ -118,8 +121,25 @@ class SegmentApi(DatasetApiResource):
query = query.where(DocumentSegment.content.ilike(f"%{keyword}%"))
total = query.count()
- segments = query.order_by(DocumentSegment.position).all()
- return {"data": marshal(segments, segment_fields), "doc_form": document.doc_form, "total": total}, 200
+ query = query.order_by(DocumentSegment.position)
+ paginated_segments = query.paginate(
+ page=page,
+ per_page=limit,
+ max_per_page=100,
+ error_out=False,
+ )
+ segments = paginated_segments.items
+
+ response = {
+ "data": marshal(segments, segment_fields),
+ "doc_form": document.doc_form,
+ "total": total,
+ "has_more": len(segments) == limit,
+ "limit": limit,
+ "page": page,
+ }
+
+ return response, 200
class DatasetSegmentApi(DatasetApiResource):
diff --git a/web/app/(commonLayout)/datasets/template/template.en.mdx b/web/app/(commonLayout)/datasets/template/template.en.mdx
index a5f4c40ef60..d0e1d9fbc61 100644
--- a/web/app/(commonLayout)/datasets/template/template.en.mdx
+++ b/web/app/(commonLayout)/datasets/template/template.en.mdx
@@ -961,6 +961,12 @@ import { Row, Col, Properties, Property, Heading, SubProperty, PropertyInstructi
<Property name='status' type='string' key='status'>
Search status, completed
</Property>
+ <Property name='page' type='string' key='page'>
+ Page number (optional)
+ </Property>
+ <Property name='limit' type='string' key='limit'>
+ Number of items returned, default 20, range 1-100 (optional)
+ </Property>
</Properties>
</Col>
<Col sticky>
@@ -1004,7 +1010,11 @@ import { Row, Col, Properties, Property, Heading, SubProperty, PropertyInstructi
"error": null,
"stopped_at": null
}],
- "doc_form": "text_model"
+ "doc_form": "text_model",
+ "has_more": false,
+ "limit": 20,
+ "total": 9,
+ "page": 1
}
```
</CodeGroup>
diff --git a/web/app/(commonLayout)/datasets/template/template.zh.mdx b/web/app/(commonLayout)/datasets/template/template.zh.mdx
index 282849f3dbc..d4ed85233b6 100644
--- a/web/app/(commonLayout)/datasets/template/template.zh.mdx
+++ b/web/app/(commonLayout)/datasets/template/template.zh.mdx
@@ -961,6 +961,12 @@ import { Row, Col, Properties, Property, Heading, SubProperty, PropertyInstructi
<Property name='status' type='string' key='status'>
搜索状态,completed
</Property>
+ <Property name='page' type='string' key='page'>
+ 页码,可选
+ </Property>
+ <Property name='limit' type='string' key='limit'>
+ 返回条数,可选,默认 20,范围 1-100
+ </Property>
</Properties>
</Col>
<Col sticky>
@@ -1004,7 +1010,11 @@ import { Row, Col, Properties, Property, Heading, SubProperty, PropertyInstructi
"error": null,
"stopped_at": null
}],
- "doc_form": "text_model"
+ "doc_form": "text_model",
+ "has_more": false,
+ "limit": 20,
+ "total": 9,
+ "page": 1
}
```
</CodeGroup>