High-performance async Python backend for real-time AI conversations with Quart, Supabase, and OpenAI.
- Real-Time Streaming: Low-latency AI responses via WebSockets.
- Session Persistence: automatically saves all conversations to Supabase (PostgreSQL).
- Tool Calling: Real-time integration with Exchange Rate API, JokeAPI, and Open-Meteo.
- Async Architecture: Non-blocking I/O using
Quartandasyncio. - Background Tasks: Automatic session summarization on disconnect.
- Framework: Quart (Async Flask-like)
- Database: Supabase (PostgreSQL)
- AI: OpenAI GPT-4o (Streaming)
- Language: Python 3.11+
-
Install Dependencies:
pip install -r technvirons-backend/requirements.txt
-
Configure Environment: Create a
.envfile intechnvirons-backend/with:SUPABASE_URL=https://your-project.supabase.co SUPABASE_KEY=your-service-role-key OPENAI_API_KEY=sk-proj-... PORT=8000
-
Database Setup:
Run the following SQL in your Supabase SQL Editor to create the required tables:
CREATE TABLE IF NOT EXISTS sessions ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id TEXT, start_time TIMESTAMPTZ DEFAULT now(), end_time TIMESTAMPTZ, summary TEXT ); CREATE TABLE IF NOT EXISTS logs ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, session_id UUID REFERENCES sessions(id), timestamp TIMESTAMPTZ DEFAULT now(), role TEXT, content TEXT );
-
Run Server:
python -m uvicorn technvirons-backend.app:app --reload --port 8000
-
Manual Setup:
- Create a new Web Service.
- Connect your GitHub repo.
- Limit: Root Directory (Leave empty or
.). - Build Command:
pip install -r technvirons-backend/requirements.txt - Start Command:
uvicorn technvirons-backend.app:app --host 0.0.0.0 --port $PORT - Environment Variables: Add
SUPABASE_URL,SUPABASE_KEY,OPENAI_API_KEY.
Open your browser to: http://localhost:8000/ui to use the built-in test client.
- Framework Selection (Quart): Chosen for its native
asynciosupport and compatibility with standard Python async libraries (asyncpg,openaiasync client), which is critical for handling concurrent WebSocket connections and streaming without blocking. - State Management: Implemented an in-memory session context (list of messages) within the default WebSocket loop. This ensures low latency access to valid conversation history for the duration of the connection.
- Database Pattern: Adopted a "Session + Logs" schema. This separates high-level metadata (start/end times, summaries) from high-volume granular event logs, optimizing for both analytical queries (summaries) and audit trails (full logs).
- Tool Execution: Integrated "Tool Calling" logic directly into the message loop instead of a separate service to keep the architecture monolithic and simpler to deploy/debug for this assignment.
app.py: Main application and WebSocket routes.services.py: Core business logic, AI handling, and tool execution.db.py: Database interaction layer (sessions, logs).config.py: Environment configuration.static/index.html: Simple frontend for testing.



