A Django-based AI completion API with PostgreSQL database storage for prompts and responses.
- 🤖 AI-powered text completions using OpenAI API
- 🗄️ PostgreSQL database storage for all prompts and responses
- 📊 Admin interface to view and manage completions
- ⚡ RESTful API endpoints
- 📈 Request metadata tracking (IP, user agent, processing time)
- 🎨 Beautiful homepage with status indicator
pip install -r requirements.txt
- Update Database Settings
Edit
technologychannelai/settings.py
and update the DATABASES section:DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'your_database_name', 'USER': 'your_postgres_username', 'PASSWORD': 'your_postgres_password', 'HOST': 'localhost', 'PORT': '5432', } }
-
Install PostgreSQL (if not already installed)
- Windows: Download from https://www.postgresql.org/download/windows/
- Linux:
sudo apt-get install postgresql postgresql-contrib
- Mac:
brew install postgresql
-
Start PostgreSQL Service
- Windows: Check Services app for PostgreSQL service
- Linux/Mac:
sudo systemctl start postgresql
-
Create Database and User
CREATE DATABASE technologychannelai_db; CREATE USER technologychannelai_user WITH PASSWORD 'your_secure_password'; GRANT ALL PRIVILEGES ON DATABASE technologychannelai_db TO technologychannelai_user;
-
Update Settings Edit
technologychannelai/settings.py
:DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'technologychannelai_db', 'USER': 'technologychannelai_user', 'PASSWORD': 'your_secure_password', 'HOST': 'localhost', 'PORT': '5432', } }
Set your OpenAI API key as an environment variable:
# Windows
set OPENAI_API_KEY=your_openai_api_key_here
# Linux/Mac
export OPENAI_API_KEY=your_openai_api_key_here
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
- URL:
/api/complete/
- Method: POST
- Body:
{"prompt": "Your question here"}
- Response:
{ "response": "AI generated response", "completion_id": 1, "processing_time": 2.345, "tokens_used": 150 }
- URL:
/api/completions/
- Method: GET
- Query Parameters:
page
(default: 1)limit
(default: 10)
- Response: Paginated list of all stored completions
Access the admin interface at /admin/
to:
- View all AI completions
- Filter by model, date, IP address
- Search through prompts and responses
- View processing times and metadata
The AICompletion
model stores:
- prompt: User's input text
- response: AI's generated response
- model_used: AI model version
- temperature: Generation temperature setting
- tokens_used: Number of tokens consumed
- processing_time: Request processing time in seconds
- ip_address: Client IP address
- user_agent: Client user agent string
- created_at: Timestamp of creation
- updated_at: Timestamp of last update
-
Check if PostgreSQL is running
# Windows net start postgresql-x64-15 # Linux/Mac sudo systemctl status postgresql
-
Verify credentials
- Check username and password in settings.py
- Ensure database exists
- Verify user has proper permissions
-
Test connection
psql -h localhost -U your_username -d your_database
- "password authentication failed": Check PostgreSQL password in settings.py
- "database does not exist": Create the database first
- "permission denied": Grant proper privileges to the user
python setup_postgres.py
This will display detailed setup instructions for PostgreSQL configuration.
-
Test AI Completion
curl -X POST http://localhost:8000/api/complete/ \ -H "Content-Type: application/json" \ -d '{"prompt": "What is artificial intelligence?"}'
-
View Stored Completions
curl http://localhost:8000/api/completions/
This project is open source and available under the MIT License.