Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
n3d1117 committed Mar 3, 2023
1 parent ec88c52 commit bbfad4f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ A [Telegram bot](https://core.telegram.org/bots/api) that integrates with OpenAI
- [x] Typing indicator while generating a response
- [x] Access can be restricted by specifying a list of allowed users
- [x] Docker support
- [x] Proxy support
- [x] (NEW!) Support multiple answers via the `n_choices` configuration parameter
- [x] (NEW!) Customizable model parameters (see [configuration](#configuration) section)
- [x] (NEW!) See token usage after each answer
Expand All @@ -37,14 +38,14 @@ Customize the configuration by copying `.env.example` and renaming it to `.env`,
```bash
OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN"
ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,USER_ID_2,..." # Defaults to "*" (everyone)
PROXY="<HTTP/HTTPS_PROXY>" # E.g. "http://localhost:8080", defaults to none
ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,USER_ID_2,..." # Defaults to `*` (everyone)
PROXY="YOUR_PROXY" # E.g. "http://localhost:8080", defaults to `None`
SHOW_USAGE=false
```
* `OPENAI_API_KEY`: Your OpenAI API key, get if 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))
* `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). **Important**: by default, *everyone* is allowed (`*`)
* `PROXY`: Proxy to be used for OpenAI and telegram bot
* `PROXY`: Proxy to be used for OpenAI and Telegram bot
* `SHOW_USAGE`: Whether to show OpenAI token usage information after each response. Optional, defaults to `false`

Additional optional model parameters can be configured from the `main.py` file:
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def main():
openai_config = {
'api_key': os.environ['OPENAI_API_KEY'],
'show_usage': os.environ.get('SHOW_USAGE', 'false').lower() == 'true',
'proxy': os.environ.get('PROXY'),
'proxy': os.environ.get('PROXY', None),

# 'gpt-3.5-turbo' or 'gpt-3.5-turbo-0301'
'model': 'gpt-3.5-turbo',
Expand Down Expand Up @@ -61,7 +61,7 @@ def main():
telegram_config = {
'token': os.environ['TELEGRAM_BOT_TOKEN'],
'allowed_user_ids': os.environ.get('ALLOWED_TELEGRAM_USER_IDS', '*'),
'proxy': os.environ.get('PROXY')
'proxy': os.environ.get('PROXY', None)
}

# Setup and run ChatGPT and Telegram bot
Expand Down
4 changes: 2 additions & 2 deletions openai_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def __init__(self, config: dict):
:param config: A dictionary containing the GPT configuration
"""
openai.api_key = config['api_key']
if config.get('proxy'):
openai.proxy = config['proxy']
openai.proxy = config['proxy']
self.config = config
self.sessions: dict[int: list] = dict() # {chat_id: history}

Expand Down Expand Up @@ -98,6 +97,7 @@ def generate_image(self, prompt: str) -> str:
logging.exception(e)
raise e


def reset_chat_history(self, chat_id):
"""
Resets the conversation history.
Expand Down
13 changes: 5 additions & 8 deletions telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,11 @@ def run(self):
"""
Runs the bot indefinitely until the user presses Ctrl+C
"""
if self.config.get('proxy'):
proxy_url = self.config['proxy']
application = ApplicationBuilder().token(
self.config['token']).proxy_url(
proxy_url).get_updates_proxy_url(proxy_url).build()
else:
application = ApplicationBuilder().token(
self.config['token']).build()
application = ApplicationBuilder() \
.token(self.config['token']) \
.proxy_url(self.config['proxy']) \
.get_updates_proxy_url(self.config['proxy']) \
.build()

application.add_handler(CommandHandler('reset', self.reset))
application.add_handler(CommandHandler('help', self.help))
Expand Down

0 comments on commit bbfad4f

Please sign in to comment.