diff --git a/.gitignore b/.gitignore index 2b39825..818207a 100644 --- a/.gitignore +++ b/.gitignore @@ -173,7 +173,7 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ # Abstra # Abstra is an AI-powered process automation framework. @@ -186,7 +186,7 @@ cython_debug/ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore # and can be added to the global gitignore or merged into this file. However, if you prefer, # you could uncomment the following to ignore the entire vscode folder -# .vscode/ +.vscode/ # Ruff stuff: .ruff_cache/ diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index ff5300e..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "python.languageServer": "None" -} \ No newline at end of file diff --git a/README.md b/README.md index 13b803e..df64fcd 100644 --- a/README.md +++ b/README.md @@ -116,12 +116,18 @@ export ZAI_BASE_URL="https://api.z.ai/api/paas/v4/" # Optional #### Code Configuration ```python -from zai import ZaiClient +from zai import ZaiClient, ZhipuAiClient client = ZaiClient( api_key="your-api-key", base_url="https://api.z.ai/api/paas/v4/" # Optional ) + +# if you want to use ZhipuAiClient +zhipu_client = ZhipuAiClient( + api_key="your-api-key", + base_url="https://open.bigmodel.cn/api/paas/v4/" # Optional +) ``` ### Advanced Configuration diff --git a/README_CN.md b/README_CN.md index 7ff2647..e0f3e5f 100644 --- a/README_CN.md +++ b/README_CN.md @@ -97,12 +97,20 @@ export ZAI_BASE_URL="https://api.z.ai/api/paas/v4/" # 可选 **代码配置:** ```python -from zai import ZaiClient +from zai import ZaiClient, ZhipuAiClient client = ZaiClient( api_key="your_api_key_here", # 填写您的 APIKey -) + base_url="https://api.z.ai/api/paas/v4/" # 可选 +) + +# if you want to use ZhipuAiClient +zhipu_client = ZhipuAiClient( + api_key="your_api_key_here", # 填写您的 APIKey + base_url="https://open.bigmodel.cn/api/paas/v4/" # 可选 +) ``` + **高级配置:** SDK提供了灵活的客户端配置选项: diff --git a/examples/basic_usage.py b/examples/basic_usage.py index ca0072a..2e3b4bd 100644 --- a/examples/basic_usage.py +++ b/examples/basic_usage.py @@ -1,4 +1,4 @@ -from zai import ZaiClient +from zai import ZaiClient, ZhipuAiClient def completion(): # Initialize client @@ -158,9 +158,20 @@ def audio_transcription(): ) print(response.text) +def ofZai(): + client = ZaiClient() + print(client.base_url) + response = client.chat.completions.create( + model='glm-4', + messages=[{'role': 'user', 'content': 'Hello, Z.ai!'}], + temperature=0.7, + ) + print(response.choices[0].message.content) + def ofZhipu(): - client = ZaiClient() - response = client.zhipu.chat.completions.create( + client = ZhipuAiClient() + print(client.base_url) + response = client.chat.completions.create( model='glm-4', messages=[{'role': 'user', 'content': 'Hello, Z.ai!'}], temperature=0.7, @@ -174,5 +185,6 @@ def ofZhipu(): # role_play() # assistant_conversation() # video_generation() + ofZai() ofZhipu() - + diff --git a/src/zai/__init__.py b/src/zai/__init__.py index e09fefe..f362804 100644 --- a/src/zai/__init__.py +++ b/src/zai/__init__.py @@ -1,4 +1,4 @@ -from ._client import ZaiClient +from ._client import ZaiClient, ZhipuAiClient from ._version import __version__ -__all__ = ['ZaiClient', '__version__'] +__all__ = ['ZaiClient', 'ZhipuAiClient', '__version__'] diff --git a/src/zai/_client.py b/src/zai/_client.py index ce2a591..5f911bb 100644 --- a/src/zai/_client.py +++ b/src/zai/_client.py @@ -33,8 +33,7 @@ _jwt_token, ) - -class ZaiClient(HttpClient): +class BaseClient(HttpClient): """ Main client for interacting with the ZAI API @@ -47,7 +46,8 @@ class ZaiClient(HttpClient): chat: Chat api_key: str - _disable_token_cache: bool = True + base_url: str + disable_token_cache: bool = True source_channel: str def __init__( @@ -85,14 +85,15 @@ def __init__( raise ZaiError('api_key not provided, please provide it through parameters or environment variables') self.api_key = api_key self.source_channel = source_channel - self._disable_token_cache = disable_token_cache + self.disable_token_cache = disable_token_cache if base_url is None: base_url = os.environ.get('ZAI_BASE_URL') if base_url is None: - base_url = 'https://api.z.ai/api/paas/v4' - from ._version import __version__ + base_url = self.default_base_url + self.base_url = base_url + from ._version import __version__ super().__init__( version=__version__, base_url=base_url, @@ -103,10 +104,9 @@ def __init__( _strict_response_validation=_strict_response_validation, ) - @cached_property - def zhipu(self): - self.base_url = 'https://open.bigmodel.cn/api/paas/v4' - return self + @property + def default_base_url(self): + raise NotImplementedError("Subclasses must define default_base_url") @cached_property def chat(self) -> Chat: @@ -197,7 +197,7 @@ def moderations(self) -> Moderations: def auth_headers(self) -> dict[str, str]: api_key = self.api_key source_channel = self.source_channel or 'python-sdk' - if self._disable_token_cache: + if self.disable_token_cache: return { 'Authorization': f'Bearer {api_key}', 'x-source-channel': source_channel, @@ -217,3 +217,13 @@ def __del__(self) -> None: return self.close() + +class ZaiClient(BaseClient): + @property + def default_base_url(self): + return 'https://api.z.ai/api/paas/v4' + +class ZhipuAiClient(BaseClient): + @property + def default_base_url(self): + return 'https://open.bigmodel.cn/api/paas/v4' \ No newline at end of file