This document explains how to set up the AI Chat Interface feature in ShieldBot.
- Vercel AI SDK Integration: Uses the latest Vercel AI SDK with Google Gemini API
- Real-time Streaming: Character-by-character streaming responses for better UX
- Enhanced Message Rendering: Supports markdown, code highlighting, and rich text
- Error Handling: Comprehensive error handling with user-friendly messages
- Loading States: Proper loading indicators and typing animations
- Reusable Components: Modular architecture with reusable chat components
The following packages have been added to package.json:
{
"@ai-sdk/google": "^0.0.50",
"ai": "^3.4.7"
}Add the following environment variable to your .env file:
# Google Generative AI (Gemini) - Required for AI Chat
GOOGLE_GENERATIVE_AI_API_KEY=your-gemini-api-key- Go to Google AI Studio
- Sign in with your Google account
- Click "Create API Key"
- Copy the generated API key
- Add it to your
.envfile
-
Install the new dependencies:
npm install
-
Add your Gemini API key to the
.envfile -
Start the development server:
npm run dev
src/lib/server/ai.ts: AI service using Vercel AI SDKsrc/routes/api/chat/+server.ts: Standard chat API endpointsrc/routes/api/chat/stream/+server.ts: Streaming chat API endpoint
src/lib/services/chatService.ts: Chat service for API communicationsrc/lib/stores/chatStore.ts: Svelte store for chat state managementsrc/lib/components/EnhancedMessageRenderer.svelte: Enhanced message rendering with markdown supportsrc/routes/chatbot/+page.svelte: Updated chatbot page with AI integration
- Streaming Responses: Real-time character-by-character streaming
- Error Handling: Comprehensive error handling with user feedback
- Loading States: Visual indicators for typing and streaming states
- Markdown Support: Rich text rendering with code highlighting
- Conversation Context: Maintains conversation history for better responses
- Navigate to
/chatbotin your application - Start typing a message
- The AI will respond with streaming text
- Messages support markdown formatting including:
- Bold text
- Italic text
Inline codeCode blocks- Headers (# ## ###)
- Lists
Standard chat endpoint that returns a complete response.
Request:
{
"message": "Hello, how are you?",
"conversationHistory": [
{
"role": "user",
"content": "Previous message"
}
]
}Response:
{
"response": "I'm doing well, thank you!",
"timestamp": "2024-01-01T00:00:00.000Z"
}Streaming chat endpoint that returns a stream of text chunks.
Request: Same as above
Response: Server-sent events with chunks:
{"type": "chunk", "content": "Hello", "timestamp": "..."}
{"type": "chunk", "content": " there", "timestamp": "..."}
{"type": "complete", "timestamp": "..."}
You can modify the AI model settings in src/lib/server/ai.ts:
const { text } = await generateText({
model: this.google("gemini-1.5-flash"),
prompt,
maxTokens: 1000, // Adjust response length
temperature: 0.7, // Adjust creativity (0-1)
});The chat interface uses TailwindCSS and can be customized by modifying:
src/routes/chatbot/+page.svelte- Main chat layoutsrc/routes/chatbot/components/- Individual chat componentssrc/lib/components/EnhancedMessageRenderer.svelte- Message styling
-
"Missing GOOGLE_GENERATIVE_AI_API_KEY"
- Ensure the API key is set in your
.envfile - Restart the development server after adding the key
- Ensure the API key is set in your
-
"Unauthorized" errors
- Check that the user is properly authenticated
- Verify the session is valid
-
Streaming not working
- Check browser console for errors
- Ensure the API key has proper permissions
Enable debug logging by adding to your .env:
DEBUG=chat:*- API keys are stored server-side only
- User authentication is required for all chat endpoints
- Input validation is performed on all messages
- Rate limiting should be implemented in production
- Streaming responses provide immediate feedback
- Conversation history is limited to prevent memory issues
- Efficient re-rendering with Svelte's reactivity
- Optimized for real-time chat experience