def install_elser()

in example-apps/chatbot-rag-app/data/index_data.py [0:0]


def install_elser():
    # This script is re-entered on ctrl-c or someone just running it twice.
    # Hence, both steps need to be careful about being potentially redundant.

    # Step 1: Ensure ELSER_MODEL is defined
    try:
        es.ml.get_trained_models(model_id=ELSER_MODEL)
    except NotFoundError:
        print(f'"{ELSER_MODEL}" model not available, downloading it now')
        es.ml.put_trained_model(
            model_id=ELSER_MODEL, input={"field_names": ["text_field"]}
        )

    while True:
        status = es.ml.get_trained_models(
            model_id=ELSER_MODEL, include="definition_status"
        )
        if status["trained_model_configs"][0]["fully_defined"]:
            break
        time.sleep(1)

    # Step 2: Ensure ELSER_MODEL is fully allocated
    if not is_elser_fully_allocated():
        try:
            es.ml.start_trained_model_deployment(
                model_id=ELSER_MODEL, wait_for="fully_allocated"
            )
            print(f'"{ELSER_MODEL}" model is deployed')
        except BadRequestError:
            # Already started, and likely fully allocated
            pass

    print(f'"{ELSER_MODEL}" model is ready')