summaryrefslogtreecommitdiffstats
path: root/etc/examples/api.py
diff options
context:
space:
mode:
Diffstat (limited to 'etc/examples/api.py')
-rw-r--r--etc/examples/api.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/etc/examples/api.py b/etc/examples/api.py
index 1ab9b51b..f8f5d5ec 100644
--- a/etc/examples/api.py
+++ b/etc/examples/api.py
@@ -6,14 +6,19 @@ body = {
"provider": "",
"stream": True,
"messages": [
- {"role": "assistant", "content": "What can you do? Who are you?"}
+ {"role": "user", "content": "What can you do? Who are you?"}
]
}
-lines = requests.post(url, json=body, stream=True).iter_lines()
-for line in lines:
+response = requests.post(url, json=body, stream=True)
+response.raise_for_status()
+for line in response.iter_lines():
if line.startswith(b"data: "):
try:
- print(json.loads(line[6:]).get("choices", [{"delta": {}}])[0]["delta"].get("content", ""), end="")
+ json_data = json.loads(line[6:])
+ if json_data.get("error"):
+ print(json_data)
+ break
+ print(json_data.get("choices", [{"delta": {}}])[0]["delta"].get("content", ""), end="")
except json.JSONDecodeError:
pass
print() \ No newline at end of file