mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2026-01-29 09:46:23 +01:00
added customizable initial assistant prompt
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
OPENAI_API_KEY="XXX"
|
||||
TELEGRAM_BOT_TOKEN="XXX"
|
||||
ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,<USER_ID_2" # comma separated list of telegram user ids, or * to allow all
|
||||
ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,<USER_ID_2" # comma separated list of telegram user ids, or * to allow all
|
||||
ASSISTANT_PROMPT="You are a helpful assistant."
|
||||
@@ -11,13 +11,13 @@ A [Telegram bot](https://core.telegram.org/bots/api) that integrates with OpenAI
|
||||
## Features
|
||||
- [x] Reply to specific messages
|
||||
- [x] Support markdown in answers
|
||||
- [x] Can reset conversation thread with the `/reset` command
|
||||
- [x] Reset conversation with the `/reset` command
|
||||
- [x] Typing indicator while generating a response
|
||||
- [x] Access can be restricted by specifying a list of allowed users
|
||||
- [x] (NEW!) Docker support
|
||||
- [x] Docker support
|
||||
- [x] (NEW!) Customizable initial assistant prompt
|
||||
|
||||
## Coming soon
|
||||
- [ ] Customizable initial prompt
|
||||
- [ ] Customizable temperature
|
||||
- [ ] Better handling of rate limiting errors
|
||||
- [ ] See remaining tokens and current usage
|
||||
@@ -48,8 +48,10 @@ TELEGRAM_BOT_TOKEN="<YOUR_TELEGRAM_BOT_TOKEN>"
|
||||
Additional optional (but recommended) configuration values:
|
||||
```bash
|
||||
ALLOWED_TELEGRAM_USER_IDS="<USER_ID_1>,<USER_ID_2>,..." # Defaults to "*"
|
||||
ASSISTANT_PROMPT="..." # Defaults to "You are a helpful assistant."
|
||||
```
|
||||
* `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 (`*`)
|
||||
* `ASSISTANT_PROMPT`: A system message that controls the behavior of the assistant. See [the docs](https://platform.openai.com/docs/guides/chat/introduction) for more details
|
||||
|
||||
### Installing
|
||||
1. Clone the repository and navigate to the project directory:
|
||||
|
||||
@@ -13,9 +13,8 @@ class GPTHelper:
|
||||
:param config: A dictionary containing the GPT configuration
|
||||
"""
|
||||
openai.api_key = config['api_key']
|
||||
self.prompt = "You are a helpful assistant. You answer with concise, straight-forward answers. You sometimes " \
|
||||
"make jokes, if appropriate. You are never rude. You are always helpful."
|
||||
self.history = [{"role": "system", "content": self.prompt}]
|
||||
self.initial_history = [{"role": "system", "content": config['assistant_prompt']}]
|
||||
self.history = self.initial_history
|
||||
|
||||
def get_response(self, query) -> str:
|
||||
"""
|
||||
@@ -41,8 +40,15 @@ class GPTHelper:
|
||||
logging.exception(e)
|
||||
return "Error"
|
||||
|
||||
def reset(self):
|
||||
def reset_history(self):
|
||||
"""
|
||||
Resets the conversation history.
|
||||
"""
|
||||
self.history = [{"role": "system", "content": self.prompt}]
|
||||
self.history = self.initial_history
|
||||
|
||||
if __name__ == '__main__':
|
||||
gpt = GPTHelper({'api_key': 'YOUR_API_KEY'})
|
||||
|
||||
while True:
|
||||
query = input("You: ")
|
||||
print("AI: {}".format(gpt.get_response(query)))
|
||||
5
main.py
5
main.py
@@ -26,7 +26,10 @@ def main():
|
||||
|
||||
# Setup configurations
|
||||
gpt_config = {
|
||||
'api_key': os.environ['OPENAI_API_KEY']
|
||||
'api_key': os.environ['OPENAI_API_KEY'],
|
||||
'assistant_prompt': os.environ.get(
|
||||
'ASSISTANT_PROMPT', 'You are a helpful assistant. You always answer with concise, straight-forward answers.'
|
||||
)
|
||||
}
|
||||
telegram_config = {
|
||||
'token': os.environ['TELEGRAM_BOT_TOKEN'],
|
||||
|
||||
@@ -42,9 +42,8 @@ class ChatGPT3TelegramBot:
|
||||
return
|
||||
|
||||
logging.info(f'Resetting the conversation for user {update.message.from_user.name}...')
|
||||
self.gpt.reset()
|
||||
response = self.gpt.get_response('hi')
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=response)
|
||||
self.gpt.reset_history()
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text='Done!')
|
||||
|
||||
async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user