1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- # 智普AI
- import time
- import jwt
- api_key = '0cb476be333858ae0ff0ac5a87b740ff.rWBkpfSOH95ohVYG'
- def generate_token(apikey: str, exp_seconds: int):
- try:
- id, secret = apikey.split(".")
- except Exception as e:
- raise Exception("invalid apikey", e)
- payload = {
- "api_key": id,
- "exp": int(round(time.time() * 1000)) + exp_seconds * 1000,
- "timestamp": int(round(time.time() * 1000)),
- }
- return jwt.encode(
- payload,
- secret,
- algorithm="HS256",
- headers={"alg": "HS256", "sign_type": "SIGN"},
- )
- # 有效期5年
- # print(generate_token(api_key, 5 * 365 * 24 * 3600))
- token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsInNpZ25fdHlwZSI6IlNJR04ifQ.eyJhcGlfa2V5IjoiMGNiNDc2YmUzMzM4NThhZTBmZjBhYzVhODdiNzQwZmYiLCJleHAiOjE4NjQzNDg1NzE5NTUsInRpbWVzdGFtcCI6MTcwNjY2ODU3MTk1NX0.nBPJiXfx7qrzZOaAL22Ch7oRl6hX80VowviE3VfiUIA'
- # Content-Type : application/json
- # Authorization
- import requests
- url = 'https://open.bigmodel.cn/api/paas/v4/chat/completions'
- headers = {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer ' + token,
- }
- data = {
- "model": "GLM-3-Turbo",
- "messages": [
- {"role": "user", "content": "你好"}
- ]
- }
- response = requests.request("POST", url, headers=headers, json=data)
- print(response.text)
|