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

CaptchaError: Bing: Retry: CaptchaChallenge: User needs to solve CAPTCHA to continue. #1955

Open
leekery opened this issue May 15, 2024 · 11 comments
Assignees
Labels
bug Something isn't working stale

Comments

@leekery
Copy link

leekery commented May 15, 2024

Bug description
When utilizing GPT-4 Turbo through Bing in async mode and with the parameter cookies={"_U":"my_cookies_from_Bing_here"}, the error "Bing: Retry: CaptchaChallenge: User needs to solve CAPTCHA to continue" frequently appears.

Screenshots
image

Environment

  • python==3.11.8
  • g4f==0.3.1.1
@leekery leekery added the bug Something isn't working label May 15, 2024
@hlohaus
Copy link
Collaborator

hlohaus commented May 16, 2024

You don't need cookies for Bing, but if you do add some, you should add all of them and maybe solve the captcha in the browser.

@MeltingObsidian
Copy link

You don't need cookies for Bing, but if you do add some, you should add all of them and maybe solve the captcha in the browser.

@hlohaus even without cookies, I too am getting the same error. I have updated the library to the latest version. According to me, for conversation, Bing shouldn't need any cookies. It will still need them for image generation though.

@MeltingObsidian
Copy link

MeltingObsidian commented May 18, 2024

You don't need cookies for Bing, but if you do add some, you should add all of them and maybe solve the captcha in the browser.

@hlohaus even without cookies, I too am getting the same error. I have updated the library to the latest version. According to me, for conversation, Bing shouldn't need any cookies. It will still need them for image generation though.

This is my code. Pretty simple and just for an initial test of the library's working

import g4f

prompt = "Hello"

completion = g4f.ChatCompletion.create(
    model=g4f.models.gpt_4, 
    messages=[{"role": "user", "content": prompt}],
)

print(completion)

The installations I did:

pip install g4f[all] and when that didn't work, I tried pip install g4f[webdriver] (still got the same error)

Python version: 3.12.3

The error I got was:

