ragflow/example/websocket
2025-12-03 07:14:40 -03:00
..
index.html feat: Add WebSocket API for streaming responses 2025-12-03 06:49:36 -03:00
python_client.py fix the CLI issue 2025-12-03 07:14:40 -03:00
README.md feat: Add WebSocket API for streaming responses 2025-12-03 06:49:36 -03:00

RAGFlow WebSocket Examples

This directory contains example implementations for using RAGFlow's WebSocket API for real-time streaming responses.

📁 Files

  • python_client.py - Python WebSocket client with interactive mode
  • index.html - Web-based demo with interactive UI

🚀 Quick Start

Python Client

Prerequisites

pip install websocket-client

Single Question Mode

python python_client.py \
    --url ws://localhost/v1/ws/chat \
    --token ragflow-your-api-token \
    --chat-id your-chat-id \
    --question "What is RAGFlow?"

Interactive Mode

python python_client.py \
    --url ws://localhost/v1/ws/chat \
    --token ragflow-your-api-token \
    --chat-id your-chat-id \
    --interactive

Continue Existing Session

python python_client.py \
    --url ws://localhost/v1/ws/chat \
    --token ragflow-your-api-token \
    --chat-id your-chat-id \
    --session-id existing-session-id \
    --question "Follow-up question?"

Web Demo

  1. Open index.html in your web browser
  2. Enter your RAGFlow server URL, API token, and chat ID
  3. Click "Connect"
  4. Start chatting!

The web demo features:

  • Real-time streaming responses
  • Session persistence
  • Error handling
  • Auto-reconnection support
  • Settings saved in localStorage

📖 Usage Examples

Python Client Features

Interactive conversation:

python python_client.py --url ws://localhost/v1/ws/chat \
                       --token your-token \
                       --chat-id your-chat-id \
                       --interactive

# Then type questions interactively
👤 You: What is machine learning?
🤖 Answer: Machine learning is a subset of artificial intelligence...
✓ Stream completed

👤 You: Can you give examples?
🤖 Answer: Sure! Here are some examples...

Debug mode:

python python_client.py --url ws://localhost/v1/ws/chat \
                       --token your-token \
                       --chat-id your-chat-id \
                       --question "Hello" \
                       --debug

Web Demo Features

Auto-save settings: The web demo automatically saves your connection settings to localStorage, so you don't need to enter them every time.

Session continuity: The demo maintains the session ID, allowing multi-turn conversations without reconnecting.

Visual feedback:

  • Connection status indicator
  • Streaming animation
  • Error messages
  • Message timestamps

🔧 Configuration

Environment Variables

You can also use environment variables with the Python client:

export RAGFLOW_WS_URL="ws://localhost/v1/ws/chat"
export RAGFLOW_API_TOKEN="ragflow-your-token"
export RAGFLOW_CHAT_ID="your-chat-id"

python python_client.py --question "Hello"

SSL/TLS

For secure connections, use wss:// instead of ws://:

python python_client.py --url wss://your-ragflow-host/v1/ws/chat ...

📚 Documentation

For complete documentation, see:

🐛 Troubleshooting

Connection Refused

Problem: WebSocket error: Connection refused

Solution:

  1. Verify RAGFlow server is running
  2. Check the WebSocket URL is correct
  3. Ensure no firewall is blocking the connection

Authentication Failed

Problem: Error 401: Authentication required

Solution:

  1. Verify your API token is correct
  2. Check token hasn't expired
  3. Ensure proper token format: ragflow-xxxxx

Invalid Chat ID

Problem: Error 404: Dialog not found

Solution:

  1. Verify the chat ID exists
  2. Check you have access to the dialog
  3. Ensure you're using the correct tenant

SSL Certificate Error

Problem: Certificate verification failed with wss://

Solution:

For Python client, disable SSL verification (development only):

# In websocket.WebSocketApp
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})

For production, use valid SSL certificates.

🎯 Best Practices

  1. Reuse connections: Don't create new WebSocket for each message
  2. Handle reconnection: Implement exponential backoff for reconnection
  3. Validate inputs: Check all parameters before sending
  4. Error handling: Always handle connection errors gracefully
  5. Clean up: Close WebSocket when done

📝 License

Copyright 2024 The InfiniFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0.

🤝 Support

For issues or questions:

🌟 Contributing

We welcome contributions! Please see our Contributing Guide for details.