Skip to content

Ali-domi/Ali.chat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🤖 Local Chat API

A lightweight Flask-based REST API that wraps OpenAI's GPT-4o-mini model and exposes a simple HTTP endpoint for chat interactions.


📁 Project Structure

.
├── local_chat_api.py   # Main Flask application
├── requirements.txt    # Python dependencies
└── start.sh            # Production server startup script

⚙️ Prerequisites


🚀 Setup & Installation

1. Clone the repository

git clone <your-repo-url>
cd <your-repo-folder>

2. Install dependencies

pip install -r requirements.txt

3. Set your OpenAI API key

export OPENAI_API_KEY="sk-..."

Tip: Add this line to your .bashrc or .env file to persist it across sessions.


▶️ Running the Server

Development mode

python local_chat_api.py

Server will start at http://0.0.0.0:5000

Production mode (Gunicorn)

bash start.sh

This launches the app with 2 Gunicorn workers on port 5000.


📡 API Reference

GET /chat

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?"
}

🔧 Configuration

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

📦 Dependencies

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

⚠️ Notes & Limitations

  • The /chat endpoint 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 /chat endpoint — 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.

🛡️ Security Recommendations

  • Never hardcode your OPENAI_API_KEY in source code.
  • Use environment variables or a secrets manager (e.g., AWS Secrets Manager, .env with python-dotenv).
  • Add rate limiting (e.g., Flask-Limiter) before deploying to production.

📄 License

MIT — feel free to use and modify.

About

A lightweight Flask-based REST API that wraps OpenAI's GPT-4o-mini model and exposes a simple HTTP endpoint for chat interactions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors