Skip to content

Commit

Permalink
fix to fit new auto-gpt master
Browse files Browse the repository at this point in the history
  • Loading branch information
Wladastic committed Oct 9, 2023
1 parent 98d49ce commit 65ebb9f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
11 changes: 6 additions & 5 deletions src/autogpt_plugins/sophie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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": "<message that awaits user input>",
"prompt": "string",
},
self.telegram_sophie_utils.ask_user,
)
Expand All @@ -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": "<message that awaits user input>",
"prompt": "string",
},
self.telegram_sophie_utils.ask_user_voice,
)
Expand All @@ -80,7 +81,7 @@ def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator:
"send_message",
"Send a message to the user without awaiting response.",
{
"message": "<message to send>",
"message": "string",
},
self.telegram_sophie_utils.send_message,
)
Expand All @@ -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 to send>",
"message": "string",
},
self.telegram_sophie_utils.send_message_and_speak,
)
Expand Down
29 changes: 21 additions & 8 deletions src/autogpt_plugins/sophie/sophie_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()

Expand Down
21 changes: 13 additions & 8 deletions src/autogpt_plugins/telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -241,23 +241,29 @@ 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

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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/autogpt_plugins/telegram/telegram_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 65ebb9f

Please sign in to comment.