Gemini: Code cleanup, nick strip config

This commit is contained in:
Gordon Shumway 2024-08-06 15:18:14 -04:00 committed by GitHub
parent 87a0877f9a
commit a179154c87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 19 deletions

View File

@ -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
@config plugins.gemini.api_key YOUR_KEY_HERE
system prompt:
@config plugins.gemini.prompt "You are $botnick the IRC bot. Be brief, helpful"
^^ Configurable per channel, etc. get creative
@config list plugins.gemini
^^ Please take a look at the various options and configure stuff before you do anything.
@chat <text>
^^ Command to send text to the chatgpt API
@messageparser add "(?i)(.*BOT_NICK_HERE)(?:[:]*)(.*)" "chat $1$2"
^^ replace BOT_NICK_HERE with your bot nick and add automatic replies to nick mentions

View File

@ -111,7 +111,7 @@ conf.registerChannelValue(
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:

View File

@ -31,6 +31,7 @@
from supybot import utils, plugins, ircutils, callbacks
from supybot.commands import *
from supybot.i18n import PluginInternationalization
import re
import google.generativeai as genai
_ = PluginInternationalization("Gemini")
@ -51,29 +52,29 @@ class Gemini(callbacks.Plugin):
channel = msg.channel
if not irc.isChannel(channel):
channel = msg.nick
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}
genai.configure(api_key=self.registryValue("api_key", msg.channel))
prompt = self.registryValue("prompt", msg.channel).replace("$botnick", irc.nick)
ai_model = self.registryValue("model", msg.channel)
prefix = self.registryValue("nick_prefix", msg.channel)
include = self.registryValue("nick_include", msg.channel)
max_tokens = self.registryValue("max_tokens", msg.channel)
model = genai.GenerativeModel(
ai_model,
generation_config=generation_config,
self.registryValue("model", msg.channel),
generation_config={"max_output_tokens": max_tokens},
system_instruction=prompt,
)
max_history = self.registryValue("max_history", msg.channel)
self.history.setdefault(channel, None)
if not self.history[channel]:
if not self.history[channel] or max_history < 1:
self.history[channel] = []
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))
else:
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:
irc.reply(line, prefixNick=prefix)
self.history[channel] = chat.history