Simple_chat_bot_gpt_Sveltekit Svelte Themes

Simple_chat_bot_gpt_sveltekit

Mini Chat GPT Β· SvelteKit Demo

A modern mini-chat application built with SvelteKit, TypeScript, TailwindCSS, and integration with Supabase and OpenAI. The project showcases a clean interface, global navigation, and a lightweight API to interact with a language model.

🎯 Learning Objectives

  • SvelteKit Client & Server-Side: Navigation, shared layout, and a custom API route to relay messages to the model.
  • OpenAI Integration: A ready-to-use /api/chat route using the chat.completions API.
  • Optional Supabase: Helpers are available to persist conversations (automatically disabled if the variables are not configured).
  • Modern UI with Tailwind: Sticky header, light/dark theme, and centered cards for the home and chat pages.
  • TypeScript: Strict typing on both front-end and back-end scripts to facilitate scalability.

🧱 Architecture

src/
 β”œβ”€ lib/
 β”‚   β”œβ”€ assets/                 β†’ favicon & static assets
 β”‚   └─ server/
 β”‚        β”œβ”€ api_llm/openai.ts  β†’ OpenAI client (chat completions)
 β”‚        └─ supabase/client.ts β†’ Supabase client and functions
 β”œβ”€ routes/
 β”‚   β”œβ”€ +layout.svelte          β†’ Navigation bar, light/dark theme
 β”‚   β”œβ”€ +layout.ts              β†’ Loads shared data (e.g. current path, user, settings)
 β”‚   β”œβ”€ +page.svelte            β†’ Home page
 β”‚   β”œβ”€ speak-with-llm/
 β”‚   β”‚   β”œβ”€ +page.svelte        β†’ Chat interface & form actions
 β”‚   β”‚   └─ +page.server.ts     β†’ Load messages & save to DB actions
 β”‚   └─ api/chat/+server.ts     β†’ POST endpoint to relay messages to LLM
 └─ app.css / app.html          β†’ Global styles & SvelteKit shell

πŸš€ Quick Start

1. Install Dependencies

npm install

2. Environment Variables

Create a .env file at the root with the available keys:

OPENAI_API_KEY="sk-..."
OPENAI_API_MODEL="gpt-4o-mini"
PUBLIC_SUPABASE_URL="https://xxxx.supabase.co"
PUBLIC_SUPABASE_ANON_KEY="public-anon-key"
  • Without an OpenAI key, the API response will return a controlled error on the UI side.
  • If Supabase is not configured, the helpers are simply not called.

3. Create bdd in supabase web interface

Get PUBLIC_SUPABASE_URL and PUBLIC_SUPABASE_ANON_KEY after bdd created

4. Launch the Application

npm run dev -- --open

The SvelteKit development server runs by default on http://localhost:5173.

5. Production Build

npm run build
npm run preview

Then, deploy to the hosting provider of your choice (requires the appropriate SvelteKit adapter for the target).


πŸ’¬ User Journey

  1. Homepage: An introduction to the project and a call-to-action (CTA) to the chat space.
  2. "Speak with LLM" Page: Input field, messages aligned according to their role, and a generation indicator.
  3. /api/chat Route: Receives the message, calls OpenAI, and returns the JSON response.
  4. Theme Switcher: Toggles between light and dark mode, with the choice persisted via localStorage.

πŸ§‘β€πŸŽ¨ Experience & UI

  • Responsive layout adapted for both desktop and mobile.
  • Translucent header and footer with active states on navigation links.
  • Soft gradients, rounded cards, and light shadows for comfortable reading.
  • Animated loading indicator while the assistant is generating a response.

Top categories

Loading Svelte Themes