The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Remove metabot describe API, openai client, and unused deps (#71774)
Edward Paget1038b9d7bdf5
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 3028cabb3bf..a53a8574713 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -216,8 +216,7 @@analyze{:team "Graphy"- :api #{metabase.analyze.core- metabase.analyze.query-results}+ :api #{metabase.analyze.core}:uses #{configdriverlib@@ -1176,7 +1175,6 @@slackbot}:uses #{activity-feedanalytics- analyzeapiapp-dbaudit-appdiff --git a/deps.edn b/deps.ednindex 78eff2462ea..d8cd42c0dc1 100644--- a/deps.edn+++ b/deps.edn@@ -94,7 +94,6 @@junegunn/grouper {:mvn/version "0.1.1"} ; Batch processing helperkixi/stats {:mvn/version "0.5.7" ; Various statistic measures implemented as transducers:exclusions [org.clojure/data.avl]}- lambdaisland/uri {:mvn/version "1.19.155"} ; Used by openai-clojuremedley/medley {:mvn/version "1.4.0"} ; lightweight lib of useful functionsmetabase/connection-pool {:mvn/version "1.2.0"} ; simple wrapper around C3P0. JDBC connection poolsmetabase/saml20-clj {:mvn/version "4.2.1"@@ -109,8 +108,6 @@:git/sha "b4a2d78647a049870b316753d7aae7fb4ade3ae5"} ; patched to resolve an issue causing OOMnet.carcdr/oidc-provider {:mvn/version "0.6.2"} ; OAuth/OIDC server abstract implementationnet.cgrand/macrovich {:mvn/version "0.2.2"} ; utils for writing macros for both Clojure & ClojureScript- net.clojars.wkok/openai-clojure {:mvn/version "0.23.0"- :exclusions [lambdaisland/uri]} ; OpenAInet.redhogs.cronparser/cron-parser-core {:mvn/version "3.5" ; describe Cron schedule in human-readable language:exclusions [org.slf4j/slf4j-api]}net.sf.cssbox/cssbox {:mvn/version "5.0.2" ; HTML / CSS renderingdiff --git a/docs/developers-guide/api-changelog.md b/docs/developers-guide/api-changelog.mdindex 0a416427438..9c63b43b5b6 100644--- a/docs/developers-guide/api-changelog.md+++ b/docs/developers-guide/api-changelog.md@@ -4,6 +4,12 @@ title: API changelog# Breaking changes to the API interface+## Metabase 0.61.0++- `POST /api/metabot/describe/card` and `POST /api/metabot/describe/dashboard/:id` have been removed. These endpoints+ provided LLM-powered autodescription of cards and dashboards using the legacy OpenAI client. This functionality has+ been superseded by the Metabot agent.+## Metabase 0.57.0- MBQL queries (in Cards and elsewhere) are now serialized as MBQL 5 as opposed to MBQL 4 (aka legacy MBQL) in thediff --git a/src/metabase/metabot/api.clj b/src/metabase/metabot/api.cljindex 98d7cfcc22e..f8c9948efdf 100644--- a/src/metabase/metabot/api.clj+++ b/src/metabase/metabot/api.clj@@ -10,7 +10,6 @@[metabase.app-db.core :as app-db][metabase.config.core :as config][metabase.metabot.agent.core :as agent]- [metabase.metabot.api.describe][metabase.metabot.api.document][metabase.metabot.api.metabot][metabase.metabot.config :as metabot.config]@@ -32,9 +31,6 @@(:import(java.io OutputStream)))-(comment- metabase.metabot.api.describe/keep-me)-(set! *warn-on-reflection* true)(defn- store-aiservice-messages!@@ -433,5 +429,4 @@"/document" metabase.metabot.api.document/routes;; premium check happens in the route so we still ack events to prevent slack retrying"/slack" metabase.slackbot.api/routes})- (api.macros/ns-handler 'metabase.metabot.api.describe +auth)(api.macros/ns-handler *ns* +auth)))diff --git a/src/metabase/metabot/api/describe.clj b/src/metabase/metabot/api/describe.cljdeleted file mode 100644index 5da49e04b5d..00000000000--- a/src/metabase/metabot/api/describe.clj+++ /dev/null@@ -1,44 +0,0 @@-(ns metabase.metabot.api.describe- "API endpoints for LLM-powered autodescription of cards and dashboards."- (:require- [metabase.analyze.query-results :as qr]- [metabase.api.macros :as api.macros]- [metabase.metabot.config :as metabot.config]- [metabase.metabot.tasks.describe-dashboard :refer [describe-dashboard]]- [metabase.metabot.tasks.describe-question :refer [describe-question]]- [metabase.parameters.schema :as parameters.schema]- [metabase.util.malli.schema :as ms]))--;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to-;; use our API + we will need it when we make auto-TypeScript-signature generation happen-;;-#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}-(api.macros/defendpoint :post "/describe/card"- "Summarize a question."- [_route-params- _query-params- body :- [:map- [:dataset {:optional true} [:maybe :boolean]]- [:dataset_query ms/Map]- [:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter]]]- [:parameter_mappings {:optional true} [:maybe [:sequential ::parameters.schema/parameter-mapping]]]- [:description {:optional true} [:maybe ms/NonBlankString]]- [:display ms/NonBlankString]- [:visualization_settings ms/Map]- [:collection_id {:optional true} [:maybe ms/PositiveInt]]- [:collection_position {:optional true} [:maybe ms/PositiveInt]]- [:result_metadata {:optional true} [:maybe qr/ResultsMetadata]]- [:cache_ttl {:optional true} [:maybe ms/PositiveInt]]]]- (metabot.config/check-metabot-enabled!)- {:summary (describe-question body)})--;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to-;; use our API + we will need it when we make auto-TypeScript-signature generation happen-;;-#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}-(api.macros/defendpoint :post "/describe/dashboard/:id"- "Provide a summary of a dashboard."- [{:keys [id]} :- [:map- [:id ms/PositiveInt]]]- (metabot.config/check-metabot-enabled!)- {:summary (describe-dashboard id)})diff --git a/src/metabase/metabot/openai_client.clj b/src/metabase/metabot/openai_client.cljdeleted file mode 100644index a7da9a93700..00000000000--- a/src/metabase/metabot/openai_client.clj+++ /dev/null@@ -1,195 +0,0 @@-(ns metabase.metabot.openai-client- "A wrapper around the OpenAI client API.-- The `create-chat-completion` function combines several useful middlewares to create a client that can be used for AI- completions. Note that this is a function that returns a function. Example usage is as follows:-- ```clojure- ((create-chat-completion)- {:messages [{:role \"system\" :content \"You are a helpful assistant.\"}- {:role \"user\" :content \"Who won the world series in 2020?\"}- {:role \"assistant\" :content \"The Los Angeles Dodgers won the World Series in 2020.\"}- {:role \"user\" :content \"Where was it played?\"}]})- ```-- The user need only provide a map of messages following the standard- [OpenAI chat completions API format](https://platform.openai.com/docs/guides/text-generation/chat-completions-api).-- Note that while this is not a _de jure_ standard, it is evolving as a _de facto_ standard as can be seen by use of the- [OpenAI Client](https://platform.openai.com/docs/libraries) with [Llama API](https://docs.llama-api.com/essentials/chat).- "- (:require- [metabase.analytics.core :as analytics]- [metabase.llm.settings :as llm-settings]- [metabase.util.json :as json]- [metabase.util.log :as log]- [wkok.openai-clojure.api :as openai.api]))--(set! *warn-on-reflection* true)--(defn- wrap-usage- "A middleware that captures usage data and logs it for analytics and billing purposes."- [openai-fn]- (fn wrap-usage*- ([params options]- (let [{:keys [usage] :as response} (openai-fn params options)- usage-summary (-> (dissoc response :usage :choices)- (merge usage)- (select-keys [:id :object :created :model :prompt_tokens :completion_tokens :total_tokens :system_fingerprint]))]- (analytics/track-event! :snowplow/llm_usage- (assoc- usage-summary- :event :llm-usage))- response))- ([params] (wrap-usage* params nil))))--(defn- wrap-openai-exceptions- "Wrap our openai calls with a standard set of exceptions that will percolate up any issues to the UI as meaningful error messages."- [openai-fn]- (fn wrap-openai-exceptions*- ([params options]- (try- (openai-fn params options)- (catch Exception e- (log/warnf "Exception when calling function: %s" (.getMessage e))- (throw- ;; If we have ex-data, we'll assume were intercepting an openai.api/create-chat-completion response- (if-some [status (:status (ex-data e))]- (let [{:keys [body]} (ex-data e)- {:keys [error]} (json/decode+kw body)- {error-type :type :keys [message code]} error]- (case (int status)- 400 (do- (log/warnf "%s: %s" code message)- (ex-info- message- {:message message- :status-code 400}))- 401 (ex-info- "Credentials are incorrect or not set.\nCheck with your administrator that the correct API keys are set."- {:message "Credentials are incorrect or not set.\nCheck with your administrator that the correct API keys are set."- ;; Don't actually produce a 401 because you'll get redirect do the home page.- :status-code 400})- 429 (if (= error-type "insufficient_quota")- (ex-info- "You exceeded your current OpenAI billing quota, please check your OpenAI plan and billing details."- {:message "You exceeded your current OpenAI billing quota, please check your OpenAI plan and billing details."- :status-code status})- (ex-info- "Server is under heavy load and cannot process your request at this time.\nPlease try again."- {:message "The server is under heavy load and cannot process your request at this time.\nPlease try again."- :status-code status}))- ;; Just re-throw it until we get a better handle on- (ex-info- "Error calling remote server.\nPlease try again."- {:message "The server is under heavy load and cannot process your request at this time.\nPlease try again."- :status-code 500})))- ;; If there's no ex-data, we'll assume it's some other issue and generate a 400- (ex-info- (ex-message e)- {:exception-data (ex-data e)- :status-code 400}))))))- ([params] (wrap-openai-exceptions* params nil))))--(defn wrap-model-defaults- "Add the default model from settings into the request."- [openai-fn]- (fn wrap-model-defaults*- ([params options]- (openai-fn- (merge- {:model (llm-settings/llm-openai-model)}- params)- options))- ([params] (wrap-model-defaults* params nil))))--(defn wrap-auth- "Add the API key from settings into the request."- [openai-fn]- (fn wrap-auth*- ([params options]- (openai-fn- params- (merge- {:api-key (llm-settings/llm-openai-api-key)}- options)))- ([params]- (wrap-auth* params nil))))--(defn wrap-find-result- "Return the first choice for which a message is present and has content."- [openai-fn]- (fn wrap-find-result*- ([params options]- (let [{:keys [choices]} (openai-fn params options)]- (some- (fn [{:keys [message]}] (:content message))- choices)))- ([params]- (wrap-find-result* params nil))))--(defn wrap-parse-json- "Parse a JSON result from the response. Note that this must be called after- wrap-find-result or similar such that the response is a string."- ([openai-fn postprocess-fn]- (fn wrap-parse-json*- ([params options]- (let [response (openai-fn params options)]- (if (string? response)- (try- ;; Usually you just get json back, but sometimes you get a markdown block.- (let [md-regex #"(?s)\s*```(?:json)?(.*)```\s*"- response (or- (second (re-matches md-regex response))- response)]- (postprocess-fn (json/decode+kw response)))- (catch Exception e- (throw- (do- (log/warnf "Unparseable JSON string: %s" response)- (ex-info- (.getMessage e)- {:message (.getMessage e)- :response response- :status-code 500})))))- (throw- (do- (log/warnf "Not a string: %s" response)- (ex-info- "Response was not a string"- {:message "Response was not a string"- :response response- :status-code 500}))))))- ([params] (wrap-parse-json* params nil))))- ([openai-fn] (wrap-parse-json openai-fn identity)))--(defn- default-chat-completion-endpoint- "OpenAI is the default completion endpoint"- ([params options]- (openai.api/create-chat-completion- (select-keys params [:model :n :messages])- options))- ([params] (default-chat-completion-endpoint params nil)))--(def ^:dynamic ^{:arglists '([params options])}- *create-chat-completion-endpoint*- "The endpoint used to invoke the remote LLM"- default-chat-completion-endpoint)--(defn create-chat-completion- "Call the llm service and return the response.- Takes messages to be used as instructions and a function that will find the- first valid result from the messages.-- This is a function since we don't want to capture the scope of the- *create-chat-completion-endpoint* var in the event that it needs to be rebound.-- As with all middlewares, note that preprocessing middlewares happen from the- bottom up and postprocessing middlewares happen from the top down."- []- (-> *create-chat-completion-endpoint*- wrap-auth- wrap-model-defaults- wrap-openai-exceptions- wrap-usage- wrap-find-result))diff --git a/src/metabase/metabot/tasks/describe_dashboard.clj b/src/metabase/metabot/tasks/describe_dashboard.cljdeleted file mode 100644index 415f43ade2b..00000000000--- a/src/metabase/metabot/tasks/describe_dashboard.clj+++ /dev/null@@ -1,92 +0,0 @@-(ns metabase.metabot.tasks.describe-dashboard- "LLM task(s) for generating dashboard descriptions"- (:require- [clojure.set :refer [rename-keys]]- [clojure.walk :as walk]- [metabase.metabot.openai-client :as openai-client]- [metabase.util :as u]- [metabase.util.json :as json]- [toucan2.core :as t2]))--(defn- dashboard->prompt-data- "Create a data-oriented summary of a dashboard as input to an LLM for summarization."- [dashboard-id]- (let [{dashboard-name :name :keys [parameters dashcards]}- (t2/hydrate (t2/select-one :model/Dashboard dashboard-id) [:dashcards- :card- :series- :dashcard/action- :dashcard/linkcard-info]- :tabs- :param_fields)- param-id->param (zipmap- (map :id parameters)- (map (fn [param]- (-> (select-keys param [:name :type])- (rename-keys {:name :parameter-name :type :filter-type})- (update :filter-type name)))- parameters))]- {:dashboard-name dashboard-name- :charts (for [{:keys [card parameter_mappings]} dashcards- :let [{card-name :name- :keys [display- description- visualization_settings- result_metadata]} card- field-name->display-name (zipmap- (map :name result_metadata)- (mapv (some-fn :display_name :name) result_metadata))- visualization_settings (->> visualization_settings- u/remove-nils- (walk/prewalk (fn [v] (field-name->display-name v v))))]]- (cond->- {:chart-name card-name- :chart-description description- :chart-type display- :data-column-names (vals field-name->display-name)- :chart-parameters (mapv- (comp :parameter-name param-id->param :parameter_id)- parameter_mappings)}- (seq visualization_settings)- (assoc :chart-settings visualization_settings)))- :global-parameters (vals param-id->param)}))--(defn describe-dashboard- "Create a human-friendly summary of a dashboard. Returns a map of the form:- {:description \"Some inferred description\"}"- [dashboard-id]- (let [dashboard-summary (dashboard->prompt-data dashboard-id)- summary-with-prompts (merge dashboard-summary- {:description "%%FILL_THIS_DESCRIPTION_IN%%"- :keywords "%%FILL_THESE_KEYWORDS_IN%%"- :questions "%%FILL_THESE_QUESTIONS_IN%%"})- json-str (json/encode summary-with-prompts)- client (-> (openai-client/create-chat-completion)- (openai-client/wrap-parse-json- (fn [{:keys [description keywords questions]}]- {:description (format "Keywords: %s\n\nDescription: %s\n\nQuestions:\n%s"- keywords- description- questions)})))]- (client- {:messages- [{:role "system"- :content "You are a helpful assistant that summarizes dashboards I am generating for my customers by- filling in the missing \"description\", \"keywords\", and \"questions\" keys in a json fragment."}- {:role "assistant"- :content "The \"description\" key is a user friendly description of the dashboard containing up to- two sentences. This description may not be more than 256 characters."}- {:role "assistant"- :content "The \"keywords\" key is 3-5 single-quoted, comma-separated key words- describing the dashboard (e.g. 'keyword1', 'key word'). Keywords might be used to categorize, concisely- describe, or label the entire dashboard."}- {:role "assistant"- :content "The \"questions\" key contains a markdown-formatted hyphenated list of up to 5 questions this- dashboard might help a user answer. Each question should be on its own line."}- {:role "assistant"- :content "The parts you replace are \"%%FILL_THIS_DESCRIPTION_IN%%\", \"%%FILL_THESE_KEYWORDS_IN%%\",- and \"%%FILL_THESE_QUESTIONS_IN%%\"."}- {:role "assistant"- :content "Return only a json object with the \"description\", \"keywords\", and \"questions\" fields and nothing else."}- {:role "user"- :content json-str}]})))diff --git a/src/metabase/metabot/tasks/describe_question.clj b/src/metabase/metabot/tasks/describe_question.cljdeleted file mode 100644index 8305e83825c..00000000000--- a/src/metabase/metabot/tasks/describe_question.clj+++ /dev/null@@ -1,67 +0,0 @@-(ns metabase.metabot.tasks.describe-question- "LLM task(s) for generating question descriptions"- (:require- [clojure.set :refer [rename-keys]]- [metabase.metabot.openai-client :as openai-client]- [metabase.query-processor.compile :as qp.compile]- [metabase.util :as u]- [metabase.util.json :as json]))--(defn- question->prompt-data- "Create a data-oriented summary of a question as input to an LLM for summarization."- [{:keys [display visualization_settings dataset_query result_metadata]}]- (let [visualization_settings (u/remove-nils visualization_settings)- {:keys [query]} (qp.compile/compile-with-inline-parameters dataset_query)]- (cond->- {:sql_query query- :display_type display- :column_descriptions (zipmap- (map (some-fn :display_name :name) result_metadata)- (map (some-fn :semantic_type :effective_type) result_metadata))}- (seq visualization_settings)- (assoc :visualization_settings visualization_settings))))--(defn describe-question- "Create a human-friendly summary of a card. Returns a map of the form:- {:title \"Some inferred title\"- :description \"Some inferred description\"}"- [question]- (let [{:keys [visualization_settings] :as description} (question->prompt-data question)- summary-with-prompts (merge description- {:friendly_title "%%FILL_THIS_TITLE_IN%%"- :friendly_summary "%%FILL_THIS_SUMMARY_IN%%"})- json-str (json/encode summary-with-prompts)- client (-> (openai-client/create-chat-completion)- (openai-client/wrap-parse-json- (fn [rsp] (rename-keys rsp {:friendly_title :title- :friendly_summary :description}))))]- (client- {:messages- [{:role "system"- :content- "You are a helpful assistant that fills in the missing \"friendly_title\" and- \"friendly_summary\" keys in a json fragment.-- Your summary and title should:- - be concise- - be informative- - be easy to understand by a non-technical audience from a variety of backgrounds- - be discoverable by search engines- - include caveats and limitations from filters (the query clauses) if applicable- - not have caveats stated as fact but rather as suggestions- - not explicitly talk about the visualization type- - not provide any conclusions about the data-- You like to occasionally use emojis to express yourself but are otherwise very serious and professional."}- {:role "assistant"- :content (cond-> "The \"display\" key is how I intend to present the final data."- (seq visualization_settings)- (str " The \"visualization_settings\" key has chart settings."))}- {:role "assistant"- :content "The parts you replace are \"%%FILL_THIS_TITLE_IN%%\" and \"%%FILL_THIS_SUMMARY_IN%%\"."}- {:role "assistant"- :content "Return only a json object with the \"friendly_title\" and \"friendly_summary\" fields and nothing else."}- {:role "assistant"- :content "The \"friendly_title\" must be no more than 64 characters long."}- {:role "user"- :content json-str}]})))diff --git a/test/metabase/metabot/api/describe_test.clj b/test/metabase/metabot/api/describe_test.cljdeleted file mode 100644index e0d3ad8878c..00000000000--- a/test/metabase/metabot/api/describe_test.clj+++ /dev/null@@ -1,90 +0,0 @@-(ns metabase.metabot.api.describe-test- (:require- [clojure.test :refer :all]- [metabase.metabot.openai-client :as openai-client]- [metabase.test :as mt]- [metabase.util.json :as json]))--(deftest metabot-disabled-test- (testing "describe endpoints return 403 when all metabot instances are disabled"- (mt/with-temporary-setting-values [metabot-enabled? false- embedded-metabot-enabled? false]- (mt/dataset test-data- (mt/with-temp [:model/Card card {:name "Orders"- :dataset_query (mt/mbql-query orders)}- :model/Dashboard {dash-id :id} {:name "Dashboard"}]- (is (= "Metabot is not enabled."- (mt/user-http-request :rasta :post 403 "metabot/describe/card" card)))- (is (= "Metabot is not enabled."- (mt/user-http-request :rasta :post 403 (format "metabot/describe/dashboard/%d" dash-id)))))))))--(deftest summarize-card-test- (testing "POST /api/metabot/describe/card"- (mt/dataset test-data- (mt/with-temp [:model/Card card {:name "Orders"- :dataset_query (mt/mbql-query orders)}]- (let [fake-response {:title "Title" :description "Description"}- json-response (json/encode fake-response)- expected {:summary fake-response}]- (testing "Card summarization works in the happy path"- (with-redefs [openai-client/*create-chat-completion-endpoint*- (fn [_ _]- {:choices [{:message {:content json-response}}]})]- (is (= expected- (mt/user-http-request :rasta :post 200 "metabot/describe/card" card)))))- (testing "We can handle json in markdown"- (with-redefs [openai-client/*create-chat-completion-endpoint*- (fn [_ _]- {:choices [{:message {:content- (format- "```json%s```"- json-response)}}]})]- (is (= expected- (mt/user-http-request :rasta :post 200 "metabot/describe/card" card)))))- (testing "We can't handle bad responses"- (with-redefs [openai-client/*create-chat-completion-endpoint*- (fn [_ _]- {:choices [{:message {:content- (format- "This is not a good json message -- %s"- json-response)}}]})]- (is (= 500- (get-in- (mt/user-http-request :rasta :post 500 "metabot/describe/card" card)- [:data :status-code]))))))))))--(deftest summarize-dashboard-test- (testing "POST /api/metabot/describe/dashboard/:id"- (mt/dataset test-data- (mt/with-temp [:model/Card {card-id :id} {:name "Orders"- :dataset_query (mt/mbql-query orders)}- :model/Dashboard {dash-id :id} {:name "Dashboard"}- :model/DashboardCard {_ :id} {:dashboard_id dash-id- :card_id card-id}]- (let [url (format "metabot/describe/dashboard/%s" dash-id)- fake-response {:description "Description"- :keywords "awesome, amazing"- :questions "- What is this?"}- json-response (json/encode fake-response)- expected {:summary {:description "Keywords: awesome, amazing\n\nDescription: Description\n\nQuestions:\n- What is this?"}}]- (testing "Dashboard summarization works in the happy path"- (with-redefs [openai-client/*create-chat-completion-endpoint*- (fn [_ _]- {:choices [{:message {:content json-response}}]})]- (is (= expected- (mt/user-http-request :rasta :post 200 url)))))- (testing "We can handle json in markdown"- (with-redefs [openai-client/*create-chat-completion-endpoint*- (fn [_ _]- {:choices [{:message {:content (format "```json%s```" json-response)}}]})]- (is (= expected- (mt/user-http-request :rasta :post 200 url)))))- (testing "We can't handle bad responses"- (with-redefs [openai-client/*create-chat-completion-endpoint*- (fn [_ _]- {:choices [{:message {:content- (format "This is not a good json message -- %s" json-response)}}]})]- (is (= 500- (get-in- (mt/user-http-request :rasta :post 500 url)- [:data :status-code]))))))))))