TypeError: OpenAI.__init__() takes 1 positional argument but 2 were given

Hello. I am new here. I am trying to learn how can i use openai api. But for this basic beginner code:

from openai import OpenAI

client = OpenAI(“sk-gMAw****************”)

chat_completion = client.chat.completions.create(
model=“gpt-3.5-turbo”,
messages=[{“role”: “user”, “content”: “Hello world”}])

I have got this error message:

client = OpenAI("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■SGLtuCcD")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

TypeError: OpenAI.init() takes 1 positional argument but 2 were given

Could you help me, pls?

1 Like

Welcome to the dev forum @ayseguldalgic

You can either provide an api_key keyword argument using:

client = OpenAI(api_key="Your API Key")

Or use the recommended library python-dotenv to add OPENAI_API_KEY=“My API Key” to your .env file so that your API Key is not stored in source control.

In this case once you have loaded the .env file with the environment variable mapped to the API key, simply use:

client = OpenAI()
5 Likes

It works now. Thanks for your quick response :cherry_blossom:

2 Likes