Skip to content

Latest commit

 

History

History
99 lines (69 loc) · 2.2 KB

File metadata and controls

99 lines (69 loc) · 2.2 KB

OpenAI Python I/O

English | 简体中文

A lightweight Python module that replaces native input and print functions with SSE streaming capabilities compliant with OpenAI Chat Completion specifications.

Features

  • SSE streaming output support
  • Compliant with OpenAI Chat Completion API format
  • Supports Chinese and UTF-8 encoding
  • Automatic message pagination (using "---" separator)
  • Simple message queue management
  • Plug-and-play API design

Installation

conda create -n OpenAIio python=3.12
conda activate OpenAIio

# Install dependencies using requirements.txt
pip install -r requirements.txt

# Or install in development mode
pip install -e .

Quick Start

  1. Use in your code:
from openai_python_io import sse_print, sse_input

# Output message
sse_print("Hello, World!")

# Get user input
name = sse_input("What's your name? ")
sse_print(f"Nice to meet you, {name}!")
  1. Frontend connection:

Send POST request to http://localhost:2122/v1/chat/completions with SSE connection. Request format:

{
  "stream": true,
  "messages": [{"role": "user", "content": "Hello"}]
}

API Reference

sse_print(*values, sep: str = " ", end: str = "\n")

Stream content to frontend via SSE.

  • When end is the default "\n", adds pagination "\n\n---\n\n"
  • When end is specified, uses the given ending

sse_input(prompt: str = "") -> str

Wait for and get user input. Sends a special marker to notify frontend that user input is required.

Examples

Check openai_python_io/example.py for complete usage examples including:

  • Basic message output
  • User input handling
  • Chinese and Symbol support

Run the example:

python -m openai_python_io.example

Cherry Studio Example

Use with Cherry Studio as an example:

  1. Configuration: Configuration

  2. Example usage: Example

Project Structure

openai_python_io/
├── __init__.py    # Main interface
├── server.py      # SSE server implementation
├── utils.py       # Utility classes and message queue
└── example.py     # Usage examples

License

MIT License