first commit

This commit is contained in:
konrad 2023-03-20 12:02:37 +00:00
commit 10f72111ad
3 changed files with 119 additions and 0 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 knrd1 <konrad@nygus.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

58
README.md Normal file
View File

@ -0,0 +1,58 @@
# ChatGPT
ChatGPT is a simple IRC bot written in python using Open AI endpoints to answer questions.
ChatGPT uses official bindings from OpenAI to interact with the API through HTTP requests:
https://platform.openai.com/docs/api-reference
### Prerequisities:
Create an account and obtain your API key: https://platform.openai.com/account/api-keys
Install python3 and the official Python bindings:
```
apt install python3 (Debian/Ubuntu)
yum install python3 (RedHat/CentOS)
pip3 install openai
```
### Configuration:
Edit chatgpt.py and change variables to the file. Example configuration for IRCNet:
```
openai.api_key = "sk-XXXXXXXXXXXXXXXX"
server = "open.ircnet.net"
port = 6667
channel = "#irc"
nickname = "MyBot"
```
Optionally you can edit the engine, the list of compatible engines below:
```
engine="text-davinci-003",
```
IMPORTANT: When you specify "nickname", make sure you replace ALL occurences of ChatGPT in chatgpt.py file to the new nickname. For example, if your bot nick name is "MyBot", do:
```
$ sed -i 's/ChatGPT/MyBot/g' chatgpt.py
```
### Connecting bot to IRC server:
```
$ python3 chatgpt.py
```
### Interaction:
ChatGPT will interact only reply if you mention its nickname:
```
10:31:12 <@knrd1> ChatGPT: hello, how are you?
10:31:14 < ChatGPT> Hi there, I'm doing well, thank you. How about you?
```
### Model endpoint compatibility
ChatGPT uses endpoint /v1/completions. Following engines are compatible:
```
text-davinci-003
text-davinci-002
text-curie-001
text-babbage-001
text-ada-001
davinci
curie
babbage
ada
```

40
chatgpt.py Normal file
View File

@ -0,0 +1,40 @@
import openai
import socket
import time
# Set up OpenAI API key
openai.api_key = ""
# Set up IRC connection settings
server = ""
port =
channel = ""
nickname = ""
# Connect to IRC server
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((server, port))
irc.send(bytes("USER " + nickname + " " + nickname + " " + nickname + " :ChatGPT\n", "UTF-8"))
irc.send(bytes("NICK " + nickname + "\n", "UTF-8"))
irc.send(bytes("JOIN " + channel + "\n", "UTF-8"))
# Listen for messages from users
while True:
message = irc.recv(2048).decode("UTF-8")
if message.find("PING") != -1:
# Respond to server PING requests
irc.send(bytes("PONG " + message.split()[1] + "\n", "UTF-8"))
elif message.find("PRIVMSG " + channel + " :" + nickname + ":") != -1:
question = message.split(nickname + ":")[1].strip()
response = openai.Completion.create(
engine="text-davinci-003",
prompt="Q: " + question + "\nA:",
temperature=1,
max_tokens=300,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
answer = response.choices[0].text.strip()
irc.send(bytes("PRIVMSG " + channel + " :" + answer + "\n", "UTF-8"))
time.sleep(1)