Hallo,
ich programmiere gerade einen Sprachassistenten. Das ist der Code dafür:
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
import sounddevice as sd
import pvporcupine
import openai
openai.api_key = 'sk-...'
model_id = "gpt-4"
engine = pyttsx3.init()
def recognize_speech(recognizer, 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(
model=model_id,
messages=[{"role": "user", "content": prompt}],
max_tokens=150
)
api_usage = response['usage']
print('Total Token consumed: {0}'.format(api_usage['total_tokens']))
prompt += response.choices[0].message['content']
return prompt
def speak(text=None):
if text:
engine.say(text)
engine.runAndWait()
def hey_luna_in_q(q):
return "hey luna" in q.lower()
def wait_for_wake_word(recognizer, source):
while True:
print("Warte auf Wake-Word...")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio, language="de-DE")
if hey_luna_in_q(text):
print("Wake-Word erkannt!")
return
except sr.UnknownValueError:
pass
except sr.RequestError as e:
print(f"Fehler bei der Anfrage an die Google Web Speech API: {e}")
def execute():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
wait_for_wake_word(recognizer, source)
while True:
q = recognize_speech(recognizer, source)
print("Erkannter Text:", q)
if q == "not_understood":
speak("Entschuldigung! Das habe ich nicht verstanden.")
continue
if "hallo" in q.lower():
speak("Hallo! Womit kann ich dir behilflich sein?")
continue
prompt = q
response = openai_request(prompt)
if response.lower().startswith(q.lower()):
response = response[len(q):].strip()
speak(response)
return
if __name__ == '__main__':
execute()
Wenn ich in PyCharm das Programm ausführe und dann z.B. frage: Wann war der erste Weltkrieg? Dann funktioniert alles perfekt (ich bekomme eine Antwort und es gibt keine Fehler). Wenn ich das Programm nun aber im Terminal ausführe, kommt dieser Fehlercode:
Text: wann war der erste Weltkrieg
Erkannter Text: wann war der erste Weltkrieg
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\voiceAssistant\main.py", line 106, in <module>
execute()
File "C:\Users\User\PycharmProjects\voiceAssistant\main.py", line 95, in execute
response = openai_request(prompt)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\voiceAssistant\main.py", line 39, in openai_request
response = openai.Completion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python312\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.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, e.g. `pip install openai==0.28`
A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
C:\Users\User\PycharmProjects\voiceAssistant>
Ich weiss wirklich nicht, wie ich diesen Fehler beheben kann. Bitte helft mir.
Freundliche Grüsse