Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

3.5 KB
Raw Blame
(ns metabase-enterprise.search.embeddings.client
  "Embedding-service RPC for callers outside the search engine.

  This namespace is a thin neutral entry point so consumers like the data-complexity-score's
  synonym axis can request embeddings without taking a transitive dependency on a
  search-prefixed module for what is just an HTTP call to ai-service.

  TODO: relocate the implementation here. Today the provider dispatch + HTTP impls (ollama,
  ai-service, openai) and the embedding-service settings (`ee-embedding-service-base-url`,
  `ee-embedding-service-api-key`, etc.) all live in `metabase-enterprise.search.semantic.*` —
  they predate this namespace because semantic-search was the first consumer.

  Steps for the move:
    1. Migrate the `get-embedding` / `get-embeddings-batch` / `pull-model` defmultis and
       provider impls (currently in `metabase-enterprise.search.semantic.embedding`) into
       `metabase-enterprise.search.embeddings.*`.
    2. Migrate the embedding-service settings into the same module (or a sibling
       `metabase-enterprise.search.embeddings.settings` ns).
    3. Decouple the inline token-tracking calls (`semantic.models.token-tracking/record-tokens`,
       `:metabase-search/semantic-embedding-tokens`, the snowplow `track-token-usage!`) — make
       them a hookable callback so callers register their own analytics scope. Semantic-search
       keeps recording its current metrics via that hook; data-complexity-score can opt out or
       register its own.
    4. Flip the dependency: `semantic-search.embedding` becomes a thin re-export from this
       namespace (or goes away entirely once consumers update).
    5. Search-index naming helpers (`abbrev-provider-name`, `abbrev-model-name`,
       `clean-model-name`) and `get-configured-model` / `process-embeddings-streaming` stay in
       semantic-search — they're search-engine-specific."
  (:require
   [metabase-enterprise.search.semantic.core :as semantic-search]))

(set! *warn-on-reflection* true)

(defn- normalize-model-descriptor
  "Translate the neutral embedding descriptor's dimension key to semantic search's internal key."
  [{:keys [model-dimensions vector-dimensions] :as embedding-model}]
  (when (and model-dimensions vector-dimensions (not= model-dimensions vector-dimensions))
    (throw (ex-info "Embedding model descriptor has conflicting dimension values."
                    {:model-dimensions  model-dimensions
                     :vector-dimensions vector-dimensions})))
  (cond-> (dissoc embedding-model :model-dimensions)
    model-dimensions (assoc :vector-dimensions model-dimensions)))

(defn get-embeddings-batch
  "Return a sequential collection of embedding vectors, in the same order as the input texts.

  `embedding-model` — `{:provider :model-name :model-dimensions}` map; provider must be one of those registered on
                      [[metabase.search.embeddings.provider]] (`ai-service`, `openai`, `ollama`).
  `texts`           — sequential collection of input strings.
  `opts`            — optional keyword opts (e.g. `:type :doc`). Accepts alternating kwargs or a single trailing map;
                      forwarded as kwargs to the provider.

  A provider that needs an AI provider connection embeds through the one keyed after it, whatever connection
  semantic search's `ee-embedding-provider` names."
  [embedding-model texts & {:as opts}]
  (apply semantic-search/get-embeddings-batch
         (normalize-model-descriptor embedding-model)
         texts
         (mapcat identity (assoc opts :connection-key (:provider embedding-model)))))