Unknown Issue with Calling API / Getting Respone

Hello! I’m new to coding and trying to create a speech based AI-assistant. This is my first major project and would appreciate any help!

I am getting the error that OpenAI doesn’t have a “chat” command and thats why it isn’t working, but unsure what the latest call function is. Code pasted below:

import speech_recognition as sr
import pyttsx3
import os
import time
from dotenv import load_dotenv
from openai import OpenAI

Load environment variables and set the API key for OpenAI

load_dotenv()
OpenAI_Key = os.getenv(‘OPENAI_KEY’)
OpenAI_api_key = OpenAI_Key

def SpeakText(command):
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()

r = sr.Recognizer()
mic_index = 1 # Ensure this is the correct microphone index

def record_text():
try:
with sr.Microphone(device_index=mic_index) as source2:
r.adjust_for_ambient_noise(source2, duration=0.2)
print(“I’m listening”)
audio2 = r.listen(source2)
return r.recognize_google(audio2)
except sr.RequestError as e:
print(f"Could not request results; {e}")
except sr.UnknownValueError:
print(“Unknown error occurred”)
return “”

def send_to_chatGPT(messages, model=“gpt-3.5-turbo”):
try:
response = OpenAI.chat.completions.create(
model=model,
messages=messages,
max_tokens=100,
n=1,
stop=None,
temperature=0.5,
)
message = response.choices[0].message[‘content’]
messages.append(response.choices[0].message)
return message

except Exception as e:
    print(f"Error in sending request to OpenAI: {e}")
    return "I'm having trouble connecting to the server right now."

messages =

while True:
text = record_text()
if text:
print(f"You said: {text}")
messages.append({“role”: “user”, “content”: text})
response = send_to_chatGPT(messages)
SpeakText(response)
print(response)

# Wait for 10 seconds after each request, regardless of success
time.sleep(10)

Thank you!