React Components
Ask questions and get answers
The ChatBox component provides an AI-powered chat interface for your documents. Users can ask questions and get answers with optional streaming for a better user experience.
bashnpm install @easyrag/react-components
tsximport { ChatBox } from '@easyrag/react-components'; function App() { return ( <ChatBox token={yourToken} datasetId="my-dataset" /> ); }
| Prop | Type | Description |
|---|---|---|
token | string | Authentication token (frontend token recommended) |
datasetId | string | Dataset ID to query |
| Prop | Type | Default | Description |
|---|---|---|---|
apiUrl | string | https://api.easyrag.com/v1 | API base URL |
filters | array | [] | Metadata filters to apply |
stream | boolean | true | Enable streaming responses |
maxHeight | string | '400px' | Maximum chat height |
initialMessages | array | [] | Pre-populate with messages |
theme | 'default' | 'minimal' | 'custom' | 'default' | Visual theme |
className | string | '' | Additional CSS classes |
customStyles | object | {} | Custom CSS class overrides |
labels | object | {} | Custom text labels |
showTimestamp | boolean | false | Show message timestamps |
showPoweredBy | boolean | true | Show "Powered by EasyRAG" |
poweredByUrl | string | https://easyrag.com | Powered by link URL |
| Prop | Type | Description |
|---|---|---|
onMessage | (message: Message) => void | Called when message is added |
onError | (error: Error) => void | Called when an error occurs |
onStreamStart | () => void | Called when streaming starts |
onStreamEnd | () => void | Called when streaming ends |
tsx<ChatBox token={token} datasetId="my-dataset" />
tsx<ChatBox token={token} datasetId="my-dataset" stream={true} maxHeight="600px" />
tsx<ChatBox token={token} datasetId="my-dataset" stream={false} />
tsx<ChatBox token={token} datasetId="support-kb" initialMessages={[ { id: 1, role: 'assistant', content: 'Hi! I can help you find information in our knowledge base. What would you like to know?' } ]} />
tsx<ChatBox token={token} datasetId="company-docs" filters={[ { key: 'department', match: { value: 'engineering' } }, { key: 'confidential', match: { value: false } } ]} />
tsx<ChatBox token={token} datasetId="my-dataset" onMessage={(message) => { console.log('New message:', message); // Save to database saveMessage(message); }} onStreamStart={() => { console.log('Streaming started'); }} onStreamEnd={() => { console.log('Streaming ended'); }} onError={(error) => { console.error('Chat error:', error); toast.error('Failed to send message'); }} />
tsx<ChatBox token={token} datasetId="my-dataset" theme="custom" customStyles={{ container: 'my-chat-wrapper', messagesContainer: 'my-messages', messageUser: 'my-user-message', messageAssistant: 'my-assistant-message', input: 'my-input', button: 'my-button', }} />
tsx<ChatBox token={token} datasetId="my-dataset" labels={{ placeholder: 'Ask me anything...', buttonSend: 'Submit', buttonSending: 'Sending...', emptyState: 'Start a conversation!', }} />
Modern, polished design with EasyRAG branding:
tsx<ChatBox token={token} datasetId="dataset" theme="default" />
Clean, simple styling:
tsx<ChatBox token={token} datasetId="dataset" theme="minimal" />
Full control over styling:
tsx<ChatBox token={token} datasetId="dataset" theme="custom" customStyles={{ container: 'bg-white rounded-lg shadow-lg', messagesContainer: 'bg-gray-50 p-4 space-y-3', messageUser: 'bg-blue-600 text-white rounded-lg p-3', messageAssistant: 'bg-white border rounded-lg p-3', input: 'border-2 border-gray-300 rounded-lg p-3', button: 'bg-blue-600 text-white px-6 py-3 rounded-lg', }} />
Full TypeScript support included:
typescriptimport type { ChatBoxProps, Message } from '@easyrag/react-components'; interface Message { id: number | string; role: 'user' | 'assistant'; content: string; } const props: ChatBoxProps = { token: 'your-token', datasetId: 'my-dataset', stream: true, onMessage: (message: Message) => { console.log(message); } };
Target specific elements with CSS:
css/* Container */ .easyrag-chat-box { } /* Messages container */ .easyrag-chat-box .messages { } /* User message */ .easyrag-chat-box .message-user { } /* Assistant message */ .easyrag-chat-box .message-assistant { } /* Input form */ .easyrag-chat-box form { } /* Input field */ .easyrag-chat-box input { } /* Send button */ .easyrag-chat-box button { }
Shows answers as they're generated:
tsx<ChatBox token={token} datasetId="dataset" stream={true} onStreamStart={() => { console.log('Generating answer...'); }} onStreamEnd={() => { console.log('Answer complete'); }} />
Pros:
Cons:
Waits for complete answer:
tsx<ChatBox token={token} datasetId="dataset" stream={false} />
Pros:
Cons:
Chat with user-specific data:
tsx<ChatBox token={token} datasetId="shared-dataset" filters={[ { key: 'userId', match: { value: currentUser.id } }, { key: 'teamId', match: { value: currentUser.teamId } } ]} />
tsxfunction DocumentChat() { const [hasFiles, setHasFiles] = useState(false); return ( <> <FileUpload token={token} datasetId="dataset" onUploadComplete={() => setHasFiles(true)} /> {hasFiles && ( <ChatBox token={token} datasetId="dataset" initialMessages={[ { id: 1, role: 'assistant', content: 'Files uploaded! What would you like to know?' } ]} /> )} </> ); }
tsxfunction SearchAndChat() { const [searchResults, setSearchResults] = useState([]); return ( <> <SearchBox token={token} datasetId="dataset" onResults={(results) => setSearchResults(results)} /> {searchResults.length > 0 && ( <ChatBox token={token} datasetId="dataset" initialMessages={[ { id: 1, role: 'assistant', content: `I found ${searchResults.length} relevant documents. What would you like to know about them?` } ]} /> )} </> ); }
tsx<ChatBox token={token} datasetId="support-kb" initialMessages={[ { id: 1, role: 'assistant', content: 'Hi! I can help with account, billing, and technical questions. How can I assist you?' } ]} filters={[ { key: 'category', match: { value: 'public' } } ]} />
tsx<ChatBox token={token} datasetId="company-docs" filters={[ { key: 'department', match: { value: user.department } }, { key: 'accessLevel', match: { value: user.accessLevel } } ]} initialMessages={[ { id: 1, role: 'assistant', content: `Welcome ${user.name}! Ask me about ${user.department} documentation.` } ]} />
tsx<ChatBox token={token} datasetId="research-papers" stream={true} maxHeight="700px" initialMessages={[ { id: 1, role: 'assistant', content: 'I can help you explore research papers. Ask me anything!' } ]} />
Generate short-lived tokens from your backend:
typescript// Backend app.post('/api/tokens/create', authenticate, async (req, res) => { const token = signFrontendToken({ customerId: req.user.id, datasetId: req.body.datasetId, ttlSeconds: 3600 }); res.json({ token }); }); // Frontend const token = await fetch('/api/tokens/create') .then(r => r.json()) .then(d => d.token);
tsx<ChatBox token={token} datasetId="dataset" onMessage={(message) => { // Save to your database saveMessage({ userId: currentUser.id, datasetId: 'dataset', role: message.role, content: message.content, timestamp: new Date() }); }} />
tsx<ChatBox token={token} datasetId="dataset" onMessage={(message) => { if (message.role === 'user') { analytics.track('chat_message_sent', { datasetId: 'dataset', messageLength: message.content.length }); } }} />
tsx<ChatBox token={token} datasetId="dataset" onError={(error) => { console.error('Chat error:', error); toast.error('Failed to send message. Please try again.'); // Report to error tracking Sentry.captureException(error); }} />
tsx<ChatBox token={token} datasetId="dataset" filters={[ // Security { key: 'userId', match: { value: currentUser.id } }, // Relevance { key: 'language', match: { value: 'en' } }, { key: 'status', match: { value: 'published' } } ]} />
The ChatBox component includes:
Need help? Contact us at: