Skip to content

Commit

Permalink
🧰 Made upstream 2.1.2 features work fine
Browse files Browse the repository at this point in the history
✨ 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
  • Loading branch information
takuya-o committed Aug 13, 2023
1 parent be8e4bf commit fde42d4
Show file tree
Hide file tree
Showing 26 changed files with 5,699 additions and 1,539 deletions.
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/typescript-node
{
"name": "Node.js & TypeScript",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/typescript-node:1-16-bookworm"

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
1 change: 1 addition & 0 deletions .env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ OPENAI_API_KEY=sk-234234234234234234
#AZURE_OPENAI_API_DEPLOYMENT_NAME=gpt-35-turbo
#AZURE_OPENAI_API_VERSION=2023-05-15

PLUGINS=image-plugin
#MAX_PROMPT_TOKEN=3000
#OPENAI_MAX_TOKENS=2000
#OPENAI_TEMPERATURE=1
Expand Down
1 change: 1 addition & 0 deletions .gitallowed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
README.md
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/*.crt
docker-compose.override.yml

.env
/.vscode/launch.json
!.env-sample
.env*
# envFileを使えは問題なし /.vscode/launch.json
/out/

/.npm/
Expand Down Expand Up @@ -70,7 +71,8 @@ flycheck_*.el
.LSOverride

# Icon must end with two \r
Icon
Icon


# Thumbnails
._*
Expand Down
56 changes: 56 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
// IntelliSense を使用して利用可能な属性を学べます。
// 既存の属性の説明をホバーして表示します。
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run npm start by env.debug",
"request": "launch",
"envFile": "${workspaceFolder}/.env.debug",
"command": "npm start",
"type": "node-terminal"
},
{
"name": "Run npm start by OpenAI",
"request": "launch",
"envFile": "${workspaceFolder}/.env.openai",
"command": "npm start",
"type": "node-terminal"
},
{
"name": "TypeScriptプログラムの起動 via NPM",
"request": "launch",
"runtimeArgs": [
"run",
"start"
],
"envFile": "${workspaceFolder}/.env.debug",
"runtimeExecutable": "npm",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
{
"name": "Launch Program",
"program": "${workspaceFolder}/dist/botservice.mjs",
"envFile": "${workspaceFolder}/.env.debug",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
{
"name": "tsc output Launch Program",
"program": "${workspaceFolder}/out/botservice.js",
"envFile": "${workspaceFolder}/.env.debug",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
}
]
}
17 changes: 12 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# NPM builder image
FROM node:20-slim as npm_builder
FROM node:16-slim as npm_builder
#20.3.0-bookworm-slim (Debian 12)

WORKDIR /app
COPY [ "package.json", "package-lock.json", "tsconfig.json", "./"]
COPY [ "package.json", "package-lock.json", ".npmrc", \
".eslintrc.json", ".prettierignore", ".prettierrc", \
"tsconfig.json", \
"esbuild.config.js", \
"./" ]
COPY [ "src/", "./src/" ]

RUN npm ci --omit dev
RUN npm ci
RUN npm run lint
RUN npm run build
RUN rm -rf node_modules/ && npm ci --omit dev


# NPM runtime image
FROM node:20-slim as npm_runtime
FROM node:16-slim as npm_runtime

WORKDIR /app

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

ENTRYPOINT [ "node", "out/botservice.mjs" ]
ENTRYPOINT [ "node", "src/botservice.mjs" ]
44 changes: 40 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
## Enhanced from the [original yGuy/chatgpt-mattermost-bot](https://github.com/yGuy/chatgpt-mattermost-bot)

* Support Azure OpenAI API
+ Use the original OpenAI for image generation even when using Azure OpenAI API
* Build enhancement (The original is now in TypeScript as well by version 2.0.)
+ Formatted by Prettier
+ Lint by eslint
+ Build by esbuild
+ SWC for debug
* Token-count-based conversation thread management
* Splitting message that are too long
* Support GitLab AutoDevOps by test dummy

# A ChatGPT-powered Chatbot for Mattermost

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

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

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

Expand All @@ -30,6 +43,11 @@ or when [running the docker image](#using-the-ready-made-docker-image) or when c
| OPENAI_MODEL_NAME | no | `gpt-3.5-turbo` | The OpenAI language model to use, defaults to `gpt-3.5-turbo` |
| OPENAI_MAX_TOKENS | no | `2000` | The maximum number of tokens to pass to the OpenAI API, defaults to 2000 |
| 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. |
| MAX_PROMPT_TOKENS | no | `2096` | Maximum number of prompt tokens, default to 2000. For example on GPT-4 = 8196 - OPENAI_MAX_TOKENS. |
| 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. |
| AZURE_OPENAI_API_INSTANCE_NAME | no | `example-name` | The instance name on the Azure OpenAI Service |
| AZURE_OPENAI_API_DEPLOYMENT_NAME | no | `gpt-35-turbo` | The name of the deployed model on the Azure OpenAI Service |
| AZURE_OPENAI_API_VERSION | no | `2023-03-15-preview` | The Azure OpenAI version |
| YFILES_SERVER_URL | no | `http://localhost:3835` | The URL to the yFiles graph service for embedding auto-generated diagrams. |
| 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 |
| MATTERMOST_BOTNAME | no | `"@chatgpt"` | the name of the bot user in Mattermost, defaults to '@chatgpt' |
Expand All @@ -56,6 +74,17 @@ docker run -d --restart unless-stopped \
gitlab.on-o.com/docker/chatgpt-mattermost-bot/release:latest
```

As Azure OpenAI Service case
```bash
docker run -d --restart unless-stopped \
-e MATTERMOST_URL=https://mattermost.server \
-e MATTERMOST_TOKEN=abababacdcdcd \
-e AZURE_OPENAI_API_KEY=234234234234234234 \
-e AZURE_OPENAI_API_INSTANCE_NAME=example-name \
--name chatbot \
gitlab.on-o.com/docker/chatgpt-mattermost-bot/release:latest
```

## Building the Docker image manually

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

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!
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!

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:

Expand Down Expand Up @@ -149,7 +178,14 @@ services:
```
### Run the container as a service
When all configuration is complete, start the container service.
When all configuration is complete, build and start the container service.
Build docker image:
```bash
docker compose build
```

Start container:
```bash
docker compose up -d
```
Expand All @@ -175,7 +211,7 @@ Here's an example chat that I just had with our bot:
>chatgpt (BOT):
>>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!
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!
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!

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

Expand Down
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
npx esbuild --bundle --minify --sourcemap --platform=node --format=esm --packages=external --outfile=dist/botservice.mjs src/botservice.ts
cp -p ./node_modules/tiktoken-node/dist/tiktoken-node.linux-x64-gnu.node ./dist/
5 changes: 5 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ services:
environment:
MATTERMOST_URL: ${MATTERMOST_URL}
MATTERMOST_TOKEN: ${MATTERMOST_TOKEN}
MATTERMOST_BOTNAME: ${MATTERMOST_BOTNAME}
OPENAI_API_KEY: ${OPENAI_API_KEY}
OPENAI_MODEL_NAME: ${OPENAI_MODEL_NAME}
OPENAI_MAX_TOKENS: ${OPENAI_MAX_TOKENS}
MAX_PROMPT_TOKENS: ${MAX_PROMPT_TOKENS}
PLUGINS: ${PLUGINS}
AZURE_OPENAI_API_KEY: ${AZURE_OPENAI_API_KEY}
AZURE_OPENAI_API_INSTANCE_NAME: ${AZURE_OPENAI_API_INSTANCE_NAME}
AZURE_OPENAI_API_DEPLOYMENT_NAME: ${AZURE_OPENAI_API_DEPLOYMENT_NAME}
Expand Down
Loading

0 comments on commit fde42d4

Please sign in to comment.