Why can't I use openai?
Hello,
I'm currently programming a voice assistant and have uploaded the code from my laptop to the Raspberry Pi. Everything worked fine on the laptop, and even if I say "Hello" here, I get a good response, but as soon as I ask something that GPT-4 is supposed to answer, I get this error message:
Say something…
Text: when was the Second World War
Recognized text: when was the Second World War
Traceback (most recent call last):
File "/tmp/voiceAssistant/main.py", line 76, in <module>
execute()
File "/tmp/voiceAssistant/main.py", line 71, in execution
response = openai_request(user_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/voiceAssistant/main.py", line 42, in openai_request
response = openai.Completion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/grafjulian08/Downloads/interpreter/lib/python3.11/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.Completion, but this is no longer supported in openai>=1.0.0 – see the README at https://github.com/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, eg `pip install openai==0.28`
A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
Process finished with exit code 1
My code looks like this:
import openai import pyttsx3 import re import pyjokes import speech_recognition as sr from pyowm import OWM import spotipy from spotipy.oauth2 import SpotifyOAuth import time import random import datetime import pytz from geopy.geocoders import Nominatim from timezonefinder import TimezoneFinder openai.api_key = 'sk-...' model_id = "gpt-4" engine = pyttsx3.init() def recognize_speech(): recognizer = sr.Recognizer() while True: with sr.Microphone() as source: print("Sage etwas...") audio = recognizer.listen(source, timeout=15) try: text = recognizer.recognize_google(audio, language="de-DE") print("Text: " + text) return text except sr.UnknownValueError: return "not_understood" except sr.RequestError as e: print(f"Fehler bei der Anfrage an die Google Web Speech API: {e}") return "" def openai_request(prompt): response = openai.Completion.create( engine=model_id, prompt=prompt, max_tokens=50 ) return response.choices[0].text.strip() def speak(text): engine.say(text) engine.runAndWait() def execute(): while True: user_input = recognize_speech() print("Erkannter Text:", user_input) if user_input == "not_understood": speak("Entschuldigung! Das habe ich nicht verstanden.") break elif not user_input: break elif "hallo" in user_input: speak('Hallo! Womit kann ich dir behilflich sein?') else: response = openai_request(user_input) speak(response) if __name__ == '__main__': execute()
I also installed openai (pip install openai) but it still doesn't work.
Thank you in advance
Yes is why it doesn’t work
You use an outdated version
Yes, the problem is that I’ve tried both (migrate and openai==0.28) but it always comes this mistake