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
|
# 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
|
# 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.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
.idea/
|
||||||
|
|
||||||
# Abstra
|
# Abstra
|
||||||
# Abstra is an AI-powered process automation framework.
|
# 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
|
# 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,
|
# 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
|
# you could uncomment the following to ignore the entire vscode folder
|
||||||
# .vscode/
|
.vscode/
|
||||||
|
|
||||||
# Ruff stuff:
|
# Ruff stuff:
|
||||||
.ruff_cache/
|
.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
|
#### Code Configuration
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from zai import ZaiClient
|
from zai import ZaiClient, ZhipuAiClient
|
||||||
|
|
||||||
client = ZaiClient(
|
client = ZaiClient(
|
||||||
api_key="your-api-key",
|
api_key="your-api-key",
|
||||||
base_url="https://api.z.ai/api/paas/v4/" # Optional
|
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
|
### 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
|
```python
|
||||||
from zai import ZaiClient
|
from zai import ZaiClient, ZhipuAiClient
|
||||||
|
|
||||||
client = ZaiClient(
|
client = ZaiClient(
|
||||||
api_key="your_api_key_here", # 填写您的 APIKey
|
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提供了灵活的客户端配置选项:
|
SDK提供了灵活的客户端配置选项:
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from zai import ZaiClient
|
from zai import ZaiClient, ZhipuAiClient
|
||||||
|
|
||||||
def completion():
|
def completion():
|
||||||
# Initialize client
|
# Initialize client
|
||||||
|
|
@ -158,9 +158,20 @@ def audio_transcription():
|
||||||
)
|
)
|
||||||
print(response.text)
|
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():
|
def ofZhipu():
|
||||||
client = ZaiClient()
|
client = ZhipuAiClient()
|
||||||
response = client.zhipu.chat.completions.create(
|
print(client.base_url)
|
||||||
|
response = client.chat.completions.create(
|
||||||
model='glm-4',
|
model='glm-4',
|
||||||
messages=[{'role': 'user', 'content': 'Hello, Z.ai!'}],
|
messages=[{'role': 'user', 'content': 'Hello, Z.ai!'}],
|
||||||
temperature=0.7,
|
temperature=0.7,
|
||||||
|
|
@ -174,5 +185,6 @@ if __name__ == '__main__':
|
||||||
# role_play()
|
# role_play()
|
||||||
# assistant_conversation()
|
# assistant_conversation()
|
||||||
# video_generation()
|
# video_generation()
|
||||||
|
ofZai()
|
||||||
ofZhipu()
|
ofZhipu()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from ._client import ZaiClient
|
from ._client import ZaiClient, ZhipuAiClient
|
||||||
from ._version import __version__
|
from ._version import __version__
|
||||||
|
|
||||||
__all__ = ['ZaiClient', '__version__']
|
__all__ = ['ZaiClient', 'ZhipuAiClient', '__version__']
|
||||||
|
|
|
||||||
|
|
@ -33,8 +33,7 @@ from .core import (
|
||||||
_jwt_token,
|
_jwt_token,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class BaseClient(HttpClient):
|
||||||
class ZaiClient(HttpClient):
|
|
||||||
"""
|
"""
|
||||||
Main client for interacting with the ZAI API
|
Main client for interacting with the ZAI API
|
||||||
|
|
||||||
|
|
@ -47,7 +46,8 @@ class ZaiClient(HttpClient):
|
||||||
|
|
||||||
chat: Chat
|
chat: Chat
|
||||||
api_key: str
|
api_key: str
|
||||||
_disable_token_cache: bool = True
|
base_url: str
|
||||||
|
disable_token_cache: bool = True
|
||||||
source_channel: str
|
source_channel: str
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
@ -85,14 +85,15 @@ class ZaiClient(HttpClient):
|
||||||
raise ZaiError('api_key not provided, please provide it through parameters or environment variables')
|
raise ZaiError('api_key not provided, please provide it through parameters or environment variables')
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
self.source_channel = source_channel
|
self.source_channel = source_channel
|
||||||
self._disable_token_cache = disable_token_cache
|
self.disable_token_cache = disable_token_cache
|
||||||
|
|
||||||
if base_url is None:
|
if base_url is None:
|
||||||
base_url = os.environ.get('ZAI_BASE_URL')
|
base_url = os.environ.get('ZAI_BASE_URL')
|
||||||
if base_url is None:
|
if base_url is None:
|
||||||
base_url = 'https://api.z.ai/api/paas/v4'
|
base_url = self.default_base_url
|
||||||
from ._version import __version__
|
self.base_url = base_url
|
||||||
|
|
||||||
|
from ._version import __version__
|
||||||
super().__init__(
|
super().__init__(
|
||||||
version=__version__,
|
version=__version__,
|
||||||
base_url=base_url,
|
base_url=base_url,
|
||||||
|
|
@ -103,10 +104,9 @@ class ZaiClient(HttpClient):
|
||||||
_strict_response_validation=_strict_response_validation,
|
_strict_response_validation=_strict_response_validation,
|
||||||
)
|
)
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def zhipu(self):
|
def default_base_url(self):
|
||||||
self.base_url = 'https://open.bigmodel.cn/api/paas/v4'
|
raise NotImplementedError("Subclasses must define default_base_url")
|
||||||
return self
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def chat(self) -> Chat:
|
def chat(self) -> Chat:
|
||||||
|
|
@ -197,7 +197,7 @@ class ZaiClient(HttpClient):
|
||||||
def auth_headers(self) -> dict[str, str]:
|
def auth_headers(self) -> dict[str, str]:
|
||||||
api_key = self.api_key
|
api_key = self.api_key
|
||||||
source_channel = self.source_channel or 'python-sdk'
|
source_channel = self.source_channel or 'python-sdk'
|
||||||
if self._disable_token_cache:
|
if self.disable_token_cache:
|
||||||
return {
|
return {
|
||||||
'Authorization': f'Bearer {api_key}',
|
'Authorization': f'Bearer {api_key}',
|
||||||
'x-source-channel': source_channel,
|
'x-source-channel': source_channel,
|
||||||
|
|
@ -217,3 +217,13 @@ class ZaiClient(HttpClient):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.close()
|
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