diff --git a/.gitignore b/.gitignore index d4a74c3..36b6b7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -chatgpt_new.py +chatgpt_dev.py chat.conf diff --git a/README.md b/README.md index 8094529..9b0c2dd 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ request_timeout=180 ``` Also, you can edit the model, the list of compatible models below: ``` -model="text-davinci-003", +model="gpt-3.5-turbo", ``` ### Connecting bot to IRC server: ``` @@ -61,16 +61,8 @@ ChatGPT will interact only if you mention its nickname: ``` ### Model endpoint compatibility -ChatGPT uses endpoint v1/completions. Following models are compatible. +ChatGPT uses endpoint v1/chat/completions. Following models are compatible. ``` -text-davinci-003 -text-davinci-002 -text-curie-001 -text-babbage-001 -text-ada-001 -davinci -curie -babbage -ada +gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301 ``` More details about models: https://platform.openai.com/docs/models diff --git a/chatgpt.py b/chatgpt.py index 5b1753c..855d21b 100644 --- a/chatgpt.py +++ b/chatgpt.py @@ -34,9 +34,11 @@ while True: # Listen for messages from users while True: - message = irc.recv(2048).decode("UTF-8") + try: + message = irc.recv(2048).decode("UTF-8") + except UnicodeDecodeError: + continue if message.find("PING") != -1: - # Respond to server PING requests irc.send(bytes("PONG " + message.split()[1] + "\n", "UTF-8")) elif message.find("KICK " + channel + " " + nickname) != -1: irc.send(bytes("JOIN " + channel + "\n", "UTF-8")) @@ -44,22 +46,29 @@ while True: elif message.find("PRIVMSG " + channel + " :" + nickname + ":") != -1: question = message.split(nickname + ":")[1].strip() try: - response = openai.Completion.create( - model="text-davinci-003", - prompt="Q: " + question + "\nA:", + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": question}], temperature=0.8, max_tokens=1000, top_p=1, frequency_penalty=0, presence_penalty=0, - request_timeout=180 # Set request timeout to 3 minutes + request_timeout=180 ) - answers = [x.strip() for x in response.choices[0].text.strip().split('\n')] + answers = [x.strip() for x in response.choices[0].message.content.strip().split('\n')] for answer in answers: while len(answer) > 0: - irc.send(bytes("PRIVMSG " + channel + " :" + answer[:400] + "\n", "UTF-8")) - answer = answer[400:] - except openai.error.Timeout as e: # Catch the timeout error + if len(answer) <= 392: + irc.send(bytes("PRIVMSG " + channel + " :" + answer + "\n", "UTF-8")) + answer = "" + else: + last_space_index = answer[:392].rfind(" ") + if last_space_index == -1: + last_space_index = 392 + irc.send(bytes("PRIVMSG " + channel + " :" + answer[:last_space_index] + "\n", "UTF-8")) + answer = answer[last_space_index:].lstrip() + except openai.error.Timeout as e: print("Error: " + str(e)) irc.send(bytes("PRIVMSG " + channel + " :API call timed out. Try again later.\n", "UTF-8")) except Exception as e: diff --git a/example-chat.conf b/example-chat.conf index 58aa2a5..7b5dc90 100644 --- a/example-chat.conf +++ b/example-chat.conf @@ -4,5 +4,5 @@ api_key = sk-XXXXXXXXXXXXXXX [irc] server = open.ircnet.net port = 6667 -channel = #irc +channel = #ircnet nickname = MyBot