About running ChatGPT with Python

        import speech_recognition as sr
        import time
        import os
        import openai
        import serial

        # Set up your OpenAI API key
        #openai.api_key = 'sk-proj-9wGxyYGvulHuFnh50CPgT3BlbkFJYj6XPjbLKnZUrDsSEGzA'
        #api_key = "sk-proj-9wGxyYGvulHuFnh50CPgT3BlbkFJYj6XPjbLKnZUrDsSEGzA"
        client = openai.OpenAI(
            # This is the default and can be omitted
            api_key=os.getenv("sk-proj-9wGxyYGvulHuFnh50CPgT3BlbkFJYj6XPjbLKnZUrDsSEGzA")
        )

        # Serial port for communication with Arduino Nano
        SERIAL_PORT = '/dev/ttyUSB0'  # Adjust as needed
        SERIAL_BAUDRATE = 9600

        # Function to send command to Arduino Nano
        def send_command_to_robot(command):
            try:
                arduino = serial.Serial(SERIAL_PORT, SERIAL_BAUDRATE)
                arduino.write(command.encode())
                arduino.close()
                print(f"Sent command to Arduino: {command}")
            except Exception as e:
                print(f"Error sending command to Arduino: {e}")

        # Function to recognize speech from microphone
        def recognize_speech_from_mic(recognizer, microphone, timeout=5):
            # Adjust the recognizer sensitivity to ambient noise and record audio
            with microphone as source:
                recognizer.adjust_for_ambient_noise(source)
                print("Listening...")
                try:
                    audio = recognizer.listen(source, timeout=timeout)
                except sr.WaitTimeoutError:
                    return {"success": False, "error": "TimeoutError", "transcription": None}

            # Initialize response
            response = {
                "success": True,
                "error": None,
                "transcription": None
            }

            # Try recognizing the speech in the recording
            try:
                response["transcription"] = recognizer.recognize_google(audio)
            except sr.RequestError:
                # API was unreachable or unresponsive
                response["success"] = False
                response["error"] = "API unavailable"
            except sr.UnknownValueError:
                # Speech was unintelligible
                response["error"] = "Unable to recognize speech"

            return response

        # Function to understand command using ChatGPT
        def understand_command_with_chatgpt(command):
            try:
                response = client.completions.create(
                    model="gpt-3.5-turbo",
                    #engine="text-davinci-002",
                    prompt=f"What is the standard command for '{command}'?",
                    temperature=0.5,
                    max_tokens=50
                )
                standardized_command = response.choices[0].text.strip()
                return standardized_command
            except Exception as e:
                print(f"Error understanding command with ChatGPT: {e}")
                return None

        # Main function
        def main():
            # Create recognizer and mic instances
            recognizer = sr.Recognizer()
            microphone = sr.Microphone()

            print("Say 'Hey' to start controlling the robot.")

            activated = False
            last_command = None

            while True:
                response = recognize_speech_from_mic(recognizer, microphone)

                if response["success"]:
                    transcription = response["transcription"]
                    if transcription:
                        transcription = transcription.lower()
                        print(f"You said: {transcription}")
                        
                        if not activated:
                            if "hey" in transcription:
                                print("Voice control activated. Please say a command: 'forward', 'backward', 'left', 'right', 'stop'.")
                                activated = True
                            else:
                                print("Say 'Hey' to start controlling the robot.")
                        else:
                            if "exit" in transcription:
                                print("Voice control deactivated. Say 'Hey' to activate again.")
                                activated = False
                            else:
                                # Replace synonyms with standard commands using ChatGPT
                                for word in transcription.split():
                                    standardized_command = understand_command_with_chatgpt(word)
                                    if standardized_command:
                                        transcription = transcription.replace(word, standardized_command)
                                        print(f"Standardized command: {transcription}")

                                if "forward" in transcription:
                                    send_command_to_robot('F')
                                    last_command = 'F'
                                elif "backward" in transcription:
                                    send_command_to_robot('B')
                                    last_command = 'B'
                                elif "left" in transcription:
                                    send_command_to_robot('L')
                                    last_command = 'L'
                                elif "right" in transcription:
                                    send_command_to_robot('R')
                                    last_command = 'R'
                                elif "stop" in transcription:
                                    send_command_to_robot('S')
                                    last_command = 'S'
                                else:
                                    print("Unrecognized command. Please try again.")
                    else:
                        print("Didn't catch that. Say 'Hey' to start controlling the robot.")
                
                if response["error"]:
                    print(f"Error: {response['error']}")

                if not activated:
                    print("Listening for activation keyword...")

                # Add a short delay to avoid too fast looping
                time.sleep(1)

        if __name__ == "__main__":
            main()
  • Sorry if I done mistake in this code, I am a newbie with only basic coding knowledge. I am using ChatGPT to code this for me.

I am try to create a code to do natural language processing which will change the voice command to something a robot can understand. The old code work fine without the ChatGPT but after I add in the ChatGPT function, there is a bunch error and the ChatGPT cannot fix it for me.

The error show after I run the code:
Traceback (most recent call last):

  File "C:UsersganshDesktopRaspberry piVoice_Command_GPT.py", line 10, in <module>
    client = openai.OpenAI(
  File "C:UsersganshAppDataRoamingPythonPython310site-packagesopenai_client.py", line 104, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

I try to use env on the API key but still not working.
Please help me to fix this error. Appreciate your help.

Hope I can run the code after your help.

New contributor

Gan Shao Yu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật