Blog
Tutorials2 min di lettura

GLM-4 API Guide: Zhipu AI with Web Search & Tool Use

Introduction

GLM-4 by Zhipu AI offers built-in web search and tool use capabilities, along with strong bilingual Chinese-English proficiency. Through AI API Hub, access GLM-4 via the standard OpenAI-compatible endpoint.

Setup

import openai
client = openai.OpenAI(api_key="your-api-key", base_url="https://api.apiyihe.org/v1")
response = client.chat.completions.create(
    model="glm-4-plus",
    messages=[{"role": "user", "content": "Explain the latest developments in AI chip manufacturing"}],
    max_tokens=1000
)
print(response.choices[0].message.content)

Web Search Integration

GLM-4 can search the web for real-time information:

response = client.chat.completions.create(
    model="glm-4-plus",
    messages=[{
        "role": "user",
        "content": "Search for: latest GPT model release date 2026"
    }],
    tools=[{
        "type": "function",
        "function": {
            "name": "web_search",
            "description": "Search the web for current information",
            "parameters": {"type": "object", "properties": {"query": {"type": "string"}}}
        }
    }]
)

Tool Use / Function Calling

import json

def query_database(sql):
    return {"rows": [{"id": 1, "name": "Test"}]}

tools = [{
    "type": "function",
    "function": {
        "name": "query_database",
        "description": "Execute SQL query",
        "parameters": {"type": "object", "properties": {"sql": {"type": "string"}}}
    }
}]

response = client.chat.completions.create(
    model="glm-4-plus",
    messages=[{"role": "user", "content": "Show me all users"}],
    tools=tools
)

Bilingual Processing

# English → Chinese
en = client.chat.completions.create(
    model="glm-4-plus",
    messages=[{"role": "user", "content": "Explain quantum physics in Chinese"}]
)

# Chinese → English
zh = client.chat.completions.create(
    model="glm-4-plus",
    messages=[{"role": "user", "content": "用英文解释量子物理"}]
)

FAQ

Q: Does GLM-4 support streaming? A: Yes. Q: What's the pricing? A: GLM-4 Plus: $0.70/1M input, $2.80/1M output. Q: Is GLM-4 open source? A: GLM-4 is available via API. Check Zhipu AI for open-source versions.

Get Your API Key Now

Start building with 50+ AI models through one unified gateway.

Crea Account
Ottieni Chiave API