Skip to main content

A SDK library for accessing big model apis from Z.ai

Project description

Z.ai Open Platform Python SDK

PyPI version License: MIT Python

中文文档 | English

Z.ai Open Platform The official Python SDK for Z.ai's large model open interface, making it easier for developers to call Z.ai's open APIs.

✨ Core Features

🤖 Chat Completions

  • Standard Chat: Create chat completions with various models including glm-4, charglm-3
  • Streaming Support: Real-time streaming responses for interactive applications
  • Tool Calling: Function calling capabilities for enhanced AI interactions
  • Character Role-Playing: Support for character-based conversations with charglm-3 model
  • Multimodal Chat: Image understanding capabilities with vision models

🧠 Embeddings

  • Text Embeddings: Generate high-quality vector embeddings for text
  • Configurable Dimensions: Customizable embedding dimensions
  • Batch Processing: Support for multiple inputs in a single request

🎥 Video Generation

  • Text-to-Video: Generate videos from text prompts
  • Image-to-Video: Create videos from image inputs
  • Customizable Parameters: Control quality, duration, FPS, and size
  • Audio Support: Optional audio generation for videos

🎵 Audio Processing

  • Speech Transcription: Convert audio files to text
  • Multiple Formats: Support for various audio file formats

🤝 Assistant API

  • Conversation Management: Structured conversation handling
  • Streaming Conversations: Real-time assistant interactions
  • Metadata Support: Rich conversation context and user information

🔧 Advanced Tools

  • Web Search: Integrated web search capabilities
  • File Management: Upload, download, and manage files
  • Batch Operations: Efficient batch processing for multiple requests
  • Content Moderation: Built-in content safety and moderation
  • Image Generation: AI-powered image creation

📦 Installation

Requirements

  • Python: 3.8+
  • Package Manager: pip

Install via pip

pip install zai-sdk

📋 Technical Specifications

Python Support

  • Python Versions: 3.8, 3.9, 3.10, 3.11, 3.12
  • Async Support: Full async/await compatibility
  • Cross-platform: Windows, macOS, Linux support

Core Dependencies

Package Version Purpose
httpx >=0.23.0 HTTP client for API requests
pydantic >=1.9.0,<3.0.0 Data validation and serialization
typing-extensions >=4.0.0 Enhanced type hints support
cachetools >=4.2.2 Caching utilities
pyjwt >=2.8.0 JSON Web Token (JWT) handling

🚀 Quick Start

Create API Key

Get API Key

API BASE URL

  • Mainland China regions: https://open.bigmodel.cn/api/paas/v4/
  • Overseas regions: https://api.z.ai/api/paas/v4/

Usage Steps

  1. Create client with API key
  2. Call the corresponding API methods

For complete examples, please refer to the open platform API Reference and User Guide, and remember to replace with your own API key.

Basic Usage

from zai import ZaiClient, ZhipuAiClient

# For Overseas users, create the ZaiClient
client = ZaiClient(api_key="your-api-key")

# For Chinese users, create the ZhipuAiClient
client = ZhipuAiClient(api_key="your-api-key")

# Create chat completion
response = client.chat.completions.create(
    model="glm-4",
    messages=[
        {"role": "user", "content": "Hello, Z.ai!"}
    ]
)
print(response.choices[0].message.content)

Client Configuration

The SDK supports multiple ways to configure API keys:

Environment Variables

export ZAI_API_KEY="your-api-key"
export ZAI_BASE_URL="https://api.z.ai/api/paas/v4/"  # Optional

Code Configuration

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 Zhipu's domain service
zhipu_client = ZhipuAiClient(
    api_key="your-api-key",
    base_url="https://open.bigmodel.cn/api/paas/v4/"  # Optional
)

Advanced Configuration

Customize client behavior with additional parameters:

from zai import ZaiClient
import httpx

client = ZaiClient(
    api_key="your-api-key",
    timeout=httpx.Timeout(timeout=300.0, connect=8.0),  # Request timeout
    max_retries=3,  # Retry attempts
    base_url="https://api.z.ai/api/paas/v4/"  # Custom API endpoint
)

📖 Usage Examples

Streaming Chat

from zai import ZaiClient

# Initialize client
client = ZaiClient(api_key="your-api-key")

# Create chat completion
response = client.chat.completions.create(
    model='glm-4',
    messages=[
        {'role': 'system', 'content': 'You are a helpful assistant.'},
        {'role': 'user', 'content': 'Tell me a story about AI.'},
    ],
    stream=True,
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end='')

Chat With Tool Call

from zai import ZaiClient

# Initialize client
client = ZaiClient(api_key="your-api-key")

# Create chat completion
response = client.chat.completions.create(
    model='glm-4',
    messages=[
        {'role': 'system', 'content': 'You are a helpful assistant.'},
        {'role': 'user', 'content': 'What is artificial intelligence?'},
    ],
    tools=[
        {
            'type': 'web_search',
            'web_search': {
                'search_query': 'What is artificial intelligence?',
                'search_result': True,
            },
        }
    ],
    temperature=0.5,
    max_tokens=2000,
)

