Files
railseek6/start_server_fixed.py

148 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""
Fixed server starter that handles Unicode encoding issues on Windows
"""
import os
import sys
import subprocess
import time
def main():
"""Start the LightRAG server with proper encoding settings"""
print("Starting LightRAG server with fixed encoding...")
# Set environment variables for UTF-8 encoding
env = os.environ.copy()
env['PYTHONIOENCODING'] = 'utf-8'
env['PYTHONUTF8'] = '1'
# Read configuration from .env file
config = {}
try:
with open('.env', 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
if '=' in line:
key, value = line.split('=', 1)
config[key.strip()] = value.strip()
except FileNotFoundError:
print("Warning: .env file not found, using defaults")
# Ensure critical LLM settings have defaults to prevent accidental OpenAI usage
if 'LLM_BINDING_HOST' not in config:
config['LLM_BINDING_HOST'] = 'https://api.deepseek.com/v1'
print("Warning: LLM_BINDING_HOST not set, defaulting to DeepSeek API")
if 'OPENAI_API_BASE' not in config:
config['OPENAI_API_BASE'] = config.get('LLM_BINDING_HOST', 'https://api.deepseek.com/v1')
if 'LLM_MODEL' not in config:
config['LLM_MODEL'] = 'deepseek-chat'
if 'LLM_BINDING_API_KEY' not in config and 'OPENAI_API_KEY' not in config:
print("ERROR: LLM_BINDING_API_KEY or OPENAI_API_KEY must be set in .env")
sys.exit(1)
# Get configuration values with defaults
port = config.get('PORT', '3015')
host = config.get('HOST', '0.0.0.0')
llm_binding = config.get('LLM_BINDING', 'openai')
embedding_binding = config.get('EMBEDDING_BINDING', 'ollama')
rerank_binding = config.get('RERANK_BINDING', 'jina')
# Set LLM-related environment variables
llm_keys = [
'LLM_BINDING_HOST',
'LLM_BINDING_API_KEY',
'LLM_MODEL',
'OPENAI_API_KEY',
'OPENAI_API_BASE',
'ENABLE_LLM_CACHE',
'ENABLE_LLM_CACHE_FOR_EXTRACT',
'TIMEOUT',
'TEMPERATURE',
'MAX_ASYNC',
'MAX_TOKENS',
'OPTIMIZE_ENTITY_EXTRACTION'
]
for key in llm_keys:
if key in config:
env[key] = config[key]
# Set embedding-related environment variables
embedding_keys = [
'EMBEDDING_MODEL',
'EMBEDDING_DIM',
'EMBEDDING_BINDING_HOST',
'EMBEDDING_BATCH_NUM',
'EMBEDDING_FUNC_MAX_ASYNC'
]
for key in embedding_keys:
if key in config:
env[key] = config[key]
# Set rerank-related environment variables
rerank_keys = [
'RERANK_MODEL'
]
for key in rerank_keys:
if key in config:
env[key] = config[key]
# Build command
cmd = [
sys.executable, '-m', 'lightrag.api.lightrag_server',
'--port', port,
'--host', host,
'--working-dir', 'rag_storage',
'--input-dir', '../inputs',
'--key', 'jleu1212',
'--auto-scan-at-startup',
'--llm-binding', llm_binding,
'--embedding-binding', embedding_binding,
'--rerank-binding', rerank_binding
]
print(f"Command: {' '.join(cmd)}")
print(f"Starting server on http://{host}:{port}")
try:
# Change to LightRAG-main directory
os.chdir('LightRAG-main')
# Start the server
process = subprocess.Popen(
cmd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding='utf-8',
errors='replace'
)
# Print output in real-time
print("\nServer output:")
print("-" * 50)
# Read and print output
for line in iter(process.stdout.readline, ''):
# Filter out problematic Unicode characters
cleaned_line = ''.join(c if ord(c) < 128 else '?' for c in line)
print(cleaned_line.rstrip())
process.wait()
except KeyboardInterrupt:
print("\nServer stopped by user")
if process:
process.terminate()
except Exception as e:
print(f"Error starting server: {e}")
import traceback
traceback.print_exc()
return 1
return 0
if __name__ == "__main__":
sys.exit(main())