AttributeError: module 'openai' has no attribute 'OpenAI'

Hi , i have very simple script, when i ran the script it has error “no attribute OpenAI”

Initialize the OpenAI client

client = openai.OpenAI()

Error:- AttributeError: module ‘openai’ has no attribute ‘OpenAI’

Installed openAI version
Name: openai
Version: 0.28.0

//B

Hi! Welcome to the forum!

Did you check out the documentation?

https://platform.openai.com/docs/api-reference/introduction?lang=python

If you still struggle, do you wanna post the whole code sample?

Thanks for quick help! here is the code where i see the error

import os
import openai
import time
import csv
from tqdm import tqdm

Set your OpenAI API key

OPENAI_API_TOKEN = “YOUR_API_KEY_HERE”
os.environ[“OPENAI_API_KEY”] = OPENAI_API_TOKEN

Initialize the OpenAI client

client = openai.OpenAI() —> Error in this line

Function to upload a file to OpenAI

def upload_file(file_path, purpose):
with open(file_path, “rb”) as file:
response = client.files.create(file=file, purpose=purpose)
return response.id

oof

well…

1

you’re way behind on your openai version for that code. current is >1 : pip install -U openai

2

this is the example if you follow the docs to github:

import os
from openai import OpenAI

client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"), #you can put the key here directy
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="gpt-3.5-turbo",
)

:grimacing:

but I think that should help you get started!

2 Likes

It worked :smile:
Thank you for your support!

2 Likes