V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
flyingelement
V2EX  ›  程序员

glm 的 2api 思路

  •  
  •   flyingelement · 18 天前 · 1006 次点击

    处理了新增的签名问题。截止 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()
    
    2 条回复
    retain
        1
    retain  
       18 天前   ❤️ 1
    好好的开个会员不好吗? 也没多贵
    justxwy
        2
    justxwy  
       15 天前
    谢谢分享
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2426 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 10:21 · PVG 18:21 · LAX 03:21 · JFK 06:21
    ♥ Do have faith in what you're doing.