xtekky / gpt4free

The official gpt4free repository | various collection of powerful language models
https://g4f.ai
GNU General Public License v3.0
59.06k stars 13.16k forks source link

OpenAi Provider Error (add api_key and I've added it already) #1993

Open noneherel opened 1 month ago

noneherel commented 1 month ago

this is my code:

from g4f.client import Client as client_ai
from g4f.Provider import OpenaiChat

client = client_ai(provider=OpenaiChat)
response = client.chat.completions.create(
    api_key='sk-proj-XvUaNpdorLWIbFWhq0ctT...etc',
    model="gpt-4-turbo",
    messages=[{"role": "user", "content": 'how are you?'}],
)
mess= response.choices[0].message.content
print(mess)

this is the error i got:

Traceback (most recent call last):
  File "d:\VisualStudio\AI_tests\chatting.py", line 19, in <module>
    response = client.chat.completions.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\client\client.py", line 114, in create
    return response if stream else next(response)
                                   ^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\client\client.py", line 53, in iter_append_model_and_provider
    for chunk in response:
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\client\client.py", line 28, in iter_response
    for idx, chunk in enumerate(response):
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\providers\base_provider.py", line 216, in create_completion  
    yield loop.run_until_complete(await_callback(gen.__anext__))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\providers\base_provider.py", line 45, in await_callback
    return await callback()
           ^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\Provider\needs_auth\OpenaiChat.py", line 379, in create_async_generator
    cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\Provider\needs_auth\OpenaiChat.py", line 199, 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
hlohaus commented 1 month ago

If you've got a real API key, you can use the Openai provider. OpenAI Chat is for free access to Web Chat GPT.

Anuragmukati commented 1 month ago

`from g4f.client import Client as client_ai from g4f.Provider import OpenaiChat

Initialize the client with the provider and API key

client = client_ai(provider=OpenaiChat, api_key='sk-proj-XvUaNpdorLWIbFWhq0ctT...etc')

Create a completion

response = client.chat.completions.create( model="gpt-4-turbo", messages=[{"role": "user", "content": 'how are you?'}], )

Extract and print the message content

mess = response.choices[0].message.content print(mess) `

try this... Maybe it works

hlohaus commented 1 month ago

Try this:

client = client_ai(provider=Openai, api_key='sk-proj-XvUaNpdorLWIbFWhq0ctT...etc')

Not: OpenaiChat

noneherel commented 1 month ago

``> Try this:

client = client_ai(provider=Openai, api_key='sk-proj-XvUaNpdorLWIbFWhq0ctT...etc')

Not: OpenaiChat

i tried this and i got this error:

g4f.errors.ResponseStatusError: Response 404: {
    "error": {
        "message": "The model `gpt-4-turbo` does not exist or you do not have access to it.",
        "type": "invalid_request_error",
        "param": null,
        "code": "model_not_found"
    }
}

any model i put in models it gives me this error, and then i tried this

client = client_ai(provider=Openai, api_key='sk-proj-qhL1QHdCQV7XyQW0kL...')
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": 'how are you?'}],
)

without (turbo) and i got this error:

g4f.errors.ResponseStatusError: Response 404: {
    "error": {
        "message": "The model `gpt-4` does not exist or you do not have access to it.",
        "type": "invalid_request_error",
        "param": null,
        "code": "model_not_found"
    }
}

the last thing i tried is (gpt-3.5-turbo) model and then i got this error:

g4f.errors.RateLimitError: Response 429: Rate limit reached
noneherel commented 1 month ago

`from g4f.client import Client as client_ai from g4f.Provider import OpenaiChat

Initialize the client with the provider and API key

client = client_ai(provider=OpenaiChat, api_key='sk-proj-XvUaNpdorLWIbFWhq0ctT...etc')

Create a completion

response = client.chat.completions.create( model="gpt-4-turbo", messages=[{"role": "user", "content": 'how are you?'}], )

Extract and print the message content

mess = response.choices[0].message.content print(mess) `

try this... Maybe it works

thank you for your reply but it didn't work )':

mak448a commented 1 month ago

Sorry to bother you, but I'm having the same error. I grabbed my token from https://chatgpt.com/api/auth/session and stuck in my access token like this:

response = await g4f.ChatCompletion.create_async(
        model=g4f.models.gpt_35_turbo,
        provider=g4f.Provider.OpenaiChat,
        messages=messages,
        access_token=chatgpt_key_taken_from_chatgpt,
        stream=False
)

and then I got the error. g4f.errors.MissingAuthError: Add a "api_key" or a .har file

Am I doing something wrong @hlohaus?

dayajiji commented 1 month ago

Same

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

github-actions[bot] commented 1 week ago

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

mak448a commented 1 week ago

Stop closing this issues, please. bump.

github-actions[bot] commented 2 hours ago

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

mak448a commented 2 hours ago

BUMP!!!