Commit 65c3030
Changed files (1)
src
others
src/others/gpt.py
@@ -71,20 +71,22 @@ async def gpt_response(client: Client, message: Message, **kwargs):
res = await send2tg(client, message, texts=msg, **kwargs)
kwargs["progress"] = res[0]
headers = {"authorization": f"Bearer {model_conf['key']}"}
- if model_conf["model_type"] == "audio":
- resp = await hx_req(model_conf["url"], "POST", headers=headers, post_json=model_conf["payload"], proxy=PROXY.GPT, check_has_kv=["output.choices"], timeout=model_conf["timeout"])
- choices = resp.json()["output"]["choices"]
- ai_response = choices[0].get("message", {}).get("content", [{}])[0].get("text", "")
- else:
- resp = await hx_req(model_conf["url"], "POST", headers=headers, post_json=model_conf["payload"], proxy=PROXY.GPT, check_has_kv=["choices"], timeout=model_conf["timeout"])
- choices = resp.json()["choices"]
- ai_response = choices[0].get("message", {}).get("content")
- cleanup(contexts)
- if not ai_response:
- await modify_progress(text=f"π€{model_conf['friendly_name']}ζͺεεΊ, θ―·η¨ειθ―...", force_update=True, **kwargs)
+ try:
+ if model_conf["model_type"] == "audio":
+ resp = await hx_req(model_conf["url"], "POST", headers=headers, post_json=model_conf["payload"], proxy=PROXY.GPT, check_has_kv=["output.choices"], timeout=model_conf["timeout"])
+ choices = resp.json()["output"]["choices"]
+ ai_response = choices[0].get("message", {}).get("content", [{}])[0].get("text", "")
+ else:
+ resp = await hx_req(model_conf["url"], "POST", headers=headers, post_json=model_conf["payload"], proxy=PROXY.GPT, check_has_kv=["choices"], timeout=model_conf["timeout"])
+ choices = resp.json()["choices"]
+ ai_response = choices[0].get("message", {}).get("content")
+ except Exception as e:
+ logger.error(f"GPT request failed: {e}")
+ await modify_progress(text=f"π€{model_conf['friendly_name']}ζͺεεΊ, θ―·η¨ειθ―.\n{e}", force_update=True, **kwargs)
await asyncio.sleep(3)
await modify_progress(del_status=True, **kwargs)
return
+ cleanup(contexts)
texts = f"{model_conf['bot_msg_prefix']}\n\n{ai_response}"
logger.debug(texts)
await send2tg(client, message, texts=texts, **kwargs)