File size: 1,949 Bytes
0ba585f
db83b53
0ba585f
 
 
 
 
 
 
db83b53
0ba585f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db83b53
0ba585f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db83b53
0ba585f
 
 
db83b53
0ba585f
 
 
 
db83b53
0ba585f
 
db83b53
 
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
"""Test Cerebras API key directly."""

import os
import httpx
from openai import AsyncOpenAI

CEREBRAS_API_KEY = "csk-2ewy2h26eeph4yex94kmjnfwwx35pdpyyxkv3j6wcj4cxc3t"
CEREBRAS_BASE_URL = "https://api.cerebras.ai/v1"


async def test_models_list():
    """Test listing models."""
    print("=== Testing /v1/models ===")
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(
                f"{CEREBRAS_BASE_URL}/models",
                headers={"Authorization": f"Bearer {CEREBRAS_API_KEY}"},
                timeout=10.0,
            )
            print(f"Status: {response.status_code}")
            print(f"Response: {response.text[:800]}")
        except Exception as e:
            print(f"Error: {e}")


async def test_chat():
    """Test a chat completion."""
    print("\n=== Testing /v1/chat/completions ===")
    client = AsyncOpenAI(
        api_key=CEREBRAS_API_KEY,
        base_url=CEREBRAS_BASE_URL,
        timeout=httpx.Timeout(60.0),
    )

    models_to_try = [
        "qwen-3-235b-a22b-instruct-2507",
        "zai-glm-4.7",
        "gpt-oss-120b",
        "llama3.1-8b",
    ]

    for model in models_to_try:
        print(f"\nTrying model: {model}")
        try:
            response = await client.chat.completions.create(
                model=model,
                messages=[{"role": "user", "content": "hi"}],
                max_tokens=20,
            )
            print(f"Success!")
            print(f"Model: {response.model}")
            print(f"Content: {response.choices[0].message.content}")
            break
        except Exception as e:
            error_text = str(e)
            if hasattr(e, "response") and e.response:
                error_text = e.response.text
            print(f"Error: {error_text[:300]}")


async def main():
    await test_models_list()
    await test_chat()


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())