Skip to content

Commit 717567b

Browse files
authored
feat: support to specify token limits (#179)
1 parent 75d4626 commit 717567b

File tree

10 files changed

+111
-836
lines changed

10 files changed

+111
-836
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
# # OpenAI API 模型名称,默认值为 `gpt-3.5-turbo`,这是目前可用的最好的模型。
5252
# OPENAI_API_MODEL_NAME=
5353

54+
# # 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`
55+
# # OpenAI Token 限制,用于在调用 Chat Completion API 之前计算文本的分割和截断,一般设置为模型的最大令牌限制,然后交由 insights-bot 决定如何处理,默认为 `4096`
56+
# OPENAI_API_TOKEN_LIMIT=4096
57+
5458
# # PostgreSQL database URL. Such as `postgres://postgres:postgres@localhost:5432/postgres`.
5559
# # You could also suffix with `?search_path=<schema name>` if you want to specify a schema
5660
# # PostgreSQL 数据库 URL。例如 `postgres://postgres:postgres@localhost:5432/postgres`。

README.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Arguments: None
139139
```
140140

141141
```txt
142-
[Forward a message]
142+
<Forwarded messages>
143143
```
144144

145145
```txt
@@ -183,20 +183,37 @@ $ ./insights-bot
183183
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
184184
```
185185

186-
### Run with docker-compose
186+
### Run with Docker Compose
187+
188+
Clone this project:
189+
190+
```shell
191+
git clone github.com/nekomeowww/insights-bot
192+
```
193+
194+
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):
195+
196+
```shell
197+
curl -O https://raw.githubusercontent.com/nekomeowww/insights-bot/main/.env.example
198+
curl -O https://raw.githubusercontent.com/nekomeowww/insights-bot/main/docker-compose.yml
199+
```
187200

188201
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.
189202

203+
```shell
204+
cp .env.example .env
205+
```
206+
190207
Replace your OpenAI token and other environment variables in `.env`, and then run:
191208

192209
```shell
193-
docker-compose --profile hub up -d
210+
docker compose --profile hub up -d
194211
```
195212

196-
If you prefer run docker image from local codes, then run:
213+
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:
197214

198215
```shell
199-
docker-compose --profile local up -d --build
216+
docker compose --profile local up -d --build
200217
```
201218

202219
### Build on your own
@@ -236,7 +253,8 @@ docker buildx build --platform linux/arm64,linux/amd64 -t <tag> -f Dockerfile .
236253
| `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). |
237254
| `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` |
238255
| `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` |
239-
| `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 |
256+
| `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` |
257+
| `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. |
240258
| `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) |
241259
| `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) |
242260
| `SLACK_WEBHOOK_PORT` | `false` | `7070` | Port for Slack Bot/App Webhook server, default is 7070 |

