Skip to content

Commit

Permalink
feat: support to specify token limits (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomeowww committed Oct 17, 2023
1 parent 75d4626 commit 717567b
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 836 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
# # OpenAI API 模型名称,默认值为 `gpt-3.5-turbo`,这是目前可用的最好的模型。
# OPENAI_API_MODEL_NAME=

# # OpenAI token limit, used to calculate text split and truncation before calling Chat Completion API, usually set to the max token limit of the model and let insights-bot decide how to handle, default is `4096`
# # OpenAI Token 限制,用于在调用 Chat Completion API 之前计算文本的分割和截断,一般设置为模型的最大令牌限制,然后交由 insights-bot 决定如何处理,默认为 `4096`
# OPENAI_API_TOKEN_LIMIT=4096

# # PostgreSQL database URL. Such as `postgres://postgres:postgres@localhost:5432/postgres`.
# # You could also suffix with `?search_path=<schema name>` if you want to specify a schema
# # PostgreSQL 数据库 URL。例如 `postgres://postgres:postgres@localhost:5432/postgres`。
Expand Down
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Arguments: None
```

```txt
[Forward a message]
<Forwarded messages>
```

```txt
Expand Down Expand Up @@ -183,20 +183,37 @@ $ ./insights-bot
docker run -it --rm -e TELEGRAM_BOT_TOKEN=<Telegram Bot API Token> -e OPENAI_API_SECRET=<OpenAI API Secret Key> -e DB_CONNECTION_STR="<PostgresSQL connection URL>" insights-bot ghcr.io/nekomeowww/insights-bot:latest
```

### Run with docker-compose
### Run with Docker Compose

Clone this project:

```shell
git clone github.com/nekomeowww/insights-bot
```

Or only copy or download the necessary `.env.example` and `docker-compose.yml` files (but you will only be able to run the bot with pre-bundled docker image):

```shell
curl -O https://raw.githubusercontent.com/nekomeowww/insights-bot/main/.env.example
curl -O https://raw.githubusercontent.com/nekomeowww/insights-bot/main/docker-compose.yml
```

Create your `.env` by making a copy of the contents from `.env.example` file. The .env file should be placed at the root of the project directory next to your `docker-compose.yml` file.

```shell
cp .env.example .env
```

Replace your OpenAI token and other environment variables in `.env`, and then run:

```shell
docker-compose --profile hub up -d
docker compose --profile hub up -d
```

If you prefer run docker image from local codes, then run:
If you prefer run docker image from local codes (which means build it manually, you will need the entire source code of this project), then run:

```shell
docker-compose --profile local up -d --build
docker compose --profile local up -d --build
```

### Build on your own
Expand Down Expand Up @@ -236,7 +253,8 @@ docker buildx build --platform linux/arm64,linux/amd64 -t <tag> -f Dockerfile .
| `OPENAI_API_SECRET` | `true` | | OpenAI API Secret Key that looks like `sk-************************************************`, you can obtain one by signing in to OpenAI platform and create one at [http://platform.openai.com/account/api-keys](http://platform.openai.com/account/api-keys). |
| `OPENAI_API_HOST` | `false` | `https://api.openai.com` | OpenAI API Host, you can specify one if you have a relay or reversed proxy configured. Such as `https://openai.example.workers.dev` |
| `OPENAI_API_MODEL_NAME` | `false` | `gpt-3.5-turbo` | OpenAI API model name, default is `gpt-3.5-turbo`, you can specify one if you want to use another model. Such as `gpt-4` |
| `DB_CONNECTION_STR` | `true` | | PostgreSQL database URL. Such as `postgres://postgres:postgres@localhost:5432/postgres`. You could also suffix with `?search_path=<schema name>` if you want to specify a schema |
| `OPENAI_API_TOKEN_LIMIT` | `false` | `4096` | OpenAI API token limit used to computed the splits and truncations of texts before calling Chat Completion API generally set to the maximum token limit of a model, and let insights-bot to determine how to process it, default is `4096` |
| `DB_CONNECTION_STR` | `true` | `postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable` | PostgreSQL database URL. Such as `postgres://postgres:postgres@localhost:5432/postgres`. You could also suffix with `?search_path=<schema name>` if you want to specify a schema. |
| `SLACK_CLIENT_ID` | `false` | | Slack app client id, you can create a slack app and get it, see: [tutorial](https://api.slack.com/tutorials/slack-apps-and-postman) |
| `SLACK_CLIENT_SECRET` | `false` | | Slack app client secret, you can create a slack app and get it, see: [tutorial](https://api.slack.com/tutorials/slack-apps-and-postman) |
| `SLACK_WEBHOOK_PORT` | `false` | `7070` | Port for Slack Bot/App Webhook server, default is 7070 |
Expand Down
28 changes: 23 additions & 5 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,37 @@ $ ./insights-bot
docker run -it --rm -e TELEGRAM_BOT_TOKEN=<Telegram Bot API 令牌> -e OPENAI_API_SECRET=<OpenAI API 密钥y> -e DB_CONNECTION_STR="<PostgresSQL 连接 URL>" insights-bot ghcr.io/nekomeowww/insights-bot:latest
```

### 使用 docker-compose 运行
### 使用 Docker Compose 运行

克隆这个项目:

```shell
git clone github.com/nekomeowww/insights-bot
```

或者只复制或下载必要的`.env.example``docker-compose.yml`文件(但只能使用预构建的 Docker 镜像来运行 insights-bot):

```shell
curl -O https://raw.githubusercontent.com/nekomeowww/insights-bot/main/.env.example
curl -O https://raw.githubusercontent.com/nekomeowww/insights-bot/main/docker-compose.yml
```

通过复制 `.env.example` 文件中的内容来创建 `.env` 文件。`.env` 文件应该放在项目根目录下,与 `docker-compose.yml` 文件同级。

```shell
cp .env.example .env
```

通过替换 `.env` 文件中的 OpenAI 令牌和其他环境变量,然后运行:

```shell
docker-compose --profile hub up -d
docker compose --profile hub up -d
```

如果你想从本地代码编译、构建并运行 Docker 镜像,那么运行:
如果你想从本地代码编译、构建并运行 Docker 镜像(也就是手动构建,你需要这个项目的全部源代码,可以选择先克隆下来),那么运行:

```shell
docker-compose --profile local up -d --build
docker compose --profile local up -d --build
```

### 亲自构建
Expand Down Expand Up @@ -238,7 +255,8 @@ docker buildx build --platform linux/arm64,linux/amd64 -t <tag> -f Dockerfile .
| `OPENAI_API_SECRET` | `true` | | OpenAI API 密钥,通常类似于 `sk-************************************************` 的结构,你可以登录到 Open AI 并在 [http://platform.openai.com/account/api-keys](http://platform.openai.com/account/api-keys) 上创建一个。 |
| `OPENAI_API_HOST` | `false` | `https://api.openai.com` | OpenAI API 的域名,如果配置了中继或反向代理,则可以指定一个。比如 `https://openai.example.workers.dev` |
| `OPENAI_API_MODEL_NAME` | `false` | `gpt-3.5-turbo` | OpenAI API 模型名称,默认为 `gpt-3.5-turbo`,如果你使用其他模型,比如 `gpt-4` 则可以制指定一个。 |
| `DB_CONNECTION_STR` | `true` | | PostgreSQL 数据库连接 URL。结构类似于 `postgres://postgres:postgres@localhost:5432/postgres`。如果你需要指定 schema,则可以通过在后缀加上 `?search_path=<schema name>` 来实现。 |
| `OPENAI_API_TOKEN_LIMIT` | `false` | `4096` | OpenAI API Token 限制,用于在调用 Chat Completion API 之前计算文本的分割和截断,一般设置为模型的最大令牌限制,然后交由 insights-bot 决定如何处理,默认为 `4096`|
| `DB_CONNECTION_STR` | `true` | `postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable` | PostgreSQL 数据库连接 URL。结构类似于 `postgres://postgres:postgres@localhost:5432/postgres`。如果你需要指定 schema,则可以通过在后缀加上 `?search_path=<schema name>` 来实现。 |
| `SLACK_CLIENT_ID` | `false` | | Slack app client id,你可以参考[教程](https://api.slack.com/tutorials/slack-apps-and-postman)来创建一个。 |
| `SLACK_CLIENT_SECRET` | `false` | | Slack app client secret,你可以参考[教程](https://api.slack.com/tutorials/slack-apps-and-postman)来创建一个。 |
| `SLACK_WEBHOOK_PORT` | `false` | `7070` | Slack Bot/App Webhook 服务监听端口,默认为 7070。 |
Expand Down
36 changes: 28 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@ services:
# default insights-bot service that would pull the image from docker hub
# and runs on production environment with db_local service and redis_local
# service
#
# you need to use either docker compose up --profile hub -d or docker-compose --profile hub up -d
# to ask docker compose to run this service
insights_bot:
profiles:
- hub
image: ghcr.io/nekomeowww/insights-bot:latest
restart: always
environment:
- TIMEZONE_SHIFT_SECONDS
- TELEGRAM_BOT_TOKEN
- OPENAI_API_SECRET
- OPENAI_API_HOST
- OPENAI_API_MODEL_NAME
- SLACK_CLIENT_ID
- SLACK_CLIENT_SECRET
- DB_CONNECTION_STR=postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable
- OPENAI_API_TOKEN_LIMIT
- DB_CONNECTION_STR
- REDIS_HOST=redis_local
- REDIS_PORT=6379
- REDIS_TLS_ENABLED=false
- REDIS_PASSWORD=123456
- LOG_LEVEL
# # uncomment the following lines if you want to enable discord bot webhook
# # uncomment the following lines if you configured telegram webhook in .env file
# - TELEGRAM_BOT_WEBHOOK_URL
# - TELEGRAM_BOT_WEBHOOK_PORT
# # uncomment the following lines if you configured slack and it's webhook options in .env file
# - SLACK_CLIENT_ID
# - SLACK_CLIENT_SECRET
# - SLACK_WEBHOOK_PORT
# # uncomment the following lines if you configured discord bot webhook in .env file
# - DISCORD_BOT_TOKEN
# - DISCORD_BOT_PUBLIC_KEY
# - DISCORD_BOT_WEBHOOK_PORT
Expand All @@ -45,6 +55,9 @@ services:
# On-demand build insights-bot service that would build the image from
# source code and runs on production environment with db_local service
# and redis_local service
#
# you need to use either docker compose up --profile local -d or docker-compose --profile local up -d
# to ask docker compose to run this service
insights_bot_local:
profiles:
- local
Expand All @@ -54,19 +67,26 @@ services:
image: insights-bot
restart: always
environment:
- TIMEZONE_SHIFT_SECONDS
- TELEGRAM_BOT_TOKEN
- OPENAI_API_SECRET
- OPENAI_API_HOST
- OPENAI_API_MODEL_NAME
- SLACK_CLIENT_ID
- SLACK_CLIENT_SECRET
- DB_CONNECTION_STR=postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable
- OPENAI_API_TOKEN_LIMIT
- DB_CONNECTION_STR
- REDIS_HOST=redis_local
- REDIS_PORT=6379
- REDIS_TLS_ENABLED=false
- REDIS_PASSWORD=123456
- LOG_LEVEL
# # uncomment the following lines if you want to enable discord bot webhook
# # uncomment the following lines if you configured telegram webhook in .env file
# - TELEGRAM_BOT_WEBHOOK_URL
# - TELEGRAM_BOT_WEBHOOK_PORT
# # uncomment the following lines if you configured slack and it's webhook options in .env file
# - SLACK_CLIENT_ID
# - SLACK_CLIENT_SECRET
# - SLACK_WEBHOOK_PORT
# # uncomment the following lines if you configured discord bot webhook in .env file
# - DISCORD_BOT_TOKEN
# - DISCORD_BOT_PUBLIC_KEY
# - DISCORD_BOT_WEBHOOK_PORT
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ require (
go.uber.org/multierr v1.11.0
go.uber.org/ratelimit v0.3.0
go.uber.org/zap v1.26.0
gopkg.in/telebot.v3 v3.1.3
)

require (
Expand Down
Loading

0 comments on commit 717567b

Please sign in to comment.