返回提交历史
Modified
g4f/Provider/DeepInfra.py
+16
-9
XFEstudio/gpt4free
DeepInfra: Fix token duplication
640c861a
代码差异
1 个文件
+16
-9
@@ -58,16 +58,23 @@ class DeepInfra(AsyncGeneratorProvider):
58
58
response.raise_for_status()
59
59
first = True
60
60
async for line in response.iter_lines():
61
if not line.startswith(b"data: "):
62
continue
63
61
64
try:
62
if line.startswith(b"data: [DONE]"):
65
decoded_line = line.decode().lstrip("data: ")
66
json_line = json.loads(decoded_line)
67
68
choices = json_line.get("choices", [{}])
69
finish_reason = choices[0].get("finish_reason", "")
70
if finish_reason:
63
71
break
64
elif line.startswith(b"data: "):
65
chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
66
if chunk:
72
token = choices[0].get("delta", {}).get("content", "")
73
74
if token:
67
75
if first:
68
chunk = chunk.lstrip()
69
if chunk:
70
first = False
71
yield chunk
76
token = token.lstrip()
77
first = False
78
yield token
72
79
except Exception:
73
raise RuntimeError(f"Response: {line}")
80
raise RuntimeError(f"Response: {line}")