mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-18 21:25:10 +01:00
lower stream latency and added some backoff
This commit is contained in:
12
README.md
12
README.md
@@ -43,12 +43,12 @@ PRs are always welcome!
|
||||
### Configuration
|
||||
Customize the configuration by copying `.env.example` and renaming it to `.env`, then editing the required parameters as desired:
|
||||
|
||||
| Parameter | Description |
|
||||
|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `OPENAI_API_KEY` | Your OpenAI API key, you can get it from [here](https://platform.openai.com/account/api-keys) |
|
||||
| `TELEGRAM_BOT_TOKEN` | Your Telegram bot's token, obtained using [BotFather](http://t.me/botfather) (see [tutorial](https://core.telegram.org/bots/tutorial#obtain-your-bot-token)) |
|
||||
| `ADMIN_USER_IDS` | Telegram user IDs of admins. These users have access to special admin commands and information and no budget restrictions. Admin IDs don't have to be added to `ALLOWED_TELEGRAM_USER_IDS`. **Note**: by default, no admin ('-') |
|
||||
| `ALLOWED_TELEGRAM_USER_IDS` | A comma-separated list of Telegram user IDs that are allowed to interact with the bot (use [getidsbot](https://t.me/getidsbot) to find your user ID). **Note**: by default, *everyone* is allowed (`*`) |
|
||||
| Parameter | Description |
|
||||
|-----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `OPENAI_API_KEY` | Your OpenAI API key, you can get it from [here](https://platform.openai.com/account/api-keys) |
|
||||
| `TELEGRAM_BOT_TOKEN` | Your Telegram bot's token, obtained using [BotFather](http://t.me/botfather) (see [tutorial](https://core.telegram.org/bots/tutorial#obtain-your-bot-token)) |
|
||||
| `ADMIN_USER_IDS` | Telegram user IDs of admins. These users have access to special admin commands, information and no budget restrictions. Admin IDs don't have to be added to `ALLOWED_TELEGRAM_USER_IDS`. **Note**: by default, no admin ('-') |
|
||||
| `ALLOWED_TELEGRAM_USER_IDS` | A comma-separated list of Telegram user IDs that are allowed to interact with the bot (use [getidsbot](https://t.me/getidsbot) to find your user ID). **Note**: by default, *everyone* is allowed (`*`) |
|
||||
|
||||
### Optional configuration
|
||||
| Parameter | Description | Default value |
|
||||
|
||||
@@ -330,6 +330,7 @@ class ChatGPT3TelegramBot:
|
||||
i = 0
|
||||
prev = ''
|
||||
sent_message = None
|
||||
backoff = 0
|
||||
|
||||
async for content, tokens in stream_response:
|
||||
if len(content.strip()) == 0:
|
||||
@@ -339,7 +340,9 @@ class ChatGPT3TelegramBot:
|
||||
# group chats have stricter flood limits
|
||||
cutoff = 180 if len(content) > 1000 else 120 if len(content) > 200 else 90 if len(content) > 50 else 50
|
||||
else:
|
||||
cutoff = 120 if len(content) > 1000 else 100 if len(content) > 200 else 85 if len(content) > 50 else 40
|
||||
cutoff = 90 if len(content) > 1000 else 45 if len(content) > 200 else 25 if len(content) > 50 else 15
|
||||
|
||||
cutoff += backoff
|
||||
|
||||
if i == 0:
|
||||
try:
|
||||
@@ -369,14 +372,17 @@ class ChatGPT3TelegramBot:
|
||||
|
||||
except RetryAfter as e:
|
||||
logging.warning(str(e))
|
||||
backoff += 5
|
||||
await asyncio.sleep(e.retry_after)
|
||||
|
||||
except TimedOut as e:
|
||||
logging.warning(str(e))
|
||||
await asyncio.sleep(1)
|
||||
backoff += 5
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
except Exception as e:
|
||||
logging.warning(str(e))
|
||||
backoff += 5
|
||||
continue
|
||||
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
Reference in New Issue
Block a user