Initial commit

This commit is contained in:
Brayd 2023-01-15 20:46:19 +01:00
commit 8c76d77458
Signed by: brayd
GPG Key ID: C2B176B9DEB8BA48
5 changed files with 91 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.DS_Store
crypto_store
session.txt

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright © 2023 Brayd (https://code.braydmedia.de/brayd)
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.

42
README.md Normal file
View File

@ -0,0 +1,42 @@
# Matrix ChatGPT Bot
![License](https://img.shields.io/badge/license-MIT-blue)
This is a simple Bot written in Python to bring ChatGPT directly into Python.
## Disclaimer
This bot is not suited for the use in big channels or by many users. Your OpenAI API Key *will* get rate limited, if many requests are made to the bot in a short period of time. See this bot primarly for something for personal use or in small groups with your friends or family. I'll maybe add the option to whitelist specific users who are allowed to use the bot via a config or similar in the future.
## Setup
1. Install Python3 and pip3
2. Follow the README.md of the libolm repository to install libolm 3 or newer. You will need this for the bot to work in addition to the requirements.txt!
3. Install e2e support for matrix-nio by running:
```bash
pip3 install "matrix-nio[e2e]"
```
4. Clone this repo using:
```bash
git clone https://code.braydmedia.de/brayd/matrix-chatgpt-bot.git
```
5. Install requirements from requirements.txt using:
```bash
pip3 install -r requirements.txt
```
6. Create a bot account on your Matrix server
7. Logout of your Matrix client with the bot account (if you haven't created it via a Synapse command or the AdminAPI), so the session will be cancelled
8. Open main.py with a text editor (for e.g. nano, vim or neovim) and edit:
```python
creds = botlib.Creds("https://matrix.example.com", "bot_user_name", "password")
```
with your Matrix Homeserver URL, your bot username (without the @) and your bot accounts password.
Also edit:
```python
openai.api_key = "OPENAI TOKEN HERE"
```
with your OpenAI Token which you can get in your account settings directly from OpenAI.
9. Save the edited main.py file
10. Start the bot using:
```bash
python3 main.py
```
You can now start a chat with your bot and type your request like for e.g.:
```
! Tell me something about the Matrix Protocol
```

37
main.py Normal file
View File

@ -0,0 +1,37 @@
import simplematrixbotlib as botlib
import openai
creds = botlib.Creds("https://matrix.example.com", "bot_user_name", "password")
config = botlib.Config()
config.encryption_enabled = True
config.ignore_unverified_devices = True
config.store_path = './crypto_store/'
bot = botlib.Bot(creds, config)
# Set your prefix here. ! means that every message beginning with ! will tell the bot to respond to it
PREFIX = "!"
@bot.listener.on_message_event
async def request(room, message):
match = botlib.MessageMatch(room, message, bot, PREFIX)
if match.is_not_from_this_bot() and match.prefix():
openai.api_key = "OPENAI TOKEN HERE"
messagetobot = " ".join(arg for arg in match.args())
response = openai.Completion.create(
engine = "text-davinci-002",
prompt = messagetobot,
temperature = 0.7,
max_tokens = 709,
top_p = 1,
frequency_penalty = 0,
presence_penalty = 0
)
responsetomatrix = response.choices[0].text
await bot.api.send_markdown_message(
room.room_id, responsetomatrix
)
bot.run()

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
openai==0.26.1
simplematrixbotlib==2.8.0