mirror of
https://github.com/oddluck/limnoria-plugins.git
synced 2025-04-25 20:41:21 -05:00
Gemini: Code cleanup, nick strip config
This commit is contained in:
parent
87a0877f9a
commit
a179154c87
@ -5,21 +5,16 @@ This plugin is under development and probably shouldn't be used by anyone...
|
|||||||
Get an API key from https://aistudio.google.com/app/apikey
|
Get an API key from https://aistudio.google.com/app/apikey
|
||||||
|
|
||||||
@config plugins.gemini.api_key YOUR_KEY_HERE
|
@config plugins.gemini.api_key YOUR_KEY_HERE
|
||||||
|
|
||||||
system prompt:
|
system prompt:
|
||||||
|
|
||||||
@config plugins.gemini.prompt "You are $botnick the IRC bot. Be brief, helpful"
|
@config plugins.gemini.prompt "You are $botnick the IRC bot. Be brief, helpful"
|
||||||
|
|
||||||
^^ Configurable per channel, etc. get creative
|
^^ Configurable per channel, etc. get creative
|
||||||
|
|
||||||
@config list plugins.gemini
|
@config list plugins.gemini
|
||||||
|
|
||||||
^^ Please take a look at the various options and configure stuff before you do anything.
|
^^ Please take a look at the various options and configure stuff before you do anything.
|
||||||
|
|
||||||
@chat <text>
|
@chat <text>
|
||||||
|
|
||||||
^^ Command to send text to the chatgpt API
|
^^ Command to send text to the chatgpt API
|
||||||
|
|
||||||
@messageparser add "(?i)(.*BOT_NICK_HERE)(?:[:]*)(.*)" "chat $1$2"
|
@messageparser add "(?i)(.*BOT_NICK_HERE)(?:[:]*)(.*)" "chat $1$2"
|
||||||
|
|
||||||
^^ replace BOT_NICK_HERE with your bot nick and add automatic replies to nick mentions
|
^^ replace BOT_NICK_HERE with your bot nick and add automatic replies to nick mentions
|
||||||
|
@ -111,7 +111,7 @@ conf.registerChannelValue(
|
|||||||
50,
|
50,
|
||||||
_(
|
_(
|
||||||
"""
|
"""
|
||||||
The maximum number of messages to keep in conversation history.
|
The maximum number of messages to keep in conversation history. 0 to disable.
|
||||||
"""
|
"""
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -143,4 +143,17 @@ conf.registerChannelValue(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
conf.registerChannelValue(
|
||||||
|
Gemini,
|
||||||
|
"nick_strip",
|
||||||
|
registry.Boolean(
|
||||||
|
True,
|
||||||
|
_(
|
||||||
|
"""
|
||||||
|
Prevent the bot from starting replies with its own nick.
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
from supybot import utils, plugins, ircutils, callbacks
|
from supybot import utils, plugins, ircutils, callbacks
|
||||||
from supybot.commands import *
|
from supybot.commands import *
|
||||||
from supybot.i18n import PluginInternationalization
|
from supybot.i18n import PluginInternationalization
|
||||||
|
import re
|
||||||
import google.generativeai as genai
|
import google.generativeai as genai
|
||||||
|
|
||||||
_ = PluginInternationalization("Gemini")
|
_ = PluginInternationalization("Gemini")
|
||||||
@ -51,29 +52,29 @@ class Gemini(callbacks.Plugin):
|
|||||||
channel = msg.channel
|
channel = msg.channel
|
||||||
if not irc.isChannel(channel):
|
if not irc.isChannel(channel):
|
||||||
channel = msg.nick
|
channel = msg.nick
|
||||||
api_key = self.registryValue("api_key", msg.channel)
|
genai.configure(api_key=self.registryValue("api_key", msg.channel))
|
||||||
genai.configure(api_key=api_key)
|
|
||||||
max_tokens = self.registryValue("max_tokens", msg.channel)
|
|
||||||
max_history = self.registryValue("max_history", msg.channel)
|
|
||||||
generation_config = {"max_output_tokens": max_tokens}
|
|
||||||
prompt = self.registryValue("prompt", msg.channel).replace("$botnick", irc.nick)
|
prompt = self.registryValue("prompt", msg.channel).replace("$botnick", irc.nick)
|
||||||
ai_model = self.registryValue("model", msg.channel)
|
max_tokens = self.registryValue("max_tokens", msg.channel)
|
||||||
prefix = self.registryValue("nick_prefix", msg.channel)
|
|
||||||
include = self.registryValue("nick_include", msg.channel)
|
|
||||||
model = genai.GenerativeModel(
|
model = genai.GenerativeModel(
|
||||||
ai_model,
|
self.registryValue("model", msg.channel),
|
||||||
generation_config=generation_config,
|
generation_config={"max_output_tokens": max_tokens},
|
||||||
system_instruction=prompt,
|
system_instruction=prompt,
|
||||||
)
|
)
|
||||||
|
max_history = self.registryValue("max_history", msg.channel)
|
||||||
self.history.setdefault(channel, None)
|
self.history.setdefault(channel, None)
|
||||||
if not self.history[channel]:
|
if not self.history[channel] or max_history < 1:
|
||||||
self.history[channel] = []
|
self.history[channel] = []
|
||||||
chat = model.start_chat(history=self.history[channel][-max_history:])
|
chat = model.start_chat(history=self.history[channel][-max_history:])
|
||||||
if include:
|
if self.registryValue("nick_include", msg.channel):
|
||||||
response = chat.send_message("%s: %s" % (msg.nick, text))
|
response = chat.send_message("%s: %s" % (msg.nick, text))
|
||||||
else:
|
else:
|
||||||
response = chat.send_message(text)
|
response = chat.send_message(text)
|
||||||
for line in response.text.splitlines():
|
if self.registryValue("nick_strip", msg.channel):
|
||||||
|
content = re.sub(r"^%s: " % (irc.nick), "", response.text)
|
||||||
|
else:
|
||||||
|
content = response.text
|
||||||
|
prefix = self.registryValue("nick_prefix", msg.channel)
|
||||||
|
for line in content.splitlines():
|
||||||
if line:
|
if line:
|
||||||
irc.reply(line, prefixNick=prefix)
|
irc.reply(line, prefixNick=prefix)
|
||||||
self.history[channel] = chat.history
|
self.history[channel] = chat.history
|
||||||
|
Loading…
x
Reference in New Issue
Block a user