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
957 stars 113 forks source link

AttributeError: 'State' object has no attribute 'tokens' #167

Closed realnoob007 closed 5 months ago

realnoob007 commented 5 months ago

After setting up the openai api endpoint by setting up secrets.json and running api.py, I get the following error making the request: { "model": "gpt-3.5-turbo", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "hello, how are you?" } ] } ] }

Error: 127.0.0.1:52791 - - [21/Jun/2024:12:59:29] "POST /v1/chat/completions" 500 21 2024-06-21 12:59:29,818 ERROR Exception inside application: 'State' object has no attribute 'tokens' Traceback (most recent call last): File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\datastructures.py", line 699, in getattr return self._state[key]


KeyError: 'tokens'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\miniconda\envs\py3.11\Lib\asyncio\tasks.py", line 277, in __step
    result = coro.send(None)
             ^^^^^^^^^^^^^^^
  File "D:\miniconda\envs\py3.11\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
    raise exc
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 72, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "D:\miniconda\envs\py3.11\Lib\site-packages\fastapi\routing.py", line 278, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\miniconda\envs\py3.11\Lib\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\code\poe-api-wrapper\poe_api_wrapper\openai\api.py", line 48, in chat_completions
    token = random.choice(app.state.tokens)
                          ^^^^^^^^^^^^^^^^
  File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\datastructures.py", line 702, in __getattr__
    raise AttributeError(message.format(self.__class__.__name__, key))
AttributeError: 'State' object has no attribute 'tokens'
snowby666 commented 5 months ago

Did you specify your tokens in secrets.json?

Or you can setup with this for convenience:

from poe_api_wrapper import PoeServer
tokens = [
    {"p-b": "XXXXXXXX", "p-lat": "XXXXXXXX"},
    {"p-b": "XXXXXXXX", "p-lat": "XXXXXXXX"},
    {"p-b": "XXXXXXXX", "p-lat": "XXXXXXXX"}
]
PoeServer(tokens=tokens)
snowby666 commented 5 months ago

Can you try again with new version. The app.state got ignored when using cli

realnoob007 commented 5 months ago

Running with the version you provided, I think it is working now. But I got the following error, maybe it's about the internet connections issue? 2024-06-21 14:01:29.959 | INFO | poe_api_wrapper.bundles:init_window:41 - Web data initialized 2024-06-21 14:01:29.961 | INFO | poe_api_wrapper.bundles:get_form_key:82 - Retrieved formkey successfully: 0e4ef955677c72cacd8367dc315203ff
2024-06-21 14:01:30,310 INFO HTTP Request: GET https://poe.com/api/settings "HTTP/2 200 OK" 2024-06-21 14:01:30,917 INFO HTTP Request: POST https://poe.com/api/gql_POST "HTTP/2 200 OK" 2024-06-21 14:01:51,979 ERROR [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 - goodbye 2024-06-21 14:01:51.979 | WARNING | poe_api_wrapper.async_api:on_ws_close:294 - Connection to remote host was lost. Reconnecting... 2024-06-21 14:01:51,980 ERROR error from callback <function AsyncPoeApi.connect_ws.... at 0x0000028938614040>: There is no current event loop in thread 'Thread-2 (ws_run_thread)'. 127.0.0.1:56137 - - [21/Jun/2024:14:02:02] "POST /v1/chat/completions" 500 21 2024-06-21 14:02:02,308 ERROR Exception inside application: Timed out waiting for websocket to connect. Traceback (most recent call last): File "D:\miniconda\envs\py3.11\Lib\asyncio\tasks.py", line 277, in step result = coro.send(None) ^^^^^^^^^^^^^^^ File "D:\miniconda\envs\py3.11\Lib\site-packages\fastapi\applications.py", line 1054, in call await super().call(scope, receive, send) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\applications.py", line 123, in call await self.middleware_stack(scope, receive, send) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\middleware\errors.py", line 186, in call raise exc File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\middleware\errors.py", line 164, in call await self.app(scope, receive, _send) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\middleware\exceptions.py", line 65, in call await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette_exception_handler.py", line 64, in wrapped_app raise exc File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 756, in call__ await self.middleware_stack(scope, receive, send) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 776, in app await route.handle(scope, receive, send) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 297, in handle await self.app(scope, receive, send) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 77, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette_exception_handler.py", line 64, in wrapped_app raise exc File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "D:\miniconda\envs\py3.11\Lib\site-packages\starlette\routing.py", line 72, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "D:\miniconda\envs\py3.11\Lib\site-packages\fastapi\routing.py", line 278, in app raw_response = await run_endpoint_function( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\miniconda\envs\py3.11\Lib\site-packages\fastapi\routing.py", line 191, in run_endpoint_function return await dependant.call(**values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\miniconda\envs\py3.11\Lib\site-packages\poe_api_wrapper\openai\api.py", line 49, in chat_completions client = await AsyncPoeApi(token).create() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\miniconda\envs\py3.11\Lib\site-packages\poe_api_wrapper\async_api.py", line 90, in create await self.connect_ws() File "D:\miniconda\envs\py3.11\Lib\site-packages\poe_api_wrapper\async_api.py", line 278, in connect_ws raise RuntimeError("Timed out waiting for websocket to connect.") RuntimeError: Timed out waiting for websocket to connect

snowby666 commented 5 months ago

Are you using Clash or any 3rd proxy client? If so please try implementing this code:

import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
os.environ["all_proxy"] = "socks5://127.0.0.1:7890"
realnoob007 commented 5 months ago

Thanks a lot, this works well.