Traceback (most recent call last):
  File "c:\[PROJECT_DIRECTORY]\app.py", line 5, in <module>
    completion = g4f.ChatCompletion.create(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\__init__.py", line 68, in create
    return result if stream else ''.join([str(chunk) for chunk in result])
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\providers\retry_provider.py", line 214, in create_completion        
    yield from super().create_completion(model, messages, stream, **kwargs)
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\providers\retry_provider.py", line 72, in create_completion
    raise_exceptions(exceptions)
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\providers\retry_provider.py", line 337, in raise_exceptions
    raise RetryProviderError("RetryProvider failed:\n" + "\n".join([
g4f.errors.RetryProviderError: RetryProvider failed:
Liaobots: RateLimitError: Response 402: Rate limit reached
Bing: RateLimitError: CaptchaChallenge: Use other cookies or/and ip address

@hlohaus
Copy link
Collaborator

hlohaus commented May 19, 2024

When the default cookies don't work, try passing your own cookies. Also, you should pass the captcha in this session. I don't need to solve a captcha with my IP address.

@MeltingObsidian
Copy link

I tried passing my own cookies as well (multiple times from different accounts, in fact!)

This is the code:

from g4f.client import Client
from g4f import Provider, models
from g4f import set_cookies

client = Client()

set_cookies('.bing.com', {
    '_U': '[BING_COOKIES]'
})

conversation = []

conversation.append({'role': 'user', 'content': 'Hello, how are you?'})

completion = client.chat.completions.create(
    model=models.gpt_4, 
    messages=conversation,
    ignored=["Ylokh", "GptGo", "AItianhu", "Aibn", "Myshell", "FreeGpt" ],
)

print(completion.choices[0].message.content)

I got this error, which I don't think is any different from the one I got earlier:

Traceback (most recent call last):
  File "c:\[PROJECT_DIRECTORY]\app.py", line 15, in <module>
    completion = client.chat.completions.create(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\client\client.py", line 114, in create
    return response if stream else next(response)
                                   ^^^^^^^^^^^^^^
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\client\client.py", line 53, in iter_append_model_and_provider       
    for chunk in response:
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\client\client.py", line 28, in iter_response
    for idx, chunk in enumerate(response):
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\providers\retry_provider.py", line 68, in create_completion
    raise_exceptions(exceptions)
  File "C:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\providers\retry_provider.py", line 324, in raise_exceptions
    raise RetryProviderError("RetryProvider failed:\n" + "\n".join([
g4f.errors.RetryProviderError: RetryProvider failed:
Bing: RateLimitError: CaptchaChallenge: Use other cookies or/and ip address
Liaobots: RateLimitError: Response 402: Rate limit reached

I have also updated the library to the latest version (0.3.1.6)

I also have a www.bing.com.har file in a folder named har_and_cookies in the project directory (updated today)
Still, I don't seem to get it working

@MeltingObsidian
Copy link

@hlohaus About the captcha challenge you talked about; I cannot see any captcha in my browser. I have used the EdgeGPT library in the past (It is now archived and does not receive any updates anymore). After many API based conversations, I too had to open the browser and solve the captcha myself. But I don't see any such captcha in my browser now. Maybe it's something else which gets caught as a captcha challenge error

@hlohaus
Copy link
Collaborator

hlohaus commented May 19, 2024

You need to pass the cookies as a dictionary here in Bing:

completion = client.chat.completions.create(
    ...
    cookies={
        "Name1": "value1",
        "Name2": "value2",
        ...
    }
)

It doesn't use the get_cookies function.

You could use it to pass the cookies from file:

from g4f.cookies import get_cookies

completion = client.chat.completions.create(
    ...
    cookies=get_cookies(".bing.com"),
)

@MeltingObsidian
Copy link

Well, I added this line to the chat completion function:
cookies=get_cookies('.bing.com'),

Still, I get the exact same error

@MeltingObsidian
Copy link

@hlohaus Hey, it started working somehow. cheers! But now there is the issue that it does not work with streamlit
I got this error:

Failed to check g4f version: Failed to get PyPI version: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /pypi/g4f/json (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001D817C8AB70>: Failed to resolve 'pypi.org' ([Errno 11001] getaddrinfo failed)"))
2024-05-19 13:04:34.642 Uncaught app exception
Traceback (most recent call last):
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\connector.py", line 1203, in _create_direct_connection
    hosts = await self._resolve_host(host, port, traces=traces)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\connector.py", line 880, in _resolve_host
    return await asyncio.shield(resolved_host_task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\connector.py", line 917, in _resolve_host_with_throttle
    addrs = await self._resolver.resolve(host, port, family=self._family)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\resolver.py", line 33, in resolve
    infos = await self._loop.getaddrinfo(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 901, in getaddrinfo
    return await self.run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\Lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\Lib\socket.py", line 963, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11004] getaddrinfo failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 600, in _run_script    
    exec(code, module.__dict__)
  File "C:\[PROJECT_DIRECTORY]\app.py", line 23, in <module>
    converse(prompt)
  File "C:\[PROJECT_DIRECTORY]\model.py", line 22, in converse
    completion = client.chat.completions.create(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\client\client.py", line 114, in create
    return response if stream else next(response)
                                   ^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\client\client.py", line 53, in iter_append_model_and_provider
    for chunk in response:
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\client\client.py", line 28, in iter_response
    for idx, chunk in enumerate(response):
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\providers\base_provider.py", line 216, in create_completion
    yield loop.run_until_complete(await_callback(gen.__anext__))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 687, in run_until_complete     
    return future.result()
           ^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\providers\base_provider.py", line 45, in await_callback
    return await callback()
           ^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\Provider\Bing.py", line 421, in stream_generate
    conversation = await create_conversation(session, headers, tone)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\g4f\Provider\bing\conversation.py", line 39, in create_conversation
    async with session.get(url, headers=headers) as response:
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\client.py", line 1197, in __aenter__
    self._resp = await self._coro
                 ^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\client.py", line 581, in _request
    conn = await self._connector.connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\connector.py", line 544, in connect
    proto = await self._create_connection(req, traces, timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\connector.py", line 944, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\[PROJECT_DIRECTORY]\.venv\Lib\site-packages\aiohttp\connector.py", line 1209, in _create_direct_connection
    raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host www.bing.com:443 ssl:default [getaddrinfo failed]

Any idea on how to fix this?

@hlohaus
Copy link
Collaborator

hlohaus commented May 19, 2024

This error appears to be related to a DNS resolution issue. It is possible that there is a problem with your internet connection or DNS settings.

Copy link

Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.

@github-actions github-actions bot added the stale label May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

4 participants