Skip to content

Commit 508eaf4

Browse files
wellenzhengzhengweijun
andauthored
fix: video example, remove finetuning\knowledge\document module (#17)
Co-authored-by: zhengweijun <[email protected]>
1 parent d344be2 commit 508eaf4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+173
-1644
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
2-
ZAI_API_KEY={your apikey}
2+
ZAI_API_KEY={your apikey}

examples/basic_usage.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ def ofZhipu():
180180

181181
if __name__ == '__main__':
182182
# completion()
183-
# completion_with_websearch()
184-
# multi_modal_chat()
185-
# role_play()
186-
# assistant_conversation()
187-
# video_generation()
188-
ofZai()
189-
ofZhipu()
183+
# completion_with_stream()
184+
# completion_with_websearch()
185+
multi_modal_chat()
186+
# role_play()
187+
# assistant_conversation()
188+
# video_generation()
189+
# ofZai()
190+
# ofZhipu()
190191

examples/function_call_example.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from zai import ZhipuAiClient
2+
import json
3+
client = ZhipuAiClient()
4+
5+
def get_flight_number(date: str, departure: str, destination: str):
6+
flight_number = {
7+
"Beijing": {
8+
"Shanghai": "1234",
9+
"Guangzhou": "5678",
10+
},
11+
"Shanghai": {
12+
"Beijing": "4321",
13+
"Guangzhou": "8765",
14+
}
15+
}
16+
return {"flight_number": flight_number[departure][destination]}
17+
18+
def get_ticket_price(date: str, flight_number: str):
19+
return {"ticket_price": "1000"}
20+
21+
def parse_function_call(model_response, messages):
22+
# Handle function call results. According to the model's returned parameters, call the corresponding function.
23+
# After getting the function result, construct a tool message and call the model again, passing the function result as input.
24+
# The model will return the function result to the user in natural language.
25+
if model_response.choices[0].message.tool_calls:
26+
tool_call = model_response.choices[0].message.tool_calls[0]
27+
args = tool_call.function.arguments
28+
function_result = {}
29+
if tool_call.function.name == "get_flight_number":
30+
function_result = get_flight_number(**json.loads(args))
31+
if tool_call.function.name == "get_ticket_price":
32+
function_result = get_ticket_price(**json.loads(args))
33+
messages.append({
34+
"role": "tool",
35+
"content": f"{json.dumps(function_result)}",
36+
"tool_call_id": tool_call.id
37+
})
38+
response = client.chat.completions.create(
39+
model="glm-4", # Specify the model name to use
40+
messages=messages,
41+
tools=tools,
42+
)
43+
print(response.choices[0].message)
44+
messages.append(response.choices[0].message.model_dump())
45+
46+
messages = []
47+
tools = [
48+
{
49+
"type": "function",
50+
"function": {
51+
"name": "get_flight_number",
52+
"description": "Query the flight number for a given date, departure, and destination",
53+
"parameters": {
54+
"type": "object",
55+
"properties": {
56+
"departure": {
57+
"description": "Departure city",
58+
"type": "string"
59+
},
60+
"destination": {
61+
"description": "Destination city",
62+
"type": "string"
63+
},
64+
"date": {
65+
"description": "Date",
66+
"type": "string",
67+
}
68+
},
69+
"required": ["departure", "destination", "date"]
70+
},
71+
}
72+
},
73+
{
74+
"type": "function",
75+
"function": {
76+
"name": "get_ticket_price",
77+
"description": "Query the ticket price for a specific flight on a specific date",
78+
"parameters": {
79+
"type": "object",
80+
"properties": {
81+
"flight_number": {
82+
"description": "Flight number",
83+
"type": "string"
84+
},
85+
"date": {
86+
"description": "Date",
87+
"type": "string",
88+
}
89+
},
90+
"required": ["flight_number", "date"]
91+
},
92+
}
93+
},
94+
]
95+
96+
# Clear conversation
97+
messages = []
98+
messages.append({"role": "system", "content": "Do not assume or guess the values of function parameters. If the user's description is unclear, ask the user to provide the necessary information."})
99+
messages.append({"role": "user", "content": "Help me check the flights from Beijing to Guangzhou on January 23."})
100+
101+
response = client.chat.completions.create(
102+
model="glm-4", # Specify the model name to use
103+
messages=messages,
104+
tools=tools,
105+
)
106+
print(response.choices[0].message)
107+
messages.append(response.choices[0].message.model_dump())
108+
109+
parse_function_call(response, messages)
110+
111+
messages.append({"role": "user", "content": "What is the price of flight 8321?"})
112+
response = client.chat.completions.create(
113+
model="glm-4", # Specify the model name to use
114+
messages=messages,
115+
tools=tools,
116+
)
117+
print(response.choices[0].message)
118+
messages.append(response.choices[0].message.model_dump())
119+
120+
parse_function_call(response, messages)

examples/glm4_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def stream_web_search_example():
2929
stream=True
3030
)
3131
for chunk in response:
32-
print(chunk.choices[0].delta)
32+
print(chunk.choices[0].delta.content, end="", flush=True)
3333

3434
def sync_example():
3535
print("=== GLM-4 Synchronous Example ===")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zai-sdk"
3-
version = "0.0.1b3"
3+
version = "0.0.1b4"
44
description = "A SDK library for accessing big model apis from Z.ai"
55
authors = ["Z.ai"]
66
readme = "README.md"

src/zai/_client.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
from zai.api_resource.chat import Chat
1717
from zai.api_resource.embeddings import Embeddings
1818
from zai.api_resource.files import Files
19-
from zai.api_resource.fine_tuning import FineTuning
2019
from zai.api_resource.images import Images
21-
from zai.api_resource.knowledge import Knowledge
2220
from zai.api_resource.moderations import Moderations
2321
from zai.api_resource.tools import Tools
2422
from zai.api_resource.videos import Videos
@@ -132,24 +130,12 @@ def embeddings(self) -> Embeddings:
132130

133131
return Embeddings(self)
134132

135-
@cached_property
136-
def fine_tuning(self) -> FineTuning:
137-
from zai.api_resource.fine_tuning import FineTuning
138-
139-
return FineTuning(self)
140-
141133
@cached_property
142134
def batches(self) -> Batches:
143135
from zai.api_resource.batch import Batches
144136

145137
return Batches(self)
146138

147-
@cached_property
148-
def knowledge(self) -> Knowledge:
149-
from zai.api_resource.knowledge import Knowledge
150-
151-
return Knowledge(self)
152-
153139
@cached_property
154140
def tools(self) -> Tools:
155141
from zai.api_resource.tools import Tools

src/zai/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__title__ = 'Z.ai'
2-
__version__ = '0.0.1b2'
2+
__version__ = '0.0.1b4'

src/zai/api_resource/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
)
1212
from .embeddings import Embeddings
1313
from .files import Files, FilesWithRawResponse
14-
from .fine_tuning import FineTuning
1514
from .images import Images
16-
from .knowledge import Knowledge
1715
from .moderations import Moderations
1816
from .tools import Tools
1917
from .videos import (
@@ -30,9 +28,7 @@
3028
'Embeddings',
3129
'Files',
3230
'FilesWithRawResponse',
33-
'FineTuning',
3431
'Batches',
35-
'Knowledge',
3632
'Tools',
3733
'Assistant',
3834
'Audio',

src/zai/api_resource/fine_tuning/__init__.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/zai/api_resource/fine_tuning/fine_tuning.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)