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 the X-API-Key Header.

  • $WORKSPACE_ID: Target workspace ID, passed through X-Workspace-ID Header.

  • $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

model_id

integer

Knowledge base ID.

source_row_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

if_match

string

Conditional

data.etag read before the call; may be omitted when using the If-Match header.

level

string

No

Segment level; defaults to chunk when omitted.

content

string

No

Text segment content.

ocr_text

string

No

OCR recognized text.

image_description

string

No

Image content description.

bbox

JSON

No

Boundary information for the segment in the original content.

metadata

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

code

string

OK on success.

msg

string

OK on success.

data.document

object

Snapshot of the source document after writing.

data.document.source.row_id

string

Source record ID.

data.document.segment_status

object

Current segment availability status and total number.

data.document.etag

string

ETag of the updated snapshot; returned when available. Use it for subsequent writes.

data.document.segments[].segment_id

string

New segment ID.

data.document.segments[].level

string

Segment level.

data.document.segments[].content

string

Segmented text; may be omitted if content is not returned.

data.document.segments[].enabled

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

400

ErrParamInvalid

The path ID, request body, or ETag precondition is invalid.

Read the document again and supply its ETag using if_match or the If-Match header.

401

ErrUnauthorized

The API Key is invalid or has expired.

Check API Key.

403

ErrForbidden

The caller does not have permission to update this source.

Check workspace and object authorization.

404

ErrNotFound

The knowledge base or source does not exist or is not visible to the current caller.

Reread source list to confirm ID.

409

ErrConflict

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.

500

ErrServer

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.

Last updated on