Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatGPT provider fails to read .har file #1896

Open
plia7 opened this issue Apr 26, 2024 · 43 comments
Open

ChatGPT provider fails to read .har file #1896

plia7 opened this issue Apr 26, 2024 · 43 comments
Assignees
Labels
bug Something isn't working

Comments

@plia7
Copy link

plia7 commented Apr 26, 2024

Hello.

It seems like it doesn't work anymore?
I updated g4f to latest version and added a new HAR file, but I'm still getting a message that HAR file is missing?

me:~# Successfully installed g4f-0.3.0.6

me: chatgpt “test“

GPT: Traceback (most recent call last):
  File "/root/./mnt/docs/chatgpt.py", line 63, in <module>
    for chunk in stream:
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 216, in create_completion
    yield loop.run_until_complete(await_callback(gen.__anext__))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 45, in await_callback
    return await callback()
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 383, in create_async_generator
    cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 184, in get_default_model
    raise MissingAuthError('Add a "api_key" or a .har file' if cls._api_key is None else "Invalid api key")
g4f.errors.MissingAuthError: Add a "api_key" or a .har file

Does it still work for you?

Thanks.

@plia7 plia7 added the bug Something isn't working label Apr 26, 2024
@hlohaus
Copy link
Collaborator

hlohaus commented Apr 27, 2024

Is the .har file in /mnt/docs?

@plia7
Copy link
Author

plia7 commented Apr 27, 2024

Is the .har file in /mnt/docs?

Yes, it's the same place I always placed it and it worked before. I'm also obtaining the HAR file exactly like I used to before so not sure why it doesn't work anymore?

@plia7
Copy link
Author

plia7 commented Apr 28, 2024

Is the .har file in /mnt/docs?

Are you able to reproduce the issue on your end? Is there an alternative way to supply it with the login credentials without the HAR file such as email and password or some session key?

Thanks.

@hlohaus
Copy link
Collaborator

hlohaus commented Apr 29, 2024

.har files are needed for plus accounts. Others work with NoDriver too.
To install NoDriver, use: pip install nodriver.

The .har file must be in the current directory and must end with .har.

@plia7
Copy link
Author

plia7 commented Apr 29, 2024

.har files are needed for plus accounts. Others work with NoDriver too.
To install NoDriver, use: pip install nodriver.

The .har file must be in the current directory and must end with .har.

I'm using ChatGPT v3.5 (I don't think it's ChatGPT v4 which is a plus account). So do I still need the .har file? If not, what do I need to supply it with alternatively?

Yes, it's placed same place as before and it stopped working:

me:/mnt/docs# ls
chat.openai.com.har   
chatgpt.py            
me:/mnt/docs# python3 ./chatgpt.py

You: chatgpt are you here 
New g4f version: 0.3.0.7 (current: 0.3.0.6) | pip install -U g4f

GPT: Traceback (most recent call last):
  File "/root/mnt/docs/./chatgpt.py", line 63, in <module>
    for chunk in stream:
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 216, in create_completion
    yield loop.run_until_complete(await_callback(gen.__anext__))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 45, in await_callback
    return await callback()
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 383, in create_async_generator
    cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 184, in get_default_model
    raise MissingAuthError('Add a "api_key" or a .har file' if cls._api_key is None else "Invalid api key")
g4f.errors.MissingAuthError: Add a "api_key" or a .har file

Does it work for you?

Thanks.

@plia7
Copy link
Author

plia7 commented May 3, 2024

.har files are needed for plus accounts. Others work with NoDriver too.
To install NoDriver, use: pip install nodriver.

The .har file must be in the current directory and must end with .har.

I'm using ChatGPT v3.5 (I don't think it's ChatGPT v4 which is a plus account). So do I still need the .har file? If not, what do I need to supply it with alternatively?

Yes, it's placed same place as before and it stopped working:

me:/mnt/docs# ls
chat.openai.com.har   
chatgpt.py            
me:/mnt/docs# python3 ./chatgpt.py

You: chatgpt are you here 
New g4f version: 0.3.0.7 (current: 0.3.0.6) | pip install -U g4f

