File size: 705 Bytes
56de101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""NVIDIA NIM client - OpenAI-compatible chat completions."""
import os
import requests

BASE = os.getenv("NVIDIA_NIM_BASE", "https://integrate.api.nvidia.com/v1")
KEY = os.getenv("NVIDIA_API_KEY")


def _h():
    headers = {"Content-Type": "application/json"}
    if KEY:
        headers["Authorization"] = f"Bearer {KEY}"
    return headers


def chat(messages, model="meta/llama-3.1-8b-instruct", stream=False, **kwargs):
    body = {"model": model, "messages": messages, "stream": stream, **kwargs}
    return requests.post(f"{BASE}/chat/completions", headers=_h(), json=body, timeout=120, stream=stream)


def list_models():
    return requests.get(f"{BASE}/models", headers=_h(), timeout=30).json()