Skip to content

MohamedElsayed-debug/mse_ai_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔥 mse_ai_api — ChatGPT AI Automation Helper + CLI Client

FastAPI Playwright Python Docker License: MIT

Turn ChatGPT into a FREE OpenAI-compatible API in seconds — no API key required.


Watch the Full Tutorial on YouTube

YouTube Tutorial
Learn how to install, set up, and build AI workflows with mse_ai_api

❤️ Sponsors


🌟 Overview

mse_ai_api is a high-performance proxy server built with FastAPI and Playwright. It perfectly mimics the official OpenAI API structure by automating ChatGPT's free web interface in the background — making it a seamless drop-in replacement for n8n's OpenAI and HTTP nodes.

This version has been completely rewritten to support Advanced AI Agent Tool Calling reliably, bypassing strict JSON validation errors that plagued earlier approaches.


What's New — CLI Client

NEW UPDATE: Introducing cli.py — a beautiful, interactive terminal client that connects directly to your mse_ai_api server!

MSE_AI CLI Demo
Interactive CLI client with colored output, response timing, and conversation history

Highlights:

  • 🎨 Beautiful Terminal UI — Colored output with response numbering, timestamps, and elapsed time
  • 💬 Full Conversation Memory — Maintains chat history within a session
  • Zero Dependencies — Uses only Python standard library (urllib, json, textwrap)
  • 🔧 Fully Configurable — Server URL, API key, model, and display width via environment variables

✨ Key Features

  • 💸 Zero API Costs — Uses ChatGPT's web interface via background browser automation
  • Lightning Fast — Built on asynchronous Python with FastAPI
  • 🤖 Full n8n Agent Support — Advanced ChatGPT tool calling and JSON parsing
  • 🔌 OpenAI-Compatible — Drop-in replacement for OpenAI API clients
  • 🐳 Dockerized — One-command deployment
  • 🔒 Secure — Protected by your own configurable API secret key
  • 🖥️ CLI Client — Interactive terminal chat client included (cli.py)

⚙️ How It Works

This proxy relies on a robust technical architecture in main.py designed for production AI workflows:

  1. AsyncBrowserThread (The Engine) — Instead of spinning up a new browser per request, the API launches a single detached Python thread running an async Chrome browser via Playwright. It uses anti-bot bypass techniques (--disable-blink-features=AutomationControlled, spoofed user agents, webdriver hiding) so ChatGPT treats it as a real user session.

  2. Smart Prompt Injection (format_prompt & format_tools_instruction) — When n8n sends tool definitions (via AI Agent nodes), the script dynamically rebuilds the prompt and injects precise system instructions commanding ChatGPT to output only valid JSON — guaranteeing it behaves like the programmatic API.

  3. Regex Parsing (parse_tool_calls) — Once ChatGPT replies, the proxy scans the response for JSON blocks. If a tool call is detected, it extracts and formats it to meet OpenAI's strict function calling schema, then signals n8n to execute the tool locally.

  4. Flexible Endpoints — Natively supports both /v1/chat/completions (legacy n8n setups) and /v1/responses (modern Responses API) for broad compatibility.


🛠️ Quick Start

Option 1 — Docker (Recommended)

Deploying with Docker guarantees all dependencies (including headless Chrome and fonts) are perfectly configured.

git clone https://github.com/MohamedElsayed-debug/mse_ai_api.git
cd mse_ai_api
docker-compose up --build -d

Server starts on http://localhost:7777

Option 2 — Manual

Requires Python 3.10+.

pip install -r requirements.txt
python main.py

Option 3 — CLI Client

Once your server is running (via Docker or manually), you can chat with it directly from the terminal:

python cli.py

Or with custom settings:

MSE_SERVER=http://your-server:7777 MSE_API_KEY=your-key MSE_MODEL=gpt-4o python cli.py

CLI Commands Reference

Command Description
.help Show available commands
.clear Reset conversation history
.history Show message count this session
.model Show active model name
.status Ping server and show latency
.exit Quit the CLI

🔧 Configuration

Server (main.py)

Variable Default Description
API_SECRET_KEY change-secret-key-2026 Your server's API key

CLI Client (cli.py)

Variable Default Description
MSE_SERVER http://localhost:7777 Server URL to connect to
MSE_API_KEY change-secret-key-2026 API key for authentication
MSE_MODEL gpt-4o-mini Model name to send in requests
MSE_WIDTH 80 Terminal output width

📡 API Reference

Base URL: http://localhost:7777

Endpoints:

  • POST /v1/chat/completions — Legacy OpenAI-compatible endpoint
  • POST /v1/responses — Modern Responses API endpoint

Header: Authorization: Bearer <your-secret-key>

Example — cURL

curl -X POST "http://localhost:7777/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer change-secret-key-2026" \
  -d '{
    "messages": [{ "role": "user", "content": "Hello, AI!" }],
    "model": "gpt-4o-mini"
  }'

🔌 Connecting to n8n

Via HTTP Node

  1. Add an HTTP Request node in your n8n workflow
  2. Method: POST
  3. URL: http://localhost:7777/v1/chat/completions
  4. Header: Authorization: Bearer change-secret-key-2026
  5. Body (JSON):
{
  "messages": [{ "role": "user", "content": "Hello, AI!" }],
  "model": "gpt-4o-mini"
}

Via OpenAI Node (Recommended)

  1. In n8n, create a new OpenAI Account credential
  2. Set Base URL to: http://localhost:7777/v1
  3. Set API Key to your server's secret (change-secret-key-2026 by default)
  4. Use this credential across any AI Agent or LLM node seamlessly!

💎 PRO Version

mse_ai_api is great for personal workflows. The PRO Version is built for teams and production environments on Django.

Feature This Repo PRO Version
Free ChatGPT Web Backend
Advanced n8n Tool Calling
Image Analysis (HTTP URL)
Image Analysis (Base64/Binary)
Admin Dashboard (GUI)
Multi-User Management
Usage Statistics & Logging
Token Tracking & Quotas
Priority Support

📬 Contact

Interested in the PRO version or a custom integration?


📄 License

MIT License — see LICENSE for details.

Disclaimer: This project automates a web browser for personal and educational use. Usage must comply with the terms of service of the underlying platforms. The authors are not responsible for misuse.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

 
 
 

Contributors