Spaces:
Build error
Ollama Chat Configuration Guide
Overview
This application now supports conversation memory and environment-based configuration for switching between local Ollama (testing) and production Ollama servers.
What Was Fixed
1. Conversation Memory Issue
The model wasn't remembering previous messages because:
- The app was using Ollama's
/api/generateendpoint (stateless, single prompt) - No conversation history was being passed to the model
Solution:
- Switched to Ollama's
/api/chatendpoint - Now reads chat history from
chat-history.jsonand passes all previous messages to the model - The model now maintains full conversation context
2. Environment-Based Configuration
Added support for automatic switching between local and production Ollama servers.
Configuration
Environment Variables
Create a .env.local file in the root directory:
# For local development (default)
OLLAMA_BASE_URL=http://localhost:11434
NODE_ENV=development
# For production deployment
# OLLAMA_BASE_URL=https://your-ollama-server.com
# NODE_ENV=production
Automatic Switching
The application will automatically use:
- Local Ollama (
http://localhost:11434) whenOLLAMA_BASE_URLis not set - Production Ollama when you set
OLLAMA_BASE_URLto your deployed server
Deployment Setup
Option 1: Environment Variables (Recommended)
Set the OLLAMA_BASE_URL environment variable in your deployment platform:
Vercel:
vercel env add OLLAMA_BASE_URL
# Enter: https://your-ollama-server.com
Docker:
docker run -e OLLAMA_BASE_URL=https://your-ollama-server.com ...
Railway/Render/Heroku:
Add OLLAMA_BASE_URL in the environment variables section of your dashboard.
Option 2: Git-Based Switching
Create different .env files for different environments:
# .env.development (for local)
OLLAMA_BASE_URL=http://localhost:11434
# .env.production (for deployment)
OLLAMA_BASE_URL=https://your-ollama-server.com
Add to .gitignore:
.env.local
.env*.local
Commit only .env.example to git, and set up your CI/CD to copy the appropriate file during deployment.
Testing
Local Testing
Make sure Ollama is running locally:
ollama serveStart the development server:
npm run devThe app will connect to
http://localhost:11434
Production Testing
- Set
OLLAMA_BASE_URLto your production server - Build and deploy:
npm run build npm start
How It Works
Conversation Flow
- User sends a message
- Message is saved to
chat-history.json - API reads all previous messages from history
- Sends complete conversation to Ollama's
/api/chatendpoint - Model responds with context from entire conversation
- Response is saved to history and displayed
API Endpoints Updated
pages/api/generate.ts- Now uses chat API with historypages/api/models.ts- Uses environment-based URLpages/api/pull.ts- Uses environment-based URL
Configuration Files
next.config.js- ExposesOLLAMA_BASE_URLto the app.env.local- Local environment configuration (not committed).env.example- Template for environment variables (committed)
Troubleshooting
Model not remembering conversations
- Check that
chat-history.jsonexists and contains messages - Verify the API is using
/api/chatendpoint (not/api/generate) - Clear chat history and start a new conversation
Connection errors
- Verify
OLLAMA_BASE_URLis set correctly - Check that Ollama server is running and accessible
- Test the URL directly:
curl http://localhost:11434/api/tags
Environment variable not working
- Restart the development server after changing
.env.local - Verify the variable is set:
console.log(process.env.OLLAMA_BASE_URL) - For production, ensure the platform has the environment variable set
Additional Notes
- The chat history is stored in
chat-history.jsonin the project root - Clearing chat history (🗑️ button) will reset the conversation context
- Each message includes metadata (timestamp, model used)
- The system automatically filters and formats messages for Ollama's API