zhipu.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # 智普AI
  2. import time
  3. import jwt
  4. api_key = '0cb476be333858ae0ff0ac5a87b740ff.rWBkpfSOH95ohVYG'
  5. def generate_token(apikey: str, exp_seconds: int):
  6. try:
  7. id, secret = apikey.split(".")
  8. except Exception as e:
  9. raise Exception("invalid apikey", e)
  10. payload = {
  11. "api_key": id,
  12. "exp": int(round(time.time() * 1000)) + exp_seconds * 1000,
  13. "timestamp": int(round(time.time() * 1000)),
  14. }
  15. return jwt.encode(
  16. payload,
  17. secret,
  18. algorithm="HS256",
  19. headers={"alg": "HS256", "sign_type": "SIGN"},
  20. )
  21. # 有效期5年
  22. # print(generate_token(api_key, 5 * 365 * 24 * 3600))
  23. token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsInNpZ25fdHlwZSI6IlNJR04ifQ.eyJhcGlfa2V5IjoiMGNiNDc2YmUzMzM4NThhZTBmZjBhYzVhODdiNzQwZmYiLCJleHAiOjE4NjQzNDg1NzE5NTUsInRpbWVzdGFtcCI6MTcwNjY2ODU3MTk1NX0.nBPJiXfx7qrzZOaAL22Ch7oRl6hX80VowviE3VfiUIA'
  24. # Content-Type : application/json
  25. # Authorization
  26. import requests
  27. url = 'https://open.bigmodel.cn/api/paas/v4/chat/completions'
  28. headers = {
  29. 'Content-Type': 'application/json',
  30. 'Authorization': 'Bearer ' + token,
  31. }
  32. data = {
  33. "model": "GLM-3-Turbo",
  34. "messages": [
  35. {"role": "user", "content": "你好"}
  36. ]
  37. }
  38. response = requests.request("POST", url, headers=headers, json=data)
  39. print(response.text)