Skip to content

Creating knowledge model widgets for external use

Constructor Model knowledge models can be embedded as widgets on external websites.

Here is a sample code to create a widget for an existing model:

Define the constants:

python
import requests

KM_ID = '{YOUR_KM_ID}' 
API_URL = 'https://constructor.app/api/platform-kmapi/v1' 
API_KEY = '{YOUR_API_KEY}'

Define the headers:

python
# Define the headers 
headers = {
    'X-KM-AccessKey': f'Bearer {API_KEY}' 
}

To enable the widget, review the settings of the model and change shared_type to all.

python
def get_model_settings(km_id):
    response = requests.get(f'{API_URL}/knowledge-models/{km_id}', headers=headers)
    return response.json()
python
def change_model_settings(km_id, name=None, description=None, shared_type=None):
    data = {
        "name": "Materials knowledge model",
        "description": description,
        "shared_type": shared_type
    }
    response = requests.patch(f'{API_URL}/knowledge-models/{km_id}', headers=headers, json=data)
    return response.json()
python
print(get_model_settings(KM_ID))
json
{
  "id": "ce08638f070d48919bca1c8aa0e26f09",
  "name": "ML in Materials Science",
  "description": "Overview of the current trends in Materials Science",
  "owner": {
    "user_id": "7843184a-fc90-4ef8-8a63-639f08e34bac",
    "tenant_id": "3867ee0c-f987-4cb8-8fd4-c92b3e917d99"
  },
  "shared_type": "all",
  "created_at": "2024-12-18T08:41:17.632033Z"
}
python
print(change_model_settings(KM_ID, shared_type="all"))
json
{
  "id": "ce08638f070d48919bca1c8aa0e26f09",
  "name": "Materials knowledge model",
  "description": "Overview of the current trends in Materials Science",
  "owner": {
    "user_id": "7843184a-fc90-4ef8-8a63-639f08e34bac",
    "tenant_id": "3867ee0c-f987-4cb8-8fd4-c92b3e917d99"
  },
  "shared_type": "all",
  "created_at": "2024-12-18T08:41:17.632033Z"
}
python
def review_widget_settings(km_id):
    response = requests.get(f'{API_URL}/knowledge-models/{km_id}/widget', headers=headers)
    return response.json()
python
print(review_widget_settings(KM_ID))
json
{
  'session_rpm': 5,
  'message_rpm': 3,
  'enabled': True,
  'llm_id': '83db90a3c67648e0ba43b48c2f2c39b6'
}
python
requests.post
python
print(requests.get(f'{API_URL}/knowledge-models/{KM_ID}', headers=headers).json())
json
{
  "id": "ce08638f070d48919bca1c8aa0e26f09",
  "name": "ML in Materials Science",
  "description": "Overview of the current trends in Materials Science",
  "owner": {
    "user_id": "7843184a-fc90-4ef8-8a63-639f08e34bac",
    "tenant_id": "3867ee0c-f987-4cb8-8fd4-c92b3e917d99"
  },
  "shared_type": "all",
  "created_at": "2024-12-18T08:41:17.632033Z"
}

Change the widget settings, including the LLM to be used for all the sessions in widget as follows:

python
def update_widget_settings(km_id, session_rpm=None, message_rpm=None, enabled=None, llm_id=None):
    data = {
        "session_rpm": session_rpm,
        "message_rpm": message_rpm,
        "enabled": enabled,
        "llm_id": llm_id
    }
    response = requests.patch(f'{API_URL}/knowledge-models/{km_id}/widget', headers=headers, json=data)
    return response.json()

If necessary, update the model settings as follows:

python
widget_settings = update_widget_settings(KM_ID, enabled="true") 
print(widget_settings)
json
{
  'session_rpm': 5,
  'message_rpm': 3,
  'enabled': True,
  'llm_id': '83db90a3c67648e0ba43b48c2f2c39b6'}

Use the following script to enable widget on your website:

html
<script type="module" 
  src="https://training.constructor.app/scripts/chat-widget.js"
  data-knowledge-model-id={model_id}>
</script>

where model_id is KM_ID, the identifier of the knowledge model.

Make sure that:

  • shared_type="all"
  • widget.enabled = true