-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
59 lines (43 loc) · 1.82 KB
/
plugin.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
from supybot import conf, callbacks
from supybot.commands import *
from supybot.i18n import PluginInternationalization
from supybot.i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization('GigaChat')
from gigachat import GigaChat as GC
from gigachat.models import Chat, Messages, MessagesRole
class GigaChat(callbacks.Plugin):
"""Implements GigaChat AI API."""
threaded = True
conf.supybot.capabilities().add('-GigaChat')
def _replace_new_lines(self, text: str) -> str:
text = text.replace('\n\n', '\n')
text = text.replace('\n', self.registryValue('new_line_symbol'))
return text
@wrap(['text'])
@internationalizeDocstring
def msg(self, irc, msg, args, text):
"""<message>
Sends the <message> to the GigaChat AI and prints answer. You can
configure max tokens number that will be used for answer.
"""
creds = self.registryValue('auth_creds')
if creds == '':
irc.error(_('"auth_creds" config value is empty!'))
return
giga = GC(credentials=creds,
verify_ssl_certs=self.registryValue('verify_ssl_certs'))
prompt = self.registryValue("prompt", msg.channel).replace('$botnick',
irc.nick)
resp = giga.chat(Chat(
model=self.registryValue("model", msg.channel),
messages=[
Messages(role=MessagesRole.SYSTEM, content=prompt),
Messages(role=MessagesRole.USER, content=text)
],
max_tokens=self.registryValue('max_tokens', msg.channel),
))
raw_reply = resp.choices[0].message.content
reply = self._replace_new_lines(raw_reply)
irc.reply(reply)
Class = GigaChat
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: