matrix_chatgpt_bot/README.md

139 lines
3.9 KiB
Markdown
Raw Permalink Normal View History

2023-03-05 14:07:25 +00:00
## Introduction
2023-04-11 03:11:59 +00:00
2024-03-08 07:07:01 +00:00
This is a simple Matrix bot that support using OpenAI API, Langchain to generate responses from user inputs. The bot responds to these commands: `!gpt`, `!chat`, `!v`, `!pic`, `!new`, `!lc` and `!help` depending on the first word of the prompt.
2024-04-26 10:15:23 +00:00
![ChatGPT](https://i.imgur.com/xP4bwMr.jpeg)
2023-04-13 15:49:12 +00:00
## Feature
2023-04-11 03:11:59 +00:00
2023-09-17 04:27:16 +00:00
1. Support official openai api and self host models([LocalAI](https://localai.io/model-compatibility/))
2023-09-13 00:19:12 +00:00
2. Support E2E Encrypted Room
3. Colorful code blocks
4. Langchain([Flowise](https://github.com/FlowiseAI/Flowise))
2023-09-17 04:27:16 +00:00
5. Image Generation with [DALL·E](https://platform.openai.com/docs/api-reference/images/create) or [LocalAI](https://localai.io/features/image-generation/) or [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API)
2024-03-08 07:07:01 +00:00
6. GPT Vision(openai or [GPT Vision API](https://platform.openai.com/docs/guides/vision) compatible such as [LocalAI](https://localai.io/features/gpt-vision/))
2024-04-23 12:18:21 +00:00
7. Room level and thread level chat context
2023-04-07 01:28:47 +00:00
2023-03-05 14:07:25 +00:00
## Installation and Setup
2023-04-11 03:11:59 +00:00
2023-04-10 02:52:18 +00:00
Docker method(Recommended):<br>
Edit `config.json` or `.env` with proper values <br>
2023-04-20 05:02:35 +00:00
For explainations and complete parameter list see: https://github.com/hibobmaster/matrix_chatgpt_bot/wiki <br>
2023-09-13 00:19:12 +00:00
Create two empty file, for persist database only<br>
2023-04-11 03:11:59 +00:00
2023-03-09 15:45:25 +00:00
```bash
2024-04-23 12:18:21 +00:00
touch sync_db context.db manage_db
2023-03-09 15:45:25 +00:00
sudo docker compose up -d
```
2024-04-23 12:18:21 +00:00
manage_db(can be ignored) is for langchain agent, sync_db is for matrix sync database, context.db is for bot chat context<br>
2023-03-09 15:45:25 +00:00
<hr>
2023-04-11 03:11:59 +00:00
Normal Method:<br>
system dependece: <code>libolm-dev</code>
2023-03-09 15:45:25 +00:00
1. Clone the repository and create virtual environment:
2023-04-11 03:11:59 +00:00
2023-03-05 14:07:25 +00:00
```
git clone https://github.com/hibobmaster/matrix_chatgpt_bot.git
python -m venv venv
source venv/bin/activate
2023-03-05 14:07:25 +00:00
```
2023-04-11 03:11:59 +00:00
2023-03-05 14:07:25 +00:00
2. Install the required dependencies:<br>
2023-04-11 03:11:59 +00:00
2023-03-05 14:07:25 +00:00
```
2023-04-11 03:11:59 +00:00
pip install -U pip setuptools wheel
2023-03-05 14:07:25 +00:00
pip install -r requirements.txt
```
2023-04-11 03:11:59 +00:00
2023-09-13 00:19:12 +00:00
3. Create a new config.json file and complete it with the necessary information:<br>
2023-04-11 03:11:59 +00:00
If not set:<br>
`room_id`: bot will work in the room where it is in <br>
2023-03-05 14:07:25 +00:00
```json
{
2023-04-11 03:11:59 +00:00
"homeserver": "YOUR_HOMESERVER",
"user_id": "YOUR_USER_ID",
"password": "YOUR_PASSWORD",
"device_id": "YOUR_DEVICE_ID",
"room_id": "YOUR_ROOM_ID",
2023-06-05 03:27:37 +00:00
"openai_api_key": "YOUR_API_KEY",
2023-09-13 07:27:34 +00:00
"gpt_api_endpoint": "xxxxxxxxx"
2023-03-05 14:07:25 +00:00
}
```
2023-04-11 03:11:59 +00:00
2023-09-13 00:19:12 +00:00
4. Launch the bot:
2023-04-11 03:11:59 +00:00
2023-03-05 14:07:25 +00:00
```
2023-06-05 03:27:37 +00:00
python src/main.py
2023-03-05 14:07:25 +00:00
```
2023-04-11 03:11:59 +00:00
2023-03-05 14:07:25 +00:00
## Usage
2023-04-11 03:11:59 +00:00
2023-09-17 04:27:16 +00:00
To interact with the bot, simply send a message to the bot in the Matrix room with one of the following prompts:<br>
- `!help` help message
2023-04-11 03:11:59 +00:00
- `!gpt` To generate a one time response:
2023-04-11 03:11:59 +00:00
2023-03-05 14:07:25 +00:00
```
!gpt What is the meaning of life?
```
2023-04-11 03:11:59 +00:00
2023-03-10 17:01:25 +00:00
- `!chat` To chat using official api with context conversation
2023-04-11 03:11:59 +00:00
2023-03-05 14:07:25 +00:00
```
!chat Can you tell me a joke?
2023-03-10 15:45:38 +00:00
```
2023-04-11 03:11:59 +00:00
2024-04-26 10:15:23 +00:00
- GPT Vision command
You can refer the screenshot
2024-03-08 07:07:01 +00:00
```
2024-04-26 10:15:23 +00:00
Room Level: quote a image and @bot + {prompt}
Thread Level: quote a image with a {prompt}
2024-03-08 07:07:01 +00:00
```
- `!lc` To chat using langchain api endpoint
```
2023-09-13 00:19:12 +00:00
!lc All the world is a stage
```
2023-09-13 00:19:12 +00:00
- `!pic` To generate an image using openai DALL·E or LocalAI
2023-04-11 03:11:59 +00:00
2023-03-22 14:41:01 +00:00
```
!pic A bridal bouquet made of succulents
```
2023-09-17 15:33:43 +00:00
- `!agent` display or set langchain agent
```
!agent list
!agent use {agent_name}
```
2023-09-13 00:19:12 +00:00
- `!new + {chat}` Start a new converstaion
2023-04-11 03:11:59 +00:00
2023-09-17 15:33:43 +00:00
LangChain(flowise) admin: https://github.com/hibobmaster/matrix_chatgpt_bot/wiki/Langchain-(flowise)
2023-06-05 03:27:37 +00:00
2023-09-13 00:19:12 +00:00
## Image Generation
2023-09-17 04:27:16 +00:00
![demo1](https://i.imgur.com/voeomsF.jpg)
![demo2](https://i.imgur.com/BKZktWd.jpg)
2023-04-11 03:11:59 +00:00
https://github.com/hibobmaster/matrix_chatgpt_bot/wiki/ <br>
2023-09-13 00:19:12 +00:00
2024-04-26 10:15:23 +00:00
## GPT Vision
Room Level:
![GPT Vision Room Level](https://i.imgur.com/pUaxeps.jpeg)
Thread Level:
![GPT Vision Thread Level](https://i.imgur.com/Rw7wy3Y.jpeg)
2024-04-23 12:18:21 +00:00
## Thread level Context
Mention bot with prompt, bot will reply in thread.
To keep context just send prompt in thread directly without mention it.
![thread level context 1](https://i.imgur.com/4vLvNCt.jpeg)
![thread level context 2](https://i.imgur.com/1eb1Lmd.jpeg)
2023-04-13 15:49:12 +00:00
## Thanks
1. [matrix-nio](https://github.com/poljar/matrix-nio)
2. [acheong08](https://github.com/acheong08)
3. [8go](https://github.com/8go/)