Skip to content

Commit fde42d4

Browse files
committed
🧰 Made upstream 2.1.2 features work fine
✨ feat(openai-wrapper.ts): Added a function to use the original OpenAI for image generation even if you are using Azure OpenAI 🔧 fix(Dockerfile, compose.yaml): downgrade Node version from 20 to 16 and add new environment variables 🐛 fix(botservice.ts): correct update of answer variable in postMessage function to prevent duplication of error messages 🔧 Refactoring: Converted plugin and types files to ES modules and performed code formatting 🔧 fix(tsconfig.json): update module resolution settings 🔧 fix(tsconfig.json): change TypeScript module resolution strategy from "node" to "bundler", set baseUrl and paths 🔧 chore(esbuild.config.js): change output directory from 'out' to 'dist' ✨ feat(build.sh): add build script 🔧 chore(.gitignore): update .gitignore to ignore .env extensions 📝 docs(README.md): Update project details to reflect new features and changes 🔥 remove(openai-thread-completion.ts): deleted openai-thread-completion.ts file
1 parent be8e4bf commit fde42d4

26 files changed

+5699
-1539
lines changed

.devcontainer/devcontainer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/typescript-node
3+
{
4+
"name": "Node.js & TypeScript",
5+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6+
"image": "mcr.microsoft.com/devcontainers/typescript-node:1-16-bookworm"
7+
8+
// Features to add to the dev container. More info: https://containers.dev/features.
9+
// "features": {},
10+
11+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
12+
// "forwardPorts": [],
13+
14+
// Use 'postCreateCommand' to run commands after the container is created.
15+
// "postCreateCommand": "yarn install",
16+
17+
// Configure tool-specific properties.
18+
// "customizations": {},
19+
20+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
21+
// "remoteUser": "root"
22+
}

.env-sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ OPENAI_API_KEY=sk-234234234234234234
66
#AZURE_OPENAI_API_DEPLOYMENT_NAME=gpt-35-turbo
77
#AZURE_OPENAI_API_VERSION=2023-05-15
88

9+
PLUGINS=image-plugin
910
#MAX_PROMPT_TOKEN=3000
1011
#OPENAI_MAX_TOKENS=2000
1112
#OPENAI_TEMPERATURE=1

.gitallowed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
README.md

