|
|
1 год назад | |
|---|---|---|
| .. | ||
| src | 1 год назад | |
| venv | 1 год назад | |
| .DS_Store | 1 год назад | |
| .env | 1 год назад | |
| .env.example | 1 год назад | |
| Dockerfile | 1 год назад | |
| README.md | 1 год назад | |
| pyproject.toml | 1 год назад | |
| requirements.txt | 1 год назад | |
| run.py | 1 год назад | |
| 使用豆包实时API说明.md | 1 год назад | |
| 后端框架说明.md | 1 год назад | |
AI-Powered English Conversation Backend with real-time voice chat capabilities.
Clone and navigate to backend directory:
cd backend
bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install dependencies:
pip install -r requirements.txt
Install system dependencies (for audio processing): ```bash
brew install ffmpeg
sudo apt update sudo apt install ffmpeg
sudo yum install ffmpeg ```
Configure environment:
cp .env.example .env
# Edit .env with your API keys and configuration
bash
python run.py
Copy .env.example to .env and configure the following:
# Doubao realtime voice conversation API
DOUBAO_APP_ID=your_doubao_app_id_here
DOUBAO_ACCESS_KEY=your_doubao_access_key_here
DOUBAO_BASE_URL=wss://openspeech.bytedance.com/api/v3/realtime/dialogue
DOUBAO_TTS_SPEAKER=zh_male_yunzhou_jupiter_bigtts
# Doubao traditional API (backup)
DOUBAO_API_KEY=your_doubao_api_key_here
DOUBAO_MODEL=your_doubao_model_id_here
# OpenAI API (backup)
OPENAI_API_KEY=your_openai_api_key_here
# Server
HOST=0.0.0.0
PORT=8765
DEBUG=false
# Audio
MAX_AUDIO_DURATION=60
AUDIO_SAMPLE_RATE=24000
# Rate Limiting
RATE_LIMIT_PER_MINUTE=100
MAX_CONNECTIONS_PER_IP=10
# Security
CORS_ORIGINS=http://localhost:3000,https://yourdomain.com
The backend now supports Doubao's real-time voice conversation API, which provides:
To use Doubao Realtime API:
Get API credentials from ByteDance:
DOUBAO_APP_ID: Your application IDDOUBAO_ACCESS_KEY: Your access keyConfigure in .env:
DOUBAO_APP_ID=9775523795
DOUBAO_ACCESS_KEY=70G0Dky0P24Ai2VsizJQ5bDLHIkKzLNt
bash
DOUBAO_TTS_SPEAKER=zh_male_yunzhou_jupiter_bigtts # Voice selection
DOUBAO_BASE_URL=wss://openspeech.bytedance.com/api/v3/realtime/dialogue
The server implements a WebSocket-based API for real-time communication. See API Documentation for detailed specifications.
ws://localhost:8765
session_start - Start a new conversation sessionsession_end - End a conversation sessionaudio_data - Send audio data for processingaudio_response - Receive AI-generated audio responseerror - Error messagesping/pong - Connection keepaliveThe backend uses a priority-based AI service selection:
DOUBAO_APP_ID and DOUBAO_ACCESS_KEY are configuredDOUBAO_API_KEY and DOUBAO_MODEL are configuredOPENAI_API_KEY is configuredbackend/
├── src/
│ ├── config/
│ │ ├── config.py # Main configuration
│ │ └── doubao_config.py # Doubao realtime API config
│ ├── services/
│ │ ├── ai_service.py # AI service manager
│ │ ├── doubao_client.py # Doubao realtime client
│ │ ├── websocket_server.py
│ │ ├── audio_processor.py
│ │ └── session_manager.py
│ └── ...
backend/
├── src/
│ ├── core/ # Core configuration and utilities
│ │ ├── config.py # Configuration management
│ │ └── logger.py # Logging setup
│ ├── config/ # Configuration modules
│ │ ├── config.py # Main settings
│ │ └── doubao_config.py # Doubao realtime config
│ ├── models/ # Data models
│ │ └── messages.py # WebSocket message models
│ ├── services/ # Business logic services
│ │ ├── session_manager.py # Session lifecycle management
│ │ ├── websocket_server.py # WebSocket server implementation
│ │ ├── audio_processor.py # Audio processing and conversion
│ │ ├── ai_service.py # AI integration manager
│ │ └── doubao_client.py # Doubao realtime client
│ └── main.py # Application entry point
├── tests/ # Test files
├── requirements.txt # Python dependencies
├── pyproject.toml # Project configuration
├── .env.example # Environment variables template
└── run.py # Simple startup script
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run tests with coverage
pytest --cov=src
# Format code
black src tests
# Lint code
flake8 src tests
# Type checking
mypy src
For development with auto-reload:
# Set environment variables
export RELOAD=true
export DEBUG=true
# Run with auto-reload
python run.py
# Build image
docker build -t tolan-backend .
# Run container
docker run -d \
--name tolan-backend \
-p 8765:8765 \
--env-file .env \
tolan-backend
Create /etc/systemd/system/tolan-backend.service:
[Unit]
Description=Tolan Backend Server
After=network.target
[Service]
Type=exec
User=tolan
Group=tolan
WorkingDirectory=/opt/tolan/backend
Environment=PATH=/opt/tolan/backend/venv/bin
EnvironmentFile=/opt/tolan/backend/.env
ExecStart=/opt/tolan/backend/venv/bin/python run.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Enable and start:
sudo systemctl daemon-reload
sudo systemctl enable tolan-backend
sudo systemctl start tolan-backend
Audio processing fails:
Doubao realtime API unavailable:
openspeech.bytedance.comAI service unavailable:
WebSocket connection fails:
Enable debug logging:
export DEBUG=true
export LOG_LEVEL=DEBUG
python run.py
Audio Processing:
Concurrency:
MIT License - see LICENSE file for details.