-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_basic.py
More file actions
65 lines (48 loc) · 1.58 KB
/
Copy pathagent_basic.py
File metadata and controls
65 lines (48 loc) · 1.58 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
"""
Minimal Tara /agent example — Anthropic Messages shape, no tools.
Usage:
export TARA_API_KEY=tara_sk_...
python agent_basic.py
"""
from __future__ import annotations
import os
import sys
import requests
TARA_API_BASE = "https://tara.tratok.com/api/v1"
def agent(messages: list[dict], system: str | None = None, max_tokens: int = 1024) -> dict:
api_key = os.environ.get("TARA_API_KEY")
if not api_key:
raise SystemExit("Set TARA_API_KEY in your environment first.")
body: dict = {"messages": messages, "max_tokens": max_tokens}
if system:
body["system"] = system
response = requests.post(
f"{TARA_API_BASE}/agent.php",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
},
json=body,
timeout=60,
)
response.raise_for_status()
return response.json()
def extract_text(response: dict) -> str:
"""Concatenate all text blocks from an /agent response."""
return "".join(
block.get("text", "")
for block in response.get("content", [])
if block.get("type") == "text"
)
def main() -> None:
user_input = " ".join(sys.argv[1:]) or "Suggest 3 things to do in Dubai in May."
result = agent(
messages=[{"role": "user", "content": user_input}],
system="Be concise. Use bullet points.",
max_tokens=512,
)
print(extract_text(result))
print()
print(f"--- stop_reason={result['stop_reason']}, tokens={result['usage']['total_tokens']}")
if __name__ == "__main__":
main()