Skip to content

Chatbot

pnpm dlx shadcn@latest add https://fastrag-ui.agrospai.udl.cat/r/chatbot.json
import { Chatbot } from "@fastrag-ui/chatbot"
<Chatbot
endpoint="http://localhost:8000"
strings={{ title: "Chatbot Assistant", promptSuggestions: [] }}
messages={messages}
setMessages={setMessages}
/>

endpoint is the base URL of a FastRAG backend serving fastrag.serve.create_app (see sdk/app/, and the SDK’s serving guide — sdk/docs/source/guide/serving/http-api.md and streaming.md — for the full contract): POST /v1/chats and POST /v1/chats/{chat_id}/messages. The widget assembles those paths itself; do not pass a per-chat path.

  • Sessions. The example above uses the backend’s default mode: POST /v1/chats mints a session and sets it as an httpOnly cookie, and every subsequent request is sent with credentials: "include". The widget never invents its own chat id — it learns the server-assigned one from the stream’s message_start event.
  • Bearer tokens. When the backend instead runs with ServeConfig.bearer_tokens enabled, pass a token via the bearerToken prop (<Chatbot bearerToken={token} ... />) and the widget sends it as Authorization: Bearer <token> on every request instead of relying on the cookie.
  • CORS. The backend’s ServeConfig.allowed_origins (FASTRAG_ALLOWED_ORIGINS for sdk/app/) must include the origin this component is served from — credentialed cross-origin requests fail otherwise. The registry’s own dev server (astro dev) runs at http://localhost:4321.
  • Errors and cancellation. A 4xx/5xx returned before the stream opens (e.g. 429 with code: "RATE_LIMIT" when the backend has rate limiting enabled) is shown as the assistant message’s error, using the server’s {code, message} envelope. Clicking the input’s stop control (or the “Clear chat” button, mid-stream) aborts the fetch and marks the message finishReason: "interrupted" — the same terminal state the server itself uses for a torn-down stream — without treating the cancellation as an error.