snowby666 / poe-api-wrapper

👾 A Python API wrapper for Poe.com. With this, you will have free access to GPT-4, Claude, Llama, Gemini, Mistral and more! 🚀
https://pypi.org/project/poe-api-wrapper/
GNU General Public License v3.0
858 stars 99 forks source link

All requests get - RuntimeError: Rate limit exceeded for sending requests to poe.com. Please try again later. #121

Closed bluusun closed 6 months ago

bluusun commented 6 months ago

Any request (even first request from new IP) brings up that error. Started about 12 hours ago (no real throttling before that with minimal load we do).

Requirement already satisfied: poe-api-wrapper in .../site-packages (1.3.8)

from poe_api_wrapper import PoeApi client = PoeApi(token)

bot = "a2" message = "What is reverse engineering?"

Non-streamed example:

for chunk in client.send_message(bot, message): pass print(chunk["text"])


Traceback (most recent call last): File "test.py", line 6, in client = PoeApi(token) ^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/poe_api_wrapper/api.py", line 177, in init self.connect_ws() File "/opt/homebrew/lib/python3.11/site-packages/poe_api_wrapper/api.py", line 301, in connect_ws raise RuntimeError("Rate limit exceeded for sending requests to poe.com. Please try again later.") RuntimeError: Rate limit exceeded for sending requests to poe.com. Please try again later.

snowby666 commented 6 months ago

Can you check your compute points balance in Settings section?

I've tested with virtual machine and it still works for me

bluusun commented 6 months ago

iDB 2024-03-12 at 08 47 28 That was my first thought too but plenty left. What else can trigger that error (same user agent)?

bluusun commented 6 months ago

I am using p-b value from poe.com (as before). Tried url decoded and not url decoded. Same result.

bluusun commented 6 months ago

iDB 2024-03-12 at 08 54 23 Works in web interface...

bluusun commented 6 months ago

Is there a way to get verbose output for the connection?

def connect_ws(self, timeout=20): if self.ws_connected: return

    if self.ws_connecting:
        while not self.ws_connected:
            sleep(0.01)
        return
snowby666 commented 6 months ago

Can you try with m-b cookie from quora? The wrapper sends requests to quora.com/poe_api/ so quora cookie is more recommended. Maybe they've removed the p-b cookie. Let me if I can troubleshoot this.

bluusun commented 6 months ago

After creating a Quora account for that email (was only poe.com before) it started to work again with either token.

snowby666 commented 6 months ago

Thanks for the info. I'll update the documentation specifying this problem with token later.

bluusun commented 6 months ago

Seems the issues is back now - seems there is a real new rate limiter at work (very aggressive). Do you have an example what proxy to use?

I have the impression the block is there for any IP that is connecting (so proxy may not work, switching IPs locally did not do anything).

The issue is the same using p-b or m-b token now. No issues using poe.com website.

To recreate the issue - make a request every 20 seconds (must be unique prompt or might be cached). Block happens after about 60-80 requests and lasts???

bluusun commented 6 months ago

Also odd (after blocking) I switched IP (and got RuntimeError: Rate limit exceeded for sending requests to poe.com. Please try again later.) and then created a new poe.com account and changed token (and kept IP). Same RuntimeError: Rate limit exceeded for sending requests to poe.com. Please try again later.!

bluusun commented 6 months ago

Maybe it is worth looking into the static useragent and other static elements for ws connection in api.py?

bluusun commented 6 months ago

I used this to connect to the WS endpoint I got from my browser and had no trouble connecting and streaming messages) despite the 'blocked'. Note I did not specify a useragent or even cookie (browser already authenticated me earlier :)

websocat "wss://tch385627.tch.poe.com/up/chan67-8888/updates?min_seq=numbers&channel=poe-chanxx-yyyy-validsecret&hash=secret" \ --header="Pragma: no-cache" \ --header="Origin: https://poe.com" \ --header="Accept-Language: en-US,en;q=0.9" \ --header="Sec-WebSocket-Key: secret"

bluusun commented 6 months ago

Thanks for your help with this! Just sent some buymeacoffee :) Your Discord link seems broken...

nlpander commented 6 months ago

Yep seeing the same issue here, verified that I have credits left with my account, getting the same message: RuntimeError: Rate limit exceeded for sending requests to poe.com. Please try again later.

Could something have been altered with the graphql client or cookie requirements ? 'apollographql-client-version': '1.1.6-65'

xcrong commented 6 months ago

may the error was caused by Cloudflare's Firewall ?

I got error when connect_ws -> get_channel_settings -> subscribe -> send_request.

the result of send_request is 403. the following is response header.

Headers({
   'date': 'Wed, 13 Mar 2024 02:01:04 GMT',
   'content-type': 'text/html; charset=UTF-8',
   'transfer-encoding': 'chunked',
   'connection': 'close',
   'accept-ch': 'Sec-CH-UA-Bitness, Sec-CH-UA-Arch, Sec-CH-UA-Full-Version, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform-Version, Sec-CH-UA-Full-Version-List, Sec-CH-UA-Platform, Sec-CH-UA, UA-Bitness, UA-Arch, UA-Full-Version, UA-Mobile, UA-Model, UA-Platform-Version, UA-Platform, UA',
   'cross-origin-embedder-policy': 'require-corp',
   'cross-origin-opener-policy': 'same-origin',
   'cross-origin-resource-policy': 'same-origin',
   'origin-agent-cluster': '?1',
   'permissions-policy': 'accelerometer=(),autoplay=(),browsing-topics=(),camera=(),clipboard-read=(),clipboard-write=(),geolocation=(),gyroscope=(),hid=(),interest-cohort=(),magnetometer=(),microphone=(),payment=(),publickey-credentials-get=(),screen-wake-lock=(),serial=(),sync-xhr=(),usb=()',
   'referrer-policy': 'same-origin',
   'x-frame-options': 'SAMEORIGIN',
   'cf-mitigated': 'challenge',
   'cf-chl-out': 'bo2Mzka3wqWOCHwN4s96a3/XnMLSILZGMkjTrhDBd8roBn8kDbAr4M0PEmW4HVTuIQlk56B54MBjzIde58zLmT7Tuy1Hd/zEa8ZaIEHA09wU6yXeOV5Um/55xP452GRzaI4XLx6sdosGLBuAhKSphw==$wrJJG5dkDGoe+6Wvjk5xjw==',
   'cache-control': 'private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
   'expires': 'Thu, 01 Jan 1970 00:00:01 GMT',
   'vary': 'Accept-Encoding',
   'server': 'cloudflare',
   'cf-ray': '86386f1dd96636d8-YYZ',
   'content-encoding': 'gzip',
   'alt-svc': 'h3=":443"; ma=86400'
})

Claude 3 (sonnet) say:

Based on the response headers you provided, the 403 error you received is caused by the Cloudflare Web Application Firewall (WAF).

The cf-mitigated: challenge header indicates that Cloudflare detected your request as potentially non-human or malicious, and therefore sent a challenge to validate whether you are a legitimate human user.

Additionally, the cf-chl-out header contains an encrypted data, which may include instructions needed for the validation.

To resolve this issue, you have the following options:

  1. Try resending the request Sometimes Cloudflare temporarily blocks certain requests, and resending may succeed.

  2. Solve the Cloudflare challenge Follow the instructions in the cf-chl-out header to solve the challenge provided by Cloudflare, proving that you are a legitimate human user. This usually involves parsing a JavaScript challenge or completing a captcha.

  3. Check and optimize client behavior Review your request patterns for any anomalies, such as sending too many requests, requests with short intervals, etc., and optimize accordingly.

  4. Contact the website owner If the above methods fail, you can try contacting the website owner and request that your client IP or user agent be whitelisted to avoid triggering Cloudflare's WAF rules.

