| title | Modular embedding SDK - AI chat |
|---|---|
| summary | Embed an AI chat component in your app that can create queries from natural language questions. |
{% include plans-blockquote.html feature="Modular embedding SDK" sdk=true convert_pro_link_to_embedding=true%}
You can embed an AI chat in your application similar to Metabot in Metabase.
Embedded Metabot is a more focused version of Metabot designed to work well in an embedded context. Embedded Metabot can only display ad-hoc questions and metrics; it doesn't know about dashboards.
To help embedded Metabot more easily find and focus on the data you care about most, select the collection containing the models and metrics it should be able to use to create queries.
If you're embedding the Metabot component in an app, you can specify a different collection that embedded Metabot is allowed to use for creating queries.
You can check out a demo of the AI chat component on our Shoppy demo site.
{% include_file "{{ dirname }}/snippets/questions/ai-question.tsx" %}{% include_file "{{ dirname }}/api/snippets/MetabotQuestionProps.md" snippet="properties" %}
To configure your embedded AI chat in your Metabase:
- Click the grid icon in the upper right.
- Select Admin.
- Click the AI tab.
- In the left sidebar, click Embedded Metabot.
When embedding the Metabot component in your app, you should specify a collection that embedded Metabot is allowed to use for creating queries. Embedded Metabot will only have access to that collection.
For tips and more, see Metabot settings.
Use the layout prop to specify which layout to use for the Metabot component:
auto(default): Metabot uses thestackedlayout on mobile screens, and asidebarlayout on larger screens.stacked: the question visualization stacks on top of the chat interface.sidebar: the question visualization appears to the left of the chat interface, which is on a sidebar on the right.
If MetabotQuestion's built-in layouts don't fit your app, use the useMetabot hook to read Metabot's conversation state directly and render your own UI. The hook gives you the messages, the chart the agent most recently produced, processing and error state, and actions to submit, cancel, retry, or reset the conversation.
When an agent responds, the message can contain a Chart component. You can walk the agent's messages and render charts inline alongside the chat transcript:
{% include_file "{{ dirname }}/snippets/questions/use-metabot-inline-charts.tsx" %}The CurrentChart component is bound to the latest chart the agent produced. Render CurrentChart once, and it will swap in new charts as the agent creates them. You'll want to filter chart messages out of the transcript so they don't render twice:
{% include_file "{{ dirname }}/snippets/questions/use-metabot-dedicated-chart.tsx" %}- Guard against null while waiting for the SDK bundle:
useMetabotreturnsnulluntil the SDK bundle has loaded and<MetabaseProvider>has mounted. Always guard before use. If you don't guard it, the first render will throwCannot read properties of nullwhen you reach formetabot.messages,metabot.submitMessage, etc., because the SDK ships its Metabot internals via a code-split chunk that isn't available synchronously. - Bring your own Markdown renderer:
MetabotQuestionrenders agent text messages internally, including markdown formatting, transcript scrolling, and input styling. TheuseMetabothook hands you the raw conversation state, which means you own the rendering. In particular, agent text messages (message.type === 'text') contain markdown: links, bold, lists, inline code. The snippets above rendermessage.messageas plain text for brevity, but production usage should pass the text through a markdown renderer (react-markdown,markdown-to-jsx, or your own) so links and formatting display correctly. - Strip links returned by the agent: the agent text may include links pointing back to the host Metabase (like a link to a chart it created). Those links require an authenticated Metabase session, so people won't be able to view the links.


