diff --git a/app/extra_config.py b/app/extra_config.py index 4ada626..9e1a468 100644 --- a/app/extra_config.py +++ b/app/extra_config.py @@ -22,6 +22,10 @@ MESSAGE_LOGGER_CHAT: int = int( environ.get("MESSAGE_LOGGER_CHAT", environ.get("LOG_CHAT")) ) +OPENAI_CLIENT = environ.get("OPENAI_CLIENT", "") + +OPENAI_MODEL = environ.get("OPENAI_MODEL", "gpt-4o") + PM_GUARD: bool = False PM_LOGGER: bool = False diff --git a/app/plugins/ai/openai.py b/app/plugins/ai/openai.py new file mode 100644 index 0000000..9a5ab35 --- /dev/null +++ b/app/plugins/ai/openai.py @@ -0,0 +1,63 @@ +import openai +from pyrogram.enums import ParseMode + +from app import BOT, LOGGER, Message +from app.extra_config import OPENAI_CLIENT, OPENAI_MODEL +from app.plugins.ai.models import SYSTEM_INSTRUCTIONS + +try: + CLIENT = getattr(openai, f"Async{OPENAI_CLIENT}OpenAI")() +except Exception as e: + LOGGER.error(e) + CLIENT = None + + +@BOT.add_cmd(cmd="gpt") +async def chat_gpt(bot: BOT, message: Message): + """ + CMD: GPT + INFO: Ask a question to chat gpt. + + SETUP: + To use this command you need to set either of these vars. + + For Default Client: + OPENAI_API_KEY = your API key + OPENAI_MODEL = model (optional, defaults to gpt-4o) + OPENAI_BASE_URL = a custom endpoint (optional) + + For Azure Client: + OPENAI_CLIENT="Azure" + OPENAI_API_VERSION = your version + OPENAI_MODEL = your azure model + AZURE_OPENAI_API_KEY = your api key + AZURE_OPENAI_ENDPOINT = your azure endpoint + + USAGE: + .gpt hi + .gpt [reply to a message] + """ + if CLIENT is None: + await message.reply(f"OpenAI Creds not set or are invalid.\nCheck Help.") + return + + reply_text = message.replied.text if message.replied else "" + + prompt = f"{reply_text}\n\n\n{message.input}".strip() + + if not prompt: + await message.reply("Ask a Question | Reply to a message.") + return + + chat_completion = await CLIENT.chat.completions.create( + messages=[ + {"role": "system", "content": SYSTEM_INSTRUCTIONS}, + {"role": "user", "content": prompt}, + ], + model=OPENAI_MODEL, + ) + + response = chat_completion.choices[0].message.content + await message.reply( + text=f"```{prompt}```**GPT**:\n{response}", parse_mode=ParseMode.MARKDOWN + ) diff --git a/app/plugins/tg_tools/delete.py b/app/plugins/tg_tools/delete.py index ca922e7..12e03ca 100644 --- a/app/plugins/tg_tools/delete.py +++ b/app/plugins/tg_tools/delete.py @@ -53,10 +53,7 @@ async def purge_(bot: BOT, message: Message) -> None: chat_id=message.chat.id, message_id=message.message_thread_id, limit=100 ): message_ids.append(_message.id) - if ( - _message.id == message.reply_id - or len(message_ids) > 100 - ): + if _message.id == message.reply_id or len(message_ids) > 100: break else: # Generate Message Ids