.gitignore

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
/*.crt
44
docker-compose.override.yml
55

6-
.env
7-
/.vscode/launch.json
6+
!.env-sample
7+
.env*
8+
# envFileを使えは問題なし /.vscode/launch.json
89
/out/
910

1011
/.npm/
@@ -70,7 +71,8 @@ flycheck_*.el
7071
.LSOverride
7172

7273
# Icon must end with two \r
73-
Icon
74+
Icon
75+
7476

7577
# Thumbnails
7678
._*

.vscode/launch.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
// IntelliSense を使用して利用可能な属性を学べます。
3+
// 既存の属性の説明をホバーして表示します。
4+
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Run npm start by env.debug",
9+
"request": "launch",
10+
"envFile": "${workspaceFolder}/.env.debug",
11+
"command": "npm start",
12+
"type": "node-terminal"
13+
},
14+
{
15+
"name": "Run npm start by OpenAI",
16+
"request": "launch",
17+
"envFile": "${workspaceFolder}/.env.openai",
18+
"command": "npm start",
19+
"type": "node-terminal"
20+
},
21+
{
22+
"name": "TypeScriptプログラムの起動 via NPM",
23+
"request": "launch",
24+
"runtimeArgs": [
25+
"run",
26+
"start"
27+
],
28+
"envFile": "${workspaceFolder}/.env.debug",
29+
"runtimeExecutable": "npm",
30+
"skipFiles": [
31+
"<node_internals>/**"
32+
],
33+
"type": "node"
34+
},
35+
{
36+
"name": "Launch Program",
37+
"program": "${workspaceFolder}/dist/botservice.mjs",
38+
"envFile": "${workspaceFolder}/.env.debug",
39+
"request": "launch",
40+
"skipFiles": [
41+
"<node_internals>/**"
42+
],
43+
"type": "node"
44+
},
45+
{
46+
"name": "tsc output Launch Program",
47+
"program": "${workspaceFolder}/out/botservice.js",
48+
"envFile": "${workspaceFolder}/.env.debug",
49+
"request": "launch",
50+
"skipFiles": [
51+
"<node_internals>/**"
52+
],
53+
"type": "node"
54+
}
55+
]
56+
}

Dockerfile

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
# NPM builder image
2-
FROM node:20-slim as npm_builder
2+
FROM node:16-slim as npm_builder
33
#20.3.0-bookworm-slim (Debian 12)
44

55
WORKDIR /app
6-
COPY [ "package.json", "package-lock.json", "tsconfig.json", "./"]
6+
COPY [ "package.json", "package-lock.json", ".npmrc", \
7+
".eslintrc.json", ".prettierignore", ".prettierrc", \
8+
"tsconfig.json", \
9+
"esbuild.config.js", \
10+
"./" ]
711
COPY [ "src/", "./src/" ]
812

9-
RUN npm ci --omit dev
13+
RUN npm ci
14+
RUN npm run lint
1015
RUN npm run build
16+
RUN rm -rf node_modules/ && npm ci --omit dev
17+
1118

1219
# NPM runtime image
13-
FROM node:20-slim as npm_runtime
20+
FROM node:16-slim as npm_runtime
1421

1522
WORKDIR /app
1623

@@ -25,4 +32,4 @@ COPY --from=npm_builder [ "/app/node_modules/", "./node_modules/" ]
2532
COPY --from=npm_builder [ "/app/dist/", "./src/" ]
2633
COPY [ "./license.md", "./" ]
2734

28-
ENTRYPOINT [ "node", "out/botservice.mjs" ]
35+
ENTRYPOINT [ "node", "src/botservice.mjs" ]

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
## Enhanced from the [original yGuy/chatgpt-mattermost-bot](https://github.com/yGuy/chatgpt-mattermost-bot)
2+
3+
* Support Azure OpenAI API
4+
+ Use the original OpenAI for image generation even when using Azure OpenAI API
5+
* Build enhancement (The original is now in TypeScript as well by version 2.0.)
6+
+ Formatted by Prettier
7+
+ Lint by eslint
8+
+ Build by esbuild
9+
+ SWC for debug
10+
* Token-count-based conversation thread management
11+
* Splitting message that are too long
12+
* Support GitLab AutoDevOps by test dummy
13+
114
# A ChatGPT-powered Chatbot for Mattermost
215

316
![A chat window in Mattermost showing the chat between the OpenAI bot and "yGuy"](./mattermost-chat.png)
417

518
The bot can talk to you like a regular mattermost user. It's like having chat.openai.com built collaboratively built into Mattermost!
6-
But that's not all, you can also use it to generate images via Dall-E or diagram visualizations via a yFiles plugin!
19+
But that's not all, you can also use it to generate images via Dall-E or diagram visualizations via a yFiles plugin!
720

821
Here's how to get the bot running - it's easy if you have a Docker host.
922

@@ -30,6 +43,11 @@ or when [running the docker image](#using-the-ready-made-docker-image) or when c
3043
| OPENAI_MODEL_NAME | no | `gpt-3.5-turbo` | The OpenAI language model to use, defaults to `gpt-3.5-turbo` |
3144
| OPENAI_MAX_TOKENS | no | `2000` | The maximum number of tokens to pass to the OpenAI API, defaults to 2000 |
3245
| OPENAI_TEMPERATURE | no | `0.2` | The sampling temperature to use, between 0 and 2, defaults to 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. |
46+
| MAX_PROMPT_TOKENS | no | `2096` | Maximum number of prompt tokens, default to 2000. For example on GPT-4 = 8196 - OPENAI_MAX_TOKENS. |
47+
| AZURE_OPENAI_API_KEY | no | `0123456789abcdefghijklmno` | The Azure OpenAI Service API key to authoenticate. If OPENAI_API_KEY is also set, the original OpenAI is used for image generation. |
48+
| AZURE_OPENAI_API_INSTANCE_NAME | no | `example-name` | The instance name on the Azure OpenAI Service |
49+
| AZURE_OPENAI_API_DEPLOYMENT_NAME | no | `gpt-35-turbo` | The name of the deployed model on the Azure OpenAI Service |
50+
| AZURE_OPENAI_API_VERSION | no | `2023-03-15-preview` | The Azure OpenAI version |
3351
| YFILES_SERVER_URL | no | `http://localhost:3835` | The URL to the yFiles graph service for embedding auto-generated diagrams. |
3452
| NODE_EXTRA_CA_CERTS | no | `/file/to/cert.crt` | a link to a certificate file to pass to node.js for authenticating self-signed certificates |
3553
| MATTERMOST_BOTNAME | no | `"@chatgpt"` | the name of the bot user in Mattermost, defaults to '@chatgpt' |
@@ -56,6 +74,17 @@ docker run -d --restart unless-stopped \
5674
gitlab.on-o.com/docker/chatgpt-mattermost-bot/release:latest
5775
```
5876

77+
As Azure OpenAI Service case
78+
```bash
79+
docker run -d --restart unless-stopped \
80+
-e MATTERMOST_URL=https://mattermost.server \
81+
-e MATTERMOST_TOKEN=abababacdcdcd \
82+
-e AZURE_OPENAI_API_KEY=234234234234234234 \
83+
-e AZURE_OPENAI_API_INSTANCE_NAME=example-name \
84+
--name chatbot \
85+
gitlab.on-o.com/docker/chatgpt-mattermost-bot/release:latest
86+
```
87+
5988
## Building the Docker image manually
6089

6190
First step is to clone this repo.
@@ -64,7 +93,7 @@ First step is to clone this repo.
6493
git clone https://github.com/takuya-o/chatgpt-mattermost-bot.git && cd chatgpt-mattermost-bot
6594
```
6695

67-
For testing, you could now just run `npm install` and `npm run start` directly, but be sure to set the [environment variables](#options) or pass them to the node process, first!
96+
For testing, you could now just run `npm ci` and `npm run start` or directly, but be sure to set the [environment variables](#options) or pass them to the node process, first!
6897

6998
For production use, in order to create a service on a docker container that will always provide the service without you having to run it on your own machine, you can do the following:
7099

@@ -149,7 +178,14 @@ services:
149178
```
150179
151180
### Run the container as a service
152-
When all configuration is complete, start the container service.
181+
When all configuration is complete, build and start the container service.
182+
183+
Build docker image:
184+
```bash
185+
docker compose build
186+
```
187+
188+
Start container:
153189
```bash
154190
docker compose up -d
155191
```
@@ -175,7 +211,7 @@ Here's an example chat that I just had with our bot:
175211
>chatgpt (BOT):
176212
>>Hello there! I'm @chatgpt, a friendly and reliable Mattermost bot designed to make your communication experience smoother and more efficient. I'm honored to be part of the team at yWorks, where I help employees get things done faster and with less hassle. With my advanced machine learning algorithms, I'm capable of understanding complex natural language queries and providing succinct and accurate responses in a matter of seconds. Whether you're a developer looking for information on your codebase or a manager seeking to streamline your team's workflows, I'm here to help. Installing a Mattermost chatbot like me is a must for any organization looking to boost its productivity and enjoy a more pleasant communication experience. So what are you waiting for? Let's chat!
177213
178-
I built this tool as a quick hack on a rainy Saturday afternoon, but we are using the bot in production in our Mattermost instance at our office at [yworks](https://www.yworks.com) and the bot has proved to be very helpful for many simple tasks. Give it a try and provide feedback if you like! It's really not very expensive: We had it running for about 30 users for two weeks and that cost us less than half a dollar for the ChatGPT service!
214+
I built this tool as a quick hack on a rainy Saturday afternoon, but we are using the bot in production in our Mattermost instance at our office at [yworks](https://www.yworks.com) and the bot has proved to be very helpful for many simple tasks. Give it a try and provide feedback if you like! It's really not very expensive: We had it running for about 30 users for two weeks and that cost us less than half a dollar for the ChatGPT service!
179215

180216
I will also accept helpful pull requests if you find an issue or have an idea for an improvement.
181217

build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
npx esbuild --bundle --minify --sourcemap --platform=node --format=esm --packages=external --outfile=dist/botservice.mjs src/botservice.ts
3+
cp -p ./node_modules/tiktoken-node/dist/tiktoken-node.linux-x64-gnu.node ./dist/

compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ services:
77
environment:
88
MATTERMOST_URL: ${MATTERMOST_URL}
99
MATTERMOST_TOKEN: ${MATTERMOST_TOKEN}
10+
MATTERMOST_BOTNAME: ${MATTERMOST_BOTNAME}
1011
OPENAI_API_KEY: ${OPENAI_API_KEY}
12+
OPENAI_MODEL_NAME: ${OPENAI_MODEL_NAME}
13+
OPENAI_MAX_TOKENS: ${OPENAI_MAX_TOKENS}
14+
MAX_PROMPT_TOKENS: ${MAX_PROMPT_TOKENS}
15+
PLUGINS: ${PLUGINS}
1116
AZURE_OPENAI_API_KEY: ${AZURE_OPENAI_API_KEY}
1217
AZURE_OPENAI_API_INSTANCE_NAME: ${AZURE_OPENAI_API_INSTANCE_NAME}
1318
AZURE_OPENAI_API_DEPLOYMENT_NAME: ${AZURE_OPENAI_API_DEPLOYMENT_NAME}

0 commit comments

Comments
 (0)