Skip to main content

Automate Knowledge Boxes (KBs) Creation and Deletion

This guide provides a script to automate the creation and deletion of Knowledge Boxes (KBs) using Nuclia's API.

Prerequisites

You need a NUA key with specific access rights. Ensure the "Allow management of Knowledge Boxes" option is selected when creating the NUA key.

Setup

First, ensure you have the necessary imports and constants defined:

import requests

MANAGEMENT_API = "https://nuclia.cloud/api/v1"
ZONE = "europe-1"
ZONE_MANAGEMENT_API = f"https://{ZONE}.nuclia.cloud/api/v1"
ACCOUNT_ID = "YOUR-ACCOUNT-ID"

NUA_API_KEY = "YOUR-NUA-KEY"

Functions Overview

1. List Knowledge Boxes (KBs)

List all KBs for an account and zone (zone depends on the NUA Key).

def list_kbs():
url = f"{ZONE_MANAGEMENT_API}/account/{ACCOUNT_ID}/kbs"
response = requests.get(url, headers={"X-NUCLIA-NUAKEY": f"Bearer {NUA_API_KEY}"})
response.raise_for_status()
return response.json()

2. Create a Knowledge Box (KB)

Create a new KB with the specified title and slug.

def create_kb(title, slug):
url = f"{ZONE_MANAGEMENT_API}/account/{ACCOUNT_ID}/kbs"
payload = {
"title": title,
"slug": slug,
"learning_configuration": {
"anonymization_model": "disabled",
"semantic_model": "multilingual-2024-05-06",
},
}
response = requests.post(
url,
json=payload,
headers={"X-NUCLIA-NUAKEY": f"Bearer {NUA_API_KEY}"},
)
response.raise_for_status()
return response.json()

3. Delete a Knowledge Box (KB)

Delete a KB with the specified slug.

def delete_kb(kb_id):
url = f"{ZONE_MANAGEMENT_API}/account/{ACCOUNT_ID}/kb/{kb_id}"
response = requests.delete(url, headers={"X-NUCLIA-NUAKEY": f"Bearer {NUA_API_KEY}"})
response.raise_for_status()

4. List API Keys for a KB

List API keys for a specific KB. Note: Tokens are not returned here and must be regenerated if lost.

def list_api_keys(kb_id):
url = f"{ZONE_MANAGEMENT_API}/account/{ACCOUNT_ID}/kb/{kb_id}/service_accounts"
response = requests.get(url, headers={"X-NUCLIA-NUAKEY": f"Bearer {NUA_API_KEY}"})
response.raise_for_status()
return response.json()

5. Create an API Key for a KB

Create a new API key for a KB.

def create_kb_api_key(kb_id, api_key_name):
url = f"{ZONE_MANAGEMENT_API}/account/{ACCOUNT_ID}/kb/{kb_id}/service_accounts"
payload = {"title": api_key_name, "role": "SOWNER"}
response = requests.post(
url, json=payload, headers={"X-NUCLIA-NUAKEY": f"Bearer {NUA_API_KEY}"}
)
response.raise_for_status()
return response.json()

6. Generate an API Key Token

Generate the token for an API key.

def generate_kb_api_key_token(kb_id, api_key_id):
url = f"{ZONE_MANAGEMENT_API}/account/{ACCOUNT_ID}/kb/{kb_id}/service_account/{api_key_id}/keys"
payload = {"expires": "1729449775"} # set expiration on key
response = requests.post(
url, json=payload, headers={"X-NUCLIA-NUAKEY": f"Bearer {NUA_API_KEY}"}
)
response.raise_for_status()
return response.json()
warning

Calling this invalidates previous tokens.

This documentation and the associated code provide a comprehensive way to manage Knowledge Boxes using Nuclia's API. Each function has a specific purpose and can be used independently or together depending on your needs.