1 line
2.0 KiB
JSON
1 line
2.0 KiB
JSON
{"repo": "anning01/textcreatevideo", "files": {"SDK/ChatGPT/API2D/app.py": {"9193254bc96dd14c3547a4b34642ee6a885e8018": {"t": 1785768949.5484629, "n": 1573, "h": "f7f6c35107b166ee", "c": "#!/usr/bin/python\n# -*- coding: UTF-8 -*-\n# @author:anning\n# @email:anningforchina@gmail.com\n# @time:2023/08/01 18:08\n# @file:app.py\n\nimport requests\nfrom config import ForwardKey\nfrom requests import ConnectionError\nfrom requests import Timeout\n\nclass Main:\n ForwardKey = ForwardKey\n url = \"https://oa.api2d.net/v1/chat/completions\"\n\n def __str__(self):\n return \"API2D请求失败,请检查配置!\"\n\n def prompt_generation_chatgpt(self, param, gpt_prompt):\n # 发送HTTP POST请求\n headers = {\n 'Content-Type': 'application/json',\n 'Authorization': f'Bearer {ForwardKey}'\n # <-- 把 fkxxxxx 替换成你自己的 Forward Key,注意前面的 Bearer 要保留,并且和 Key 中间有一个空格。\n }\n data = {\n \"model\": \"gpt-3.5-turbo\",\n \"messages\": [{\"role\": \"user\", \"content\": gpt_prompt.get(\"prompt\") + param, }]\n }\n print(\"-----------开始请求API2D-----------\")\n try:\n response = requests.post(self.url, headers=headers, json=data, timeout=15)\n except Timeout:\n return False, \"API2D连接超时,15秒未响应!\"\n except ConnectionError:\n return False, f\"连接错误,{self.url} 建立连接失败,请检查网络。\"\n except Exception as e:\n return False, str(e)\n if response.status_code != 200:\n return False, response.json().get(\"message\", \"API2D返回状态码错误,请检查配置!\")\n # 发送HTTP POST请求\n result_json = response.json()\n # 输出结果\n return result_json[\"choices\"][0][\"message\"][\"content\"], \"\"\n\n\nif __name__ == '__main__':\n Main().prompt_generation_chatgpt(\"天空一声惊响,老子闪亮登场。\")\n\n"}}}} |