diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..27a075a7e --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +OPENAI_API = +OPENAI_MODEL_ENGINE = 'text-davinci-003' +OPENAI_MAX_TOKENS = 1024 +LINE_CHANNEL_SECRET = +LINE_CHANNEL_ACCESS_TOKEN = \ No newline at end of file diff --git a/Procfile b/Procfile deleted file mode 100644 index bcf680485..000000000 --- a/Procfile +++ /dev/null @@ -1 +0,0 @@ -web: python main.py \ No newline at end of file diff --git a/README.en.md b/README.en.md index e69de29bb..ce61bea96 100644 --- a/README.en.md +++ b/README.en.md @@ -0,0 +1,86 @@ +# ChatGPT Line Bot + +[中文](README.md) | English + +[![license](https://img.shields.io/pypi/l/ansicolortags.svg)](LICENSE) [![Release](https://img.shields.io/github/v/release/TheExplainthis/ChatGPT-Line-Bot)](https://github.com/TheExplainthis/ChatGPT-Line-Bot/releases/) + +## Introduction +Import the ChatGPT bot to Line and start interacting with it by simply typing text in the input box. In addition to ChatGPT, the model for `Text to Image` is also integrated. Enter `/imagine + text` to return the corresponding image, as shown in the figure below: + +![Demo](https://github.com/TheExplainthis/ChatGPT-Line-Bot/blob/main/demo/chatgpt-line-bot.gif) + +## Installation Steps +### Token Retrieval +1. Retrieve the OpenAI API Token: + 1. Register/login to your [OpenAI](https://beta.openai.com/) account. + 2. Click on the avatar on the top right corner and select `View API keys`. + 3. Click on `Create new secret key` in the middle, and the generated token will be `OPENAI_API` (to be used later). + - Note: Each API has a free quota and restrictions. For details, please refer to [OpenAI Pricing](https://openai.com/api/pricing/). +2. Retrieve the Line Token: + 1. Login to [Line Developer](https://developers.line.biz/zh-hant/). + 2. Create a bot: + 1. Create a `Provider` -> click `Create`. + 2. Create a `Channel` -> select `Create a Messaging API channel`. + 3. Enter the required basic information. + 4. After completion, there is a `Channel Secret` under `Basic Settings` -> click `Issue`, and the generated token will be `LINE_CHANNEL_SECRET` (to be used later). + 5. Under `Messaging API`, there is a `Channel access token` -> click `Issue`, and the generated token will be `LINE_CHANNEL_ACCESS_TOKEN` (to be used later). + +### Project Setup +1. Fork the Github project: + 1. Register/login to [GitHub](https://github.com/). + 2. Go to [ChatGPT-Line-Bot](https://github.com/TheExplainthis/ChatGPT-Line-Bot). + 3. Click `Star` to support the developer. + 4. Click `Fork` to copy all the code to your own repository. +2. Deploy (free space): + 1. Go to [replit](https://replit.com/). + 2. Click `Sign Up` and log in with your `Github` account and authorize it -> click `Skip` to skip the initialization settings. + 3. On the main page in the middle, click `Create` -> a pop-up window will appear, click `Import from Github` on the upper right corner. + 4. If you have not added the Github repository, click the link `Connect GitHub to import your private repos.` -> check `Only select repositories` -> select `ChatGPT-Line-Bot`. + 5. Go back to step 4. At this point, the `Github URL` can select the `ChatGPT-Line-Bot` project -> click `Import from Github`. + +### Project Execution +1. Environment variables setting: + 1. After completing the previous step of `Import`, click on `Tools` at the bottom left of the project management page in `Replit`, then click on `Secrets`. + 2. Click on `Got it` on the right side to add environment variables, which includes: + 1. OpenAI API Token: + - key: `OPENAI_API` + - value: `[obtained from step one]` + 2. The selected model: + - key: `OPENAI_MODEL_ENGINE` + - value: `text-davinci-003` + 3. ChatGPT text limit: + - key: `OPENAI_MAX_TOKENS` + - value: `128` + 4. Line Channel Secret: + - key: `LINE_CHANNEL_SECRET` + - value: `[obtained from step one]` + 5. Line Channel Access Token: + - key: `LINE_CHANNEL_ACCESS_TOKEN` + - value: `[obtained from step one]` +2. Start running: + 1. Click on `Run` on the top. + 2. After successful, the right-side screen will display `Hello World`, and the **URL** on the top of the screen should be copied down. + 3. Go back to Line Developer, paste the **URL** above `Webhook URL` under `Messaging API`, and add `/callback` to the end, for example: `https://ChatGPT-Line-Bot.explainthis.repl.co/callback` + 4. Turn on `Use webhook` below. + 5. Turn off `Auto-reply messages` below. + - Note: if there is no request within an hour, the program will be interrupted, so the following steps are needed. +3. CronJob scheduled request sending: + 1. Register/Login to [cron-job.org](https://cron-job.org/en/) + 2. In the upper right corner of the panel, select `CREATE CRONJOB` + 3. Enter `ChatGPT-Discord-Bot` in the Title field, and enter the URL from the previous step, for example: `https://ChatGPT-Line-Bot.explainthis.repl.co/` + 4. Send a request every `5 minutes` below + 5. Click on `CREATE` + +## Commands +To start a conversation with ChatGPT, simply type your message in the text input box. Other available commands include: + +| Command | Description | +| ------- | ----------- | +| `/imagine` | Type `/imagine` followed by text in the input box to call the `Text to Image` model and generate an image. | + +## Related Projects +- [gpt-ai-assistant](https://github.com/memochou1993/gpt-ai-assistant) +- [ChatGPT-Discord-Bot](https://github.com/TheExplainthis/ChatGPT-Discord-Bot) + +## License +[MIT](LICENSE) \ No newline at end of file diff --git a/README.md b/README.md index 37485220e..922af6a73 100644 --- a/README.md +++ b/README.md @@ -1,55 +1,42 @@ -# ChatGPT Discord Bot +# ChatGPT Line Bot 中文 | [English](README.en.md) -[![license](https://img.shields.io/pypi/l/ansicolortags.svg)](LICENSE) [![Release](https://img.shields.io/github/v/release/TheExplainthis/ChatGPT-Discord-Bot)](https://github.com/TheExplainthis/ChatGPT-Discord-Bot/releases/) - - -ChatGPT 串接到 Discord 上面,使得團隊在協作、溝通、效率上都能夠快速的提升,根據下面的安裝步驟,你也能在自己的 Discord 當中去導入 ChatGPT。 - +[![license](https://img.shields.io/pypi/l/ansicolortags.svg)](LICENSE) [![Release](https://img.shields.io/github/v/release/TheExplainthis/ChatGPT-Line-Bot)](https://github.com/TheExplainthis/ChatGPT-Line-Bot/releases/) ## 介紹 -在 Discord 裡的每個頻道中導入 ChatGPT Bot,只要在輸入框輸入 `/chat` 就會 有一個 `/chat message` 的關鍵字自動帶入,直接輸入文字即可與 ChatGPT 互動,如下圖所示: -![Demo](https://github.com/TheExplainthis/ChatGPT-Discord-Bot/blob/main/demo/chatgpt-discord-bot.gif) +在 Line 中去導入 ChatGPT Bot,只要在輸入框直接輸入文字,即可與 ChatGPT 開始互動,除了 ChatGPT 以外,也直接串上了 `Text to Image` 的模型,輸入 `/imagine + 文字`,就會回傳相對應的圖片,如下圖所示: +![Demo](https://github.com/TheExplainthis/ChatGPT-Line-Bot/blob/main/demo/chatgpt-line-bot.gif) ## 安裝步驟 ### Token 取得 -- 取得 OpenAI 給的 API Token: +1. 取得 OpenAI 給的 API Token: 1. [OpenAI](https://beta.openai.com/) 平台中註冊/登入帳號 2. 右上方有一個頭像,點入後選擇 `View API keys` - 3. 點選中間的 `Create new secret key` + 3. 點選中間的 `Create new secret key` -> 生成後即為 `OPENAI_API` (稍晚會用到) - 注意:每隻 API 有免費額度,也有其限制,詳情請看 [OpenAI Pricing](https://openai.com/api/pricing/) -- 取得 Discord Token: - 1. 登入 [Discord Developer](https://discord.com/developers/applications) +2. 取得 Line Token: + 1. 登入 [Line Developer](https://developers.line.biz/zh-hant/) 2. 創建機器人: - 1. 進入左方 `Applications` - 2. 點擊右上方 `New Application` 並輸入 Bot 的名稱 > 確認後進入新頁面。 - 3. 點擊左方 `Bot` - 4. 點擊右方 `Add Bot` - 5. 下方 `MESSAGE CONTENT INTENT` 需打開 - 6. 按下 `Save Change` - 7. Token 在上方選擇 `View Token` 或已申請過則會是 `Reset Token` 的按鈕。 - 3. 設定 OAuth2 - 1. 點擊左欄 `OAuth2` - 2. 點擊左欄 `URL Generator` - 3. 右欄 `SCOPES` 選擇 `bot`、右欄下方 `BOT PERMISSIONS` 選擇 `Administrator` - 4. 複製最下方網址到瀏覽器中 - 5. 選擇欲加入的伺服器 - 6. 按下 `繼續` > `授權` + 1. 創建 `Provider` -> 按下 `Create` + 2. 創建 `Channel` -> 選擇 `Create a Messaging API channel` + 3. 輸入完必填的基本資料 + 4. 輸入完成後,在 `Basic Settings` 下方,有一個 `Channel Secret` -> 按下 `Issue`,生成後即為 `LINE_CHANNEL_SECRET` (稍晚會用到) + 5. 在 `Messaging API` 下方,有一個 `Channel access token` -> 按下 `Issue`,生成後即為 `LINE_CHANNEL_ACCESS_TOKEN` (稍晚會用到) ### 專案設置 1. Fork Github 專案: 1. 註冊/登入 [GitHub](https://github.com/) - 2. 進入 [ChatGPT-Discord-Bot](https://github.com/TheExplainthis/ChatGPT-Discord-Bot) + 2. 進入 [ChatGPT-Line-Bot](https://github.com/TheExplainthis/ChatGPT-Line-Bot) 3. 點選 `Star` 支持開發者 4. 點選 `Fork` 複製全部的程式碼到自己的倉庫 2. 部署(免費空間): 1. 進入 [replit](https://replit.com/) 2. 點選 `Sign Up` 直接用 `Github` 帳號登入並授權 -> 按下 `Skip` 跳過初始化設定 3. 進入後中間主頁的部分點選 `Create` -> 跳出框,點選右上角 `Import from Github` - 4. 若尚未加入 Github 倉庫,則點選連結 `Connect GitHub to import your private repos.` -> 勾選 `Only select repositories` -> 選擇 `ChatGPT-Discord-Bot` - 5. 回到第四步,此時 `Github URL` 可以選擇 `ChatGPT-Discord-Bot` 專案 -> 點擊 `Import from Github`。 + 4. 若尚未加入 Github 倉庫,則點選連結 `Connect GitHub to import your private repos.` -> 勾選 `Only select repositories` -> 選擇 `ChatGPT-Line-Bot` + 5. 回到第四步,此時 `Github URL` 可以選擇 `ChatGPT-Line-Bot` 專案 -> 點擊 `Import from Github`。 ### 專案執行 1. 環境變數設定 @@ -57,38 +44,43 @@ ChatGPT 串接到 Discord 上面,使得團隊在協作、溝通、效率上都 2. 右方按下 `Got it` 後,即可新增環境變數,需新增: 1. OpenAI API Token: - key: `OPENAI_API` - - value: `[由上方步驟一取得] sk-FoXXXX` + - value: `[由上方步驟一取得]` 2. 欲選擇的模型: - key: `OPENAI_MODEL_ENGINE` - value: `text-davinci-003` 3. ChatGPT 回傳的文字限制 - key: `OPENAI_MAX_TOKENS` - value: `128` - 4. Discord Token: - - key: `DISCORD_TOKEN` - - value: `[由上方步驟一取得] MTA3NXXX` + 4. Line Channel Secret: + - key: `LINE_CHANNEL_SECRET` + - value: `[由步驟一取得]` + 5. Line Channel Access Token: + - key: `LINE_CHANNEL_ACCESS_TOKEN` + - value: `[由步驟一取得]` 2. 開始執行 1. 點擊上方的 `Run` - 2. 成功後右邊畫面會顯示 `Hello. I am alive!`,並將畫面中上方的**網址複製**下來,下一步驟會用到 + 2. 成功後右邊畫面會顯示 `Hello World`,並將畫面中上方的**網址複製**下來 + 3. 回到 Line Developer,在 `Messaging API` 下方的 `Webhook URL` 江上方網址貼過來,並加上 `/callback` 例如:`https://ChatGPT-Line-Bot.explainthis.repl.co/callback` + 4. 打開下方的 `Use webhook` + 5. 將下方 `Auto-reply messages` 關閉 - 注意:若一小時內沒有任何請求,則程式會中斷,因此需要下步驟 3. CronJob 定時發送請求 1. 註冊/登入 [cron-job.org](https://cron-job.org/en/) 2. 進入後面板右上方選擇 `CREATE CRONJOB` - 3. `Title` 輸入 `ChatGPT-Discord-Bot`,網址輸入上一步驟的網址 + 3. `Title` 輸入 `ChatGPT-Discord-Bot`,網址輸入上一步驟的網址,例如:`https://ChatGPT-Line-Bot.explainthis.repl.co/` 4. 下方則每 `5 分鐘` 打一次 5. 按下 `CREATE` - ## 指令 -| 指令 | 別名 | 說明 | -| --- | ---- |----- | -| `文字生成` | `/chat` | 在輸入框直接輸入 `/chat` 會後綴 `message` 直接輸入文字,即可調用 ChatGPT 模型。| -| `刪除紀錄` | `/reset` | ChatGPT 會記住前十次的問答紀錄,調用此指令則會清除。| -|`圖像生成` | `/imagine` | 在輸入框輸入 `/imagine` 會後綴 `prompt` 直接輸入文字,會調用 DALL·E 2 模型,即可生成圖像。| +在文字輸入框中直接輸入文字,即可與 ChatGPT 開始對話,而其他指令如下: +| 指令 | 說明 | +| --- | ----- | +| `/imagine` | 在輸入框輸入 `/imagine` + 文字,就會調用 `Text to Image` 模型,即可生成圖像。| ## 相關專案 -- [chatGPT-discord-bot](https://github.com/Zero6992/chatGPT-discord-bot) +- [gpt-ai-assistant](https://github.com/memochou1993/gpt-ai-assistant) +- [ChatGPT-Discord-Bot](https://github.com/TheExplainthis/ChatGPT-Discord-Bot) ## 授權 [MIT](LICENSE) \ No newline at end of file diff --git a/demo/chatgpt-line-bot.gif b/demo/chatgpt-line-bot.gif new file mode 100644 index 000000000..0c4bd0f3d Binary files /dev/null and b/demo/chatgpt-line-bot.gif differ diff --git a/runtime.txt b/runtime.txt deleted file mode 100644 index e02524340..000000000 --- a/runtime.txt +++ /dev/null @@ -1 +0,0 @@ -python-3.9.16 \ No newline at end of file diff --git a/src/memory.py b/src/memory.py index 2f2dfc265..c453f5fdd 100644 --- a/src/memory.py +++ b/src/memory.py @@ -20,7 +20,8 @@ def append(self, user_id: str, text: str) -> None: self.storage[user_id].append(text) def get(self, user_id: str) -> str: - return '\n\n'.join(self.storage.get(user_id, [])[-10:]) + HISTORY_MESSAGE_COUNT = 3 + return '\n\n'.join(self.storage.get(user_id, [])[-HISTORY_MESSAGE_COUNT:]) def remove(self, user_id: str) -> None: self.storage[user_id] = [] diff --git a/vercel.json b/vercel.json deleted file mode 100644 index ab3aa31bf..000000000 --- a/vercel.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "builds": [{ - "src": "main.py", - "use": "@vercel/python", - "config": { "maxLambdaSize": "15mb" } - }], - "routes": [ - { - "src": "/(.*)", - "dest": "main.py" - } - ] -} \ No newline at end of file