agent-zero/preload.py
Alessandro 2afffe8edc
Some checks are pending
Build And Publish Docker Images / plan (push) Waiting to run
Build And Publish Docker Images / build (push) Blocked by required conditions
Overlap embedding warm-up with chat restoration
Load sentence-transformers on the existing cache miss and construct the
preloaded model off the shared background event loop. Start warm-up before
chat restoration inside extensible init_a0, preserving start-hook control
and sharing the loaded model with ordinary runtime requests.

Remove unused speech preload imports and dead warm-up functions. Seed APT
indexes only when absent, skipping the refresh on normal container boots.
Keep the existing Python tunnel service and its provider lifecycle.

Validation: 49 focused framework tests passed, including extension-hook
short-circuit behavior, concurrent embedding-cache reuse, APT cache states,
tunnel behavior and CSRF origins. Three cold Fable chat trials per version:
median submit-to-first-token 9.588s -> 4.234s;
local preparation 6.904s -> 1.197s;
HTTP readiness 42.637s -> 41.139s;
boot-to-first-token 52.093s -> 46.657s.
Provider timing varies; these are local repeated measurements. The live
baseline already omitted APT refresh, so it receives no timing credit.
2026-10-10 16:36:08 +02:00

31 lines
1,018 B
Python

import asyncio
from helpers import runtime
from helpers.print_style import PrintStyle
import models
async def preload():
try:
from plugins._model_config.helpers.model_config import get_embedding_model_config_object
emb_cfg = get_embedding_model_config_object()
if emb_cfg.provider.lower() == "huggingface":
# Model imports and construction must not block the shared background loop.
emb_mod = await asyncio.to_thread(
models.get_embedding_model,
emb_cfg.provider,
emb_cfg.name,
model_config=emb_cfg,
**emb_cfg.build_kwargs(),
)
await emb_mod.aembed_query("test")
PrintStyle().print("Preload completed.")
except Exception as e:
PrintStyle().error(f"Error in preload: {e}")
# preload the configured embedding model
if __name__ == "__main__":
PrintStyle().print("Running preload...")
runtime.initialize()
asyncio.run(preload())