AI Chatbot¶
AI assistant trained on site content using OpenAI Assistant API.
Overview¶
- OpenAI Assistant API with file search capability
- Next.js API Route (
/api/chatbot) for backend integration - React + Framer Motion for the UI
- Dynamic import for code splitting
Setup¶
Extract Site Content¶
This creates training data from all blog posts and site information.
Create OpenAI Assistant¶
- Go to OpenAI Platform
- Create a new assistant with File Search enabled
- Upload the training data file
Configure Environment Variables¶
Components¶
ChatbotWidget- Main chat interface with suggested questions and note-aware contextDynamicChatbot- Dynamic import wrapper for code splitting
Features¶
- Floating chat button
- Smooth animations
- Suggested questions
- Message history
- Dark mode support
- Error handling
- Note-aware chat — context-scoped conversations per note
Note-Aware Chat¶
When a user is reading a specific note (/notes/[slug]), the chatbot automatically scopes to that note:
- Header changes to "Note Assistant" with subtitle "Ask about this note"
- Suggested questions switch to note-specific prompts (summarize, key takeaways, simplify)
- Chat history and thread are stored separately per note in
sessionStorage, so each note keeps its own conversation and the global chat remains independent - API context — the note slug is sent to
/api/chatbot, which loads the note content viagetArticleBySlugand injects it asadditional_instructionsinto the OpenAI run (truncated to 12,000 characters if needed)
Navigating away from a note returns the chatbot to its default global mode. Navigating back restores the note-specific conversation from session storage.
How It Works¶
BottomNavdetects the active note slug from the URL pathname- The slug is passed through
DynamicChatbot→ChatbotWidgetas anoteSlugprop ChatbotWidgetderives a context key (note-{slug}orglobal) to scope storage and state- On send, the slug is included in the POST body to
/api/chatbot - The API route validates the slug, loads the article, and builds
additional_instructionscontaining the note's title, summary, and content for the assistant run