classifiers.tool_embedding_batch module

Shared helpers for tool centroid embeddings (batch-friendly).

async classifiers.tool_embedding_batch.embed_texts_for_tool_scripts(embedding_client, texts, *, max_concurrent=None)[source]

Embed texts for tool-index / Redis flows only.

When OpenRouter-only mode is active, runs multiple client batches in parallel (bounded by semaphore). Otherwise uses sequential embed_texts so Gemini rate limits are not compounded.

Return type:

list[ndarray]

Parameters:
classifiers.tool_embedding_batch.normalize_synthetic_queries(qs)[source]

Ensure synthetic queries are a list of strings.

If qs is a single string (e.g. bad JSON), treat it as one query. Iterating a bare string in embed_texts would embed per-character.

Return type:

list[str]

Parameters:

qs (Any)

async classifiers.tool_embedding_batch.compute_tool_embedding(embedding_client, synthetic_queries, tool_name)[source]

Compute one tool’s centroid embedding from its synthetic queries.

Single-tool convenience wrapper: it normalises the synthetic queries, embeds them via embed_texts_for_tool_scripts() (which calls the OpenRouter embeddings client, fanning out batches concurrently in OpenRouter-only mode), drops any empty vectors, and reduces the rest to one L2-normalised centroid via _centroid_from_vectors(). That centroid is the per-tool vector used for cosine routing in the classifier. Embedding failures are caught and logged, returning None so a single bad tool does not abort a larger refresh.

Issues embedding HTTP requests through the passed-in client and logs a warning on failure; it does not touch Redis or the filesystem. This is the single-tool counterpart to compute_tool_centroids_bulk(), which the refresh paths use instead; no in-repo callers of this function were found (it is retained as a standalone single-tool helper).

Parameters:
  • embedding_client (OpenRouterEmbeddings) – OpenRouter embeddings client used to embed the queries.

  • synthetic_queries (list[str]) – The tool’s synthetic query strings (normalised internally; a bare string is treated as one query).

  • tool_name (str) – Tool name, used only for log context on failure.

Return type:

ndarray | None

Returns:

The unit-length centroid numpy.ndarray for the tool, or None if there are no usable queries or embedding failed.

async classifiers.tool_embedding_batch.compute_tool_centroids_bulk(embedding_client, tool_queries)[source]

Compute normalised centroids for many tools in minimal embed_texts calls.

Flattens all query strings, embeds once (subject to client batch limits), then slices vectors per tool and reduces to centroids. Avoids one HTTP round-trip per tool when refreshing many tools.

Return type:

dict[str, ndarray | None]

Parameters: