diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-11-16 13:19:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-16 13:19:51 +0100 |
commit | 6ce493d4dfc2884832ff5b5be4479a55818b2fe7 (patch) | |
tree | 92e9efce62f7832ebe56969c120d8e92e75881a3 /etc/examples/api.py | |
parent | Update internet.py (diff) | |
download | gpt4free-6ce493d4dfc2884832ff5b5be4479a55818b2fe7.tar gpt4free-6ce493d4dfc2884832ff5b5be4479a55818b2fe7.tar.gz gpt4free-6ce493d4dfc2884832ff5b5be4479a55818b2fe7.tar.bz2 gpt4free-6ce493d4dfc2884832ff5b5be4479a55818b2fe7.tar.lz gpt4free-6ce493d4dfc2884832ff5b5be4479a55818b2fe7.tar.xz gpt4free-6ce493d4dfc2884832ff5b5be4479a55818b2fe7.tar.zst gpt4free-6ce493d4dfc2884832ff5b5be4479a55818b2fe7.zip |
Diffstat (limited to 'etc/examples/api.py')
-rw-r--r-- | etc/examples/api.py | 13 |
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 |