In general, since Cloudflare's WAF rules are designed to prevent malicious behavior and attacks, reasonably optimizing client request patterns and solving the validation challenges are effective ways to bypass the 403 error.

xcrong commented 6 months ago

may the error was caused by Cloudflare's Firewall ?

I got error when connect_ws -> get_channel_settings -> send_request.

the result of send_request is 403. the following is response header.

...

Perhaps this could also explain why some people encounter errors while others don't; some people can resolve the issue by changing their IP address, but the error reappears after a while. The firewall initiates a human verification challenge after detecting a large number of anomalous requests.

snowby666 commented 6 months ago

may the error was caused by Cloudflare's Firewall ? I got error when connect_ws -> get_channel_settings -> send_request. the result of send_request is 403. the following is response header. ...

Perhaps this could also explain why some people encounter errors while others don't; some people can resolve the issue by changing their IP address, but the error reappears after a while. The firewall initiates a human verification challenge after detecting a large number of anomalous requests.

I see. I think I will use a requests library that can bypass cloudflare.

I've tried making 60 requests (make a request every 1 second) and it runs normally now

bluusun commented 6 months ago

Maybe adding the cf token will solve it - this usually makes cf accept requests without further issues.

snowby666 commented 6 months ago

Can someone help me test again if the rate limiter still works?

xcrong commented 6 months ago

Can someone help me test again if the rate limiter still works?

I tried again, and now the response code is 200, but the data in the response body indicates an error, and I still can't establish a WebSocket connection. My cookie was just copied from the browser, and I tried both poe and quora's cookies.

200
{'data': None, 'errors': [{'message': 'Server Error'}], 'extensions': {'is_final': True}}

I remember that when I got this kind of error before, I could fix it by updating the queryHash in the subscribe function; but this time, even after I tried updating it, it still returns the same error. I added two print statements in the send_request function.

def send_request(...):
    ...

    print(response.status_code)
    # print(response.text)
    # print(response.headers)

    if response.status_code == 200:
        for file in file_form:
            if hasattr(file[1], "closed") and not file[1].closed:
                file[1].close()
        print(response.json())
        return response.json()
    else:
        raise RuntimeError(
            f"An unknown error occurred. Raw response data: {response.text}"
        )
bluusun commented 6 months ago

Downloaded newest version from pip. No changes (same error) for my tokens. Noticed that the account I created yesterday works now (free account) but not any of the other accounts (Premium). Other Free accounts are still blocked. Is there a way I can reach you to provide tokens for you to test?

bluusun commented 6 months ago

My response is:

RuntimeError: Failed to subscribe by sending SubscriptionsMutation. Raw response data: {'data': None, 'errors': [{'message': 'Server Error'}], 'extensions': {'is_final': True}}

DEBUG:root:Response content: {"data": null, "errors": [{"message": "Server Error"}], "extensions": {"is_final": true}}

When I use a a simple curl using my tokens I get proper data back:

curl 'https://www.quora.com/poe_api/settings' \ -H 'authority: www.quora.com' \ -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.7' \ -H 'accept-language: en-US,en;q=0.9' \ -H 'cache-control: no-cache' \ -H 'cookie: m-b=token' \ -H 'pragma: no-cache' \ -H 'sec-ch-ua: "Not A(Brand";v="99", "Opera";v="107", "Chromium";v="121"' \ -H 'sec-ch-ua-mobile: ?0' \ -H 'sec-ch-ua-platform: "macOS"' \ -H 'sec-fetch-dest: document' \ -H 'sec-fetch-mode: navigate' \ -H 'sec-fetch-site: none' \ -H 'sec-fetch-user: ?1' \ -H 'upgrade-insecure-requests: 1' \ -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 OPR/107.0.0.0' \ --compressed

-->

{"formkey": "formkey", "tchannelData": {"minSeq": "7475948979", "channel": "mychannel", "channelHash": "hash", "boxName": "chan57-8888", "baseHost": "poe.com", "targetUrl": "", "enableWebsocket": true}}%

