ollama-server2 / README_CONFIGURATION.md
bahadur3093's picture
feat: Add environment-based configuration and chat history support
8d8e945
|
Raw
History Blame Contribute Delete
4.25 kB

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/generate endpoint (stateless, single prompt)
  • No conversation history was being passed to the model

Solution:

  • Switched to Ollama's /api/chat endpoint
  • Now reads chat history from chat-history.json and 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) when OLLAMA_BASE_URL is not set
  • Production Ollama when you set OLLAMA_BASE_URL to 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

  1. Make sure Ollama is running locally:

    ollama serve
    
  2. Start the development server:

    npm run dev
    
  3. The app will connect to http://localhost:11434

Production Testing

  1. Set OLLAMA_BASE_URL to your production server
  2. Build and deploy:
    npm run build
    npm start
    

How It Works

Conversation Flow

  1. User sends a message
  2. Message is saved to chat-history.json
  3. API reads all previous messages from history
  4. Sends complete conversation to Ollama's /api/chat endpoint
  5. Model responds with context from entire conversation
  6. Response is saved to history and displayed

API Endpoints Updated

  • pages/api/generate.ts - Now uses chat API with history
  • pages/api/models.ts - Uses environment-based URL
  • pages/api/pull.ts - Uses environment-based URL

Configuration Files

  • next.config.js - Exposes OLLAMA_BASE_URL to 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.json exists and contains messages
  • Verify the API is using /api/chat endpoint (not /api/generate)
  • Clear chat history and start a new conversation

Connection errors

  • Verify OLLAMA_BASE_URL is 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.json in 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