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

glm 的 2api 思路

  •  
  •   flyingelement 3 天前 691 次点击

    处理了新增的签名问题。截止 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() 
    1 条回复
    retain
        1
    retain  
       3 天前   1
    好好的开个会员不好吗? 也没多贵
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     864 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 21:43 PVG 05:43 LAX 14:43 JFK 17:43
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86