我试图使一个聊天完成机器人使用opeAI的GPT引擎,需要语音输入和输出文本到语音文件,然而,我一直得到一个编码错误,我不明白
import os
import speech_recognition as sr
import openai
from dotenv import load_dotenv
from os import path
from playsound import playsound
from gtts import gTTS
import simpleaudio as sa
load_dotenv()
language = 'en'
openai.api_key = os.getenv("OPENAI_API_KEY")
while True:
# this recognizes your voice input
recog = sr.Recognizer()
with sr.Microphone() as source:
audio = recog.listen(source)
#this transcribes the voice to text
with open("microphone-results.wav", "wb") as f:
f.write(audio.get_wav_data())
AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "microphone-results.wav")
my_question = recog.recognize_sphinx(audio)
#this generates a response
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a chatbot named jarvis"},
{"role": "user", "content": str(my_question)},
]
)
reply = ''.join(choice.message.content for choice in response.choices)
tts = gTTS(reply)
tts_file = "temp.wav"
tts.save(tts_file)
wave_obj = sa.WaveObject.from_wave_file(tts_file)
play_obj = wave_obj.play()
play_obj.wait_done()
os.remove(tts_file)
我尝试格式化它,以为它会输出TTS结果,它说:
File "c:\Users\tonda\python\SSPS_Projects\PortfolioApps\assistant\functions\ChatGPT\Chat.py", line 28, in <module>
response = openai.ChatCompletion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\openai\api_resources\chat_completion.py", line 25, in create
return super().create(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create
response, _, api_key = requestor.request(
^^^^^^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\openai\api_requestor.py", line 216, in request
result = self.request_raw(
^^^^^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\openai\api_requestor.py", line 516, in request_raw
result = _thread_context.session.request(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\requests\sessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\requests\sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\requests\adapters.py", line 489, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\urllib3\connectionpool.py", line 398, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\urllib3\connection.py", line 239, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\http\client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\http\client.py", line 1323, in _send_request
self.putheader(hdr, value)
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\site-packages\urllib3\connection.py", line 224, in putheader
_HTTPConnection.putheader(self, header, *values)
File "C:\Users\tonda\scoop\apps\python\3.11.0\Lib\http\client.py", line 1255, in putheader
values[i] = one_value.encode('latin-1')
^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'latin-1' codec can't encode character '\u201c' in position 7: ordinal not in range(256)
是在说一个不知名的人物吗我真的不明白这一点,部分原因是因为我是新的编码
1条答案
按热度按时间kiz8lqtg1#
当我试图使用OpenAI的API在Kubernetes中发布应用时,我也遇到了同样的错误。在我的例子中,问题是OpenAI密钥,不知何故被错误地编码了-所以它不再是拉丁1字符。确保您正在使用的密钥没有以某种方式被编码?
我把钥匙放在.yaml文件中,不得不:echo“whatever_my_key_was”|base64
并将这些值放入.yaml文件中