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
Metabot NLQ collection picker (#67051)
Noah Moss337885f0b8ca
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/api/metabot.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/api/metabot.cljindex 4c2b19c1b9f..a9412781869 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/api/metabot.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/api/metabot.clj@@ -1,7 +1,6 @@(ns metabase-enterprise.metabot-v3.api.metabot"`/api/ee/metabot-v3/metabot` routes"(:require- [metabase-enterprise.metabot-v3.config :as metabot-v3.config][metabase-enterprise.metabot-v3.suggested-prompts :as metabot-v3.suggested-prompts][metabase-enterprise.metabot-v3.tools.util :as metabot-v3.tools.u][metabase.api.common :as api]@@ -50,11 +49,6 @@(api/check-superuser)(api/check-404 (t2/exists? :model/Metabot :id id))(let [old-metabot (t2/select-one :model/Metabot :id id)]- ;; Prevent updating collection_id on the primary metabot instance- (when (and (contains? metabot-updates :collection_id)- (= (:entity_id old-metabot)- (get-in metabot-v3.config/metabot-config [metabot-v3.config/internal-metabot-id :entity-id])))- (api/check-400 false "Cannot update collection_id for the primary metabot instance."));; Prevent enabling verified content without the premium feature(when (:use_verified_content metabot-updates)(premium-features/assert-has-feature :content-verification (tru "Content verification")))diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/deftool.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/deftool.cljindex 12e909b0da7..f6d8ee53800 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/deftool.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/deftool.clj@@ -11,8 +11,10 @@;;; ---------------------------------------------------- Schemas ----------------------------------------------------(mr/def ::tool-request- "Base schema for all tool requests. Contains the conversation ID."- [:map [:conversation_id ms/UUIDString]])+ "Base schema for all tool requests. Contains the conversation ID and optional profile ID."+ [:map+ [:conversation_id ms/UUIDString]+ [:profile_id {:optional true} [:maybe :string]]])(def ^:private request-transformer"Transformer for encoding tool request arguments (snake_case -> kebab-case)."@@ -36,8 +38,8 @@- `:handler` - Function to call with encoded arguments (receives args map with :metabot-id)The handler receives a single map argument containing the encoded args plus `:metabot-id`- (extracted from the request). For no-args tools, the handler receives just `{:metabot-id ...}`."- [{:keys [arguments conversation_id] :as body}+ and `:profile-id` (extracted from the request body)."+ [{:keys [arguments conversation_id profile_id] :as body}request{:keys [api-name args-schema result-schema handler]}](metabot-v3.context/log (assoc body :api api-name) :llm.log/llm->be)@@ -45,7 +47,8 @@encoded-args (cond-> (if args-schema(mc/encode args-schema arguments request-transformer){})- metabot-id (assoc :metabot-id metabot-id))+ metabot-id (assoc :metabot-id metabot-id)+ profile_id (assoc :profile-id profile_id))raw-result (handler encoded-args)result (if result-schema(mc/decode result-schema raw-result response-transformer)@@ -61,7 +64,7 @@- Encoding arguments from snake_case to kebab-case (via `:tool-api-request` transformer)- Decoding results from kebab-case to snake_case (via `:tool-api-response` transformer)- Attaching `conversation_id` to responses- - Extracting `:metabot-id` from the request and including it in the args map+ - Extracting `:metabot-id` and `:profile-id` from the request body and including them in the args mapOptions::args-schema - Malli schema for arguments (optional - omit for no-args tools)diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/search.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/search.cljindex cc6501ce337..9733adc9dbe 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/search.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/search.clj@@ -138,7 +138,7 @@"Search for data sources (tables, models, cards, dashboards, metrics, transforms) in Metabase.Abstracted from the API endpoint logic."[{:keys [term-queries semantic-queries database-id created-at last-edited-at- entity-types limit metabot-id search-native-query weights]}]+ entity-types limit metabot-id profile-id search-native-query weights]}](log/infof "[METABOT-SEARCH] Starting search with params: %s"{:term-queries term-queries:semantic-queries semantic-queries@@ -148,6 +148,7 @@:entity-types entity-types:limit limit:metabot-id metabot-id+ :profile-id profile-id:search-native-query search-native-query:weights weights})(let [search-models (if (seq entity-types)@@ -158,6 +159,9 @@use-verified? (if metabot-id(:use_verified_content metabot)false)+ embedded-metabot? (= metabot-id metabot-v3.config/embedded-metabot-id)+ collection-id (when (or embedded-metabot? (= profile-id "nlq"))+ (:collection_id metabot))limit (or limit 50)search-fn (fn [search-string search-engine](let [search-context (search/search-context@@ -185,7 +189,9 @@weights(assoc :weights weights)search-engine- (assoc :search-engine (name search-engine))))+ (assoc :search-engine (name search-engine))+ collection-id+ (assoc :collection collection-id)))_ (log/infof "[METABOT-SEARCH] Search context models for query '%s': %s"search-string (:models search-context))search-results (search/search search-context)diff --git a/enterprise/backend/test/metabase_enterprise/metabot_v3/api/metabot_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_v3/api/metabot_test.cljindex 0c7b8430c0f..2d9867a60db 100644--- a/enterprise/backend/test/metabase_enterprise/metabot_v3/api/metabot_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot_v3/api/metabot_test.clj@@ -3,7 +3,6 @@[clojure.set :as set][clojure.test :refer :all][metabase-enterprise.metabot-v3.client :as metabot-v3.client]- [metabase-enterprise.metabot-v3.config :as metabot-v3.config][metabase-enterprise.metabot-v3.suggested-prompts :as metabot-v3.suggested-prompts][metabase.collections.models.collection :as collection][metabase.lib.core :as lib]@@ -272,26 +271,7 @@(is (= "Content verification is a paid feature not currently available to your instance. Please upgrade to use it. Learn more at metabase.com/upgrade/"(:message (mt/user-http-request :crowberto :put 402(format "ee/metabot-v3/metabot/%d" metabot-id)- {:use_verified_content true}))))))-- (testing "should prevent updating collection_id on primary metabot instance"- (let [metabot-id (metabot-v3.config/normalize-metabot-id metabot-v3.config/internal-metabot-id)]- (is (= "Cannot update collection_id for the primary metabot instance."- (mt/user-http-request :crowberto :put 400-- (format "ee/metabot-v3/metabot/%d" metabot-id)- {:collection_id collection-id-1})))- ;; Verify the collection_id was not updated- (let [unchanged-metabot (t2/select-one :model/Metabot :id metabot-id)]- (is (= nil (:collection_id unchanged-metabot))))-- ;; Verify that updating other fields still works- (with-redefs [metabot-v3.suggested-prompts/generate-sample-prompts (constantly nil)]- (let [response (mt/user-http-request :crowberto :put 200- (format "ee/metabot-v3/metabot/%d" metabot-id)- {:use_verified_content true})]- (is (true? (:use_verified_content response)))- (is (= nil (:collection_id response)))))))))))+ {:use_verified_content true}))))))))))(deftest metabot-prompt-regeneration-on-config-change-test(mt/dataset test-datadiff --git a/enterprise/backend/test/metabase_enterprise/metabot_v3/client_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_v3/client_test.cljindex 2acd9d3d0fb..67d2d470289 100644--- a/enterprise/backend/test/metabase_enterprise/metabot_v3/client_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot_v3/client_test.clj@@ -111,32 +111,38 @@"Exception data should not contain :headers in response"))))))))))(deftest example-generation-payload-unknown-field-types-test- (let [mp (mt/metadata-provider)]- (mt/with-temp [:model/Card c {:type :metric- :dataset_query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))- (lib/aggregate- (lib/count (lib.metadata/field mp (mt/id :orders :total)))))- ;; Set large view count to ensure metric is picked in `generate-sample-prompts`.- :view_count 1000}]- ;; Add column with type/* to metric result_metadata- (t2/update! :model/Card :id (:id c)- {:result_metadata (conj (:result_metadata (t2/select-one :model/Card :id (:id c)))- {:database_type "point"- :base_type :type/*- :effective_type :type/*- :name "dummy"})})- (testing "No exception is thrown when type/* column is present it result_metadata"- (is (= :not-thrown- (try- ;; Override calls to ai service as we are interested in no exception being thrown- ;; prior to those calls.- (with-redefs [metabot-v3.client/post! (constantly {:status 200- :body {:table_questions []- :metric_questions []}})]- (let [metabot-eid (get-in metabot-v3.config/metabot-config [metabot-v3.config/internal-metabot-id- :entity-id])- metabot-id (t2/select-one-fn :id :model/Metabot :entity_id metabot-eid)]- (metabot-v3.suggested-prompts/generate-sample-prompts metabot-id)- :not-thrown))- (catch Exception e- e))))))))+ (let [mp (mt/metadata-provider)+ metabot-eid (get-in metabot-v3.config/metabot-config [metabot-v3.config/internal-metabot-id+ :entity-id])+ original-collection-id (t2/select-one-fn :collection_id :model/Metabot :entity_id metabot-eid)]+ ;; Ensure internal metabot is set to the root collection for generating prompts+ (t2/update! :model/Metabot :entity_id metabot-eid {:collection_id nil})+ (try+ (mt/with-temp [:model/Card c {:type :metric+ :dataset_query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate+ (lib/count (lib.metadata/field mp (mt/id :orders :total)))))+ ;; Set large view count to ensure metric is picked in `generate-sample-prompts`.+ :view_count 1000}]+ ;; Add column with type/* to metric result_metadata+ (t2/update! :model/Card :id (:id c)+ {:result_metadata (conj (:result_metadata (t2/select-one :model/Card :id (:id c)))+ {:database_type "point"+ :base_type :type/*+ :effective_type :type/*+ :name "dummy"})})+ (testing "No exception is thrown when type/* column is present it result_metadata"+ (is (= :not-thrown+ (try+ ;; Override calls to ai service as we are interested in no exception being thrown+ ;; prior to those calls.+ (with-redefs [metabot-v3.client/post! (constantly {:status 200+ :body {:table_questions []+ :metric_questions []}})]+ (let [metabot-id (t2/select-one-fn :id :model/Metabot :entity_id metabot-eid)]+ (metabot-v3.suggested-prompts/generate-sample-prompts metabot-id)+ :not-thrown))+ (catch Exception e+ e))))))+ (finally+ (t2/update! :model/Metabot :entity_id metabot-eid {:collection_id original-collection-id})))))diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.tsxindex 00deef88571..28a656a6c76 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.tsx@@ -93,9 +93,14 @@ export function MetabotAdminPage() {<MetabotVerifiedContentConfigurationPane metabot={metabot} />- {isEmbedMetabot && (- <MetabotCollectionConfigurationPane metabot={metabot} />- )}+ <MetabotCollectionConfigurationPane+ metabot={metabot}+ title={+ isEmbedMetabot+ ? undefined+ : t`Collection for natural language querying`+ }+ /><MetabotPromptSuggestionPane metabot={metabot} /></SettingsSection>@@ -186,8 +191,10 @@ function MetabotVerifiedContentConfigurationPane({function MetabotCollectionConfigurationPane({metabot,+ title,}: {metabot: MetabotInfo;+ title?: string;}) {const metabotId = metabot.id;const metabotName = metabot.name;@@ -230,13 +237,12 @@ function MetabotCollectionConfigurationPane({}};+ const defaultTitle = c("{0} is the name of an AI assistant")+ .t`Collection ${metabotName} can use`;+return (<Box>- <SettingHeader- id="allow-metabot"- title={c("{0} is the name of an AI assistant")- .t`Collection ${metabotName} can use`}- />+ <SettingHeader id="allow-metabot" title={title ?? defaultTitle} /><CollectionInfo collection={collection} /><Flex gap="md" mt="md"><Button onClick={open} leftSection={isUpdating && <Loader size="xs" />}>diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.unit.spec.tsxindex 6556d8a4b04..a58073645f4 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.unit.spec.tsx@@ -130,12 +130,12 @@ describe("MetabotAdminPage", () => {expect(screen.getByText("Embedded Metabot")).toBeInTheDocument();});- it("should not be able to select a collection for default metabot", async () => {+ it("should show collection picker for default metabot with NLQ title", async () => {await setup();expect(await screen.findByText("Configure Metabot")).toBeInTheDocument();expect(- screen.queryByText("Collection Metabot can use"),- ).not.toBeInTheDocument();+ screen.getByText("Collection for natural language querying"),+ ).toBeInTheDocument();});it("should render a selected collection for embedded metabot", async () => {diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/state/selectors.ts b/enterprise/frontend/src/metabase-enterprise/metabot/state/selectors.tsindex 3ecc280edab..855e3d5c900 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/state/selectors.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/state/selectors.ts@@ -1,9 +1,7 @@import { createSelector } from "@reduxjs/toolkit";import _ from "underscore";-import * as Urls from "metabase/lib/urls";import { getIsEmbedding } from "metabase/selectors/embed";-import { getLocation } from "metabase/selectors/routing";import type { TransformId } from "metabase-types/api";import {@@ -35,15 +33,6 @@ export const getMetabotId = createSelector(getIsEmbedding, (isEmbedding) =>isEmbedding ? FIXED_METABOT_IDS.EMBEDDED : FIXED_METABOT_IDS.DEFAULT,);-export const getUseCase = createSelector(- getLocation,- (location): "transforms" | "omnibot" => {- return location.pathname.startsWith(Urls.transformList())- ? "transforms"- : "omnibot";- },-);-export const getDebugMode = createSelector(getMetabotState,(state) => state.debugMode,@@ -209,15 +198,13 @@ export const getProfileOverride = createSelector(export const getAgentRequestMetadata = createSelector(getHistory,getMetabotRequestState,- getUseCase,getProfileOverride,- (history, state, useCase, profileOverride) => ({+ (history, state, profileOverride) => ({state,// NOTE: need end to end support for ids on messages as BE will error if ids are presenthistory: history.map((h) =>h.id && h.id.startsWith(`msg_`) ? _.omit(h, "id") : h,),- use_case: useCase,...(profileOverride ? { profile_id: profileOverride } : {}),}),);