Skip to content

Commit

Permalink
-- and == prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
innightwolfsleep committed Sep 12, 2023
1 parent 62309ba commit d14149b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ FEATURES:
- buttons: continue previous message, regenerate last message, remove last messages from history, reset history button, new char loading menu
- you can load new characters from text-generation-webui\characters with button
- you can load new model during conversation with button
- chatting # prefix for impersonate: "#You" or "#Castle guard" or "#Alice thoughts about me"
- "!" prefix to replace last bot message
- "++" prefix permanently replace bot name during chat (switch conversation to another character)
- "📷" prefix to make photo via SD api. Write like "📷Chiharu Yamada", not single "📷"
- chatting "+" or "#" prefix for impersonate: "#Hero sister" or "+Castle guard". Or even ask bot generate your own message "+You"
- "-" or "!" prefix to replace last bot message
- "++" prefix replace bot name during chat (switch conversation to another character)
- "--" prefix replace you name during chat
- "==" prefix to add message to context
- "📷" prefix to make photo via SD api. Write like "📷Chiharu Yamada", not single "📷". Need to run ([StableDiffusion](https://github.com/AUTOMATIC1111/stable-diffusion-webui)) with --api key first.
- save/load history in chat by downloading/forwarding to chat .json file
- integrated auto-translate (you can set model/user language parameter)
- voice generating ([silero](https://github.com/snakers4/silero-models)), en and ru variants
Expand Down
36 changes: 29 additions & 7 deletions telegram_bot_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class TelegramBotWrapper:
replace_prefixes = ["!", "-"] # Prefix to replace last message
impersonate_prefixes = ["#", "+"] # Prefix for "impersonate" message
# Prefix for persistence "impersonate" message
permanent_impersonate_prefixes = ["++"]
permanent_change_name1_prefixes = ["--"]
permanent_change_name2_prefixes = ["++"]
permanent_add_context_prefixes = ["=="]
# Prefix for replace username "impersonate" message
permanent_user_prefixes = ["+="]
permanent_contex_add = ["+-"] # Prefix for adding string to context
Expand Down Expand Up @@ -437,12 +439,24 @@ def generate_answer(self, user_in, chat_id) -> Tuple[str, str]:
# if generation will fail, return "fail" answer

# Preprocessing: add user_in to history in right order:
if user_in[:2] in self.permanent_impersonate_prefixes:
if user_in[:2] in self.permanent_change_name2_prefixes:
# If user_in starts with perm_prefix - just replace name2
user.name2 = user_in[2:]
self.generator_lock.release()
return_msg_action = self.MSG_SYSTEM
return "New name: " + user.name2, return_msg_action
return "New bot name: " + user.name2, return_msg_action
if user_in[:2] in self.permanent_change_name1_prefixes:
# If user_in starts with perm_prefix - just replace name2
user.name1 = user_in[2:]
self.generator_lock.release()
return_msg_action = self.MSG_SYSTEM
return "New user name: " + user.name1, return_msg_action
if user_in[:2] in self.permanent_add_context_prefixes:
# If user_in starts with perm_prefix - just replace name2
user.context += "\n" + user_in[2:]
self.generator_lock.release()
return_msg_action = self.MSG_SYSTEM
return "Added to context: " + user_in[2:], return_msg_action
if self.bot_mode in [self.MODE_QUERY]:
user.history = []
if self.bot_mode in [self.MODE_NOTEBOOK]:
Expand Down Expand Up @@ -601,7 +615,9 @@ def prepare_text(self, original_text, user_language="en", direction="to_user"):
text = self.html_tag[0] + text + self.html_tag[1]
return text

@backoff.on_exception(backoff.expo, urllib3.exceptions.ConnectTimeoutError, max_time=60)
@backoff.on_exception(
backoff.expo, (urllib3.exceptions.HTTPError, urllib3.exceptions.ConnectTimeoutError), max_time=60
)
def send_sd_image(self, upd: Update, context: CallbackContext, answer, user_text):
chat_id = upd.message.chat.id
file_list = self.SdApi.txt_to_image(answer)
Expand All @@ -615,7 +631,9 @@ def send_sd_image(self, upd: Update, context: CallbackContext, answer, user_text
context.bot.send_photo(caption=answer, chat_id=chat_id, photo=image_file)
os.remove(image_path)

@backoff.on_exception(backoff.expo, urllib3.exceptions.ConnectTimeoutError, max_time=60)
@backoff.on_exception(
backoff.expo, (urllib3.exceptions.HTTPError, urllib3.exceptions.ConnectTimeoutError), max_time=60
)
def clean_last_message_markup(self, context: CallbackContext, chat_id: int):
if chat_id in self.users and len(self.users[chat_id].msg_id) > 0:
last_msg = self.users[chat_id].msg_id[-1]
Expand All @@ -624,7 +642,9 @@ def clean_last_message_markup(self, context: CallbackContext, chat_id: int):
except Exception as exception:
logging.error("last_message_markup_clean: " + str(exception))

@backoff.on_exception(backoff.expo, urllib3.exceptions.ConnectTimeoutError, max_time=60)
@backoff.on_exception(
backoff.expo, (urllib3.exceptions.HTTPError, urllib3.exceptions.ConnectTimeoutError), max_time=60
)
def send(self, context: CallbackContext, chat_id: int, text: str):
user = self.users[chat_id]
text = self.prepare_text(text, self.users[chat_id].language, "to_user")
Expand Down Expand Up @@ -662,7 +682,9 @@ def send(self, context: CallbackContext, chat_id: int, text: str):
return message
return message

@backoff.on_exception(backoff.expo, urllib3.exceptions.ConnectTimeoutError, max_time=60)
@backoff.on_exception(
backoff.expo, (urllib3.exceptions.HTTPError, urllib3.exceptions.ConnectTimeoutError), max_time=60
)
def edit(
self,
context: CallbackContext,
Expand Down

0 comments on commit d14149b

Please sign in to comment.