#!/usr/bin/env python3 """ AI Provider Connectivity Test for TradingAgents This script tests which AI providers are accessible from your network. """ import os import requests import json from pathlib import Path def load_env_file(): """Load environment variables from .env file""" env_file = Path('.env') if env_file.exists(): with open(env_file, 'r') as f: for line in f: if '=' in line and not line.strip().startswith('#'): key, value = line.strip().split('=', 1) os.environ[key] = value def test_anthropic_api(): """Test Anthropic Claude API""" print("๐Ÿค– Testing Anthropic (Claude) API...") api_key = os.environ.get('ANTHROPIC_API_KEY', 'test-key') if api_key == 'your_anthropic_api_key_here': print(" โš ๏ธ Please set your ANTHROPIC_API_KEY in .env file") api_key = 'test-key' try: response = requests.post( 'https://api.anthropic.com/v1/messages', headers={ 'Content-Type': 'application/json', 'x-api-key': api_key, 'anthropic-version': '2023-06-01' }, json={ 'model': 'claude-3-5-haiku-20241022', 'max_tokens': 10, 'messages': [{'role': 'user', 'content': 'Hello, respond with just "OK"'}] }, timeout=15 ) print(f" Status: {response.status_code}") if response.status_code == 200: result = response.json() print(f" โœ… SUCCESS! Claude responded: {result['content'][0]['text']}") return True elif response.status_code == 401: print(" โš ๏ธ API accessible but invalid API key") return "accessible" else: print(f" โŒ Unexpected response: {response.text[:100]}") return False except requests.exceptions.RequestException as e: print(f" โŒ Connection failed: {e}") return False def test_google_api(): """Test Google Generative AI API""" print("\n๐Ÿง  Testing Google (Gemini) API...") api_key = os.environ.get('GOOGLE_API_KEY', 'test-key') if api_key == 'your_google_api_key_here': print(" โš ๏ธ Please set your GOOGLE_API_KEY in .env file") api_key = 'test-key' try: response = requests.post( f'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={api_key}', headers={'Content-Type': 'application/json'}, json={ 'contents': [{'parts': [{'text': 'Hello, respond with just "OK"'}]}] }, timeout=15 ) print(f" Status: {response.status_code}") if response.status_code == 200: result = response.json() text = result['candidates'][0]['content']['parts'][0]['text'] print(f" โœ… SUCCESS! Gemini responded: {text}") return True elif response.status_code in [400, 403]: print(" โš ๏ธ API accessible but invalid/missing API key") return "accessible" else: print(f" โŒ Unexpected response: {response.text[:100]}") return False except requests.exceptions.RequestException as e: print(f" โŒ Connection failed: {e}") return False def test_langchain_integration(): """Test if the AI providers work with LangChain (TradingAgents backend)""" print("\n๐Ÿ”— Testing LangChain Integration...") try: # Test Anthropic with LangChain api_key = os.environ.get('ANTHROPIC_API_KEY') if api_key and api_key != 'your_anthropic_api_key_here': from langchain_anthropic import ChatAnthropic llm = ChatAnthropic( model="claude-3-5-haiku-20241022", api_key=api_key, max_tokens=10 ) response = llm.invoke("Hello, respond with just 'LangChain OK'") print(f" โœ… Anthropic + LangChain: {response.content}") return True else: print(" โš ๏ธ No valid Anthropic API key for LangChain test") return False except Exception as e: print(f" โŒ LangChain integration failed: {e}") return False def test_ollama_local(): """Test local Ollama installation""" print("\n๐Ÿ  Testing Ollama (Local AI)...") try: # Override proxy settings for local connection session = requests.Session() session.trust_env = False response = session.get('http://localhost:11434/api/tags', timeout=5) if response.status_code == 200: models = response.json().get('models', []) print(f" โœ… Ollama running with {len(models)} models:") for model in models[:3]: print(f" - {model.get('name', 'Unknown')}") return True else: print(f" โŒ Ollama responding but status: {response.status_code}") return False except requests.exceptions.RequestException as e: print(f" โŒ Ollama not accessible: {e}") print(" ๐Ÿ’ก To install: brew install ollama && ollama serve") return False def main(): """Run all tests and provide recommendations""" print("๐Ÿงช TradingAgents AI Provider Test Suite") print("=" * 50) # Load environment variables load_env_file() # Run tests anthropic_result = test_anthropic_api() google_result = test_google_api() ollama_result = test_ollama_local() if anthropic_result == True: langchain_result = test_langchain_integration() else: langchain_result = False # Summary print("\n" + "=" * 50) print("๐Ÿ“Š TEST RESULTS SUMMARY") print("=" * 50) if anthropic_result == True: print("โœ… Anthropic (Claude) - FULLY WORKING") print(" ๐ŸŽฏ RECOMMENDED: Use this for TradingAgents!") elif anthropic_result == "accessible": print("โš ๏ธ Anthropic (Claude) - Accessible but need valid API key") print(" ๐Ÿ”‘ Get key from: https://console.anthropic.com/") else: print("โŒ Anthropic (Claude) - Not accessible") if google_result == True: print("โœ… Google (Gemini) - FULLY WORKING") elif google_result == "accessible": print("โš ๏ธ Google (Gemini) - Accessible but need valid API key") else: print("โŒ Google (Gemini) - Blocked by company network") if ollama_result: print("โœ… Ollama (Local) - Available") print(" ๐Ÿ’ฐ FREE option, runs on your machine") else: print("โŒ Ollama (Local) - Not installed/running") print("\n๐Ÿš€ NEXT STEPS:") if anthropic_result: print("1. Get Anthropic API key if you haven't already") print("2. Update ANTHROPIC_API_KEY in .env file") print("3. Run TradingAgents and select 'Anthropic' as provider") elif ollama_result: print("1. Use Ollama (local) as your AI provider") print("2. Run TradingAgents and select 'Ollama' as provider") else: print("1. Consider installing Ollama for local AI") print("2. Or try getting API keys for accessible providers") if __name__ == "__main__": main()