Skip to content

Commit

Permalink
Fix terrarium and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaGusev committed Jul 9, 2024
1 parent 9f12192 commit d144528
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
28 changes: 25 additions & 3 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ def _crop_content(content: str) -> str:
return IMAGE_PLACEHOLDER


async def _reply(message: Message, text: str, **kwargs: Any) -> Union[Message, bool]:
try:
return await message.reply(text, parse_mode=ParseMode.MARKDOWN, **kwargs)
except Exception:
try:
return await message.reply(text, parse_mode=ParseMode.HTML, **kwargs)
except Exception:
return await message.reply(text, parse_mode=None, **kwargs)


async def _edit_text(message: Message, text: str, **kwargs: Any) -> Union[Message, bool]:
try:
return await message.edit_text(text, parse_mode=ParseMode.MARKDOWN, **kwargs)
except Exception:
try:
return await message.edit_text(text, parse_mode=ParseMode.HTML, **kwargs)
except Exception:
return await message.edit_text(text, parse_mode=None, **kwargs)


class LlmBot:
def __init__(
self,
Expand Down Expand Up @@ -690,6 +710,7 @@ async def toogle_tools(self, message: Message) -> None:
return
current_value = self.db.are_tools_enabled(chat_id)
self.db.set_enable_tools(chat_id, not current_value)
self.db.create_conv_id(chat_id)
if not current_value:
await message.reply(self.localization.ENABLED_TOOLS)
else:
Expand Down Expand Up @@ -898,14 +919,15 @@ async def generate(self, message: Message) -> None:
else:
answer_parts = [answer]

new_message = await placeholder.edit_text(answer_parts[0])
new_message = await _edit_text(placeholder, answer_parts[0])
assert isinstance(new_message, Message)
for part in answer_parts[1:]:
new_message = await message.reply(part)
new_message = await _reply(message, part)
assert isinstance(new_message, Message)

markup = self.likes_kb.as_markup()
new_message = await new_message.edit_text(
new_message = await _edit_text(
new_message,
answer_parts[-1],
reply_markup=markup,
)
Expand Down
3 changes: 2 additions & 1 deletion src/tools/terrarium.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def get_specification(self) -> Dict[str, Any]:
"name": "terrarium",
"description": "Calling Python interperter with an isolated environment. "
"Use only when you need to calculate something or when explicitly asked. "
"Do not use this tool when code excecution is not actually needed",
"Do not use this tool when code excecution is not actually needed. For instance, "
"when user just asks to write code, DO NOT call this tool.",
"parameters": {
"type": "object",
"properties": {
Expand Down

0 comments on commit d144528

Please sign in to comment.