Skip to content

Commit

Permalink
[+] Add Module Open AI
Browse files Browse the repository at this point in the history
  • Loading branch information
ubaynet committed Aug 29, 2023
1 parent dfda7ea commit 6d6755c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ AyiinXd/modules/sql_helper/__pycache__/*
bin/*
venv
*.pyc
.env
45 changes: 45 additions & 0 deletions AyiinXd/ayiin/aihelp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

# (C) 2020-2023 by TgCatUB@Github.

import openai
from AyiinXd import CMD_HELP
from .. import var

openai.api_key = var.OPENAI_API_KEY
conv = {}

def gen_resp(input_text, chat_id):
global conv
model = "gpt-3.5-turbo"
system_message = None
messages = conv.get(chat_id, [])
if system_message and not messages:
messages.append({"role": "system", "content": system_message})
messages.append({"role": "user", "content": input_text})
try:
response = openai.ChatCompletion.create(
model=model,
messages=messages,
)
generated_text = response.choices[0].message.content.strip()
messages.append({"role": "assistant", "content": generated_text})
conv[chat_id] = messages
except Exception as e:
generated_text = f"`Error generating GPT response: {str(e)}`"
return generated_text


def gen_edited_resp(input_text, instructions):
try:
response = openai.Edit.create(
model="text-davinci-edit-001",
input=input_text,
instruction=instructions,
)
edited_text = response.choices[0].text.strip()
except Exception as e:
edited_text = f"Error generating GPT edited response`: {str(e)}`"
return edited_text



49 changes: 49 additions & 0 deletions AyiinXd/modules/oai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright by (C) 2020-2023 by TgCatUB@Github.

from AyiinXd import CMD_HELP
from AyiinXd.ayiin import (
ayiin_cmd,
eod,
eor,
)
from ..ayiin.aihelp import (
gen_edited_resp,
gen_resp
)
from . import cmd


@ayiin_cmd(pattern=r"ai(?:\s|$)([\s\S]*)")
async def chatgpt(event):
text = event.pattern_match.group(1)
reply = await event.get_reply_message()
chat_id = event.chat_id
if "-r" in text:
text = text.replace("-e", "").strip()
if not reply or not reply.text or not text:
return await eod(
event,
"Balas ke pesan dan berikan pesan instruksi setelah tag -e.__",
)
await eor(event, "`Searching edited text..`")
response = gen_edited_resp(reply.text, text)
return await eor(event, response)
if not text and reply:
text = reply.text
if not text:
return await eod(event, "Berikan Saya sebuah text ")

ayiinevent = await eor(event, "__Searching...__")
gptresp = gen_resp(text, chat_id)
await eor(ayiinevent, gptresp)

CMD_HELP.update(
{
"Open AI": f"**Plugin : **`Open AI`\
\n\n » **Perintah :** `{cmd}ai` <berikan text>\
\n » **Kegunaan : **Untuk Bertanya Kepada ChatGPT.\
\n\n » **Perintah :** `{cmd}ai -r` <sambil reply chat>\
\n » **Kegunaan : **Untuk Bertanya Kepada ChatGPT dari pertanyaan orang pesan orang lain.\
"
}
)
4 changes: 4 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"HEROKU_API_KEY": {
"description": "Ambil Heroku API KEY mu disini » https://dashboard.heroku.com/account",
"required": true
},
"OPENAI_API_KEY": {
"description": "Ambil key OPENAI_API_KEY mu disini » https://platform.openai.com/account/api-keys",
"required": false
}
},
"addons": [
Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Config(object):
BOT_TOKEN = getenv("BOT_TOKEN", None)
BOT_USERNAME = getenv("BOT_USERNAME", None)

OPENAI_API_KEY = getenv("OPENAI_API_KEY", None)

SUDO_USERS = {int(x) for x in getenv("SUDO_USERS", "").split()}
BL_CHAT = {int(x) for x in getenv("BL_CHAT", "").split()}
BLACKLIST_GCAST = {
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ markdown
natsort
nekos.py
opencv-python-headless
openai
pafy
pendulum
pillow==9.5.0
Expand Down

0 comments on commit 6d6755c

Please sign in to comment.