Skip to content

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

13.1 KB
Raw Blame
(ns metabase-enterprise.search.semantic.settings
  (:require
   [clojure.string :as str]
   [metabase.events.core :as events]
   [metabase.llm.settings :as llm-settings]
   [metabase.request.current :as request.current]
   [metabase.search.config :as search.config]
   [metabase.settings.core :as setting :refer [defsetting]]
   [metabase.util.i18n :refer [deferred-tru tru]]))

;; Topic for the just-in-time HNSW build, handled in metabase-enterprise.search.semantic.events. Declared
;; here, not there, so it's valid wherever the setter runs regardless of handler-namespace load order.
(events/derive! :event/semantic-search-hnsw-enabled :metabase/event)

(defn- normalize-base-url
  [value]
  (some-> value str/trim not-empty))

(defsetting ee-embedding-provider
  (deferred-tru (str "The embedding provider to use: a built-in or plugin embedder, or the key of an AI provider "
                     "connection, whose type then selects the embedding API."))
  :encryption :when-encryption-key-set
  :visibility :settings-manager
  :default "ai-service"
  :type :string
  :export? false
  :doc false
  :setter (fn [new-value]
            ;; Provider plugins may be configured before they are loaded. Readiness reports an unregistered
            ;; provider as unavailable, so validation here only needs to protect the registry key contract.
            (when-not (or (nil? new-value)
                          (and (string? new-value) (not (str/blank? new-value))))
              (throw (ex-info "Embedding provider must be a non-blank string."
                              {:invalid-value new-value})))
            (setting/set-value-of-type! :string :ee-embedding-provider new-value)))

(defsetting ee-embedding-model
  (deferred-tru "Set the embedding model for the selected provider")
  :encryption :no
  :visibility :settings-manager
  :default "Snowflake/snowflake-arctic-embed-l-v2.0"
  :export? false
  :doc false)

(defsetting ee-embedding-query-prefix
  (deferred-tru
   (str "Prefix prepended to search queries (but not indexed documents) before embedding them, as expected by "
        "asymmetric retrieval models such as the snowflake-arctic-embed family. It is prepended verbatim, so "
        "include any trailing separator. Leave empty to use the default for the model, which varies by "
        "generation: Arctic Embed v2.0 expects `query: `, earlier versions expect a longer instruction."))
  :encryption :no
  :visibility :settings-manager
  :default    nil
  :type       :string
  :export?    false
  :doc        false)

(defsetting ee-embedding-model-dimensions
  (deferred-tru "Set the dimension size for the selected embedding model")
  :encryption :no
  :visibility :settings-manager
  :default 1024
  :type :positive-integer
  :export? false
  :doc false)

(defsetting ee-embedding-service-base-url
  (deferred-tru "URL of the OpenAI-compatible embedding service (e.g. a LiteLLM proxy).")
  :encryption :when-encryption-key-set
  :visibility :settings-manager
  :default    nil
  :export?    false
  ;; Normalize on read as well as write: environment values and rows persisted by older versions bypass the setter.
  :getter     (fn []
                (normalize-base-url
                 (setting/get-value-of-type :string :ee-embedding-service-base-url)))
  :setter     (fn [new-value]
                (let [new-value (normalize-base-url new-value)]
                  ;; Generic settings writes cannot atomically authorize a URL change with replacement credentials.
                  ;; Clear a stored key before changing its destination; environment keys require deployment changes.
                  ;; Bulk settings writes call every supplied setter in one transaction, including unchanged values.
                  ;; Accept an unchanged URL so resubmitting it does not roll back unrelated setting changes.
                  (when (and (request.current/current-request)
                             (not-empty (setting/get-value-of-type :string :ee-embedding-service-api-key))
                             (not= new-value (normalize-base-url
                                              (setting/get-value-of-type :string :ee-embedding-service-base-url))))
                    (throw (ex-info (if (setting/env-var-value :ee-embedding-service-api-key)
                                      (tru "The embedding service API key comes from an environment variable. Set its base URL there too.")
                                      (tru "Clear the embedding service API key before changing its base URL, then set a replacement key."))
                                    {:status-code 400
                                     :api-error   true
                                     :error-code  :embedding-base-url-change-requires-credentials})))
                  (when-let [problem (llm-settings/llm-url-problem new-value)]
                    (throw (ex-info problem {:status-code 400})))
                  (setting/set-value-of-type! :string :ee-embedding-service-base-url new-value)))
  :doc        false)

