| import sys |
| from pathlib import Path |
| import os |
|
|
| |
| project_root = Path(__file__).parent.parent |
| sys.path.append(str(project_root)) |
|
|
| from langchain_openai import OpenAIEmbeddings |
| from langchain_chroma import Chroma |
| from config.settings import Settings |
| import requests |
|
|
|
|
| def test_openai_connection(): |
| """Test OpenAI API connection""" |
| print("\n=== Testing OpenAI API Connection ===") |
| |
| |
| if not Settings.OPENAI_API_KEY: |
| print("β OpenAI API key not found!") |
| return False |
| print(f"β OpenAI API key found (length: {len(Settings.OPENAI_API_KEY)})") |
| |
| |
| try: |
| print("Testing connection to api.openai.com...") |
| response = requests.get( |
| "https://api.openai.com", |
| timeout=10, |
| verify=True |
| ) |
| print(f"β OpenAI API reachable (Status: {response.status_code})") |
| print(f"Response headers: {dict(response.headers)}") |
| except requests.exceptions.SSLError as e: |
| print(f"β SSL Error connecting to OpenAI: {e}") |
| return False |
| except requests.exceptions.ConnectionError as e: |
| print(f"β Connection Error: {e}") |
| print("Checking if proxy is needed...") |
| |
| print(f"HTTP_PROXY: {os.environ.get('HTTP_PROXY')}") |
| print(f"HTTPS_PROXY: {os.environ.get('HTTPS_PROXY')}") |
| return False |
| except Exception as e: |
| print(f"β Cannot reach OpenAI API: {type(e).__name__}: {e}") |
| return False |
| |
| |
| try: |
| print("\nTesting embeddings creation...") |
| embeddings = OpenAIEmbeddings( |
| openai_api_key=Settings.OPENAI_API_KEY, |
| timeout=30, |
| max_retries=2 |
| ) |
| print("Embeddings object created, attempting query...") |
| result = embeddings.embed_query("test") |
| print(f"β Embeddings working (vector size: {len(result)})") |
| return True |
| except Exception as e: |
| print(f"β Embeddings error: {type(e).__name__}: {e}") |
| print("Stack trace:") |
| import traceback |
| traceback.print_exc() |
| return False |
|
|
|
|
| def test_chroma_setup(): |
| """Test ChromaDB setup and functionality""" |
| print("\n=== Testing ChromaDB Setup ===") |
| |
| |
| chroma_dir = Settings.get_chroma_path() |
| print(f"Chroma directory: {chroma_dir}") |
| |
| if not chroma_dir.exists(): |
| print("β Chroma directory not found!") |
| return False |
| print("β Chroma directory exists") |
| |
| |
| sqlite_file = chroma_dir / "chroma.sqlite3" |
| if not sqlite_file.exists(): |
| print("β SQLite database not found!") |
| return False |
| print(f"β SQLite database found ({sqlite_file.stat().st_size / (1024*1024):.2f} MB)") |
| |
| |
| try: |
| embeddings = OpenAIEmbeddings( |
| openai_api_key=Settings.OPENAI_API_KEY |
| ) |
| vector_store = Chroma( |
| persist_directory=str(chroma_dir), |
| embedding_function=embeddings, |
| collection_name=Settings.CHROMA_COLLECTION_NAME |
| ) |
| |
| |
| collection = vector_store._collection |
| count = collection.count() |
| print(f"β Collection loaded ({count} documents)") |
| |
| |
| results = vector_store.similarity_search_with_score("test", k=1) |
| print("β Search functionality working") |
| |
| return True |
| except Exception as e: |
| print(f"β ChromaDB error: {e}") |
| return False |
|
|
|
|
| def test_huggingface_setup(): |
| """Test HuggingFace-specific setup""" |
| print("\n=== Testing HuggingFace Setup ===") |
| |
| if not Settings.is_huggingface(): |
| print("Skipping HuggingFace tests (not in HF environment)") |
| return True |
| |
| |
| if not Settings.HF_TOKEN: |
| print("β HuggingFace token not found!") |
| return False |
| print("β HuggingFace token found") |
| |
| |
| try: |
| from huggingface_hub import HfApi |
| api = HfApi(token=Settings.HF_TOKEN) |
| files = api.list_repo_files(Settings.HF_DATASET, repo_type="dataset") |
| print(f"β Dataset accessible ({len(files)} files)") |
| return True |
| except Exception as e: |
| print(f"β Dataset access error: {e}") |
| return False |
|
|
|
|
| def test_environment(): |
| """Run all environment tests""" |
| print(f"\nTesting environment: {Settings.DEPLOYMENT_MODE}") |
| |
| |
| if Settings.is_huggingface(): |
| hf_ok = test_huggingface_setup() |
| if not hf_ok: |
| print("\nβ HuggingFace setup failed!") |
| return False |
| |
| |
| openai_ok = test_openai_connection() |
| |
| |
| chroma_ok = test_chroma_setup() |
| |
| |
| print("\n=== Test Summary ===") |
| print(f"OpenAI Connection: {'β' if openai_ok else 'β'}") |
| print(f"ChromaDB Setup: {'β' if chroma_ok else 'β'}") |
| if Settings.is_huggingface(): |
| print(f"HuggingFace Setup: {'β' if hf_ok else 'β'}") |
| |
| return all([openai_ok, chroma_ok]) |
|
|
|
|
| if __name__ == "__main__": |
| success = test_environment() |
| sys.exit(0 if success else 1) |