-
Notifications
You must be signed in to change notification settings - Fork 4
/
chatGPT_bot.py
64 lines (50 loc) · 1.67 KB
/
chatGPT_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os # for importing env vars for the bot to use
#from twitchio.ext import commands
import openai
import pyttsx3
from gtts import gTTS
import playsound
from twitchio.ext import commands
oauth_token = "YOUR TWITCH TOKEN"
client_id = "YOUR TWITCH CLIENT ID"
bot_account = 'YOUR BOT NAME'
channel_name = 'YOUR CHANNEL NAME'
OPENAI_API_KEY = "YOUR OPEN AI key"
openai.api_key = OPENAI_API_KEY
start_sequence = "\nChatGPT"
restart_sequence = "\nChat: "
prompt = "YOUR INITAL PROMPT FOR BOT (CHANNEL DESCRIPTION AND/OR PERSONALITY INFO"
chat_log = prompt
def ask(question, chat_log=None):
prompt_text = f"{chat_log}{restart_sequence}: {question}{start_sequence}:"
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt_text,
temperature=0.98,
max_tokens=218,
top_p=1,
frequency_penalty=0.47,
presence_penalty=0.6,
stop=['\n'],
)
answer = response['choices'][0]['text']
return str(answer)
def append_interaction_to_chat_log(question, answer, chat_log=None):
if chat_log is None:
chat_log = prompt
return f"{chat_log}{restart_sequence} {question}{start_sequence}{answer}"
def speak(text):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('rate', 180)
engine.setProperty('voice', voices[1].id)
engine.say(text)
engine.runAndWait()
def read_chat(text):
speak(str(text))
def chatGPT(message):
incoming_msg = message
answer = ask(incoming_msg, chat_log)
append_interaction_to_chat_log(incoming_msg, answer, chat_log)
print(str(answer))
speak(str(answer))