Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

glm 的 2api 思路

7 October 2025 at 01:41
flyingelement:

处理了新增的签名问题。截止 20251007 有效

加上轮询,上下文和转 OpenAI 逻辑就是一个成熟的 2api 项目

对于带历史对话的,参数 t 是最近一次 user content

防止有人不知道还是提一下,token 来自网页 cookie 的 token 值,目前看来是长期有效

import time, hmac, hashlib, requests, uuid, json, base64

token = ""

def decode_jwt_payload(token):
    parts = token.split('.')
    payload = parts[1]

    padding = 4 - len(payload) % 4
    if padding != 4:
        payload += '=' * padding

    decoded = base64.urlsafe_b64decode(payload)
    return json.loads(decoded)

def zs(e, t, timestamp):
    r = str(timestamp)
    i = f"{e}|{t}|{r}"
    n = timestamp // (5 * 60 * 1000)
    key = "junjie".encode('utf-8')
    o = hmac.new(key, str(n).encode('utf-8'), hashlib.sha256).hexdigest()
    signature = hmac.new(o.encode('utf-8'), i.encode('utf-8'), hashlib.sha256).hexdigest()

    return {
        "signature": signature,
        "timestamp": timestamp
    }

def make_request():
    payload = decode_jwt_payload(token)
    user_id = payload['id']
    chat_id = str(uuid.uuid4())
    timestamp = int(time.time() * 1000)
    request_id = str(uuid.uuid4())

    t = input("Hello, how can I help you ?\n - ")

    e = f"requestId,{request_id},timestamp,{timestamp},user_id,{user_id}"

    result = zs(e, t, timestamp)
    signature = result["signature"]

    url = "https://chat.z.ai/api/chat/completions"
    params = {
        "timestamp": timestamp,
        "requestId": request_id,
        "user_id": user_id,
        "token": token,
        "current_url": f"https://chat.z.ai/c/{chat_id}",
        "pathname": f"/c/{chat_id}",
        "signature_timestamp": timestamp
    }

    headers = {
        "Authorization": f"Bearer {token}",
        "X-FE-Version": "prod-fe-1.0.95",
        "X-Signature": signature
    }

    payload = {
        "stream": True,
        "model": "GLM-4-6-API-V1",
        "messages": [
            {"role": "user", "content": t}
        ],
        "params": {},
        "features": {
            "image_generation": False,
            "web_search": False,
            "auto_web_search": False,
            "preview_mode": True,
        },
        "enable_thinking": True,
        "chat_id": chat_id,
        "id": str(uuid.uuid4())
    }

    response = requests.post(url, params=params, headers=headers, json=payload, stream=True)
    response.raise_for_status()

    for chunk in response.iter_content(chunk_size=8192):
        if chunk:
            print(chunk.decode('utf-8'), end='')

if __name__ == "__main__":
    make_request()
❌
❌