(defsetting ee-embedding-service-api-key
  (deferred-tru (str "API key for authenticating with the embedding service. Leave empty for proxying through"
                     " ai-service. In that case premium-embedding-token is used for authentication."
                     " When the API key is set through an environment variable, set the embedding service"
                     " base URL through the environment too."))
  :sensitive? true
  :visibility :settings-manager
  :export?    false
  :doc        false)

(defsetting semantic-search-embedder-circuit-breaker-enabled
  (str "Wrap embedding-service calls in a circuit breaker that fails fast after repeated failures. "
       "Runtime kill switch; the breaker thresholds are fixed.")
  :type       :boolean
  :default    true
  :encryption :no
  :export?    false
  :visibility :internal
  :doc        false)

(defsetting openai-max-tokens-per-batch
  "The maximum number of tokens sent in a single embedding API call."
  :type :integer
  :default 4000
  :encryption :no
  :export? false
  :visibility :internal
  :doc false)

(defsetting semantic-search-results-limit
  "Maximum number of results to return from a single semantic search query."
  :type :integer
  :default 1000
  :encryption :no
  :export? false
  :visibility :internal
  :doc false)

(def ^:private valid-vector-search-strategies
  "Valid semantic-search vector-search strategies, mastered in
  [[metabase.search.config/vector-search-strategies]]."
  (set search.config/vector-search-strategies))

(defsetting semantic-search-vector-strategy
  (str "Default vector-search strategy for semantic search: `hnsw` (approximate, HNSW-index-backed), "
       "`brute-force` (exact, applies non-vector filters first then computes cosine distance over the "
       "survivors), or `hnsw-iterative-relaxed`/`hnsw-iterative-strict` (HNSW-index-backed iterative scans "
       "with inline filters). Defaults to `brute-force`, which needs no index; selecting `hnsw` builds the "
       "HNSW index just-in-time. Individual requests may override this via the `vector_search_strategy` API "
       "parameter.")
  :type       :keyword
  :default    :brute-force
  :encryption :no
  :export?    false
  :visibility :internal
  :doc        false
  :setter     (fn [new-value]
                (let [kw  (some-> new-value keyword)
                      old (setting/get-value-of-type :keyword :semantic-search-vector-strategy)]
                  (when (and kw (not (contains? valid-vector-search-strategies kw)))
                    (throw (ex-info (str "Invalid vector-search strategy: " (pr-str new-value)
                                         ". Valid strategies are: " (pr-str valid-vector-search-strategies))
                                    {:invalid-value new-value
                                     :valid-values  valid-vector-search-strategies})))
                  (setting/set-value-of-type! :keyword :semantic-search-vector-strategy kw)
                  ;; Every HNSW-index-backed strategy needs the index, so build it when transitioning into one
                  ;; from a non-index-backed strategy. Gated on the transition (not every set) so switching
                  ;; between index-backed strategies -- e.g. :hnsw -> :hnsw-iterative-strict -- doesn't rebuild.
                  (let [index-backed? search.config/hnsw-index-backed-strategies]
                    (when (and (index-backed? kw) (not (index-backed? old)))
                      (events/publish-event! :event/semantic-search-hnsw-enabled {}))))))

(defsetting semantic-search-ef-search
  (str "Default pgvector `hnsw.ef_search` (HNSW candidate-list size) for the `hnsw-iterative-*` strategies. "
       "Larger values improve recall at the cost of latency. Individual requests may override this via the "
       "`vector_search_ef_search` API parameter.")
  :type       :positive-integer
  :default    40
  :encryption :no
  :export?    false
  :visibility :internal
  :doc        false)

