File size: 3,933 Bytes
7c6ffa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Manual smoke test for Sarvam AI provider.

Loads .env, sends a tiny prompt to Sarvam, prints model_used and response.
Never prints the API key.

Usage:
    python scripts/test_sarvam_provider.py [--base-url https://api.sarvam.ai/v1]

Exit codes:
    0 = success
    1 = failure
"""

from __future__ import annotations

import argparse
import json
import os
import sys
from pathlib import Path
from urllib.error import URLError
from urllib.request import Request, urlopen

BACKEND_DIR = Path(__file__).resolve().parents[1]

# Load .env manually (no dependency on python-dotenv for scripts)
env_path = BACKEND_DIR / ".env"
if env_path.exists():
    for line in env_path.read_text(encoding="utf-8").splitlines():
        line = line.strip()
        if not line or line.startswith("#"):
            continue
        if "=" in line:
            key, _, value = line.partition("=")
            key = key.strip()
            value = value.strip().strip('"').strip("'")
            os.environ.setdefault(key, value)


def main() -> None:
    parser = argparse.ArgumentParser(description="Sarvam AI Smoke Test")
    parser.add_argument(
        "--base-url",
        default=os.environ.get("SARVAM_BASE_URL", "https://api.sarvam.ai/v1"),
    )
    args = parser.parse_args()
    base = args.base_url.rstrip("/")
    api_key = os.environ.get("SARVAM_API_KEY", "")

    print("=" * 50)
    print("  DocDoe AI - Sarvam Provider Smoke Test")
    print(f"  Base URL: {base}")
    print(f"  Key configured: {'yes' if api_key else 'NO'}")
    print("=" * 50)

    if not api_key:
        print("\n  ERROR: SARVAM_API_KEY is not set in .env or environment.")
        print("  Set SARVAM_API_KEY and try again.")
        sys.exit(1)

    model = os.environ.get("SARVAM_MODEL_MAIN", "sarvam-30b")
    url = f"{base}/chat/completions"

    payload = {
        "model": model,
        "messages": [
            {"role": "system", "content": "You are a helpful tutor. Respond with one short JSON object only."},
            {"role": "user", "content": 'Return JSON with keys "greeting" and "status". Keep it short.'},
        ],
        "temperature": 0.1,
        "max_tokens": 800,
        "reasoning_effort": "low",
    }

    print(f"\n  Calling: POST {url}")
    print(f"  Model: {model}")

    data = json.dumps(payload).encode()
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}",
    }
    req = Request(url, data=data, headers=headers, method="POST")

    try:
        with urlopen(req, timeout=30) as resp:
            result = json.loads(resp.read().decode())
    except URLError as exc:
        print(f"\n  FAIL: {exc}")
        sys.exit(1)
    except Exception as exc:
        print(f"\n  FAIL: {type(exc).__name__}: {exc}")
        sys.exit(1)

    # Parse response
    content = ""
    model_used = result.get("model", "unknown")
    choices = result.get("choices", [])
    if choices:
        msg = choices[0].get("message", {})
        content = msg.get("content") or ""
    usage = result.get("usage", {})

    print(f"\n  model_used: {model_used}")
    print(f"  content: {(content or '(empty)')[:300]}")
    print(f"  raw keys: {list(result.keys())}")
    if choices:
        print(f"  choices[0] keys: {list(choices[0].keys())}")
        print(f"  message keys: {list(choices[0].get('message', {}).keys())}")
    if usage:
        print(f"  tokens: prompt={usage.get('prompt_tokens', '?')}, "
              f"completion={usage.get('completion_tokens', '?')}, "
              f"total={usage.get('total_tokens', '?')}")

    if content:
        print("\n  RESULT: PASS - Sarvam API is responding.")
        sys.exit(0)
    else:
        # Print full raw response for debugging
        print(f"\n  Full raw response: {json.dumps(result, indent=2)[:500]}")
        print("\n  RESULT: FAIL - Empty content from Sarvam.")
        sys.exit(1)


if __name__ == "__main__":
    main()