From ee0a2b3e4cf3a3acf2af90d141fb9d534d29b384 Mon Sep 17 00:00:00 2001 From: wellenzheng <40078093+wellenzheng@users.noreply.github.com> Date: Wed, 23 Jul 2025 11:53:32 +0800 Subject: [PATCH] feat: switchtozhipu (#12) Co-authored-by: zhengweijun --- examples/basic_usage.py | 12 +++++++++++- src/zai/_client.py | 9 +++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/examples/basic_usage.py b/examples/basic_usage.py index 00d6635..ca0072a 100644 --- a/examples/basic_usage.py +++ b/examples/basic_usage.py @@ -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() + diff --git a/src/zai/_client.py b/src/zai/_client.py index 53835bd..ce2a591 100644 --- a/src/zai/_client.py +++ b/src/zai/_client.py @@ -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