XenocodeRCE commented 6 months ago

A temporary fix for me (I hope it will be fixed, but I suspect Quora/Poe dev to lurk into this repo very often) is to use a mix of python and AutoHotKey script on a Windows VPS using the Poe windows app.

I send the prompt to local php on the VPS, then it open Poe app, click on the desired bot, paste the prompt, then I wait till response is done and I grab the content, copy and send back the response to the PHP ...

bluusun commented 6 months ago

It appears to me that the data provided by the endpoint https://www.quora.com/poe_api/settings does not provide a proper ws channel connection (anymore). I can run the request successfully using m-b token but the data provided does not provide a ws connection anymore (at least I seem to get no data from it). Specifically it seems the minSeq counter is (far) off by many hundreds of thousands.

However when I tried this endpoint https://poe.com/api/settings?channel=poe-chan.... I was able to get correct minSequence information and was able to establish a valid WS connection (note it seemed empty initially but started working after my first bot request).

I am not sure if channel is required for the request:

curl 'https://poe.com/api/settings?channel=poe-chan59-8888-secret' \ -H 'authority: poe.com' \ -H 'accept: /' \ -H 'accept-language: en-US,en;q=0.9' \ -H 'cache-control: no-cache' \ -H 'cookie: p-b=token' \ -H 'pragma: no-cache' \ -H 'referer: https://poe.com/settings' \ -H 'sec-ch-ua: "Not A(Brand";v="99", "Opera";v="107", "Chromium";v="121"' \ -H 'sec-ch-ua-mobile: ?0' \ -H 'sec-ch-ua-platform: "macOS"' \ -H 'sec-fetch-dest: empty' \ -H 'sec-fetch-mode: cors' \ -H 'sec-fetch-site: same-origin' \ -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 OPR/107.0.0.0' \ --compressed

Returns (removed values):

{"tchannelData":{"minSeq":"8887813834","channel":"","channelHash":"","boxName":"","baseHost":"poe.com","targetUrl":"","enableWebsocket":true}}%

It seems minSeq is a global counter but it might be random.

bluusun commented 6 months ago

Could you make the changes and check if that fixes it?

bluusun commented 6 months ago

BTW there is a long standing bug that every 20th request or so gets mismatched (i.e. bot reply is sent to wrong request ID), little annoying and likely easy to fix by strict check for message id?

bluusun commented 6 months ago

Turns out the parameter for the settings endpoint is now required to get the right minSeq .... /settings?channel=poe-chan59-8888-secret . I am not sure how it can be obtained (or it may be static per account (like the cookie?).

bluusun commented 6 months ago

Did it just come back?

nlpander commented 6 months ago

No not for me, still not able to access the wrapper - tried both p-b and m-b tokens switched IPs as well.

bluusun commented 6 months ago

Been issuing new tokens several times and it came back. However I can't use file as attachments anymore :(

snowby666 commented 6 months ago

Okay good news is I've figured out why the tokens only work on old accounts but not the new ones. Turns out poe api now requires a second token for making request. I'll update the patch soon with the uploading file fix as well

snowby666 commented 6 months ago

I've released the patch

bluusun commented 6 months ago

Awesome! For accounts that don't have the new extra token will the API be backward compatible?

snowby666 commented 6 months ago

Awesome! For accounts that don't have the new extra token will the API be backward compatible?

In that case, you can try deleting cookies and login again then there should be 2 tokens

bluusun commented 6 months ago

Rate limit exceeded is back for 1.3.9 AND 1.4.0 :( Using 2 tokens - this worked for a few days!

palislau commented 6 months ago

Rate limit exceeded is back for 1.3.9 AND 1.4.0 :( Using 2 tokens - this worked for a few days!

same for me

bluusun commented 6 months ago

I see this in newest version a lot:

2024-03-28 20:32:28.457 | DEBUG | poe_api_wrapper.api:connect_ws:164 - An unknown error occurred. Raw response data: <!DOCTYPE html>Just a moment...