aitest.py 800 B

123456789101112131415161718192021
  1. import json
  2. from http import HTTPStatus
  3. from dashscope import Application
  4. with open("output.txt", 'r', encoding="utf8") as f:
  5. prompt = f.read()
  6. response = Application.call(
  7. # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
  8. api_key="sk-0164613e1a2143fc808fc4cc2451bef0",
  9. app_id='2f288f146e2d492abb3fe22695e70635', # 替换为实际的应用 ID
  10. prompt=prompt)
  11. if response.status_code == HTTPStatus.OK:
  12. try:
  13. json_data = json.loads(response.output.text)
  14. print(json_data)
  15. except json.JSONDecodeError:
  16. print("返回内容不是有效的 JSON 格式!")
  17. else:
  18. print(f"请求失败: {response.message}")