|
| 1 | +import os |
| 2 | +from pyrogram import Client, filters, enums |
| 3 | +from pyrogram.types import Message |
| 4 | + |
| 5 | +from datetime import datetime as dt |
| 6 | + |
| 7 | +from utils.misc import modules_help, prefix |
| 8 | +from utils.scripts import format_exc, import_library |
| 9 | + |
| 10 | +g4f = import_library("g4f") |
| 11 | + |
| 12 | +from g4f.client import AsyncClient as Clients_g4f |
| 13 | + |
| 14 | +owner_base = f""" |
| 15 | +Your name is 🌝 moon ai bot. A kind and friendly AI assistant that answers in |
| 16 | +a short and concise answer. Give short step-by-step reasoning if required. Use emojis rarely or when necessary to make the answer more engaging and fun or asked by the user. |
| 17 | +- Powered by @moonuserbot on telegram |
| 18 | +- Created by @moonuserbot |
| 19 | +- Version: 1.0.0 |
| 20 | +- Date: |
| 21 | +Today is {dt.now():%A %d %B %Y %H:%M} |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +async def chat_message(question): |
| 26 | + clients_x = Clients_g4f() |
| 27 | + response = await clients_x.chat.completions.create( |
| 28 | + model="gpt-4o", |
| 29 | + messages=[ |
| 30 | + {"role": "system", "content": owner_base}, |
| 31 | + {"role": "user", "content": question}, |
| 32 | + ], |
| 33 | + ) |
| 34 | + messager = response.choices[0].message.content |
| 35 | + return messager |
| 36 | + |
| 37 | + |
| 38 | +@Client.on_message(filters.command("gpt", prefix) & filters.me) |
| 39 | +async def chatgpt(_, message: Message): |
| 40 | + if len(message.command) > 1: |
| 41 | + prompt = message.text.split(maxsplit=1)[1] |
| 42 | + elif message.reply_to_message: |
| 43 | + prompt = message.reply_to_message.text |
| 44 | + else: |
| 45 | + return await message.edit_text("Give prompt to ask from CHATGPT-4O") |
| 46 | + try: |
| 47 | + await message.edit_text("<code>Processing...</code>") |
| 48 | + messager = await chat_message(prompt) |
| 49 | + if len(messager) > 4096: |
| 50 | + with open("chat.txt", "w+", encoding="utf8") as out_file: |
| 51 | + out_file.write(messager) |
| 52 | + await message.reply_document(document="chat.txt", disable_notification=True) |
| 53 | + os.remove("chat.txt") |
| 54 | + else: |
| 55 | + await message.edit_text(messager, parse_mode=enums.ParseMode.MARKDOWN) |
| 56 | + except Exception as e: |
| 57 | + return format_exc(e) |
| 58 | + |
| 59 | + |
| 60 | +modules_help["gpt"] = { |
| 61 | + "gpt [question]": "ask gpt", |
| 62 | + "gpt [reply to message]": "ask gpt", |
| 63 | +} |
0 commit comments