Skip to content

Commit d0c8ad2

Browse files
authored
chore: add stream tools example (#41)
1 parent b0fe0fc commit d0c8ad2

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

examples/basic_usage.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ def completion():
66

77
# Create chat completion
88
response = client.chat.completions.create(
9-
model='glm-4',
9+
model='glm-4.6',
1010
messages=[{'role': 'user', 'content': 'Hello, Z.ai!'}],
11-
temperature=0.7,
11+
temperature=1.0,
1212
)
1313
print(response.choices[0].message.content)
1414

@@ -19,7 +19,7 @@ def completion_with_stream():
1919

2020
# Create chat completion
2121
response = client.chat.completions.create(
22-
model='glm-4',
22+
model='glm-4.6',
2323
messages=[
2424
{'role': 'system', 'content': 'You are a helpful assistant.'},
2525
{'role': 'user', 'content': 'Tell me a story about AI.'},
@@ -38,7 +38,7 @@ def completion_with_websearch():
3838

3939
# Create chat completion
4040
response = client.chat.completions.create(
41-
model='glm-4',
41+
model='glm-4.6',
4242
messages=[
4343
{'role': 'system', 'content': 'You are a helpful assistant.'},
4444
{'role': 'user', 'content': 'What is artificial intelligence?'},
@@ -52,7 +52,7 @@ def completion_with_websearch():
5252
},
5353
}
5454
],
55-
temperature=0.5,
55+
temperature=1.0,
5656
max_tokens=2000,
5757
)
5858

@@ -234,10 +234,10 @@ def ofZhipu():
234234
print(response.choices[0].message.content)
235235

236236
if __name__ == '__main__':
237-
# completion()
238-
# completion_with_stream()
237+
completion()
238+
completion_with_stream()
239239
# completion_with_websearch()
240-
multi_modal_chat()
240+
# multi_modal_chat()
241241
# role_play()
242242
# assistant_conversation()
243243
# video_generation()

examples/stream_tools.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from zai import ZhipuAiClient
2+
3+
def main():
4+
client = ZhipuAiClient()
5+
# create chat completion with tool calls and streaming
6+
response = client.chat.completions.create(
7+
model="glm-4.6",
8+
messages=[
9+
{"role": "user", "content": "How is the weather in Beijing and Shanghai? Please provide the answer in Celsius."},
10+
],
11+
tools=[
12+
{
13+
"type": "function",
14+
"function": {
15+
"name": "get_weather",
16+
"description": "Get the weather information for a specific location",
17+
"parameters": {
18+
"type": "object",
19+
"properties": {
20+
"location": {"type": "string", "description": "City, eg: Beijing, Shanghai"},
21+
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
22+
},
23+
"required": ["location"]
24+
}
25+
}
26+
}
27+
],
28+
stream=True, # enable streaming
29+
tool_stream=True # enable tool call streaming
30+
)
31+
32+
# init variables to collect streaming data
33+
reasoning_content = "" # reasoning content
34+
content = "" # response content
35+
final_tool_calls = {} # tool call data
36+
reasoning_started = False # is reasoning started
37+
content_started = False # is content started
38+
39+
# process streaming response
40+
for chunk in response:
41+
if not chunk.choices:
42+
continue
43+
44+
delta = chunk.choices[0].delta
45+
46+
# process streaming reasoning output
47+
if hasattr(delta, 'reasoning_content') and delta.reasoning_content:
48+
if not reasoning_started and delta.reasoning_content.strip():
49+
print("\n🧠 Thinking: ")
50+
reasoning_started = True
51+
reasoning_content += delta.reasoning_content
52+
print(delta.reasoning_content, end="", flush=True)
53+
54+
# process streaming answer content output
55+
if hasattr(delta, 'content') and delta.content:
56+
if not content_started and delta.content.strip():
57+
print("\n\n💬 Answer: ")
58+
content_started = True
59+
content += delta.content
60+
print(delta.content, end="", flush=True)
61+
62+
# process streaming tool call info
63+
if delta.tool_calls:
64+
for tool_call in delta.tool_calls:
65+
index = tool_call.index
66+
if index not in final_tool_calls:
67+
# add new tool call
68+
final_tool_calls[index] = tool_call
69+
final_tool_calls[index].function.arguments = tool_call.function.arguments
70+
else:
71+
# append tool call params by streaming index
72+
final_tool_calls[index].function.arguments += tool_call.function.arguments
73+
74+
# output the final construct tool call info
75+
if final_tool_calls:
76+
print("\n📋 Function Calls Triggered:")
77+
for index, tool_call in final_tool_calls.items():
78+
print(f" {index}: Function Name: {tool_call.function.name}, Params: {tool_call.function.arguments}")
79+
80+
if __name__ == "__main__":
81+
main()

0 commit comments

Comments
 (0)