Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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/
Expand Down
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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提供了灵活的客户端配置选项:
Expand Down
20 changes: 16 additions & 4 deletions examples/basic_usage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from zai import ZaiClient
from zai import ZaiClient, ZhipuAiClient

def completion():
# Initialize client
Expand Down Expand Up @@ -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,
Expand All @@ -174,5 +185,6 @@ def ofZhipu():
# role_play()
# assistant_conversation()
# video_generation()
ofZai()
ofZhipu()

4 changes: 2 additions & 2 deletions src/zai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._client import ZaiClient
from ._client import ZaiClient, ZhipuAiClient
from ._version import __version__

__all__ = ['ZaiClient', '__version__']
__all__ = ['ZaiClient', 'ZhipuAiClient', '__version__']
32 changes: 21 additions & 11 deletions src/zai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
_jwt_token,
)


class ZaiClient(HttpClient):
class BaseClient(HttpClient):
"""
Main client for interacting with the ZAI API

Expand All @@ -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__(
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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'