Add assets to a knowledge store

After you create a knowledge store, add assets to it for indexing. The platform processes each item asynchronously.

Prerequisites

  • You’ve already uploaded your content, and the asset has reached the ready status. See the Upload content page for details.
  • You’ve already created a knowledge store. See the Create a knowledge store page for details.

Add an asset

To add an asset to a knowledge store, use the example code below, replacing the placeholders surrounded by <> with your values:

1import requests
2
3API_KEY = "<YOUR_API_KEY>"
4BASE_URL = "https://api.twelvelabs.io/v1.3"
5HEADERS = {"x-api-key": API_KEY, "Content-Type": "application/json"}
6ASSET_ID = "<YOUR_ASSET_ID>"
7
8response = requests.post(
9 f"{BASE_URL}/knowledge-stores/{store_id}/items",
10 headers=HEADERS,
11 json={"asset_id": ASSET_ID}
12)
13item_id = response.json()["_id"]
14print(f"Item added: {item_id}")

Wait for indexing

Poll the item status until it reaches ready:

1import time
2
3while True:
4 status = requests.get(
5 f"{BASE_URL}/knowledge-stores/{store_id}/items/{item_id}",
6 headers={"x-api-key": API_KEY}
7 ).json()["status"]
8 if status == "ready":
9 break
10 elif status == "failed":
11 raise Exception("Indexing failed")
12 print(f"Status: {status}, waiting...")
13 time.sleep(10)
14print("Indexing complete")

Indexing usually takes longer than the asset processing that happens after upload. One asset can exist in multiple knowledge stores. For the full list of item statuses, see the Knowledge stores page.

Jupyter notebook

Download the notebook to run this guide interactively.

API reference