да
This commit is contained in:
1 parent
5d76e154e0
commit
763ca6062d
3 files changed
+80
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
import ai
|
||||
|
||||
from statistics import *
|
||||
@@ -277,6 +278,7 @@ async def openai_generate_text(
|
||||
|
||||
if stream:
|
||||
message = ""
|
||||
stream_error = ""
|
||||
t_counter = 0
|
||||
counter = 0
|
||||
counter_2 = 1
|
||||
@@ -294,6 +296,10 @@ async def openai_generate_text(
|
||||
"type": "error",
|
||||
"text": error["message"]
|
||||
}
|
||||
if isinstance(chunk, dict) and "choices" not in chunk:
|
||||
stream_error = str(chunk)[:300]
|
||||
elif isinstance(chunk, str) and chunk.strip():
|
||||
stream_error = chunk.strip()[:300]
|
||||
continue
|
||||
if content:
|
||||
message += content
|
||||
@@ -360,6 +366,13 @@ async def openai_generate_text(
|
||||
print(f"{YELLOW} -- {e}{RESET}")
|
||||
|
||||
text = message
|
||||
if not text and stream_error:
|
||||
error = parse_response(stream_error.lower())
|
||||
print(f"{RED} -- {error['type']} - {stream_error}{RESET}")
|
||||
return {
|
||||
"type": "error",
|
||||
"text": error["message"]
|
||||
}
|
||||
else:
|
||||
async for completion in ai.completions(**alo):
|
||||
pass
|
||||
@@ -383,6 +396,21 @@ async def openai_generate_text(
|
||||
except Exception as e:
|
||||
error = parse_response(str(e).lower())
|
||||
print(f"{RED} -- {error['type']} - {e}{RESET}")
|
||||
err_str = str(e)
|
||||
try:
|
||||
status_code = int(err_str.split(" ", 1)[0])
|
||||
except ValueError:
|
||||
status_code = 0
|
||||
if 400 <= status_code < 500:
|
||||
debug_alo = {k: v for k, v in alo.items() if k != "api_key"}
|
||||
return {
|
||||
"type": "error",
|
||||
"text": error["message"] + "\n\n" \
|
||||
f"модель: `{model}`\n" \
|
||||
f"статус: `{status_code}`\n" \
|
||||
f"raw: `{err_str[:500]}`\n" \
|
||||
f"запрос:\n```{json.dumps(debug_alo, ensure_ascii=False)[:1200]}\n```"
|
||||
}
|
||||
return {
|
||||
"type": "error",
|
||||
"text": error["message"]
|
||||
|
||||
Reference in new issue
Block a user