php-openai-gpt-stream-chat-api-webui  by qiayue

Web UI for OpenAI GPT stream chat API

created 2 years ago
433 stars

Top 69.8% on sourcepulse

GitHubView on GitHub
Project Summary

This project provides a pure PHP implementation for interacting with OpenAI's GPT chat API, featuring real-time streaming of responses and a simple web UI. It's designed for developers and users who want a straightforward, framework-agnostic way to integrate GPT capabilities into their applications, offering a functional demo out-of-the-box.

How It Works

The backend uses PHP's cURL library with CURLOPT_WRITEFUNCTION to handle streaming responses from OpenAI. A custom StreamHandler class processes incoming data chunks, buffering incomplete JSON payloads and parsing them into meaningful content. The frontend utilizes JavaScript's EventSource API to receive these streamed updates, displaying them with a typing effect and syntax highlighting for code blocks.

Quick Start & Requirements

  • Install: Download the repository.
  • Prerequisites: PHP, a web server (e.g., Apache, Nginx), and an OpenAI API key.
  • Setup: Replace 'YOUR_OPENAI_API_KEY' in chat.php with your actual API key.
  • Demo: Access index.html via your web server.
  • Docs: README

Highlighted Details

  • Implements a DFA algorithm for sensitive word detection and replacement.
  • Supports real-time code block detection and syntax highlighting.
  • Offers a typing animation effect for streamed responses.
  • Includes options for GET and POST requests for the chat interface.

Maintenance & Community

  • The project is maintained by @qiayue.
  • A WeChat group is available for discussion (contact via GitHub ID).

Licensing & Compatibility

  • License: BSD 2-Clause.
  • Compatibility: Permissive license allows for commercial use and integration into closed-source projects.

Limitations & Caveats

OpenAI's rate limiting on free accounts can significantly impact response times. Enabling sensitive word detection may introduce latency due to per-sentence processing. The project relies on native PHP cURL and frontend JavaScript, with no external backend dependencies beyond the API key.

Feedback? Help us improve.