Skip to content

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

Metabot glossary integration (#65223)

lbrdnke47e51c2298f
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/dummy_tools.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/dummy_tools.clj
index c7ba47cbe27..06341951582 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/dummy_tools.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/dummy_tools.clj
@@ -12,6 +12,7 @@
[metabase.lib.types.isa :as lib.types.isa]
[metabase.util :as u]
[metabase.util.humanization :as u.humanization]
+ [metabase.util.log :as log]
[metabase.warehouse-schema.models.field-values :as field-values]
[toucan2.core :as t2]))
@@ -27,16 +28,39 @@
{:order-by [[:id :desc]]})]
(= (:status review) "verified")))
+(def ^:private max-glossary-items
+ "Maximal number of items from glossary to include in Metabot's context."
+ 100)
+
+(def ^:private glossary-order-column
+ "Column to order by when selecting glossary items for Metabot's context injection."
+ :updated_at)
+
+(defn- glossary-for-context
+ []
+ ;; Rather small glossary sizes are anticipated. Additional count is bearable.
+ (let [glossary-size (t2/count :model/Glossary)]
+ (when (> glossary-size max-glossary-items)
+ ;; If we are notified about the following warning we should reconsider current,
+ ;; context injection, approach to glossary integration into Metabot.
+ (log/warnf "Glossary size is larger than limit for context injection (%d > %d)."
+ glossary-size max-glossary-items)))
+ (not-empty (t2/select-fn->fn :term :definition :model/Glossary
+ {:order-by [[glossary-order-column :desc]]
+ :limit max-glossary-items})))
+
(defn get-current-user
"Get information about the current user."
[]
(if-let [{:keys [id email first_name last_name]}
(or (some-> api/*current-user* deref)
(t2/select-one [:model/User :id :email :first_name :last_name] api/*current-user-id*))]
- {:structured-output {:id id
- :type :user
- :name (str first_name " " last_name)
- :email-address email}}
+ {:structured-output (merge {:id id
+ :type :user
+ :name (str first_name " " last_name)
+ :email-address email}
+ (when-some [glossary (glossary-for-context)]
+ {:glossary glossary}))}
{:output "current user not found"}))
(defn get-dashboard-details
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/api.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/api.clj
index 614825db8f2..d8dd55ad696 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/api.clj
@@ -463,7 +463,8 @@
[:id :int]
[:type [:= :user]]
[:name :string]
- [:email_address :string]]]]
+ [:email_address :string]
+ [:glossary [:maybe [:map-of :string :string]]]]]]
[:map [:output :string]]])
(mr/def ::get-dashboard-details-result
[:or
diff --git a/enterprise/backend/test/metabase_enterprise/metabot_v3/tools/api_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_v3/tools/api_test.clj
index d9057c22680..81644f637bc 100644
--- a/enterprise/backend/test/metabase_enterprise/metabot_v3/tools/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/metabot_v3/tools/api_test.clj
@@ -493,17 +493,19 @@
(deftest ^:parallel get-current-user-test
(mt/with-premium-features #{:metabot-v3}
- (let [conversation-id (str (random-uuid))
- ai-token (ai-session-token)
- response (mt/user-http-request :rasta :post 200 "ee/metabot-tools/get-current-user"
- {:request-options {:headers {"x-metabase-session" ai-token}}}
- {:conversation_id conversation-id})]
- (is (=? {:structured_output {:id (mt/user->id :rasta)
- :type "user"
- :name "Rasta Toucan"
- :email_address "[email protected]"}
- :conversation_id conversation-id}
- response)))))
+ (mt/with-temp [:model/Glossary _ {:term "asdf" :definition "Sales Team Performance"}]
+ (let [conversation-id (str (random-uuid))
+ ai-token (ai-session-token)
+ response (mt/user-http-request :rasta :post 200 "ee/metabot-tools/get-current-user"
+ {:request-options {:headers {"x-metabase-session" ai-token}}}
+ {:conversation_id conversation-id})]
+ (is (=? {:structured_output {:id (mt/user->id :rasta)
+ :type "user"
+ :name "Rasta Toucan"
+ :email_address "[email protected]"
+ :glossary {(keyword "asdf") "Sales Team Performance"}}
+ :conversation_id conversation-id}
+ response))))))
(deftest get-dashboard-details-test
(mt/with-premium-features #{:metabot-v3}