This project demonstrates how to create a Flask web application that:
http://127.0.0.1:11434The application serves as a bridge between user queries and AI responses, storing all interactions for later retrieval.
Without proper data management:
Using a database ensures data persistence and enables advanced features like search, history, and analytics.
Prerequisites:
pip install flaskhttp://127.0.0.1:11434Project structure:
project/
├── app.py
├── database.py
├── requirements.txt
└── templates/
└── index.html
Create requirements.txt:
flask
requests
When a user submits a question:
This ensures:
Each record includes:
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 conversationquestion: User's input questionanswer: Response from Ollama APItimestamp: When the conversation occurredThis schema supports efficient querying and future expansion.
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)
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 questionmodel: Which Ollama model to useResponse 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.
Steps to run the application:
ollama servepip install -r requirements.txtpython app.pyDatabase 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?"}'
This project demonstrates:
Key benefits:
Future enhancements could include: