A lightweight Flask-based REST API that wraps OpenAI's GPT-4o-mini model and exposes a simple HTTP endpoint for chat interactions.
.
├── local_chat_api.py # Main Flask application
├── requirements.txt # Python dependencies
└── start.sh # Production server startup script
- Python 3.8+
- An OpenAI API key
git clone <your-repo-url>
cd <your-repo-folder>pip install -r requirements.txtexport OPENAI_API_KEY="sk-..."Tip: Add this line to your
.bashrcor.envfile to persist it across sessions.
python local_chat_api.pyServer will start at http://0.0.0.0:5000
bash start.shThis launches the app with 2 Gunicorn workers on port 5000.
Send a message to GPT-4o-mini and receive a reply.
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
text |
string | Yes | The message to send |
Example Request
curl "http://localhost:5000/chat?text=Hello%2C+how+are+you%3F"Example Response
{
"reply": "I'm doing well, thank you! How can I help you today?"
}| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY |
Your OpenAI API key (required) | — |
PORT |
Server port (set in start.sh) |
5000 |
model |
OpenAI model used | gpt-4o-mini |
| Package | Version | Purpose |
|---|---|---|
flask |
3.0.0 | Web framework |
requests |
2.31.0 | HTTP client for OpenAI API |
gunicorn |
21.2.0 | Production WSGI server |
- The
/chatendpoint uses a GET request with query parameters. For longer messages, consider switching to a POST endpoint with a JSON body to avoid URL length limits. - There is no authentication on the
/chatendpoint — do not expose this server publicly without adding proper auth middleware. - Error handling is minimal; if the OpenAI API returns an unexpected response, the server will raise an unhandled exception.
- Never hardcode your
OPENAI_API_KEYin source code. - Use environment variables or a secrets manager (e.g., AWS Secrets Manager,
.envwithpython-dotenv). - Add rate limiting (e.g., Flask-Limiter) before deploying to production.
MIT — feel free to use and modify.