GPT: Traceback (most recent call last):
  File "/root/mnt/docs/./chatgpt.py", line 63, in <module>
    for chunk in stream:
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 216, in create_completion
    yield loop.run_until_complete(await_callback(gen.__anext__))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 45, in await_callback
    return await callback()
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 383, in create_async_generator
    cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 184, in get_default_model
    raise MissingAuthError('Add a "api_key" or a .har file' if cls._api_key is None else "Invalid api key")
g4f.errors.MissingAuthError: Add a "api_key" or a .har file

Does it work for you?

Thanks.

@hlohaus Any updates on this?

Thanks.

@hlohaus
Copy link
Collaborator

hlohaus commented May 3, 2024

Are your .har files created correctly? The .har file solution works for me and other users.

@plia7
Copy link
Author

plia7 commented May 3, 2024

Are your .har files created correctly? The .har file solution works for me and other users.

To create the HAR file, I follow these instructions:

Generating a .HAR File
To utilize the OpenaiChat provider, a .har file is required from https://chat.openai.com/. Follow the steps below to create a valid .har file:

Navigate to https://chat.openai.com/ using your preferred web browser and log in with your credentials.

Access the Developer Tools in your browser. This can typically be done by right-clicking the page and selecting "Inspect," or by pressing F12 or Ctrl+Shift+I (Cmd+Option+I on a Mac).

With the Developer Tools open, switch to the "Network" tab.

Reload the website to capture the loading process within the Network tab.

Initiate an action in the chat which can be capture in the .har file.

Right-click any of the network activities listed and select "Save all as HAR with content" to export the .har file.

It generates a file I save as "chatgpt.har" and copy it to mnt/docs folder where chatgpt.py is located.

It worked before, but it stopped working so I'm not doing anything different than what I did before.

Is there a tool or script to fetch the .har from chrome browser automatically to make sure I'm not doing anything wrong when fetching it manually following these instructions?

I also just updated the g4f package but I still get the same message:

Successfully installed g4f-0.3.0.8
me:/mnt/docs# ls
chatgpt.har           
chatgpt.py            
me:/mnt/docs# chatgpt

You: how are you?

GPT: Traceback (most recent call last):
  File "/root/mnt/docs/chatgpt.py", line 63, in <module>
    for chunk in stream:
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 216, in create_completion
    yield loop.run_until_complete(await_callback(gen.__anext__))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 45, in await_callback
    return await callback()
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 409, in create_async_generator
    cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 200, in get_default_model
    raise MissingAuthError('Add a "api_key" or a .har file' if cls._api_key is None else "Invalid api key")
g4f.errors.MissingAuthError: Add a "api_key" or a .har file

Any alternative way to specify the auth for ChatGPT 3.5 provider (free account) without using the .HAR file? Do you have Discord to discuss? Does it matter what's the full name of the file or as long as it's *.har it should be ok? Is there any way to inspect the .har or debug this case to see why it can't find or fetch the .har file?

Thanks.

@plia7 plia7 changed the title ChatGPT provider not working anymore? ChatGPT provider fails to read .har file May 4, 2024
@plia7
Copy link
Author

plia7 commented May 4, 2024

@hlohaus I just tried a second windows pc with python, I'm encountering the same issue.
So I have this in both Linux x86 machine and windows x64 machine both with python installed.

I downloaded the .har multiple times, trying to write multiple times to chatgpt before saving the .har file, but I still keep getting the same error message.

It can't be that I'm the only one that's reporting this issue?
This doesn't make any sense. Please fix this bug. Let me know what troubleshoot or debug information do you need, I can also share my screen on discord to show you the problem.

Thanks.

@iG8R
Copy link

iG8R commented May 4, 2024

@plia7
What directory did you save the har file in?

PS. I had the same problem, but version 0.3.0.7 solved it.
#1795 (comment)

@plia7
Copy link
Author

plia7 commented May 4, 2024

@plia7 What directory did you save the har file in?

PS. I had the same problem, but version 0.3.0.7 solved it. #1795 (comment)

I put it in both the root folder where chatgpt.py is located as well as inside /mnt/docs.
Again, I'm not doing anything different than I did before when it worked, but it just stopped working for me.
I also upgraded to the latest version 0.3.0.8 in both environments but it still doesn't work.

Do you have some basic code snippet that I can try that works for you to make sure my chatgpt.py script is not causing the issue?

