Create segments¶
Create a segment in the source’s current snapshot. Provide text, OCR text, or an image description for the new segment.
POST https://api.moi.matrixorigin.cn/v5/semantic-models/{model_id}/sources/{source_row_id}/segments
Preparation before calling¶
First read the document and obtain data.etag as the write precondition. Prepare a personal access token with access to the target workspace and the workspace ID.
The example below uses:
$AI_STUDIO_API_KEY: The actual personal access token, passed through theX-API-KeyHeader.$WORKSPACE_ID: Target workspace ID, passed throughX-Workspace-IDHeader.$MODEL_ID: Knowledge Base ID.$SOURCE_ROW_ID: Source record ID.
Request example¶
curl -X POST "https://api.moi.matrixorigin.cn/v5/semantic-models/$MODEL_ID/sources/$SOURCE_ROW_ID/segments" \
-H "X-API-Key: $AI_STUDIO_API_KEY" \
-H "X-Workspace-ID: $WORKSPACE_ID" \
-H 'Content-Type: application/json' \
-d '{
"if_match": "<DOCUMENT_ETAG>",
"level": "chunk",
"content": "<SEGMENT_CONTENT>"
}'
Path parameters¶
Parameters |
Type |
Description |
|---|---|---|
|
integer |
Knowledge base ID. |
|
string |
Source record ID. |
Request body¶
Submit the current snapshot’s ETag and the segment content. If the body has no nonempty if_match, supply the ETag through the If-Match header. At least one is required; a nonempty body value takes precedence.
Field |
Type |
Required |
Description |
|---|---|---|---|
|
string |
Conditional |
|
|
string |
No |
Segment level; defaults to |
|
string |
No |
Text segment content. |
|
string |
No |
OCR recognized text. |
|
string |
No |
Image content description. |
|
JSON |
No |
Boundary information for the segment in the original content. |
|
object |
No |
Custom metadata object. Managed fields use the values written by the service. |
The source must be a file source with a knowledge-base file ID, kb_file_id, and an available vector index; table sources do not support segment writes. Associated image and page-image IDs come from parsing. Submitting image_file_id or page_image_file_id during creation returns 400 with ErrParamInvalid.
Successful response¶
Returns 200 and the complete updated source document snapshot on success. The example below is a response excerpt.
{
"code": "OK",
"msg": "OK",
"data": {
"document": {
"source": {
"row_id": "src_01",
"model_id": 401
},
"segment_status": {
"available": true,
"total": 3
},
"etag": "<NEW_DOCUMENT_ETAG>",
"segments": [
{
"segment_id": "seg_03",
"level": "chunk",
"content": "新增分段",
"enabled": true
}
]
}
}
}
The response fields are as follows.
Field |
Type |
Description |
|---|---|---|
|
string |
|
|
string |
|
|
object |
Snapshot of the source document after writing. |
|
string |
Source record ID. |
|
object |
Current segment availability status and total number. |
|
string |
ETag of the updated snapshot; returned when available. Use it for subsequent writes. |
|
string |
New segment ID. |
|
string |
Segment level. |
|
string |
Segmented text; may be omitted if content is not returned. |
|
boolean |
Whether the segment participates in retrieval. |
In field paths, [] means each item in an array. For example, data.document.segments[].segment_id identifies each segment in data.document.segments.
Error response¶
A missing or blank ETag returns 400 with ErrParamInvalid. An ETag that differs from the current snapshot returns 409 with ErrConflict. Error messages depend on the request language.
{
"code": "ErrParamInvalid",
"msg": "<ERROR_MESSAGE>",
"data": null
}
Common HTTP errors¶
HTTP status code |
error code |
Common causes |
Recommended actions |
|---|---|---|---|
|
|
The path ID, request body, or ETag precondition is invalid. |
Read the document again and supply its ETag using |
|
|
The API Key is invalid or has expired. |
Check API Key. |
|
|
The caller does not have permission to update this source. |
Check workspace and object authorization. |
|
|
The knowledge base or source does not exist or is not visible to the current caller. |
Reread source list to confirm ID. |
|
|
The ETag does not match the current snapshot and the service refuses the write. |
Read the latest snapshot and review the change before retrying with its ETag. |
|
|
The service failed to create the segment. |
Keep the desensitized response information and try again. |
Follow-up operations¶
Read the document to confirm the new segment. Use the new snapshot’s ETag for subsequent writes; on 409 / ErrConflict, read the snapshot again and review the intended change.