-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_basic.py
More file actions
56 lines (42 loc) · 1.21 KB
/
Copy pathchat_basic.py
File metadata and controls
56 lines (42 loc) · 1.21 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
"""
Minimal Tara /chat example.
Usage:
export TARA_API_KEY=tara_sk_...
pip install -r requirements.txt
python chat_basic.py
"""
from __future__ import annotations
import os
import sys
import requests
TARA_API_BASE = "https://tara.tratok.com/api/v1"
def chat(message: str) -> dict:
api_key = os.environ.get("TARA_API_KEY")
if not api_key:
raise SystemExit("Set TARA_API_KEY in your environment first.")
response = requests.post(
f"{TARA_API_BASE}/chat.php",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
},
json={
"message": message,
"max_tokens": 512,
"temperature": 0.7,
},
timeout=60,
)
response.raise_for_status()
return response.json()
def main() -> None:
user_input = " ".join(sys.argv[1:]) or "What is Tratok in one sentence?"
result = chat(user_input)
if not result.get("ok"):
print(f"Error: {result.get('error', {}).get('message', 'unknown')}")
sys.exit(1)
print(result["reply"])
print()
print(f"--- tokens: {result['usage']['total_tokens']}")
if __name__ == "__main__":
main()