File size: 4,642 Bytes
31f0e50 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #!/usr/bin/env python
"""
Test Groq API Connectivity.
This script verifies that the Groq API key is configured correctly
and that we can make successful API calls.
Usage:
python scripts/test_groq_api.py
"""
import os
import sys
import time
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def test_groq_api():
"""Test Groq API connectivity as specified in TASKS.md."""
print("=" * 60)
print("Task 2.2 Verification - Groq API Connectivity")
print("=" * 60)
print()
# Step 1: Load environment
print("Step 1: Loading environment variables...")
try:
from dotenv import load_dotenv
load_dotenv()
print(" [OK] Environment loaded")
except ImportError:
print(" [ERROR] python-dotenv not installed")
print(" Run: pip install python-dotenv")
return False
# Step 2: Check API key
print("\nStep 2: Checking GROQ_API_KEY...")
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
print(" [ERROR] GROQ_API_KEY not set in environment")
print(" Add to .env file: GROQ_API_KEY=your_key_here")
return False
if api_key.startswith("gsk_"):
print(f" [OK] API key found: {api_key[:15]}...")
else:
print(" [WARN] API key format unexpected (should start with 'gsk_')")
print(f" Key: {api_key[:10]}...")
# Step 3: Initialize Groq client
print("\nStep 3: Initializing Groq client...")
try:
from groq import Groq
client = Groq(api_key=api_key)
print(" [OK] Groq client initialized")
except ImportError:
print(" [ERROR] groq library not installed")
print(" Run: pip install groq")
return False
except Exception as e:
print(f" [ERROR] Failed to initialize client: {e}")
return False
# Step 4: Test API call
print("\nStep 4: Testing API call...")
model = os.getenv("GROQ_MODEL", "llama-3.3-70b-versatile")
print(f" Model: {model}")
try:
start = time.time()
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "Hello!"}],
max_tokens=50
)
response_time = time.time() - start
# Extract response
content = response.choices[0].message.content
print(f" [OK] API call successful ({response_time:.2f}s)")
print(f" Response: {content[:100]}...")
except Exception as e:
error_msg = str(e)
print(f" [ERROR] API call failed: {error_msg}")
if "401" in error_msg or "invalid" in error_msg.lower():
print("\n Authentication Error:")
print(" 1. Check your API key is correct")
print(" 2. Get a new key from: https://console.groq.com/")
elif "rate" in error_msg.lower():
print("\n Rate Limit Error:")
print(" 1. Wait a moment and try again")
print(" 2. Check your Groq usage limits")
return False
# Step 5: Test with LangChain integration
print("\nStep 5: Testing LangChain-Groq integration...")
try:
from langchain_groq import ChatGroq
llm = ChatGroq(
api_key=api_key,
model_name=model,
temperature=0.7,
max_tokens=50
)
start = time.time()
response = llm.invoke("Say 'Hello from LangChain!'")
response_time = time.time() - start
print(f" [OK] LangChain integration working ({response_time:.2f}s)")
print(f" Response: {response.content[:100]}...")
except ImportError:
print(" [SKIP] langchain-groq not installed (optional)")
except Exception as e:
print(f" [WARN] LangChain integration test failed: {e}")
print(" (This is optional - basic Groq API works)")
# Summary
print("\n" + "=" * 60)
print("Task 2.2 Status Summary")
print("=" * 60)
print("[OK] Groq API key obtained")
print("[OK] .env file configured")
print("[OK] Test API call successful")
print("\nTask 2.2: COMPLETE")
print("=" * 60)
return True
def main():
"""Main entry point."""
success = test_groq_api()
return 0 if success else 1
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)
|