diff --git a/src/autogpt_plugins/sophie/__init__.py b/src/autogpt_plugins/sophie/__init__.py index 8630e525..a278d443 100644 --- a/src/autogpt_plugins/sophie/__init__.py +++ b/src/autogpt_plugins/sophie/__init__.py @@ -39,7 +39,8 @@ def __init__(self): self.telegram_sophie_api_key = os.getenv("TELEGRAM_SOPHIE_API_KEY", None) self.telegram_sophie_chat_id = os.getenv("TELEGRAM_SOPHIE_CHAT_ID", None) self.telegram_sophie_utils = TelegramUtils( - chat_id=self.telegram_sophie_chat_id, api_key=self.telegram_sophie_api_key + chat_id=self.telegram_sophie_chat_id, + api_key=self.telegram_sophie_api_key ) def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator: @@ -62,7 +63,7 @@ def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator: "ask_user", "Ask the user for input or tell them something and wait for their response if.", { - "prompt": "", + "prompt": "string", }, self.telegram_sophie_utils.ask_user, ) @@ -71,7 +72,7 @@ def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator: "ask_user_voice", "Same as ask_user but also sends a friendly voice message.", { - "prompt": "", + "prompt": "string", }, self.telegram_sophie_utils.ask_user_voice, ) @@ -80,7 +81,7 @@ def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator: "send_message", "Send a message to the user without awaiting response.", { - "message": "", + "message": "string", }, self.telegram_sophie_utils.send_message, ) @@ -89,7 +90,7 @@ def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator: "send_voice_message", "Same as send_message but also sends a friendly voice message.", { - "message": "", + "message": "string", }, self.telegram_sophie_utils.send_message_and_speak, ) diff --git a/src/autogpt_plugins/sophie/sophie_chat.py b/src/autogpt_plugins/sophie/sophie_chat.py index 2968d508..365e7a2f 100644 --- a/src/autogpt_plugins/sophie/sophie_chat.py +++ b/src/autogpt_plugins/sophie/sophie_chat.py @@ -6,26 +6,36 @@ import traceback from glob import glob import openai - from telegram import Bot, Update from telegram.error import TimedOut from telegram.ext import CallbackContext - +import tiktoken if os.name == "nt": import soundfile as sf else: import sox - from pathlib import Path - import torch -from autogpt.llm.utils import count_string_tokens response_queue = "" +def count_string_tokens(text, model_name="gpt-3.5-turbo"): + """Returns the number of tokens used by a list of messages.""" + model = model_name + try: + encoding = tiktoken.encoding_for_model(model) + return len(encoding.encode(text)) + except KeyError: + encoding = tiktoken.get_encoding("cl100k_base") + # note: future models may deviate from this + except Exception as e: + log(f"Sophie: Error while counting tokens: {e}") + log(traceback.format_exc()) + + def run_async(coro): try: loop = asyncio.get_running_loop() @@ -309,8 +319,11 @@ def is_authorized_user(self, update: Update): if not authorized: log("Unauthorized user: " + str(update)) chat_id = update.message.chat.id - temp_bot= Bot(self.api_key) - temp_bot.send_message(chat_id=chat_id, text="You are not authorized to use this bot. Checkout Auto-GPT-Plugins on GitHub: https://github.com/Significant-Gravitas/Auto-GPT-Plugins") + temp_bot = Bot(self.api_key) + temp_bot.send_message( + chat_id=chat_id, + text="You are not authorized to use this bot. Checkout Auto-GPT-Plugins on GitHub: https://github.com/Significant-Gravitas/Auto-GPT-Plugins", + ) return authorized def handle_response(self, update: Update, context: CallbackContext): @@ -389,7 +402,7 @@ def send_voice(self, voice_file): return "Error while sending voice message." async def _send_message_async(self, message, speak=False): - log("Sending message to Telegram.. ") + log("Sophie: Sending message to Telegram.. ") recipient_chat_id = self.chat_id bot = await self.get_bot() diff --git a/src/autogpt_plugins/telegram/__init__.py b/src/autogpt_plugins/telegram/__init__.py index eb377009..b431580b 100644 --- a/src/autogpt_plugins/telegram/__init__.py +++ b/src/autogpt_plugins/telegram/__init__.py @@ -2,7 +2,7 @@ import os import re from typing import Any, Dict, List, Optional, Tuple, TypedDict, TypeVar - +import asyncio from auto_gpt_plugin_template import AutoGPTPluginTemplate from .telegram_chat import TelegramUtils @@ -241,10 +241,10 @@ def handle_chat_completion( def can_handle_text_embedding(self, text: str) -> bool: return False - + def handle_text_embedding(self, text: str) -> list: pass - + def can_handle_user_input(self, user_input: str) -> bool: return True @@ -252,12 +252,18 @@ def user_input(self, user_input: str) -> str: user_input = remove_color_codes(user_input) # if the user_input is too long, shorten it try: + if asyncio.iscoroutinefunction(self): + print("async detected by telegram plugin!") + loop = asyncio.get_running_loop() + return loop.run_until_complete( + self.telegram_utils.ask_user_async(prompt=user_input) + ) return self.telegram_utils.ask_user(prompt=user_input) except Exception as e: print(e) print("Error sending message to telegram") return "s" - + def can_handle_report(self) -> bool: """This method is called to check that the plugin can handle the report method. @@ -266,15 +272,14 @@ def can_handle_report(self) -> bool: bool: True if the plugin can handle the report method.""" return True - def report(self, message: str) -> None: + async def report(self, message: str) -> None: message = remove_color_codes(message) # if the message is too long, shorten it - try : - self.telegram_utils.send_message(message=message) + try: + await self.telegram_utils._send_message(message=message) except Exception as e: print(e) print("Error sending message to telegram") - def can_handle_text_embedding(self, text: str) -> bool: return False diff --git a/src/autogpt_plugins/telegram/telegram_chat.py b/src/autogpt_plugins/telegram/telegram_chat.py index 2f5a1aa6..1b52e547 100644 --- a/src/autogpt_plugins/telegram/telegram_chat.py +++ b/src/autogpt_plugins/telegram/telegram_chat.py @@ -169,7 +169,7 @@ def send_voice(self, voice_file): print("Error while sending voice message") async def _send_message(self, message): - print("Sending message to Telegram.. ") + print("Sending message to Telegram.. " + message) recipient_chat_id = self.chat_id bot = await self.get_bot()