README.zh-CN.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,37 @@ $ ./insights-bot
185185
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
186186
```
187187

188-
### 使用 docker-compose 运行
188+
### 使用 Docker Compose 运行
189+
190+
克隆这个项目:
191+
192+
```shell
193+
git clone github.com/nekomeowww/insights-bot
194+
```
195+
196+
或者只复制或下载必要的`.env.example``docker-compose.yml`文件(但只能使用预构建的 Docker 镜像来运行 insights-bot):
197+
198+
```shell
199+
curl -O https://raw.githubusercontent.com/nekomeowww/insights-bot/main/.env.example
200+
curl -O https://raw.githubusercontent.com/nekomeowww/insights-bot/main/docker-compose.yml
201+
```
189202

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

205+
```shell
206+
cp .env.example .env
207+
```
208+
192209
通过替换 `.env` 文件中的 OpenAI 令牌和其他环境变量,然后运行:
193210

194211
```shell
195-
docker-compose --profile hub up -d
212+
docker compose --profile hub up -d
196213
```
197214

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

200217
```shell
201-
docker-compose --profile local up -d --build
218+
docker compose --profile local up -d --build
202219
```
203220

204221
### 亲自构建
@@ -238,7 +255,8 @@ docker buildx build --platform linux/arm64,linux/amd64 -t <tag> -f Dockerfile .
238255
| `OPENAI_API_SECRET` | `true` | | OpenAI API 密钥,通常类似于 `sk-************************************************` 的结构,你可以登录到 Open AI 并在 [http://platform.openai.com/account/api-keys](http://platform.openai.com/account/api-keys) 上创建一个。 |
239256
| `OPENAI_API_HOST` | `false` | `https://api.openai.com` | OpenAI API 的域名,如果配置了中继或反向代理,则可以指定一个。比如 `https://openai.example.workers.dev` |
240257
| `OPENAI_API_MODEL_NAME` | `false` | `gpt-3.5-turbo` | OpenAI API 模型名称,默认为 `gpt-3.5-turbo`,如果你使用其他模型,比如 `gpt-4` 则可以制指定一个。 |
241-
| `DB_CONNECTION_STR` | `true` | | PostgreSQL 数据库连接 URL。结构类似于 `postgres://postgres:postgres@localhost:5432/postgres`。如果你需要指定 schema,则可以通过在后缀加上 `?search_path=<schema name>` 来实现。 |
258+
| `OPENAI_API_TOKEN_LIMIT` | `false` | `4096` | OpenAI API Token 限制,用于在调用 Chat Completion API 之前计算文本的分割和截断,一般设置为模型的最大令牌限制,然后交由 insights-bot 决定如何处理,默认为 `4096`|
259+
| `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>` 来实现。 |
242260
| `SLACK_CLIENT_ID` | `false` | | Slack app client id,你可以参考[教程](https://api.slack.com/tutorials/slack-apps-and-postman)来创建一个。 |
243261
| `SLACK_CLIENT_SECRET` | `false` | | Slack app client secret,你可以参考[教程](https://api.slack.com/tutorials/slack-apps-and-postman)来创建一个。 |
244262
| `SLACK_WEBHOOK_PORT` | `false` | `7070` | Slack Bot/App Webhook 服务监听端口,默认为 7070。 |

docker-compose.yml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,35 @@ services:
44
# default insights-bot service that would pull the image from docker hub
55
# and runs on production environment with db_local service and redis_local
66
# service
7+
#
8+
# you need to use either docker compose up --profile hub -d or docker-compose --profile hub up -d
9+
# to ask docker compose to run this service
710
insights_bot:
811
profiles:
912
- hub
1013
image: ghcr.io/nekomeowww/insights-bot:latest
1114
restart: always
1215
environment:
16+
- TIMEZONE_SHIFT_SECONDS
1317
- TELEGRAM_BOT_TOKEN
1418
- OPENAI_API_SECRET
1519
- OPENAI_API_HOST
1620
- OPENAI_API_MODEL_NAME
17-
- SLACK_CLIENT_ID
18-
- SLACK_CLIENT_SECRET
19-
- DB_CONNECTION_STR=postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable
21+
- OPENAI_API_TOKEN_LIMIT
22+
- DB_CONNECTION_STR
2023
- REDIS_HOST=redis_local
2124
- REDIS_PORT=6379
2225
- REDIS_TLS_ENABLED=false
2326
- REDIS_PASSWORD=123456
2427
- LOG_LEVEL
25-
# # uncomment the following lines if you want to enable discord bot webhook
28+
# # uncomment the following lines if you configured telegram webhook in .env file
29+
# - TELEGRAM_BOT_WEBHOOK_URL
30+
# - TELEGRAM_BOT_WEBHOOK_PORT
31+
# # uncomment the following lines if you configured slack and it's webhook options in .env file
32+
# - SLACK_CLIENT_ID
33+
# - SLACK_CLIENT_SECRET
34+
# - SLACK_WEBHOOK_PORT
35+
# # uncomment the following lines if you configured discord bot webhook in .env file
2636
# - DISCORD_BOT_TOKEN
2737
# - DISCORD_BOT_PUBLIC_KEY
2838
# - DISCORD_BOT_WEBHOOK_PORT
@@ -45,6 +55,9 @@ services:
4555
# On-demand build insights-bot service that would build the image from
4656
# source code and runs on production environment with db_local service
4757
# and redis_local service
58+
#
59+
# you need to use either docker compose up --profile local -d or docker-compose --profile local up -d
60+
# to ask docker compose to run this service
4861
insights_bot_local:
4962
profiles:
5063
- local
@@ -54,19 +67,26 @@ services:
5467
image: insights-bot
5568
restart: always
5669
environment:
70+
- TIMEZONE_SHIFT_SECONDS
5771
- TELEGRAM_BOT_TOKEN
5872
- OPENAI_API_SECRET
5973
- OPENAI_API_HOST
6074
- OPENAI_API_MODEL_NAME
61-
- SLACK_CLIENT_ID
62-
- SLACK_CLIENT_SECRET
63-
- DB_CONNECTION_STR=postgresql://postgres:123456@db_local:5432/postgres?search_path=public&sslmode=disable
75+
- OPENAI_API_TOKEN_LIMIT
76+
- DB_CONNECTION_STR
6477
- REDIS_HOST=redis_local
6578
- REDIS_PORT=6379
6679
- REDIS_TLS_ENABLED=false
6780
- REDIS_PASSWORD=123456
6881
- LOG_LEVEL
69-
# # uncomment the following lines if you want to enable discord bot webhook
82+
# # uncomment the following lines if you configured telegram webhook in .env file
83+
# - TELEGRAM_BOT_WEBHOOK_URL
84+
# - TELEGRAM_BOT_WEBHOOK_PORT
85+
# # uncomment the following lines if you configured slack and it's webhook options in .env file
86+
# - SLACK_CLIENT_ID
87+
# - SLACK_CLIENT_SECRET
88+
# - SLACK_WEBHOOK_PORT
89+
# # uncomment the following lines if you configured discord bot webhook in .env file
7090
# - DISCORD_BOT_TOKEN
7191
# - DISCORD_BOT_PUBLIC_KEY
7292
# - DISCORD_BOT_WEBHOOK_PORT

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ require (
3333
go.uber.org/multierr v1.11.0
3434
go.uber.org/ratelimit v0.3.0
3535
go.uber.org/zap v1.26.0
36-
gopkg.in/telebot.v3 v3.1.3
3736
)
3837

3938
require (

0 commit comments

Comments
 (0)