-
Notifications
You must be signed in to change notification settings - Fork 7.5k
feat: Add WebSocket API for streaming responses - Support for WeChat Mini Programs #11704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Add WebSocket API for streaming responses - Support for WeChat Mini Programs #11704
Conversation
- Add WebSocket endpoint at /v1/ws/chat for real-time streaming - Support multiple authentication methods (API token, user session, query params) - Enable bidirectional communication for platforms like WeChat Mini Programs - Implement streaming chat completions with incremental responses - Add comprehensive error handling and connection management - Include extensive inline documentation and comments New files: - api/apps/websocket_app.py: Main WebSocket API implementation - docs/guides/websocket_api.md: Complete API documentation - example/websocket/python_client.py: Python example client - example/websocket/index.html: Web-based demo client - example/websocket/README.md: Examples documentation Features: - Persistent WebSocket connections for multi-turn conversations - Session management for conversation continuity - Real-time streaming with low latency - Compatible with WeChat Mini Programs and mobile apps - Health check endpoint for connectivity testing - Backward compatible with existing SSE endpoints Resolves: infiniflow#11683
|
@KevinHuSh Could you please review my PR? |
|
Thanks for your contribution! Can you fix the CI at first ? |
|
Wow, that was implemented very quickly. But I looked into it, and ragflow already has many of these methods. If the integration could be improved, it would be even better. How were the test results? |
|
@lizheng419 @yingfeng Thanks for the feedback! The implementation reuses RAGFlow's existing |
|
@lizheng419 Tested successfully with the included Python client (example/websocket/python_client.py)—streaming responses, authentication (API token & session), multi-turn conversations, and error handling all work as expected. Would appreciate additional testing on your end, especially for the WeChat Mini Program use case. |
|
|
Appreciations! |
|
@KevinHuSh I removed |
|
@KevinHuSh @lizheng419 Could you please review my pr? |
lizheng419
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really appreciate your suggestion. I think websocket_app.py should mimic session.py in /api/sdk, as this part is for third-party calls. The file should be moved to /api/sdk and its /agents/<agent_id>/completions and /chats/<chat_id>/completions sections should be similar to those in session.py. If convenient, please add the websocket content to test/test_sdk_api. Thank you.
|
@KevinHuSh Could you please take a look and see if there's anything wrong with my opinion? |
|
You can also write a decorator called |
lizheng419
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
websocket.py
@manager.route("/chats/<chat_id>/completions", websocket=True)
session.py
@manager.route("/chats/<chat_id>/completions", methods=["POST"])
Issue: Quart may not correctly distinguish between HTTP and WebSocket requests at the same path, depending on the framework version and configuration.
Recommendation: Use the /ws/ prefix to explicitly distinguish them:
@manager.route("/ws/chats/<chat_id>/completions")
@manager.route("/ws/agents/<agent_id>/completions")
test/testcases/test_sdk_api/test_websocket_chat_completions/test_websocket_chat_streaming.py
Outdated
Show resolved
Hide resolved
test/testcases/test_sdk_api/test_websocket_chat_completions/conftest.py
Outdated
Show resolved
Hide resolved
test/testcases/test_sdk_api/test_websocket_agent_completions/test_websocket_agent_streaming.py
Outdated
Show resolved
Hide resolved
test/testcases/test_sdk_api/test_websocket_agent_completions/conftest.py
Outdated
Show resolved
Hide resolved
lizheng419
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current code (incorrect)
@manager.route("/chats/<chat_id>/completions", websocket=True)
Correct syntax according to the Quart official documentation
@manager.websocket("/chats/<chat_id>/completions")
Source: https://github.com/pallets/quart/blob/main/docs/discussion/websockets_discussion.rst
I just removed |
8bd0669 to
82d621c
Compare
- Moved websocket_app.py to api/apps/sdk/websocket.py
- Follows same structure as session.py for SDK endpoints
- Added ws_token_required decorator in api_utils.py (mirrors token_required)
- WebSocket endpoints now use SDK pattern:
* @manager.websocket('/chats/<chat_id>/completions')
* @manager.websocket('/agents/<agent_id>/completions')
- Removed old api/apps/websocket_app.py
- Added websockets>=14.0 and pytest-asyncio>=0.24.0 to test dependencies
Addresses reviewer feedback: websocket_app.py should mimic session.py in /api/sdk
for third-party calls, with /agents/<agent_id>/completions and
/chats/<chat_id>/completions endpoints similar to those in session.py
…pi/apps/sdk/websocket.py following session.py pattern - Added ws_token_required decorator - WebSocket endpoints: /ws/chats/<id>/completions and /ws/agents/<id>/completions - Prevents routing conflicts with HTTP endpoints
7b39d32 to
081f7f7
Compare
…lders - Line 309, 330, 344 in api_utils.py - Fixes ruff F541 linting errors
@lizheng419 Thank you for your detailed feedback! I've implemented all the requested changes—moved |
🎯 Overview
This PR adds comprehensive WebSocket API support to RAGFlow, enabling real-time streaming responses for platforms that require persistent bidirectional connections, particularly WeChat Mini Programs.
Resolves #11683
🚀 Motivation
WeChat Mini Programs and many mobile applications require WebSocket connections for real-time communication. While RAGFlow's existing Server-Sent Events (SSE) API works well for web browsers, it's not compatible with WeChat Mini Programs, which mandate WebSocket for streaming data.
This implementation provides:
📦 Changes
New Files
api/apps/websocket_app.py(650+ lines)/v1/ws/chat/v1/ws/healthdocs/guides/websocket_api.md(950+ lines)example/websocket/python_client.py(450+ lines)example/websocket/index.html(600+ lines)example/websocket/README.md(250+ lines)Modified Files
README.md✨ Key Features
1. WebSocket Endpoints
/v1/ws/chat- Real-time chat completions with streaming/v1/ws/agent- Agent completions (placeholder for future)/v1/ws/health- Connection health check and diagnostics2. Authentication Methods
3. Streaming Features
4. Connection Management
5. Platform Support
📚 Usage Examples
WeChat Mini Program
Web Application
Python Client
python example/websocket/python_client.py \ --url ws://localhost/v1/ws/chat \ --token your-api-token \ --chat-id your-chat-id \ --interactive🧪 Testing
Manual Testing
Start RAGFlow server (if not already running)
Test with Python client:
Test with web demo:
example/websocket/index.htmlin a browserTest health endpoint:
Authentication Testing
Test all authentication methods:
Streaming Testing
Verify streaming behavior:
🔒 Security Considerations
Production Recommendation: Always use WSS (WebSocket Secure) with valid SSL certificates.
🔄 Backward Compatibility
📖 Documentation
Complete documentation included:
🎨 Code Quality
🐛 Known Limitations
None at this time. The implementation is production-ready.
🔮 Future Enhancements
Potential future improvements (not in this PR):
/v1/ws/agent) - placeholder added✅ Checklist
🙏 Acknowledgments
This PR resolves the feature request from @lizheng419 (#11683) for WeChat Mini Program support.
Contribution by Gittensor, learn more at https://gittensor.io/