Skip to content

Commit

Permalink
Merge pull request #87 from gparrello/new-feature
Browse files Browse the repository at this point in the history
Allow setting system content on reset
  • Loading branch information
n3d1117 authored Mar 20, 2023
2 parents b7d07f5 + 421b721 commit 7e6720f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions bot/openai_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ async def transcribe(self, filename):
result = await openai.Audio.atranscribe("whisper-1", audio)
return result.text

def reset_chat_history(self, chat_id):
def reset_chat_history(self, chat_id, content=''):
"""
Resets the conversation history.
"""
self.conversations[chat_id] = [{"role": "system", "content": self.config['assistant_prompt']}]
if content == '':
content = self.config['assistant_prompt']
self.conversations[chat_id] = [{"role": "system", "content": content}]

def __max_age_reached(self, chat_id) -> bool:
"""
Expand Down
5 changes: 3 additions & 2 deletions bot/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, config: dict, openai: OpenAIHelper):
self.openai = openai
self.commands = [
BotCommand(command='help', description='Show help message'),
BotCommand(command='reset', description='Reset the conversation'),
BotCommand(command='reset', description='Reset the conversation. Optionally pass high-level instructions for the conversation (e.g. /reset You are a helpful assistant)'),
BotCommand(command='image', description='Generate image from prompt (e.g. /image cat)'),
BotCommand(command='stats', description='Get your current usage statistics')
]
Expand Down Expand Up @@ -95,7 +95,8 @@ async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
logging.info(f'Resetting the conversation for user {update.message.from_user.name}...')

chat_id = update.effective_chat.id
self.openai.reset_chat_history(chat_id=chat_id)
reset_content = update.message.text.replace('/reset', '').strip()
self.openai.reset_chat_history(chat_id=chat_id, content=reset_content)
await context.bot.send_message(chat_id=chat_id, text='Done!')

async def image(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
Expand Down

0 comments on commit 7e6720f

Please sign in to comment.