File size: 876 Bytes
003281f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import requests
import json

headers = {
    "Content-Type": "application/json"
}

data = {'model': 'gpt-3.5-turbo', 'messages': [{'role': 'user', 'content': '你好'}],'stream':True}

# 这里以免费通道作为示例,付费通道修改data中的model参数即可使用其它模型
# 支持的模型请到网站查看
response = requests.post('https://api.zhtec.xyz/xht/chatWith16k.php', headers=headers, data=json.dumps(data), stream=True)

# 支持流式输出!
for chunk in response.iter_lines():
    if chunk:
        decoded_chunk = chunk.decode('utf-8')
        if decoded_chunk.startswith('data:'):
            # Remove the 'data: ' prefix and parse the JSON object
            try:
                parsed_chunk = json.loads(decoded_chunk[5:])
                print(parsed_chunk['choices'][0]['delta']['content'], end='')
            except:
                pass