Do you have a Discord channel or user I can show you the problem?

@hlohaus @iG8R Is there any guaranteed folder where I can put the .har file like python site packages or something or is there a way I can force it to read the file from a specific location?

@iG8R Your issue doesn't sound the same as mine as yours doesn't say that "har file is missing?" so I think you had a different issue than mine?

Thanks.

@iG8R
Copy link

iG8R commented May 4, 2024

@plia7
Yes, my issue was a little different.
In your case, when it says "har file is missing", IMHO, it means it can't find the har file in the g4f specific directory where it should be.

I do not have Linux, but I suggest you try the following on Windows, which works fine for me:

  1. Clone the g4f repository, e.g.
    c:\g4f\
  2. Create a virtual environment:
    Enter c:\g4f\ and run from command line python -m venv venv
  3. Activate the virtual environment:
    Still when you are inside c:\g4f\ run from command line .\venv\Scripts\activate
  4. Still inside c:\g4f\ run from command line pip install . (Don't forget to put a period at the end)
  5. If needed also install:
    pip install uvicorn and pip install fastapi
  6. First, run g4f api --debug without any har file in the c:\g4f\har_and_cookies\ directory
  7. Then copy your har file to the c:\g4f\har_and_cookies\ directory and run g4f api --debug again. You will then be able to see comments on how g4f handles your har file.

image

@iG8R
Copy link

iG8R commented May 5, 2024

Do you have some basic code snippet that I can try that works for you to make sure my chatgpt.py script is not causing the issue?

I don't use any code snippets, just API calls from other applications, e.g.

  1. https://github.com/pot-app/pot-desktop
    Request Path http://127.0.0.1:1337/v1/chat/completions?provider=OpenaiChat
    Api Key - whitespace

  1. https://github.com/immersive-translate/immersive-translate/

@hlohaus
Copy link
Collaborator

hlohaus commented May 7, 2024

Hey, @plia7! How do you run your script? Do you just type python chatgpt.py or do you use an absolute or relative path like python .../my_dir/chatgpt.py? Try changing to your directory first with cd .../my_dir/ and see if that works.

@plia7
Copy link
Author

plia7 commented May 7, 2024

Hey, @plia7! How do you run your script? Do you just type python chatgpt.py or do you use an absolute or relative path like python .../my_dir/chatgpt.py? Try changing to your directory first with cd .../my_dir/ and see if that works.

Yes, I change directory where chatgpt.py is located and then I run it by the command "python3 chatgpt.py" but that's failing.
Maybe you could add an option to specify the har in a parameter to make sure the package doesn't get confused like that?

Thanks.

@plia7
Copy link
Author

plia7 commented May 7, 2024

Hey, @plia7! How do you run your script? Do you just type python chatgpt.py or do you use an absolute or relative path like python .../my_dir/chatgpt.py? Try changing to your directory first with cd .../my_dir/ and see if that works.

My friend was able to get this to work in his newly set environment after getting the same error about missing har file although he placed it. He updated to 0.3.1.0 and then it started to work. I tried to do the same but I still get the error. I made sure to remove any har file copies I had so it doesn't take the wrong one.

@hlohaus Could you explain please how it determines which HAR file to use? Is there a place I can put the file to ensure it will take priority over any other har file in the running path? I think this is not environment issue rather it's a code issue that should be fixed, because it's clearly failing to pick up on the har file as in my case.

Thanks.

@hlohaus
Copy link
Collaborator

hlohaus commented May 8, 2024

Hey, why can't you just put the .har file in the current directory? It reads all the .har files, but then only uses the last one it finds that matches alphabetically.

@plia7
Copy link
Author

plia7 commented May 8, 2024

Hey, why can't you just put the .har file in the current directory? It reads all the .har files, but then only uses the last one it finds that matches alphabetically.

I already placed it in the current directory. It doesn't work.

@plia7
Copy link
Author

plia7 commented May 8, 2024

@iG8R Thanks for the detailed reply.
Could you explain please how do you make a REST POST call to this endpoint:

http://127.0.0.1:1337/v1/chat/completions?provider=OpenaiChat

What body do you use?

When I try to make a REST POST call to http://127.0.0.1:1337/v1/chat/completions?provider=OpenaiChat
without any body, this is the response I get back:

INFO: 127.0.0.1:64627 - "POST /v1/chat/completions?provider=OpenaiChat HTTP/1.1" 422 Unprocessable Entity

And this is the response body I get:

{
    "detail": [
        {
            "loc": [
                "body"
            ],
            "message": "Input should be a valid dictionary or object to extract fields from",
            "type": "model_attributes_type"
        }
    ]
}

Thanks.

@iG8R
Copy link

iG8R commented May 8, 2024

@plia7
I don't use any code snippets, all the bodies are "provided" by the apps I use (see the attached screenshot).
Try to execute them and check from the console logs - is the har file being processed as it should or not?

image

@plia7
Copy link
Author

plia7 commented May 8, 2024

@plia7

I don't use any code snippets, all the bodies are "provided" by the apps I use (see the attached screenshot).

Try to execute them and check from the console logs - is the har file being processed as it should or not?

image

Do I have to install both of these programs?

@iG8R
Copy link

iG8R commented May 8, 2024

Do I have to install both of these programs?

Any will do.

@plia7
Copy link
Author

plia7 commented May 10, 2024

Do I have to install both of these programs?

Any will do.

Ok so when I try to add the call in the pot program as in your screenshot, this is the message I get:

C:\Users\me\Documents>C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\Scripts\g4f.exe api --debug
Starting server... [g4f v-0.3.1.0] (debug)
INFO:     Will watch for changes in these directories: ['C:\\Users\\me\\Documents']
INFO:     Uvicorn running on http://0.0.0.0:1337 (Press CTRL+C to quit)
INFO:     Started reloader process [27076] using WatchFiles
INFO:     Started server process [5984]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
Using OpenaiChat provider and gpt-3.5-turbo model
INFO:     127.0.0.1:62560 - "POST /v1/chat/completions?provider=OpenaiChat HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
  + Exception Group Traceback (most recent call last):
  |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_utils.py", line 87, in collapse_excgroups
  |     yield
  |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 190, in __call__
  |     async with anyio.create_task_group() as task_group:
  |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\anyio\_backends\_asyncio.py", line 678, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\uvicorn\protocols\http\httptools_impl.py", line 411, in run_asgi
    |     result = await app(  # type: ignore[func-returns-value]
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\uvicorn\middleware\proxy_headers.py", line 69, in __call__
    |     return await self.app(scope, receive, send)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\fastapi\applications.py", line 1054, in __call__
    |     await super().__call__(scope, receive, send)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\applications.py", line 123, in __call__
    |     await self.middleware_stack(scope, receive, send)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\errors.py", line 186, in __call__
    |     raise exc
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\errors.py", line 164, in __call__
    |     await self.app(scope, receive, _send)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 189, in __call__
    |     with collapse_excgroups():
    |   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 158, in __exit__
    |     self.gen.throw(value)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_utils.py", line 93, in collapse_excgroups
    |     raise exc
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 191, in __call__
    |     response = await self.dispatch_func(request, call_next)
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\api\__init__.py", line 84, in authorization
    |     return await call_next(request)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 165, in call_next
    |     raise app_exc
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 151, in coro
    |     await self.app(scope, receive_or_disconnect, send_no_error)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
    |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    |     raise exc
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    |     await app(scope, receive, sender)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 756, in __call__
    |     await self.middleware_stack(scope, receive, send)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 776, in app
    |     await route.handle(scope, receive, send)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 297, in handle
    |     await self.app(scope, receive, send)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 77, in app
    |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    |     raise exc
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    |     await app(scope, receive, sender)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 72, in app
    |     response = await func(request)
    |                ^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\fastapi\routing.py", line 278, in app
    |     raw_response = await run_endpoint_function(
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
    |     return await dependant.call(**values)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\api\__init__.py", line 161, in chat_completions
    |     return JSONResponse((await response).to_json())
    |                          ^^^^^^^^^^^^^^
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\client\async_client.py", line 63, in iter_append_model_and_provider
    |     async for chunk in response:
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\client\async_client.py", line 37, in iter_response
    |     async for chunk in response:
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\Provider\needs_auth\OpenaiChat.py", line 419, in create_async_generator
    |     await raise_for_status(response)
    |   File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\requests\raise_for_status.py", line 28, in raise_for_status_async
    |     raise ResponseStatusError(f"Response {response.status}: {message}")
    | g4f.errors.ResponseStatusError: Response 401: {"detail":"Unauthorized"}
    +------------------------------------

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\uvicorn\protocols\http\httptools_impl.py", line 411, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\uvicorn\middleware\proxy_headers.py", line 69, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\errors.py", line 186, in __call__
    raise exc
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 189, in __call__
    with collapse_excgroups():
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(value)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_utils.py", line 93, in collapse_excgroups
    raise exc
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 191, in __call__
    response = await self.dispatch_func(request, call_next)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\api\__init__.py", line 84, in authorization
    return await call_next(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 165, in call_next
    raise app_exc
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\base.py", line 151, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\starlette\routing.py", line 72, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\fastapi\routing.py", line 278, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\api\__init__.py", line 161, in chat_completions
    return JSONResponse((await response).to_json())
                         ^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\client\async_client.py", line 63, in iter_append_model_and_provider
    async for chunk in response:
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\client\async_client.py", line 37, in iter_response
    async for chunk in response:
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\Provider\needs_auth\OpenaiChat.py", line 419, in create_async_generator
    await raise_for_status(response)
  File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\requests\raise_for_status.py", line 28, in raise_for_status_async
    raise ResponseStatusError(f"Response {response.status}: {message}")
g4f.errors.ResponseStatusError: Response 401: {"detail":"Unauthorized"}

Any idea what does it mean? I tried to place the .har file inside:

C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\Scripts

But the error indicates something was unauthorized? I used a single space for the api key like you said (and tried also without it and I get the same error). That seems to be inline with the error that I get in OpenaiChat.py:

if response.status == 401:
                    raise MissingAuthError('Add a "api_key" or a .har file' if cls._api_key is None else "Invalid api key")

Thanks.

@plia7
Copy link
Author

plia7 commented May 10, 2024

@hlohaus @iG8R Could it be a problem with my ChatGPT account? Could it be I got shadow banned or something where the .HAR that gets generated doesn't work anymore? Is it possible? Does the error I get indicates it can't find the .HAR file or could it be it does find it, but the content inside the file can't be used? Would there be a different error in that case?

The code seem to point to this error:

if response.status == 401:
                    raise MissingAuthError('Add a "api_key" or a .har file' if cls._api_key is None else "Invalid api key")

So I get status 401 unauthorized response.

Moreover, I noticed that if I go to the following URL/login to my chatgpt account:
https://platform.openai.com/playground/chat
Enter my question in the “User” text box, just as you would if you were chatting on the regular chat.openai website, then press the submit button on the bottom left, I would get the message:

"You've reached your usage limit. See your [usage dashboard](https://platform.openai.com/account/usage) and [billing settings](https://platform.openai.com/account/billing) for more details. If you have further questions, please contact us through our help center at help.openai.com."

But other people told me they are able to use this too, and provided a screenshot:

https://imgur.com/a/P4YeuA4

So I wonder if this is related and part of the root cause to the problem that I experience with the .har file (401 unauthorized response), what do you think?

Thanks.

@plia7
Copy link
Author

plia7 commented May 10, 2024

@hlohaus @iG8R Ok in one environment, I was able to get this to work with a new .HAR I just downloaded, so it must be something with my other two environments. It doesn't seem like a problem with my ChatGPT account.

But why in two environments it would get screwy like that out of the blue? Everything was working fine prior to that, it doesn't make any sense.

@hlohaus It doesn't make sense that I need to re-create the environment, re-install python and g4f just to get this to work? Why would it work in one environment with the same .HAR file but then it would fail in another environment with the same .HAR file?

Could you please fix it, so it's environment agnostic and never fails? Is there a way to force it to use the .HAR file somehow? Can I modify some code to tell it to use the file directly?

Could you please direct me what to change and try it and see if it fixes the problem? I can modify:

C:\users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\Provider\needs_auth\OpenaiChat.py

Just tell me what to change to force it to read my .HAR file that's located in:

C:\Users\me\Documents\chat.openai.com.har

Thanks.

@hlohaus
Copy link
Collaborator

hlohaus commented May 12, 2024

Please enable the logging or debug mode. This will allow you to view the .har files that are loaded in the log.

@plia7
Copy link
Author

plia7 commented May 12, 2024

Please enable the logging or debug mode. This will allow you to view the .har files that are loaded in the log.

How do I do that?
I run my script like this:

python3 chatgpt.py

So where do I enable logging for it?

Thanks.

@iG8R
Copy link

iG8R commented May 12, 2024

@plia7

So where do I enable logging for it?

Read once more my post #1896 (comment) and repeat it exactly as it was described.

@plia7
Copy link
Author

plia7 commented May 12, 2024

@plia7

So where do I enable logging for it?

Read once more my post #1896 (comment) and repeat it exactly as it was described.

@iG8R Sorry but I don't want to recreate my environment from scratch, I have other things there that I want to keep.

@iG8R
Copy link

iG8R commented May 12, 2024

@plia7
All these steps do not change your environment in any way, which is why I wrote them there. Once everything is done, all you have to do is delete the g4f directory.
Also there you will find the answer to the question "So where do I enable logging?".

@plia7
Copy link
Author

plia7 commented May 12, 2024

@plia7

All these steps do not change your environment in any way, which is why I wrote them there. Once everything is done, all you have to do is delete the g4f directory.

Also there you will find the answer to the question "So where do I enable logging?".

I'm running it on a linux x86 emulator which is very slow to uninstall/install g4f.

@plia7
Copy link
Author

plia7 commented May 18, 2024

@hlohaus @iG8R I enabled logging but I don't see it shows what .har it uses.

I enabled it by adding the logging as follows to my script:

g4f.debug.logging = True # enable logging

Any other things I can add to my script that can shed some light why this issue only happens with this environment?

Please don't tell me to reinstall the environment to set up a new virtual environment, I have other stuff in it I want to keep and it's too slow to reinstall it. There must be other ways to get this to work.

Thanks.

g4f.Provider.Ails supports: (
    model: str,
    messages: Messages,
    stream: bool,
    proxy: str = None,
)

You: test
Using OpenaiChat provider

GPT: Traceback (most recent call last):
  File "/root/mnt/docs/chatgpt.py", line 67, in <module>
    for chunk in stream:
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 216, in create_completion
    yield loop.run_until_complete(await_callback(gen.__anext__))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 45, in await_callback
    return await callback()
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 411, in create_async_generator
    cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 200, in get_default_model
    raise MissingAuthError('Add a "api_key" or a .har file' if cls._api_key is None else "Invalid api key")
g4f.errors.MissingAuthError: Add a "api_key" or a .har```

@hlohaus
Copy link
Collaborator

hlohaus commented May 18, 2024

Yo, I'll create a function that sets the cookies dir for you. This way, it won't search for files in the wrong directory. Sound good?

@plia7
Copy link
Author

plia7 commented May 18, 2024

Yo, I'll create a function that sets the cookies dir for you. This way, it won't search for files in the wrong directory. Sound good?

Perfect, I appreciate it.
I'm sure it could benefit other users when the environment goes "corrupt" like this.

Please let me know if I can do anything to help to test the function or the new package version when it comes out to see it fixes the issue for me.

Thanks.

@hlohaus
Copy link
Collaborator

hlohaus commented May 18, 2024

Hey, I just dropped the new version! Check out the readme for an example of how to use the set cookies dir function.

@plia7
Copy link
Author

plia7 commented May 19, 2024

Hey, I just dropped the new version! Check out the readme for an example of how to use the set cookies dir function.

I just tried it, and this is the message that I get now:

me:~# chatgpt
Read .har file: /root/mnt/docs/har_and_cookies/chat.openai.com.har

You: hello world
Using OpenaiChat provider

GPT: Traceback (most recent call last):
  File "/root/mnt/docs/chatgpt.py", line 72, in <module>
    for chunk in stream:
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 216, in create_completion
    yield loop.run_until_complete(await_callback(gen.__anext__))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/usr/lib/python3.9/site-packages/g4f/providers/base_provider.py", line 45, in await_callback
    return await callback()
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 378, in create_async_generator
    cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 197, in get_default_model
    cls._update_request_args(session)
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 706, in _update_request_args
    cls._cookies[c.key if hasattr(c, "key") else c.name] = c.value
TypeError: 'NoneType' object does not support item assignment

I only placed the .har file inside the folder (I never needed any other files like cookies and it works fine in another environment just with this same .har file so not sure why it's complaining about cookies or something).

This is the beginning of my script file:

from g4f.client import Client
from g4f.Provider import OpenaiChat
import os.path
from g4f.cookies import set_cookies_dir, read_cookie_files
import g4f.debug

g4f.debug.logging = True # enable logging
cookies_dir = os.path.join(os.path.dirname(__file__), "har_and_cookies")
set_cookies_dir(cookies_dir)
read_cookie_files(cookies_dir)
.
.
.

I don't think you need cookies files for ChatGPT provider?

Thanks.

@hlohaus
Copy link
Collaborator

hlohaus commented May 19, 2024

Alright. It now finds the .har file. But I don't think it's valid. Do you have an error message? Did you track a chat action? Did you reload the website after opening the browser console?

@plia7
Copy link
Author

plia7 commented May 19, 2024

Alright. It now finds the .har file. But I don't think it's valid. Do you have an error message? Did you track a chat action? Did you reload the website after opening the browser console?

Yes I did reload the website and track a chat action when fetching the .har file.
The same file works in my other working environment. So I believe it's not a problem with the .har file that I'm using but still a potential problem with the current environment.

The error which can be seen in the stack trace I posted in previous comment is:

    cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 197, in get_default_model
    cls._update_request_args(session)
  File "/usr/lib/python3.9/site-packages/g4f/Provider/needs_auth/OpenaiChat.py", line 706, in _update_request_args
    cls._cookies[c.key if hasattr(c, "key") else c.name] = c.value
TypeError: 'NoneType' object does not support item assignment

So looks like it fails here:

cls._cookies[c.key if hasattr(c, "key") else c.name] = c.value

TypeError: 'NoneType' object does not support item assignment

It tries to assign a NoneType object to the cookies dictionary?

But why does it even try to go to this code if I didn't provide any cookies and it's not applicable to ChatGPT? Please correct me if I'm wrong.

Thanks.

@hlohaus
Copy link
Collaborator

hlohaus commented May 19, 2024

The error occurs only when no .har file is found. Consider using OpenaiAccount instead, as it does not check for the free service.

@plia7
Copy link
Author

plia7 commented May 19, 2024

The error occurs only when no .har file is found. Consider using OpenaiAccount instead, as it does not check for the free service.

@hlohaus Sorry I'm a little bit confused:

  • My Chatgpt account is free. It was ChatGPT 3.5 and was recently upgraded for free to the new ChatGPT 4.o.

  • The same .har file works fine in another working environment.

  • The g4f debugging log (with the feature you added) indicates it did find the .har file, so how the new/last error indicates that no .har file was found? Isn't it contradicting?

Thanks.

@hlohaus
Copy link
Collaborator

hlohaus commented May 19, 2024

The cookies are also included in the .har file. Your last error message indicates that the cookies are not available and cannot be updated.

@plia7
Copy link
Author

plia7 commented May 19, 2024

The cookies are also included in the .har file. Your last error message indicates that the cookies are not available and cannot be updated.

Ok but as I mentioned the same .har file works fine in another working environment.
So it's not a problem with the .har file or the cookies in it.
It's still a code problem, it shouldn't fail on loading the .har/cookies file that's working fine in another environment.

Thanks.

@plia7
Copy link
Author

plia7 commented May 20, 2024

The cookies are also included in the .har file. Your last error message indicates that the cookies are not available and cannot be updated.

Ok but as I mentioned the same .har file works fine in another working environment.
So it's not a problem with the .har file or the cookies in it.
It's still a code problem, it shouldn't fail on loading the .har/cookies file that's working fine in another environment.

Thanks.

Do you agree @hlohaus ?
I don't understand why you keep telling me to get a new environment, virtual environment, new chatgpt account, free account, paid account.
I know this .har works in another environment with same g4f package. So for whatever reason it doesn't work in this environment, that means it's not a setup issue. This supposed "bad environment" is exposing an issue/bug with the g4f package and/or the way it's coded.

Why I don't have this issue with other packages that I use?

Could you please fix it?

Thanks.

@iG8R

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants
@hlohaus @iG8R @xtekky @plia7 and others