Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (59 loc) · 2.15 KB
/
Copy pathmain.py
File metadata and controls
76 lines (59 loc) · 2.15 KB
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
"""
main.py
Calls the NVIDIA Integrate chat completions API. File-to-base64 encoding
(e.g. for image inputs) is delegated to the compiled C++ extension `fastb64`
for speed -- build it first with `python setup.py build_ext --inplace`.
The API key is read from the NVIDIA_API_KEY environment variable.
Never hardcode API keys in source files.
"""
import os
import json
import requests
import fastb64 # compiled C++ extension (base64_module.cpp)
INVOKE_URL = "https://integrate.api.nvidia.com/v1/chat/completions"
MODEL = "qwen/qwen3.5-397b-a17b"
STREAM = False
API_KEY = os.environ.get("NVIDIA_API_KEY")
if not API_KEY:
raise RuntimeError(
"Set the NVIDIA_API_KEY environment variable before running this script, "
"e.g.: export NVIDIA_API_KEY='nvapi-...'"
)
def encode_file(path: str) -> str:
"""Base64-encode a local file using the fast C++ extension."""
return fastb64.read_file_b64(path)
def build_payload(prompt: str, image_path: str | None = None) -> dict:
content = prompt
if image_path:
b64 = encode_file(image_path)
content = f'{prompt} <img src="data:image/png;base64,{b64}" />'
return {
"model": MODEL,
"messages": [{"role": "user", "content": content}],
"max_tokens": 16384,
"temperature": 0.60,
"top_p": 0.95,
"top_k": 20,
"presence_penalty": 0,
"repetition_penalty": 1,
"stream": STREAM,
}
def call_api(prompt: str, image_path: str | None = None):
headers = {
"Authorization": f"Bearer {API_KEY}",
"Accept": "text/event-stream" if STREAM else "application/json",
}
payload = build_payload(prompt, image_path)
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=STREAM)
response.raise_for_status()
if STREAM:
for line in response.iter_lines():
if line:
print(line.decode("utf-8"))
return None
data = response.json()
print(json.dumps(data, indent=2))
return data
if __name__ == "__main__":
# Example: call_api("Describe this image", image_path="photo.png")
call_api("Hello! Just testing the API connection.")