"""Test Groq vision API directly.""" import os, sys, json, base64, io sys.path.insert(0, os.path.dirname(__file__)) from dotenv import load_dotenv load_dotenv() GROQ_API_KEY = os.getenv("GROQ_API_KEY", "") print(f"GROQ_API_KEY set: {bool(GROQ_API_KEY)}, starts with: {GROQ_API_KEY[:8] if GROQ_API_KEY else 'NONE'}...") # Test with a simple text completion first import httpx headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"} # Test 1: Simple chat completion (non-vision) body = { "model": "mixtral-8x7b-32768", "messages": [{"role": "user", "content": "Say 'groq_works' and nothing else"}], "max_tokens": 20, } try: r = httpx.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=body, timeout=15) r.raise_for_status() text = r.json()["choices"][0]["message"]["content"] print(f"Test 1 (text): {text.strip()}") except Exception as e: print(f"Test 1 FAILED: {e}") if hasattr(e, 'response') and e.response: print(f" Response: {e.response.text[:300]}") # Test 2: The actual vision model used by visual parser body2 = { "model": "meta-llama/llama-4-scout-17b-16e-instruct", "messages": [{"role": "user", "content": "Say 'vision_works' and nothing else"}], "max_tokens": 20, } try: r = httpx.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=body2, timeout=15) r.raise_for_status() text = r.json()["choices"][0]["message"]["content"] print(f"Test 2 (vision model, text): {text.strip()}") except Exception as e: print(f"Test 2 FAILED: {e}") if hasattr(e, 'response') and e.response: print(f" Response: {e.response.text[:500]}") # Test 3: List available models try: r = httpx.get("https://api.groq.com/openai/v1/models", headers=headers, timeout=10) r.raise_for_status() models = r.json().get("data", []) vision_models = [m["id"] for m in models if "vision" in m["id"].lower() or "llama-4" in m["id"].lower() or "scout" in m["id"].lower()] print(f"Test 3: Available vision models: {vision_models[:10]}") except Exception as e: print(f"Test 3 FAILED: {e}")