feat: add zhipuclient (#13)
Co-authored-by: zhengweijun <weijun.zheng@aminer.cn>
This commit is contained in:
parent
ee0a2b3e4c
commit
b4ff07c158
7 changed files with 58 additions and 25 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -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/
|
||||
|
|
|
|||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"python.languageServer": "None"
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
12
README_CN.md
12
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提供了灵活的客户端配置选项:
|
||||
|
|
|
|||
|
|
@ -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 @@ if __name__ == '__main__':
|
|||
# role_play()
|
||||
# assistant_conversation()
|
||||
# video_generation()
|
||||
ofZai()
|
||||
ofZhipu()
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from ._client import ZaiClient
|
||||
from ._client import ZaiClient, ZhipuAiClient
|
||||
from ._version import __version__
|
||||
|
||||
__all__ = ['ZaiClient', '__version__']
|
||||
__all__ = ['ZaiClient', 'ZhipuAiClient', '__version__']
|
||||
|
|
|
|||
|
|
@ -33,8 +33,7 @@ from .core import (
|
|||
_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 @@ class ZaiClient(HttpClient):
|
|||
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 @@ 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
|
||||
@property
|
||||
def default_base_url(self):
|
||||
raise NotImplementedError("Subclasses must define default_base_url")
|
||||
|
||||
@cached_property
|
||||
def chat(self) -> Chat:
|
||||
|
|
@ -197,7 +197,7 @@ class ZaiClient(HttpClient):
|
|||
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 @@ class ZaiClient(HttpClient):
|
|||
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'
|
||||
Loading…
Reference in a new issue