print(response)

Multimodal Chat

from zai import ZaiClient
import base64

def encode_image(image_path):
    """Encode image to base64 format"""
    with open(image_path, 'rb') as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

client = ZaiClient(api_key="your-api-key")
base64_image = encode_image('examples/test_multi_modal.jpeg')

response = client.chat.completions.create(
    model='glm-4v',
    messages=[
        {
            'role': 'user',
            'content': [
                {'type': 'text', 'text': "What's in this image?"},
                {'type': 'image_url', 'image_url': {'url': f'data:image/jpeg;base64,{base64_image}'}},
            ],
        }
    ],
    temperature=0.5,
    max_tokens=2000,
)
print(response)

Character Role-Playing

from zai import ZaiClient

# Initialize client
client = ZaiClient(api_key="your-api-key")

# Create chat completion
response = client.chat.completions.create(
    model='charglm-3',
    messages=[{'role': 'user', 'content': 'Hello, how are you doing lately?'}],
    meta={
        'user_info': 'I am a film director who specializes in music-themed movies.',
        'bot_info': 'You are a popular domestic female singer and actress with outstanding musical talent.',
        'bot_name': 'Alice',
        'user_name': 'Director',
    },
)
print(response)

Assistant Conversation

from zai import ZaiClient

# Initialize client
client = ZaiClient(api_key="your-api-key")

# Create assistant conversation
response = client.assistant.conversation(
    # You can use 65940acff94777010aa6b796 for testing
    # or you can create your own assistant_id in Z.ai console
    assistant_id='your own assistant_id',
    model='glm-4-assistant',
    messages=[
        {
            'role': 'user',
            'content': [
                {
                    'type': 'text',
                    'text': 'Help me search for the latest Z.ai product information',
                }
            ],
        }
    ],
    stream=True,
    attachments=None,
    metadata=None,
    request_id='request_1790291013237211136',
    user_id='12345678',
)

for chunk in response:
    if chunk.choices[0].delta.type == 'content':
        print(chunk.choices[0].delta.content, end='')

Video Generation

from zai import ZaiClient
client = ZaiClient(api_key="your-api-key")

# Generate video
response = client.videos.generations(
    model="cogvideox-2",
    prompt="A cat is playing with a ball.",
    quality="quality",  # Output mode, "quality" for quality priority, "speed" for speed priority
    with_audio=True, # Whether to include audio
    size="1920x1080",  # Video resolution, supports up to 4K (e.g., "3840x2160")
    fps=30,  # Frame rate, can be 30 or 60
    max_wait_time=300,  # Maximum wait time (seconds)
)
print(response)

# Get video result
result = client.videos.retrieve_videos_result(id=response.id)
print(result)

🚨 Error Handling

The SDK provides comprehensive error handling:

from zai import ZaiClient
import zai

client = ZaiClient(api_key="your-api-key")

try:
    response = client.chat.completions.create(
        model="glm-4",
        messages=[
            {"role": "user", "content": "Hello, Z.ai!"}
        ]
    )
    print(response.choices[0].message.content)
    
except zai.APIStatusError as err:
    print(f"API Status Error: {err}")
except zai.APITimeoutError as err:
    print(f"Request Timeout: {err}")
except Exception as err:
    print(f"Unexpected Error: {err}")

Error Codes

Status Code Error Type Description
400 APIRequestFailedError Invalid request parameters
401 APIAuthenticationError Authentication failed
429 APIReachLimitError Rate limit exceeded
500 APIInternalError Internal server error
503 APIServerFlowExceedError Server overloaded
N/A APIStatusError General API error

📈 Version Updates

For detailed version history and update information, please see Release-Note.md.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📞 Support

For questions and technical support, please visit Z.ai Open Platform or check our documentation.

Contact Us

For feedback and support, please contact us at: user_feedback@z.ai

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zai_sdk-0.0.4.tar.gz (71.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zai_sdk-0.0.4-py3-none-any.whl (114.7 kB view details)

Uploaded Python 3

File details

Details for the file zai_sdk-0.0.4.tar.gz.

File metadata

  • Download URL: zai_sdk-0.0.4.tar.gz
  • Upload date:
  • Size: 71.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zai_sdk-0.0.4.tar.gz
Algorithm Hash digest
SHA256 181e20c86544cfd5ee50715fc7b366cd2f4adfc6ac5e9c6d1e34b93d878129bf
MD5 3d03182aaa29344b67c6572799e8ff44
BLAKE2b-256 caff531dfac8afe65bb2ffce7fd4e96d323b2744f72dbf9802a6e8c7a02a059b

See more details on using hashes here.

File details

Details for the file zai_sdk-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: zai_sdk-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 114.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zai_sdk-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cdc15b0a0be8ba5a9edc526fc41d48a6f8e64872f7f3c4cda10a81c11d470c6d
MD5 483649becdbf724d5ac187419057da15
BLAKE2b-256 be360e5a331607f40097f8cbfbcf113436af820e0adcc5aa2fd0cb78daf215ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page