Flask App with SQLite and Ollama

Introduction to the Project

This project demonstrates how to create a Flask web application that:

  • Connects to Ollama API at http://127.0.0.1:11434
  • Queries the Ollama API with user questions
  • Saves API responses to a SQLite database
  • Handles requests asynchronously

The application serves as a bridge between user queries and AI responses, storing all interactions for later retrieval.

The Problem with Not Using the Tool

Without proper data management:

  • Loss of Data: API responses are lost after each session
  • No History: Users cannot review past conversations
  • No Persistence: All data is stored only in memory
  • Scalability Issues: Cannot handle multiple concurrent requests
  • Debugging Difficulties: Hard to trace application behavior

Using a database ensures data persistence and enables advanced features like search, history, and analytics.

Basic Setup for Using the Tool

Prerequisites:

  1. Python 3.7+
  2. Flask installed: pip install flask
  3. Ollama installed and running on http://127.0.0.1:11434
  4. SQLite3 (usually included with Python)

Project structure:

project/
├── app.py
├── database.py
├── requirements.txt
└── templates/
    └── index.html
                

Create requirements.txt:

flask
requests
                

How the Data is Saved

When a user submits a question:

  1. Question is sent to Ollama API
  2. API response is received
  3. Both question and response are saved to SQLite database
  4. Database records are timestamped

This ensures:

  • Full conversation history
  • Easy retrieval of past responses
  • Data integrity through database transactions

Each record includes:

  • Question text
  • Answer text
  • Timestamp
  • Unique identifier

Database Schema

SQLite table structure:

CREATE TABLE IF NOT EXISTS conversations (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    question TEXT NOT NULL,
    answer TEXT NOT NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
                

Fields explanation:

  • id: Unique identifier for each conversation
  • question: User's input question
  • answer: Response from Ollama API
  • timestamp: When the conversation occurred

This schema supports efficient querying and future expansion.

Flask Application Code

from flask import Flask, render_template, request, jsonify
import sqlite3
import requests
from datetime import datetime

app = Flask(__name__)

# Database setup
def init_db():
    conn = sqlite3.connect('conversations.db')
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS conversations
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  question TEXT NOT NULL,
                  answer TEXT NOT NULL,
                  timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
    conn.commit()
    conn.close()

# Save conversation to database
def save_conversation(question, answer):
    conn = sqlite3.connect('conversations.db')
    c = conn.cursor()
    c.execute("INSERT INTO conversations (question, answer) VALUES (?, ?)",
              (question, answer))
    conn.commit()
    conn.close()

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/ask', methods=['POST'])
def ask():
    question = request.json['question']
    
    # Send question to Ollama API
    response = requests.post('http://127.0.0.1:11434/api/generate',
                           json={'prompt': question, 'model': 'llama3'})
    
    if response.status_code == 200:
        answer = response.json()['response']
        save_conversation(question, answer)
        return jsonify({'answer': answer})
    else:
        return jsonify({'error': 'Failed to get response'}), 500

if __name__ == '__main__':
    init_db()
    app.run(debug=True)
                

Ollama API Integration

The Flask app communicates with Ollama through HTTP requests:

response = requests.post('http://127.0.0.1:11434/api/generate',
                       json={'prompt': question, 'model': 'llama3'})
                

Key parameters:

  • prompt: The user's question
  • model: Which Ollama model to use

Response handling:

if response.status_code == 200:
    answer = response.json()['response']
    save_conversation(question, answer)

Async processing is handled by the Flask application's request handling.

Running the Application

Steps to run the application:

  1. Ensure Ollama is running: ollama serve
  2. Install dependencies: pip install -r requirements.txt
  3. Run the Flask app: python app.py
  4. Access at http://127.0.0.1:5000

Database will be created automatically on first run.

Example curl command to test:

curl -X POST http://127.0.0.1:5000/ask \
     -H "Content-Type: application/json" \
     -d '{"question": "What is Python?"}'
                

References and Further Reading

Official Documentation

Related Resources

  • Flask Web Development by Miguel Grinberg
  • SQLite Tutorial on W3Schools
  • Building AI Applications with Ollama

Additional Tools

  • Flask-SQLAlchemy for ORM
  • Flask-RESTful for API development
  • Requests library for HTTP operations

Conclusion

This project demonstrates:

  • Integration between Flask and Ollama API
  • SQLite database integration for data persistence
  • Asynchronous request handling
  • Proper error handling and data management

Key benefits:

  • Full conversation history
  • Scalable architecture
  • Easy to extend with new features
  • Robust error handling

Future enhancements could include:

  • User authentication
  • Search functionality
  • Web interface for browsing history
  • Export capabilities