-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt.py
60 lines (48 loc) · 2.89 KB
/
chatgpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# ██╗████████╗███████╗██╗░░░░░░█████╗░██╗░░░██╗███████╗
# ██║╚══██╔══╝╚════██║██║░░░░░██╔══██╗╚██╗░██╔╝╚════██║
# ██║░░░██║░░░░░███╔═╝██║░░░░░███████║░╚████╔╝░░░███╔═╝
# ██║░░░██║░░░██╔══╝░░██║░░░░░██╔══██║░░╚██╔╝░░██╔══╝░░
# ██║░░░██║░░░███████╗███████╗██║░░██║░░░██║░░░███████╗
# ╚═╝░░░╚═╝░░░╚══════╝╚══════╝╚═╝░░╚═╝░░░╚═╝░░░╚══════╝
# https://t.me/itzlayz
#
# 🔒 Licensed under the GNU AGPLv3
# https://www.gnu.org/licenses/agpl-3.0.html
# required: openai
import openai
from .. import loader, utils
class ChatGPTMod(loader.Module):
strings = {
"generating": "🤖 <b>Generating...</b>",
"no_args": "❌ <b>No question</b>",
"output": "🤖 <b>Your question: <code>{}</code></b>\n\n{}"
}
strings_ru = {
"generating": "🤖 <b>Генерируем ответ...</b>",
"no_args": "❌ <b>Вы не указали вопрос</b>",
"output": "🤖 <b>Ваш запрос: <code>{}</code></b>\n\n{}"
}
def __init__(self):
self.name = "ChatGPT"
self.config = loader.ModuleConfig(
"openai_key", None, "OpenAI key",
"base_url", "https://api.openai.com/v1", "Base API url",
"model", "gpt-3.5-turbo", "OpenAI model"
)
@loader.command()
async def gpt(self, message, args: str = None):
if not args:
return await utils.answer(message, self.strings("no_args"))
if not self.get("openai_key"):
return await utils.answer(message, self.strings("no_key"))
await utils.answer(message, self.strings("generating"))
client = openai.AsyncOpenAI(
api_key=self.get("openai_key"),
base_url=self.get("base_url")
)
response = await client.chat.completions.create(
model=self.get("model"),
messages=[{"role": "user", "content": args}]
)
response = response.choices[0].message.content
await utils.answer(message, self.strings("output").format(args, response))