Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment optimisation #261

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ dmypy.json
.pyre/

# Custom
config/config.yml
config/config.env

docker-compose.dev.yml
Expand Down
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
FROM python:3.8-slim
FROM python:3.8-alpine

ENV PYTHONFAULTHANDLER=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONHASHSEED=random
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PIP_NO_CACHE_DIR=off
ENV PIP_DISABLE_PIP_VERSION_CHECK=on
ENV PIP_DEFAULT_TIMEOUT=100

RUN apt-get update
RUN apt-get install -y python3 python3-pip python-dev build-essential python3-venv ffmpeg
RUN apk add --no-cache python3-dev build-base libffi-dev openssl-dev ffmpeg
RUN apk add --no-cache bash

RUN mkdir -p /code
ADD . /code
WORKDIR /code

RUN pip3 install -r requirements.txt

CMD ["bash"]
CMD ["python3", "bot/bot.py"]
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ This repo is ChatGPT re-created as Telegram Bot. **And it works great.**

You can deploy your own bot, or use mine: [@chatgpt_karfly_bot](https://t.me/chatgpt_karfly_bot)


[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/template/IG_WDs?referralCode=AkEaYM)


## Features
- Low latency replies (it usually takes about 3-5 seconds)
- No request limits
Expand All @@ -37,7 +41,7 @@ You can deploy your own bot, or use mine: [@chatgpt_karfly_bot](https://t.me/cha
<p align="center">
<img src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYmM2ZWVjY2M4NWQ3ZThkYmQ3MDhmMTEzZGUwOGFmOThlMDIzZGM4YiZjdD1n/unx907h7GSiLAugzVX/giphy.gif" />
</p>

---

## 🤑 Payments
Expand Down Expand Up @@ -75,13 +79,14 @@ If you want to add payments to your bot and create profitable business – write

2. Get your Telegram bot token from [@BotFather](https://t.me/BotFather)

3. Edit `config/config.example.yml` to set your tokens and run 2 commands below (*if you're advanced user, you can also edit* `config/config.example.env`):
3. Edit `config/config.example.env` to set your tokens and run the command below:
```bash
mv config/config.example.yml config/config.yml
mv config/config.example.env config/config.env
```

4. You can edit the configs in config/config.yml to have the model behave differently. They already come with some opinionated default settings.

4. 🔥 And now **run**:
5. 🔥 And now **run**:
```bash
docker-compose --env-file config/config.env up --build
```
Expand Down
20 changes: 12 additions & 8 deletions bot/config.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import yaml
import dotenv
import os
from pathlib import Path
import ast

from dotenv import load_dotenv



config_dir = Path(__file__).parent.parent.resolve() / "config"
load_dotenv(dotenv_path=config_dir / "config.env")

# load yaml config
with open(config_dir / "config.yml", 'r') as f:
config_yaml = yaml.safe_load(f)

# load .env config
config_env = dotenv.dotenv_values(config_dir / "config.env")

# config parameters
telegram_token = config_yaml["telegram_token"]
openai_api_key = config_yaml["openai_api_key"]
openai_api_key = os.environ["OPENAI_API_KEY"]
telegram_token = os.environ["TELEGRAM_TOKEN"]
use_chatgpt_api = config_yaml.get("use_chatgpt_api", True)
allowed_telegram_usernames = config_yaml["allowed_telegram_usernames"]
env_allowed_users = os.environ.get('ALLOWED_TELEGRAM_USERNAMES', '')
allowed_telegram_usernames = (lambda s: s.split(',') if s else [])(os.environ.get('ALLOWED_TELEGRAM_USERNAMES', ''))
new_dialog_timeout = config_yaml["new_dialog_timeout"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not have a default value. it should be

new_dialog_timeout = config_yaml.get("new_dialog_timeout", 600)

enable_message_streaming = config_yaml.get("enable_message_streaming", True)
return_n_generated_images = config_yaml.get("return_n_generated_images", 1)
n_chat_modes_per_page = config_yaml.get("n_chat_modes_per_page", 5)
mongodb_uri = f"mongodb://mongo:{config_env['MONGODB_PORT']}"
mongodb_uri = os.environ["MONGO_URL"] if os.environ.get("MONGO_URL") else f"mongodb://mongo:{os.environ['MONGODB_PORT']}"

# chat_modes
with open(config_dir / "chat_modes.yml", 'r') as f:
Expand Down
11 changes: 10 additions & 1 deletion config/config.example.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
TELEGRAM_TOKEN=
OPENAI_API_KEY=

#comma separated, without spaces e.g. username_one,username_two
ALLOWED_TELEGRAM_USERNAMES=

# local path where to store MongoDB
MONGODB_PATH=./mongodb
# MongoDB port
Expand All @@ -8,4 +14,7 @@ MONGO_EXPRESS_PORT=8081
# Mongo Express username
MONGO_EXPRESS_USERNAME=username
# Mongo Express password
MONGO_EXPRESS_PASSWORD=password
MONGO_EXPRESS_PASSWORD=password



13 changes: 0 additions & 13 deletions config/config.example.yml

This file was deleted.

10 changes: 10 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use_chatgpt_api: true
new_dialog_timeout: 600 # new dialog starts after timeout (in seconds)
return_n_generated_images: 1
n_chat_modes_per_page: 5
enable_message_streaming: true # if set, messages will be shown to user word-by-word

# prices
chatgpt_price_per_1000_tokens: 0.002
gpt_price_per_1000_tokens: 0.02
whisper_price_per_1_min: 0.006