Author: tj-scripts Email: tangj1984@gmail.com Date: 2024-03-21
A demonstration project showing how to build applications compatible with the OpenAI API specification, supporting both native OpenAI API and compatible API services.
- Support for OpenAI API and compatible API services
- Asynchronous API calls
- Interactive chat interface
- Chat history management
- Configurable logging system
- Type hints and documentation
- Error handling and retries
- Clone the repository:
git clone https://github.com/yourusername/openai_compatiable_demo.git
cd openai_compatiable_demo- Create and activate Python virtual environment using uv:
# Create virtual environment
uv venv .venv --python=3.12
# Activate virtual environment
# Unix/macOS:
source .venv/bin/activate
# Windows:
.venv\Scripts\activate- Install dependencies:
# First install build tools
uv pip install hatchling
# Install package in editable mode
uv pip install -e .If you encounter any issues during installation, please ensure:
- Python 3.12 or higher is installed
- uv is properly installed and added to system PATH
- Virtual environment is activated before running installation commands
- Copy the example configuration file:
cp config.toml.example config.toml- Edit
config.tomlto set your API configuration:
[api.openai]
api_key = "your-openai-api-key"
base_url = "https://api.openai.com/v1"
model = "gpt-3.5-turbo"
temperature = 0.7
max_tokens = 2000
timeout = 30
stream = false
retry_count = 3
retry_delay = 1
[logging]
level = "INFO"
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
file = "app.log"
max_size = 10485760 # 10MB
backup_count = 5api_key: Your OpenAI API keybase_url: API endpoint URLmodel: Model name to usetemperature: Controls randomness (0.0 to 1.0)max_tokens: Maximum tokens to generatetimeout: Request timeout in secondsstream: Enable streaming responsesretry_count: Number of retries for failed requestsretry_delay: Delay between retries in seconds
level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)format: Log message formatfile: Log file pathmax_size: Maximum size of log file in bytesbackup_count: Number of backup log files to keep
Run the web UI interface:
python -m tj.scripts.uiThe web interface provides:
- Interactive chat interface
- Chat history management
- Configurable parameters (temperature, max tokens)
- Clear history button
- Responsive design
The interface will be available at:
- Local: http://localhost:7860
- Public URL: (will be shown in the terminal)
Run the interactive chat demo:
python -m tj.scripts.mainThe chat interface supports the following commands:
- Type your message and press Enter to send
history: Display chat historyclear: Clear chat historyhelp: Show available commandsquit: Exit chat- Press Ctrl+C to exit at any time
Chat session example:
Welcome to the AI Chat! Type your message and press Enter to chat.
Commands:
- 'quit': Exit the chat
- 'clear': Clear chat history
- 'history': Show chat history
- 'help': Show this help message
You: Hello, please introduce yourself.
Assistant: Hello! I'm an AI assistant that can help you with questions, coding, data analysis, and more...
You: Can you help me write a Python function?
Assistant: Of course! Please let me know what functionality you want to implement...
You: history
=== Chat History ===
System: You are a helpful AI assistant.
User: Hello, please introduce yourself.
Assistant: Hello! I'm an AI assistant that can help you with questions, coding, data analysis, and more...
User: Can you help me write a Python function?
Assistant: Of course! Please let me know what functionality you want to implement...
===================
## 🛠️ Development
1. Ensure Python 3.12 or higher is installed
2. Use virtual environment for development
3. Follow PEP 8 coding standards
4. Add type hints to all functions
5. Write docstrings for all modules, classes, and methods
### 🔧 Development Tools
The project includes several development tools configured in `pyproject.toml`:
- `black`: Code formatting
- `isort`: Import sorting
- `mypy`: Type checking
- `pytest`: Testing
To install development dependencies:
```bash
uv pip install -e ".[dev]"
- Fork this repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
