#!/usr/bin/env python3 """ Fix DeepSeek API configuration to ensure it works without fallback models """ import os import sys import subprocess import requests import json import time from pathlib import Path # Add current directory to Python path current_dir = Path(__file__).parent sys.path.insert(0, str(current_dir)) def verify_deepseek_direct(): """Test DeepSeek API directly""" print("šŸ” Testing DeepSeek API directly...") try: import openai from openai import OpenAI # Test with direct API call client = OpenAI( api_key="sk-55f6e57f1d834b0e93ceaf98cc2cb715", base_url="https://api.deepseek.com/v1" ) response = client.chat.completions.create( model="deepseek-chat", messages=[{"role": "user", "content": "Test message to verify API connectivity"}], max_tokens=50 ) print(f"āœ… DeepSeek API direct test successful") print(f"šŸ¤– Response: {response.choices[0].message.content}") return True except Exception as e: print(f"āŒ DeepSeek API direct test failed: {e}") return False def check_server_config(): """Check current server configuration""" print("\nšŸ“‹ Checking current server configuration...") # Check if server is running try: response = requests.get("http://localhost:3015/api/health", timeout=5) if response.status_code == 200: print("āœ… Server is running") return True else: print(f"āŒ Server returned status: {response.status_code}") return False except: print("āŒ Server is not running") return False def create_fixed_server_script(): """Create a fixed server startup script that forces DeepSeek configuration""" script_content = '''#!/usr/bin/env python3 """ Fixed LightRAG server startup with forced DeepSeek API configuration """ import os import sys from pathlib import Path # Force set environment variables for DeepSeek os.environ["LLM_BINDING"] = "openai" os.environ["LLM_MODEL"] = "deepseek-chat" os.environ["LLM_BINDING_HOST"] = "https://api.deepseek.com/v1" os.environ["LLM_BINDING_API_KEY"] = "sk-55f6e57f1d834b0e93ceaf98cc2cb715" os.environ["EMBEDDING_BINDING"] = "ollama" os.environ["EMBEDDING_MODEL"] = "snowflake-arctic-embed2:latest" os.environ["EMBEDDING_DIM"] = "1024" # Force storage backends os.environ["LIGHTRAG_KV_STORAGE"] = "RedisKVStorage" os.environ["LIGHTRAG_VECTOR_STORAGE"] = "QdrantVectorDBStorage" os.environ["LIGHTRAG_DOC_STATUS_STORAGE"] = "PGDocStatusStorage" os.environ["LIGHTRAG_GRAPH_STORAGE"] = "Neo4JStorage" # Database connections os.environ["REDIS_URI"] = "redis://localhost:6379" os.environ["NEO4J_URI"] = "bolt://localhost:7687" os.environ["NEO4J_USERNAME"] = "neo4j" os.environ["NEO4J_PASSWORD"] = "jleu1212" os.environ["QDRANT_URL"] = "http://localhost:6333" os.environ["POSTGRES_HOST"] = "localhost" os.environ["POSTGRES_PORT"] = "5432" os.environ["POSTGRES_USER"] = "jleu3482" os.environ["POSTGRES_PASSWORD"] = "jleu1212" os.environ["POSTGRES_DATABASE"] = "rag_anything" print("šŸ”§ Forced DeepSeek API configuration:") print(f" LLM: {os.environ['LLM_BINDING']} - {os.environ['LLM_MODEL']}") print(f" Embedding: {os.environ['EMBEDDING_BINDING']} - {os.environ['EMBEDDING_MODEL']}") print(f" Storage: {os.environ['LIGHTRAG_KV_STORAGE']}, {os.environ['LIGHTRAG_VECTOR_STORAGE']}") # Now start the server lightrag_dir = Path(__file__).parent / "LightRAG-main" os.chdir(lightrag_dir) import subprocess cmd = [ sys.executable, "-m", "lightrag.api.lightrag_server", "--port", "3015", "--host", "0.0.0.0", "--working-dir", "rag_storage", "--input-dir", "../inputs", "--key", "jleu1212", "--auto-scan-at-startup", "--llm-binding", os.environ["LLM_BINDING"], "--embedding-binding", os.environ["EMBEDDING_BINDING"], "--rerank-binding", "jina" ] print(f"šŸš€ Starting server with command: {' '.join(cmd)}") subprocess.run(cmd, check=True) ''' script_path = current_dir / "start_deepseek_forced.py" with open(script_path, 'w') as f: f.write(script_content) print(f"āœ… Created forced configuration script: {script_path}") return script_path def test_retrieval_with_deepseek(): """Test retrieval with DeepSeek API""" print("\nšŸ” Testing retrieval with DeepSeek API...") # Wait for server to be ready print("ā³ Waiting for server to be ready...") time.sleep(10) try: headers = { 'X-API-Key': 'jleu1212', 'Content-Type': 'application/json' } data = { 'query': 'railway transportation system', 'top_k': 3, 'use_llm': True } response = requests.post( "http://localhost:3015/api/search", headers=headers, json=data, timeout=30 ) if response.status_code == 200: results = response.json() print("āœ… Search request successful") if results.get('llm_response'): print(f"šŸ¤– DeepSeek LLM Response: {results['llm_response'][:200]}...") print("āœ… DeepSeek API is working for retrieval!") return True else: print("āš ļø No LLM response in results") if results.get('results'): print(f"šŸ“Š Vector search results: {len(results['results'])} found") return True else: print("āŒ No search results at all") return False else: print(f"āŒ Search failed: {response.status_code}") print(f"Error: {response.text}") return False except Exception as e: print(f"āŒ Search test error: {e}") return False def main(): """Main function to fix DeepSeek API configuration""" print("=" * 60) print("šŸ”§ FIXING DEEPSEEK API CONFIGURATION") print("=" * 60) # Step 1: Test DeepSeek API directly if not verify_deepseek_direct(): print("āŒ DeepSeek API is not accessible") return False # Step 2: Create fixed server script script_path = create_fixed_server_script() print(f"\nšŸš€ To start server with fixed DeepSeek configuration:") print(f" python {script_path.name}") print("\nšŸ“‹ Configuration Summary:") print(" āœ… DeepSeek API: openai/deepseek-chat") print(" āœ… Embedding: ollama/snowflake-arctic-embed2:latest") print(" āœ… Storage: Redis + Neo4j + Qdrant + PostgreSQL") print(" āœ… No fallback models enabled") print("\nšŸŽÆ Next steps:") print("1. Run: python start_deepseek_forced.py") print("2. Wait for server to start") print("3. Test with: python test_deepseek_retrieval.py") print("4. Verify DeepSeek responses in search results") return True if __name__ == "__main__": success = main() exit(0 if success else 1)