AI Assistant

GoodData.UI provides a React component for embedding of the chat interface for AI Assistant.

AI Assistant

Features

  • Embed UI for the AI Assistant chat.
  • Subscribe to chat events.
  • Handle links in chat messages.
  • Theming is supported out of the box through Theme Provider.

Basic integration example

GenAIChat component renders chat history and an input field for sending user messages. It does not include the logic for overlay management in case you want to render it in a floating window.

import * as React from "react";
import {
    GenAIChat,
    ChatUserMessageEvent,
    isChatUserMessageEvent,
    LinkHandlerEvent,
} from "@gooddata/sdk-ui-gen-ai";

// Import required styles
import "@gooddata/sdk-ui-gen-ai/styles/css/main.css";

const App = () => {
    return (
        <div style={{ width: 500, height: 600, display: "flex" }}>
            {/* Wrap the chat UI in a container of desired size */}
            <GenAIChat
                // Optionally, add event listeners
                eventHandlers={[
                    {
                        eval: isChatUserMessageEvent,
                        handler: (event: ChatUserMessageEvent) => {
                            console.log(`User message: ${event.question}`);
                        },
                    },
                ]}
                // Optionally, provide links handler
                onLinkClick={(linkClickEvent: LinkHandlerEvent) => {
                    linkClickEvent.preventDefault();
                    console.log(`User click: ${linkClickEvent.itemUrl}`);
                    // E.g. when user asks the chatbot to find
                    // a specific dashboard and clicks on the result
                }}
            />
        </div>
    );
};

Props

NameTypeDefaultDescription
localeILocale“en-US”Specifies the locale for internationalization
backendIAnalyticalBackend-Backend instance. Falls back to BackendProvider context if not specified
workspacestring-Workspace ID. Falls back to WorkspaceProvider context if not specified
colorPaletteIColorPalette-Color palette used for rendering the visualizations. If not provided, the default color palette will be used
catalogItemsCatalogItem[]-Catalog items used for autocompletion. If not provided - will be lazy-loaded when needed
eventHandlersChatEventHandler[]-Event handlers for user interactions with the chat UI
onLinkClick(LinkHandlerEvent) => void-Handle user clicks on the catalog items mentioned in chat.

eventHandlers

Each event handler must comply with the following interface:

export interface ChatEventHandler<TEvent extends ChatEvent = any> {
    /**
     * A guard for a specific event type.
     */
    eval: (event: ChatEvent) => event is TEvent;
    /**
     * Event handler.
     */
    handler: (event: TEvent) => void;
}

@gooddata/sdk-ui-gen-ai provides a set of guards for all the events that can be emitted by the chat UI.

Here is a list of the relevant events:

Event nameGuard nameDescription
ChatResetEventisChatResetEventChat history was reset
ChatUserMessageEventisChatUserMessageEventUser sent a message
ChatAssistantMessageEventisChatAssistantMessageEventAssistant responded with a message
ChatFeedbackEventisChatFeedbackEventUser gave a feedback
ChatVisualizationErrorEventisChatVisualizationErrorEventVisualization failed to render

onLinkClick

Handle user clicks on the catalog items mentioned in chat. If not provided, catalog items will be rendered as plain text. Each event contains the following properties:

PropertyTypeDescription
typestringType of the metadata object the user clicked on. For example, “dashboard”, “metric” etc.
idstringThe ID of the metadata object
itemUrlstringThe URL of the metadata object, if opened in GoodData Web interface
newTabbooleanWhether the link should be opened in a new tab
preventDefault() => voidPrevent default behavior of the link click

Resetting the chat thread

To reset the chat thread, use the reset method on the ChatThread interface.

const chatThread = backend.workspace(workspaceId).genAI().getChatThread();

await chatThread.reset();