86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
"""Test Claude API key — text + vision."""
|
|
import sys, os
|
|
sys.path.insert(0, 'backend')
|
|
|
|
from dotenv import load_dotenv
|
|
load_dotenv('backend/.env')
|
|
|
|
api_key = os.getenv('CLAUDE_API_KEY', '').strip()
|
|
if not api_key:
|
|
print("FAIL: CLAUDE_API_KEY not found in .env")
|
|
sys.exit(1)
|
|
|
|
print(f"Key found: sk-ant-...{api_key[-6:]}")
|
|
|
|
import anthropic
|
|
client = anthropic.Anthropic(api_key=api_key)
|
|
|
|
# Test 1: Basic text
|
|
print("\n[1] Testing text (claude-haiku-4-5)...")
|
|
try:
|
|
resp = client.messages.create(
|
|
model="claude-haiku-4-5-20251001",
|
|
max_tokens=64,
|
|
messages=[{"role": "user", "content": "Reply with exactly: KEY_WORKS"}]
|
|
)
|
|
text = resp.content[0].text.strip()
|
|
print(f" Response: {text}")
|
|
print(f" Text test: {'PASS' if 'KEY_WORKS' in text else 'FAIL'}")
|
|
except Exception as e:
|
|
print(f" Text test: FAIL — {e}")
|
|
|
|
# Test 2: Tool calling
|
|
print("\n[2] Testing tool calling...")
|
|
try:
|
|
resp = client.messages.create(
|
|
model="claude-haiku-4-5-20251001",
|
|
max_tokens=256,
|
|
tools=[{
|
|
"name": "get_weather",
|
|
"description": "Get weather for a city",
|
|
"input_schema": {
|
|
"type": "object",
|
|
"properties": {"city": {"type": "string"}},
|
|
"required": ["city"]
|
|
}
|
|
}],
|
|
messages=[{"role": "user", "content": "What's the weather in Mumbai?"}]
|
|
)
|
|
used_tool = any(b.type == "tool_use" for b in resp.content)
|
|
print(f" Tool called: {'PASS' if used_tool else 'FAIL'}")
|
|
if used_tool:
|
|
tool_block = next(b for b in resp.content if b.type == "tool_use")
|
|
print(f" Tool input: {tool_block.input}")
|
|
except Exception as e:
|
|
print(f" Tool calling: FAIL — {e}")
|
|
|
|
# Test 3: Vision
|
|
print("\n[3] Testing vision with a sample scraped image...")
|
|
import base64
|
|
from pathlib import Path
|
|
|
|
scraped_dir = Path("backend/data/scraped")
|
|
imgs = list(scraped_dir.glob("*.jpg"))[:1] if scraped_dir.exists() else []
|
|
|
|
if imgs:
|
|
img_path = imgs[0]
|
|
b64 = base64.b64encode(img_path.read_bytes()).decode()
|
|
try:
|
|
resp = client.messages.create(
|
|
model="claude-haiku-4-5-20251001",
|
|
max_tokens=128,
|
|
messages=[{"role": "user", "content": [
|
|
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": b64}},
|
|
{"type": "text", "text": "Describe this fashion image in one sentence."}
|
|
]}]
|
|
)
|
|
desc = resp.content[0].text.strip()
|
|
print(f" Vision test: PASS")
|
|
print(f" Description: {desc[:120]}")
|
|
except Exception as e:
|
|
print(f" Vision test: FAIL — {e}")
|
|
else:
|
|
print(" Vision test: SKIPPED (no scraped images yet)")
|
|
|
|
print("\nDone.")
|