-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
82 lines (63 loc) · 2.33 KB
/
example.py
File metadata and controls
82 lines (63 loc) · 2.33 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
77
78
79
80
81
82
"""Example usage of Z.AI API Client."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from zai.client import ZAIClient
from zai.core.exceptions import ZAIError
def main():
"""
Main function demonstrating Z.AI client usage.
Returns:
None: Executes example chat interactions.
"""
try:
print("Initializing Z.AI Client...")
client = ZAIClient(auto_auth=True, verbose=False)
print(f"Got token: {client.token[:20]}...")
print("\n" + "="*50)
print("Simple Chat Example")
print("="*50)
try:
response = client.simple_chat(
message="What is the capital of France?",
model="glm-4.5v",
enable_thinking=False,
temperature=0.7,
top_p=0.9,
max_tokens=500
)
if response.content:
print(f"\nResponse: {response.content}")
else:
print("\nNo content in response")
if response.thinking:
print(f"\nThinking: {response.thinking}")
if response.usage:
print(f"\nUsage: {response.usage}")
except Exception as chat_error:
print(f"Chat error: {chat_error}")
print("\n" + "="*50)
print("Testing with Code Model")
print("="*50)
try:
response2 = client.simple_chat(
message="Say 'Hello World'",
model="0727-360B-API",
enable_thinking=False,
temperature=0.5,
max_tokens=100
)
if response2.content:
print(f"\nResponse: {response2.content}")
else:
print("\nNo content in second response")
except Exception as chat2_error:
print(f"Second chat error: {chat2_error}")
except ZAIError as e:
print(f"ZAI Error: {e}")
except Exception as e:
import traceback
print(f"Unexpected error: {e}")
print(f"Traceback: {traceback.format_exc()}")
if __name__ == "__main__":
main()