AI ChatBot Customization

This is a simple App built with NextJs to demonstrate how we can customize a chat bot for following use cases:

I have not hooked the BE to LLM, this repo just shows how we can customize and pass data to the BE from the same re-useable bot and shows it in 2 different scenarios. In future I might update it to show more use cases.

How Its done

Custom Render

// Custom components for special rendering const components = { Chart: Chart, Table: Table, }; // Rules for when to use custom components const renderRules = [ { pattern: /^\/chart/, component: "Chart", }, { pattern: /^\/table/, component: "Table", }, ]; // this is how we invoke the chat bot <ConfigurableAIChat context="You are a helpful AI assistant." systemPrompt={systemPrompt} components={components} renderRules={renderRules} />;
// Custom message renderer that handles special components const MessageRenderer = ({ content, components, renderRules, }: { content: Record<string, any> | string } & Partial<AIConfig>) => { if (typeof content === "object") { // Check content against render rules for (const rule of renderRules!) { console.log(content.responseType, rule.component.toLowerCase()); if (content.responseType === rule.component.toLowerCase()) { const Component = components![rule.component]; if (Component) { return <Component content={content} />; } } } // Default text rendering return <div className="whitespace-pre-wrap">{content.data}</div>; } // Default text rendering for user type return <div className="whitespace-pre-wrap">{content}</div>; };

Dynamic Context

export const ContextProvider = ({ children, initialContext = [], }: { children: React.ReactNode; initialContext: ContextData[]; }) => { const [staticContext] = useState<ContextData[]>(initialContext); const [dynamicContext, setDynamicContext] = useState<ContextData[]>([]); const addDynamicContext = useCallback((context: ContextData) => { setDynamicContext((prev) => [...prev, context]); }, []); const removeDynamicContext = useCallback((id: string) => { setDynamicContext((prev) => prev.filter((ctx) => ctx.id !== id)); }, []); return ( <ChatContext.Provider value={{ staticContext, dynamicContext, addDynamicContext, removeDynamicContext, }} > {children} </ChatContext.Provider> ); };
<ConfigurableAIChat ... dynamicContextId="barChartData" />
const { addDynamicContext } = useContextData(); addDynamicContext({ id: "barChartData", // this is dynamicContextId type: "custom", content: { responseType: "chart", type: "bar", data: [ { x: "Product A", y: 300 }, { x: "Product B", y: 450 }, { x: "Product C", y: 280 }, { x: "Product D", y: 390 }, ], title: "Product Sales Comparison", }, });
let dyContext: (typeof dynamicContext)[number] | string = ""; if (config.dynamicContextId) { dyContext = dynamicContext.find((ctx) => ctx.id === config.dynamicContextId) || ""; } dyContext = JSON.stringify(dyContext); // Prepare request const chatRequest: ChatRequest = { messages: [...messages, userMessage], staticContext: config.context, dynamicContext: dyContext, systemPrompt: config.systemPrompt, };

Local development

First, run the development server:

nvn use npm install npm run dev

UI

Default View  Custom Render Dynamic Context Custom Render