30 lines
922 B
Python
30 lines
922 B
Python
import os, json, urllib.request, urllib.error
|
|
|
|
_env_path = os.path.join(os.path.dirname(__file__), '.env')
|
|
if os.path.exists(_env_path):
|
|
with open(_env_path) as f:
|
|
for line in f:
|
|
if '=' in line and not line.startswith('#'):
|
|
k, v = line.strip().split('=', 1)
|
|
os.environ.setdefault(k, v)
|
|
|
|
SARVAM_API_KEY = os.getenv("SARVAM_API_KEY", "")
|
|
|
|
payload = json.dumps({
|
|
"model": "sarvam-2b",
|
|
"messages": [{"role": "user", "content": "hello"}]
|
|
}).encode('utf-8')
|
|
|
|
req = urllib.request.Request(
|
|
'https://api.sarvam.ai/v1/chat/completions',
|
|
data=payload,
|
|
headers={'Content-Type': 'application/json', 'api-subscription-key': SARVAM_API_KEY}
|
|
)
|
|
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=30) as resp:
|
|
print(resp.read().decode('utf-8'))
|
|
except urllib.error.HTTPError as e:
|
|
print("STATUS:", e.code)
|
|
print("BODY:", e.read().decode('utf-8'))
|