Getting AttributeError with OpenAI library - missing module attributes

Issue with OpenAI Python Library

I’m encountered some challenges while using the OpenAI Python package. Even though I updated to the latest version, it seems like certain functions are still absent. I attempted to uninstall and reinstall the package, yet the problem persists.

As I’m relatively new to Python, I may be overlooking something basic. This code is sourced from an online tutorial.

import openai
import pyttsx3
import speech_recognition as sr
from config import SECRET_KEY

openai.api_key = SECRET_KEY

tts_engine = pyttsx3.init()
speech_rec = sr.Recognizer()
audio_input = sr.Microphone(device_index=0)

chat_history = ""
user_label = "User"
ai_label = "Assistant"

while True:
    with audio_input as source:
        print("\nListening for input...")
        speech_rec.adjust_for_ambient_noise(source, duration=0.3)
        recorded_audio = speech_rec.listen(source)
    print("Processing audio...\n")

    try:
        spoken_text = speech_rec.recognize_google(recorded_audio)
    except:
        continue

    message = user_label + ": " + spoken_text + "\n" + ai_label + ": "
    chat_history += message

    # Call OpenAI API
    api_response = openai.Completion.create(
        engine='text-davinci-003', 
        prompt=chat_history, 
        max_tokens=150
    )
    
    reply = api_response["choices"][0]["text"].replace("\n", "")
    reply = reply.split(user_label + ": ", 1)[0].split(ai_label + ": ", 1)[0]

    chat_history += reply + "\n"
    print(reply)

    tts_engine.say(reply)
    tts_engine.runAndWait()

Update: I resolved the initial issue, but now I’m facing a different error related to audio source management. It seems there are issues with the microphone context manager. Any advice would be greatly appreciated as I continue to learn.

Your code’s using the old OpenAI API syntax that’s been deprecated. That openai.Completion.create() method doesn’t exist anymore in newer versions. I hit this exact problem last month on a similar project. You’ll need to update to the current API structure - swap out that API call for the new chat completions endpoint and fix the client initialization. Also, text-davinci-003 is deprecated now, so switch to gpt-3.5-turbo or gpt-4. Your audio processing looks good though, so it’s definitely the OpenAI integration causing issues.

Had this exact same issue recently. The tutorial you’re following is probably outdated - OpenAI completely changed their SDK around version 1.0. Here’s what fixed it for me: completely uninstall the old version with pip uninstall openai, then install the latest with pip install openai>=1.0.0. You’ll need to restructure your API calls too - ditch the old completion method and switch to chat completions format since that’s what replaced the old endpoints. The response structure changed as well, so you’ll have to adjust how you pull text from the API response. I had to rewrite a bunch of scripts when this happened, but honestly the new format’s cleaner once you get the hang of it.

you’re using outdated openai syntax. the completion.create method was deprecated a while back. run pip install openai --upgrade and switch to the new chatcompletion format. unfortunately, most online tutorials still show the old api calls.