A knowledge base in Agent Storage divides each imported document into knowledge chunks. Each chunk has its own ID, title, content, type, and retrievable status. View and update knowledge chunks in the Agent Storage console, through an SDK, or by calling an API operation.
Prerequisites
A knowledge base has been created and documents have been imported into it. You can view and edit chunks only for documents whose status is
Completed.You have the
docIdof the document that contains the target chunks. Chunk operations locate chunks through thedocIdof their parent document.If you call the API or use an SDK, you have an API key, an endpoint, and an instance name. Replace the
<your-api-key>,<instance>,<region>, and<instance-name>placeholders in the examples with your actual values.The Agent Storage SDK for your language installed: the
tablestore_agent_storagepackage for Python or the@tablestore/agent-storagepackage for TypeScript.If the knowledge base has a subspace enabled, you must specify
subspacefor chunk operations.
View chunks
List the chunks of a specified document with a paged query. The response includes the chunk ID, content, title, type, and retrievable status of each chunk.
Console
Log on to the Agent Storage console.
In the Document management list, click View chunks in the Actions column of the target document.
The chunk list dialog box opens. Each chunk displays its type, ID, content, retrievable status, and creation and update times.
SDK
Python
The following example lists up to five chunks of a document and prints the ID, status, and a content preview of each chunk:
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
resp = client.list_chunks({
"knowledgeBaseName": "product_docs_kb",
"docId": "fc6ed97f-...",
"maxResults": 5
})
for chunk in resp["data"]["chunkDetails"]:
print(f"[Chunk {chunk['chunkId']}] ({chunk['status']}) {chunk['content'][:80]}...")TypeScript
The following example lists up to five chunks of a document and prints the ID, status, and a content preview of each chunk:
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
const resp = await client.listChunks({
knowledgeBaseName: 'product_docs_kb',
docId: 'fc6ed97f-...',
maxResults: 5,
});
for (const chunk of resp.data.chunkDetails) {
console.log(`[Chunk ${chunk.chunkId}] (${chunk.status}) ${chunk.content.slice(0, 80)}...`);
}API
Call ListChunks to list the chunks of a document. For the complete field descriptions, see ListChunks API.
Update chunks
Update the title, content, or status of chunks in batches. Each request can update up to 10 chunks.
Retrievable status — After you set the
statusof a chunk toinactive, the chunk does not appear in Retrieve results. You can use this to temporarily exclude inaccurate content without deleting the entire document.Result verification — An HTTP 200 response with
code: SUCCESSdoes not mean that all chunks were updated successfully. Check theupdateStatusof each chunk inupdateDetailsindividually.Field limits —
titlesupports up to 100 tokens, andcontentsupports up to 320 tokens.
Console
Log on to the Agent Storage console.
In the Document management list, click View chunks in the Actions column of the target document.
In the chunk list, click Edit for the target chunk.
Modify the title and content, and then save the changes.
To temporarily exclude a chunk, toggle its retrievable status.
SDK
Python
Modify the title and content of a chunk:
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
resp = client.update_chunks({
"knowledgeBaseName": "product_docs_kb",
"chunks": [
{
"docId": "fc6ed97f-...",
"chunkId": 1,
"title": "Updated title",
"content": "Updated content"
}
]
})
for detail in resp["data"]["updateDetails"]:
print(f"chunk {detail['chunkId']}: {detail['updateStatus']}")Temporarily exclude an inaccurate chunk (make it non-retrievable):
resp = client.update_chunks({
"knowledgeBaseName": "product_docs_kb",
"chunks": [
{
"docId": "fc6ed97f-...",
"chunkId": 0,
"status": "inactive"
}
]
})Check the updateStatus of each entry in updateDetails to confirm that the chunk was excluded.
TypeScript
Modify the title and content of a chunk:
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
const resp = await client.updateChunks({
knowledgeBaseName: 'product_docs_kb',
chunks: [
{
docId: 'fc6ed97f-...',
chunkId: 1,
title: 'Updated title',
content: 'Updated content',
},
],
});
for (const detail of resp.data.updateDetails) {
console.log(`chunk ${detail.chunkId}: ${detail.updateStatus}`);
}Temporarily exclude an inaccurate chunk (make it non-retrievable):
const excludeResp = await client.updateChunks({
knowledgeBaseName: 'product_docs_kb',
chunks: [
{
docId: 'fc6ed97f-...',
chunkId: 0,
status: 'inactive',
},
],
});API
Call UpdateChunks to update chunks. For the complete field descriptions, see UpdateChunks API.