(defsetting semantic-search-max-scan-tuples
  (str "Default pgvector `hnsw.max_scan_tuples` (soft cap on tuples an iterative scan visits) for the "
       "`hnsw-iterative-*` strategies. Larger values improve recall under selective filters at the cost of "
       "latency. Individual requests may override this via the `vector_search_max_scan_tuples` API parameter.")
  :type       :positive-integer
  :default    20000
  :encryption :no
  :export?    false
  :visibility :internal
  :doc        false)

;; The `vector_search_explain` API parameter only covers requests you author yourself; this setting exists
;; to instrument organic traffic. The frontend issues the real search requests and cannot pass the
;; parameter, so populating the vector-scan Prometheus counters and waterfall logs over production traffic
;; means flipping instrumentation on instance-wide (MB_SEMANTIC_SEARCH_EXPLAIN on hosted, no deploy).
;; EXPLAIN ANALYZE re-runs the inner vector subquery, so the intended lifecycle is on-for-an-investigation,
;; then off.
(defsetting semantic-search-explain
  (str "Run EXPLAIN (ANALYZE) instrumentation of the inner vector subquery for every semantic search? "
       "Expensive (re-executes the inner query); intended for ad-hoc analysis. Individual requests may "
       "override this via the `vector_search_explain` API parameter.")
  :type       :boolean
  :default    false
  :encryption :no
  :export?    false
  :visibility :internal
  :doc        false)

(defsetting semantic-search-min-results-threshold
  "Minimum number of semantic search results required before falling back to other engines."
  :type :integer
  :default 100
  :encryption :no
  :export? false
  :visibility :internal
  :doc false)

(defsetting index-update-thread-count
  "Number of threads to use for batched index updates, including embedding requests"
  :type :integer
  :default 2
  :encryption :no
  :export? false
  :visibility :internal
  :doc "Number of threads to use for batched index updates, including embedding requests")

(defsetting ee-search-gate-write-timeout
  (str "Timeout of gate write statements in seconds. Used to determine lag tolerance of the indexer (see the "
       "[[metabase-enterprise.search.semantic.gate/poll]]) in conjunction "
       "with `ee-search-indexer-lag-tolerance-multiplier`.")
  :type :integer
  :default 5
  :export? false
  :visibility :internal
  :doc false)

(defsetting ee-search-gate-max-batch-size
  "The maximum number of documents that can be sent to `gate-documents!` without causing an error."
  :type :integer
  :default 512
  :export? false
  :visibility :internal
  :doc false)

(defsetting ee-search-indexer-poll-limit
  "Indexer poll limit."
  :type :integer
  :default 1000
  :export? false
  :visibility :internal
  :doc false)

(defsetting ee-search-indexer-exit-early-cold-duration
  "Number of seconds indexer should wait to see new data before yielding back to quartz."
  :type :integer
  :default 30
  :export? false
  :visibility :internal
  :doc false)

(defsetting ee-search-indexer-max-run-duration
  "Number of minutes we expect to run the indexer loop for before yielding to quartz."
  :type :integer
  :default 60
  :export? false
  :visibility :internal
  :doc false)

(defsetting ee-search-indexer-lag-tolerance-multiplier
  (str "Multiplier for computation of [[metabase-enterprise.search.semantic.indexer/lag-tolerance]]. The formula "
       "is `ee-search-gate-write-timeout * ee-search-indexer-lag-tolerance-multiplier`.")
  :type :integer
  :default 2
  :export? false
  :visibility :internal
  :doc false)

(defsetting stale-index-retention-hours
  "Number of hours to retain stale semantic search indexes before cleanup."
  :type :integer
  :default 24
  :encryption :no
  :export? false
  :visibility :internal
  :doc false)

(defsetting tombstone-retention-hours
  "Number of hours to retain tombstone records in the gate table before cleanup."
  :type :integer
  :default 24
  :encryption :no
  :export? false
  :visibility :internal
  :doc false)

(defsetting repair-table-retention-hours
  "Number of hours until repair tables are considered stale and eligible for cleanup, if they were not
    cleaned up successfully after use."
  :type :integer
  :default 2
  :encryption :no
  :export? false
  :visibility :internal
  :doc false)