feat: switchtozhipu (#12)

Co-authored-by: zhengweijun <weijun.zheng@aminer.cn>
This commit is contained in:
wellenzheng 2025-07-23 11:53:32 +08:00 committed by GitHub
parent 42a921e770
commit ee0a2b3e4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 5 deletions

View file

@ -158,11 +158,21 @@ def audio_transcription():
)
print(response.text)
def ofZhipu():
client = ZaiClient()
response = client.zhipu.chat.completions.create(
model='glm-4',
messages=[{'role': 'user', 'content': 'Hello, Z.ai!'}],
temperature=0.7,
)
print(response.choices[0].message.content)
if __name__ == '__main__':
completion()
# completion()
# completion_with_websearch()
# multi_modal_chat()
# role_play()
# assistant_conversation()
# video_generation()
ofZhipu()

View file

@ -62,7 +62,6 @@ class ZaiClient(HttpClient):
disable_token_cache: bool = True,
_strict_response_validation: bool = False,
source_channel: str | None = None,
switch_to_zhipu: bool = False,
) -> None:
"""
Initialize the ZAI client
@ -79,7 +78,6 @@ class ZaiClient(HttpClient):
disable_token_cache (bool): Whether to disable JWT token caching
_strict_response_validation (bool): Whether to enable strict response validation
source_channel (str | None): Source channel identifier
switch_to_zhipu (bool): Whether to switch to Zhipu base_url
"""
if api_key is None:
api_key = os.environ.get('ZAI_API_KEY')
@ -93,8 +91,6 @@ class ZaiClient(HttpClient):
base_url = os.environ.get('ZAI_BASE_URL')
if base_url is None:
base_url = 'https://api.z.ai/api/paas/v4'
if switch_to_zhipu:
base_url = 'https://open.bigmodel.cn/api/paas/v4'
from ._version import __version__
super().__init__(
@ -107,6 +103,11 @@ class ZaiClient(HttpClient):
_strict_response_validation=_strict_response_validation,
)
@cached_property
def zhipu(self):
self.base_url = 'https://open.bigmodel.cn/api/paas/v4'
return self
@cached_property
def chat(self) -> Chat:
from zai.api_resource.chat import Chat