gpt服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

42 lines
1.1 KiB

  1. #! /usr/bin/python
  2. import requests
  3. import json
  4. def do_request():
  5. url = "https://api.baichuan-ai.com/v1/chat/completions"
  6. api_key = "9f7d5847897f8090195cdc2c3249d0a7"
  7. data = {
  8. "model": "Baichuan2",
  9. "messages": [
  10. {
  11. "role": "user",
  12. "content": "世界第一高峰是"
  13. }
  14. ],
  15. "stream": True
  16. }
  17. json_data = json.dumps(data)
  18. headers = {
  19. "Content-Type": "application/json",
  20. "Authorization": "Bearer " + api_key
  21. }
  22. response = requests.post(url, data=json_data, headers=headers, timeout=60)
  23. if response.status_code == 200:
  24. print("请求成功!")
  25. print("响应body:", response.text)
  26. print("请求成功,X-BC-Request-Id:", response.headers.get("X-BC-Request-Id"))
  27. else:
  28. print("请求失败,状态码:", response.status_code)
  29. print("请求失败,body:", response.text)
  30. print("请求失败,X-BC-Request-Id:", response.headers.get("X-BC-Request-Id"))
  31. if __name__ == "__main__":
  32. do_request()