summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornullstreak <139914347+nullstreak@users.noreply.github.com>2024-01-21 05:07:34 +0100
committerGitHub <noreply@github.com>2024-01-21 05:07:34 +0100
commit640c861ad77e14e1745d2c95b8d77aa43b74c69f (patch)
tree4e57de9c2254918a0c02838ffac62b2bcb4b4284
parentMerge pull request #1484 from hlohaus/copilot (diff)
downloadgpt4free-640c861ad77e14e1745d2c95b8d77aa43b74c69f.tar
gpt4free-640c861ad77e14e1745d2c95b8d77aa43b74c69f.tar.gz
gpt4free-640c861ad77e14e1745d2c95b8d77aa43b74c69f.tar.bz2
gpt4free-640c861ad77e14e1745d2c95b8d77aa43b74c69f.tar.lz
gpt4free-640c861ad77e14e1745d2c95b8d77aa43b74c69f.tar.xz
gpt4free-640c861ad77e14e1745d2c95b8d77aa43b74c69f.tar.zst
gpt4free-640c861ad77e14e1745d2c95b8d77aa43b74c69f.zip
-rw-r--r--g4f/Provider/DeepInfra.py25
1 files changed, 16 insertions, 9 deletions
diff --git a/g4f/Provider/DeepInfra.py b/g4f/Provider/DeepInfra.py
index 96e3a680..12bd49c7 100644
--- a/g4f/Provider/DeepInfra.py
+++ b/g4f/Provider/DeepInfra.py
@@ -58,16 +58,23 @@ class DeepInfra(AsyncGeneratorProvider):
response.raise_for_status()
first = True
async for line in response.iter_lines():
+ if not line.startswith(b"data: "):
+ continue
+
try:
- if line.startswith(b"data: [DONE]"):
+ decoded_line = line.decode().lstrip("data: ")
+ json_line = json.loads(decoded_line)
+
+ choices = json_line.get("choices", [{}])
+ finish_reason = choices[0].get("finish_reason", "")
+ if finish_reason:
break
- elif line.startswith(b"data: "):
- chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
- if chunk:
+ token = choices[0].get("delta", {}).get("content", "")
+
+ if token:
if first:
- chunk = chunk.lstrip()
- if chunk:
- first = False
- yield chunk
+ token = token.lstrip()
+ first = False
+ yield token
except Exception:
- raise RuntimeError(f"Response: {line}") \ No newline at end of file
+ raise RuntimeError(f"Response: {line}")