You Can Program ChatGPT /GPT-3 To VERBALLY Chat With You! (Python)

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024
  • You Can Program ChatGPT / GPT-3 To Talk to You! (Python) #gpt3 #airobot #chatgpt #codegpt #samplecode
    In order to use GPT, you must have an OpenAI account and an OpenAI API key.
    You will need to download and install ("pip3 install") three libraries before the code will run.
    I use MS Visual Studio Code and Python 3 to run my code.
    Questions? Leave them in the comments. I will get back to you promptly.
    This is the Python 3.x code.
    GPT-3 with Voice Input and Output Version 1.01
    #--------------------------------------
    Download and install the following Libraries:
    pip3 install SpeechRecognition pydub
    pip3 install pyaudio
    pip install playsound
    ###########################################
    VERSION CHANGES
    00 : Everything works
    01 : Added prompt saving so GPT can remember what he is talking about
    01 : Changed AI's name to "Joe". Commented out a lot of the print statements that I used for testing my code.
    To Do:
    01: Add prompt saving
    02:
    03:
    04:
    05:
    06:
    import pyttsx3 ### Text to speech library
    import winsound ### for beep sound
    import speech_recognition as sr ### for Google's speech recognition
    import openai #333 GPT 3 library
    import json
    import zzzkeys
    import time
    global MyPrompt
    MyPrompt = "Joe: What a great day
    Human: Yes it is
    Joe: A truly fine day
    Human:"
    textFromGoogleSR= "none"
    try:
    ListeningForGoodbye = "none"
    except NameError:
    None
    openai.api_key = "your openAI API key goes here!" #This code won't work without your own API key from OpenAI.
    r = sr.Recognizer()
    print("

    ")
    while ListeningForGoodbye != "goodbye":
    """ Record User's Voice and Send Audio/Get textFromGoogleSR from Google """
    """ Record User's Voice and Send Audio/Get textFromGoogleSR from Google """
    """ Record User's Voice and Send Audio/Get textFromGoogleSR from Google """
    winsound.Beep(500, 100)
    with sr.Microphone() as source:
    print("Waiting for user input... (\'goodbye\' ends the conversation)")
    audio_data = r.record(source, duration=3)
    textFromGoogleSR = r.recognize_google(audio_data) # convert speech to text
    #print("")
    #print(" -- Text heard: ", textFromGoogleSR)
    #print("")
    winsound.Beep(400, 100)
    ListeningForGoodbye = textFromGoogleSR
    MyPrompt = MyPrompt + textFromGoogleSR ## Combine initial prompt with new input from user
    """ Send Input to GPT """
    """ Send Input to GPT """
    """ Send Input to GPT """
    response = openai.Completion.create(
    model="text-davinci-002",
    prompt=f"{MyPrompt}
    ",
    #truncate=5, #cuts off GPT's reply when it sees the first period. ### DOESN"T WORK!! :(
    temperature=0.9, #randomness of replies. 0=not at all random. 1= very random.
    max_tokens=25, #maximum number of tolkens (groups of 4 characters) returned from GPT
    top_p=.9, # higher = more random selection of next word
    frequency_penalty=0.9, # reduces repitition of words
    presence_penalty=0.9, # lowers the probability of a word if it already appeared in the predicted text.
    stop=["Human:"] #used to make the model stop at a desired point
    )
    GPT_Reply=response["choices"][0]["text"] # parse out just the reply from GPT
    GPT_Reply = GPT_Reply[:GPT_Reply.find(".") or GPT_Reply.find("?")]
    MyPrompt = MyPrompt + "
    " + GPT_Reply + "
    " ## add reply to prompt
    """ Speak GPT's Reply """
    """ Speak GPT's Reply """
    """ Speak GPT's Reply """
    engine = pyttsx3.init() # object creation
    engine.setProperty('rate', 100) # setting up new voice rate
    voices = engine.getProperty('voices') #getting list of current voices
    engine.setProperty('voice', voices[0].id) #changing voices index to change voice
    engine.setProperty('rate', 150) #talking speed
    GPT_Reply=GPT_Reply.replace("Joe","") #remove the text "AI"
    engine.say(GPT_Reply) # finally SAY the reply that came from GPT
    print("")
    print(" -- Reply from GPT: ", GPT_Reply)
    print("")
    engine.runAndWait()
    engine.stop()
    print('(Conversation is over)')

Комментарии • 2

  • @thomasmaiden3356
    @thomasmaiden3356 Год назад +1

    Thomas, I asked my robot Zoe the same questions (almost) that you had asked Betty9. I wanted to compare her answers.
    The video is at ruclips.net/video/SeXxdBV8EsY/видео.html . Does it cost much to do speech recognition in the cloud? Does your GPT run on a local machine, or does it run in the cloud also?

    • @Robots-and-androids
      @Robots-and-androids  Год назад +1

      Great robot! Speech recognition is free thanks to Google!
      GPT-3 does not run locally. But GPT-J can run locally assuming you have a very fast, high end computer. A friend of mine has such a computer. It costed over $5,000.
      OpenAI has billed me $15-30 per month for using GPT-3. I am sure that price will come down as more competition comes online.