Skip to content

Commit

Permalink
Merge branch 'yGuy/master' into release
Browse files Browse the repository at this point in the history
✨ Upgrade 2.1.2 from upstream
  • Loading branch information
takuya-o committed Aug 13, 2023
2 parents 13bfe96 + 4cae8f9 commit be8e4bf
Show file tree
Hide file tree
Showing 18 changed files with 1,345 additions and 4,091 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules/
/dist/
/*.crt
docker-compose.override.yml

Expand Down
14 changes: 4 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ FROM node:20-slim as npm_builder
#20.3.0-bookworm-slim (Debian 12)

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

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


# NPM runtime image
FROM node:20-slim as npm_runtime
Expand All @@ -23,12 +16,13 @@ WORKDIR /app

ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV
ENV PLUGINS=image-plugin,graph-plugin

# Avoid running as root:
USER node

COPY --from=npm_builder [ "/app/node_modules/", "./node_modules/" ]
COPY --from=npm_builder [ "/app/out/", "./out/" ]
COPY --from=npm_builder [ "/app/dist/", "./src/" ]
COPY [ "./license.md", "./" ]

ENTRYPOINT [ "node", "out/botservice.mjs" ]
90 changes: 30 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

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

Here's how to get the bot running - it's easy if you have a Docker server.
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!

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

You need
- the [Mattermost token](https://docs.mattermost.com/integrations/cloud-bot-accounts.html) for the bot user (`@chatgpt` by default)
- the [OpenAI API key](https://platform.openai.com/account/api-keys)
- a [Docker](https://www.docker.com/) server for continuously running the service, alternatively for testing, Node.js is sufficient.
- a [Docker](https://www.docker.com/) server for continuously running the service, alternatively for testing, Node.js 16 is sufficient.

Andrew Zigler from Mattermost created a [YouTube Video](https://www.youtube.com/watch?v=Hx4Ex7YZZiA) that quickly guides you through the setup.

Expand All @@ -16,45 +19,31 @@ If you want to learn more about how this plugin came to live, [read the blog pos

## Options

These are the available options, you can set them as environment variables when running [the script](./src/botservice.js)
or when [running the docker image](#using-the-ready-made-image) or when configuring your [docker-compose](#docker-compose) file.

| Name | Required | Example Value | Description |
|---------------------|----------|-----------------------------|---------------------------------------------------------------------------------------------|
| MATTERMOST_URL | yes | `https://mattermost.server` | The URL to the server. This is used for connecting the bot to the Mattermost API |
| MATTERMOST_TOKEN | yes | `abababacdcdcd` | The authentication token from the logged in mattermost bot |
| OPENAI_API_KEY | yes | `sk-234234234234234234` | The OpenAI API key to authenticate with OpenAI |
| 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 max_tokens parameter to pass to the OpenAI API, with a default value of 2000. API will answer up to this number of tokens |
| 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. |
| AZURE_OPENAI_API_KEY | no | `0123456789abcdefghijklmno` | The Azure OpenAI Service API key to authoenticate |
| 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' |
| MAX_PROMPT_TOKENS | no | `2000` | Maximum token count of the prompt passed to the OpenAI API. default is 2000 |
| DEBUG_LEVEL | no | `TRACE` | a debug level used for logging activity, defaults to `INFO` |
These are the available options, you can set them as environment variables when running [the script](./src/botservice.ts)
or when [running the docker image](#using-the-ready-made-docker-image) or when configuring your [docker-compose](#docker-compose) file.

| Name | Required | Example Value | Description |
|----------------------|----------|------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| MATTERMOST_URL | yes | `https://mattermost.server` | The URL to the server. This is used for connecting the bot to the Mattermost API |
| MATTERMOST_TOKEN | yes | `abababacdcdcd` | The authentication token from the logged in mattermost bot |
| OPENAI_API_KEY | yes | `sk-234234234234234234` | The OpenAI API key to authenticate with OpenAI |
| 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. |
| 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' |
| PLUGINS | no | `graph-plugin, image-plugin` | The enabled plugins of the bot. By default all plugins (grpah-plugin and image-plugin) are enabled. |
| DEBUG_LEVEL | no | `TRACE` | a debug level used for logging activity, defaults to `INFO` |
| BOT_CONTEXT_MSG | no | `15` | The number of previous messages which are appended to the conversation with ChatGPT, defaults to 100 |

> **Note**
> The `YFILES_SERVER_URL` is used for automatically converting text information created by the bot into diagrams.
> This is currently in development. You can see it in action, here:
> [LinkedIn Post](https://www.linkedin.com/posts/yguy_chatgpt-yfiles-diagramming-activity-7046713027005407232-2bKH)
> If you are interested in getting your hands on the plugin, please contact [yWorks](https://www.yworks.com)!
## Enhanced from the [original yGuy/chatgpt-mattermost-bot](https://github.com/yGuy/chatgpt-mattermost-bot)

* Support Azure OpenAI API
* Migrated to TypeScript.
+ Formatted by Prettier
+ Lint by eslint
+ Build by esbuild
* Token-count-based conversation thread management
* Splitting message that are too long
* Support GitLab AutoDevOps by test dummy

## Using the ready-made image
## Using the ready-made Docker image

Use the prebuilt image from [`gitlab.on-o.com/docker/chatgpt-mattermost-bot/release`](https://gitlab.on-o.com/Docker/chatgpt-mattermost-bot/container_registry/150)

Expand All @@ -67,29 +56,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 yourself
## Building the Docker image manually

First step is to clone this repo.

```bash
git clone https://github.com/takuya-o/chatgpt-mattermost-bot.git && cd chatgpt-mattermost-bot
```

For testing, you could now just run `npm ci` and `npm run start` or `npx ts-node src/botservice.ts` 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 install` and `npm run start` 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 PC, you can do the following:
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:

Build the docker image from the [Dockerfile](./Dockerfile):
```bash
Expand Down Expand Up @@ -171,15 +148,8 @@ services:
NODE_EXTRA_CA_CERTS: /certs/certfile.crt
```
### Run the container as a daemon
When all configuration is complete, build and start the container service.
Build docker image:
```bash
docker compose build
```

Start container:
### Run the container as a service
When all configuration is complete, start the container service.
```bash
docker compose up -d
```
Expand All @@ -205,10 +175,10 @@ 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 runnning 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.

Last but not least, check out [yWorks](https://www.yworks.com)' fine diagramming SDKs for software developers [yFiles](https://yworks.com/yfiles) and our [free online graph and diagram editors](https://yworks.com/editors)!

This is under MIT license Copyright (c) 2023 Sebastian Mueller (yWorks)
This is under MIT license Copyright (c) 2023 Sebastian Mueller (yWorks) and Michael Haeglsperger (yWorks)
Binary file modified mattermost-chat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit be8e4bf

Please sign in to comment.