From 78f93bb737bc72c7a47dabe71adbc5b0349c8072 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 22 Oct 2023 15:15:43 +0200 Subject: Add rate limit error messages --- g4f/Provider/FreeGpt.py | 5 ++++- g4f/Provider/NoowAi.py | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'g4f/Provider') diff --git a/g4f/Provider/FreeGpt.py b/g4f/Provider/FreeGpt.py index 6638b67f..a6d31644 100644 --- a/g4f/Provider/FreeGpt.py +++ b/g4f/Provider/FreeGpt.py @@ -41,7 +41,10 @@ class FreeGpt(AsyncGeneratorProvider): async with session.post(f"{url}/api/generate", json=data) as response: response.raise_for_status() async for chunk in response.iter_content(): - yield chunk.decode() + chunk = chunk.decode() + if chunk == "当前地区当日额度已消耗完": + raise RuntimeError("Rate limit reached") + yield chunk @classmethod @property diff --git a/g4f/Provider/NoowAi.py b/g4f/Provider/NoowAi.py index 93748258..9dc26d35 100644 --- a/g4f/Provider/NoowAi.py +++ b/g4f/Provider/NoowAi.py @@ -61,6 +61,8 @@ class NoowAi(AsyncGeneratorProvider): yield line["data"] elif line["type"] == "end": break + elif line["type"] == "error": + raise RuntimeError(line["data"]) def random_string(length: int = 10): return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(length)) \ No newline at end of file -- cgit v1.2.3