A beginner needs help won't connect to openAI / openai.ChatCompletion.create

Hi everybody,

Thanks for takinga a look at my problem. So I’m new to this and eager to learn. My code below is giving me lots of errors in VS Code. I installed several packages and I put my API key in a different .env file. If I create another file and call on the secret Key I manage to show it in the prompt so the key should be fine. This is my code:

import openai
import os
from dotenv import load_dotenv

# Load the API-Key from the .env file
load_dotenv(dotenv_path=“TextKey.env”)
api_key = os.getenv(“OPENAI_API_KEY”)

# Create an OPEN AI client with the correct API-Key
client = openai.OpenAI(api_key=api_key)

def chat():

  • print(“Hi! I’m an AI chatbot. Type stop, if you want to stop.”)*

  • while True:*

  •    user_input = input("You: ")*
    
  •    if user_input.lower() == "stop":*
    
  •        print("Chatbot: Goodbye!")*
    
  •        break*
    
  •    response = openai.ChatCompletion.create(*
    
  •        model="gpt-4",  # Using the newest model*
    
  •        messages=[{"role": "user", "content": user_input}]*
    
  •    )*
    
  •    print("Chatbot:", response["choices"][0]["message"]["content"])*
    

if name == “main”:

  • chat()*

Am I that terribly wrong? Several topics with “answers” don’t match anything what I’m doing.
These are my errors:

Hi! I’m an AI chatbot. Type stop, if you want to stop.
You: test
Traceback (most recent call last):
File “C:\Users*\TryOutChatbot.py", line 30, in
chat()
~~~~^^
File "C:\Users*
\TryOutChatbot.py”, line 22, in chat
response = openai.ChatCompletion.create(
model=“gpt-4”, # Using the newest model
messages=[{“role”: “user”, “content”: user_input}]
)
File “C:\Users*****\AppData\Local\Programs\Python\Python313\Lib\site-packages\openai\lib_old_api.py”, line 39, in call
raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at ://github. /openai/openai-python for the API.

You can run openai migrate to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. pip install openai==0.28

A detailed migration guide is available here: //github. /openai/openai-python/discussions/742