深色模式
Python 代码调用
非流式调用
python
import requests
if __name__ == '__main__':
params = {
'model': 'taichu_llm',
'messages': [{"role": "user", "content": "你好"}],
'stream': False
}
api = 'https://ai-maas.wair.ac.cn/maas/v1/chat/completions'
headers = {'Authorization': 'Bearer $API_KEY'}
response = requests.post(api, json=params, headers=headers)
if response.status_code == 200:
print(response.json())
else:
body = response.content.decode('utf-8')
print(f'request failed,status_code:{response.status_code},body:{body}')
流式调用
python
if __name__ == '__main__':
params = {
'model': 'taichu_llm',
'messages': [{"role": "user", "content": "你好"}],
'stream': True
}
api = 'https://ai-maas.wair.ac.cn/maas/v1/chat/completions'
headers = {'Authorization': 'Bearer ${API_KEY}'}
response = requests.post(api, json=params, headers=headers, stream=True)
if response.status_code == 200:
response.encoding = 'utf-8'
for line in response.iter_lines(decode_unicode=True):
print(line)
else:
body = response.content.decode('utf-8')
print(f'request failed,status_code:{response.status_code},body:{body}')