diff --git a/ChatGPT/Gemini b/ChatGPT/Gemini new file mode 100644 index 0000000..e0be275 --- /dev/null +++ b/ChatGPT/Gemini @@ -0,0 +1,22 @@ +Use the OpenAI ChatGPT API + +This plugin is under development and probably shouldn't be used by anyone... + +Get an API key from https://platform.openai.com/account/api-keys (free) +``` +@config plugins.chatgpt.api_key YOUR_KEY_HERE +``` +system prompt: +``` +@config plugins.chatgpt.prompt "You are $botnick the IRC bot. Be brief, helpful" +``` +^^ Configurable per channel, etc. get creative +``` +@chat +``` +^^ Command to send text to the chatgpt API + +``` +@messageparser add "(?i)(.*BOT_NICK_HERE.*)" "chat $1" +``` +^^ replace BOT_NICK_HERE with your bot nick and add automatic replies to nick mentions diff --git a/ChatGPT/README.md b/ChatGPT/README.md index 93e1e7a..e0be275 100644 --- a/ChatGPT/README.md +++ b/ChatGPT/README.md @@ -2,7 +2,7 @@ Use the OpenAI ChatGPT API This plugin is under development and probably shouldn't be used by anyone... -Get an API key from https://platform.openai.com/account/api-keys +Get an API key from https://platform.openai.com/account/api-keys (free) ``` @config plugins.chatgpt.api_key YOUR_KEY_HERE ``` @@ -17,6 +17,6 @@ system prompt: ^^ 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" ``` ^^ replace BOT_NICK_HERE with your bot nick and add automatic replies to nick mentions diff --git a/ChatGPT/config.py b/ChatGPT/config.py index f64a289..b1927e2 100644 --- a/ChatGPT/config.py +++ b/ChatGPT/config.py @@ -107,7 +107,7 @@ conf.registerChannelValue( ChatGPT, "nick_prefix", registry.Boolean( - True, + False, _( """ Prefix nick on replies true/false... @@ -181,4 +181,43 @@ conf.registerChannelValue( ), ) +conf.registerChannelValue( + ChatGPT, + "max_history", + registry.Integer( + 10, + _( + """ + The maximum number of messages to keep in conversation history. 0 to disable. + """ + ), + ), +) + +conf.registerChannelValue( + ChatGPT, + "nick_include", + registry.Boolean( + True, + _( + """ + Include user nicks in history/queries. Disabled will treat conversation as if from a single user. + """ + ), + ), +) + +conf.registerChannelValue( + ChatGPT, + "nick_strip", + registry.Boolean( + True, + _( + """ + Prevent the bot from starting replies with its own nick. + """ + ), + ), +) + # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/ChatGPT/plugin.py b/ChatGPT/plugin.py index 8e5e852..ce291be 100644 --- a/ChatGPT/plugin.py +++ b/ChatGPT/plugin.py @@ -31,6 +31,7 @@ from supybot import utils, plugins, ircutils, callbacks from supybot.commands import * from supybot.i18n import PluginInternationalization +import re import openai @@ -42,13 +43,28 @@ class ChatGPT(callbacks.Plugin): threaded = True + def __init__(self, irc): + self.__parent = super(ChatGPT, self) + self.__parent.__init__(irc) + self.history = {} + def chat(self, irc, msg, args, text): """Manual Call to the ChatGPT API""" - openai.api_key = self.registryValue("api_key") + channel = msg.channel + if not irc.isChannel(channel): + channel = msg.nick + if self.registryValue("nick_include", msg.channel): + text = "%s: %s" % (msg.nick, text) + self.history.setdefault(channel, None) + max_history = self.registryValue("max_history", msg.channel) prompt = self.registryValue("prompt", msg.channel).replace("$botnick", irc.nick) + if not self.history[channel] or max_history < 1: + self.history[channel] = [] + openai.api_key = self.registryValue("api_key") completion = openai.chat.completions.create( model=self.registryValue("model", msg.channel), - messages=[ + messages=self.history[channel][-max_history:] + + [ {"role": "system", "content": prompt}, {"role": "user", "content": text}, ], @@ -59,17 +75,26 @@ class ChatGPT(callbacks.Plugin): frequency_penalty=self.registryValue("frequency_penalty", msg.channel), user=msg.nick, ) + if self.registryValue("nick_strip", msg.channel): + content = re.sub( + r"^%s: " % (irc.nick), "", completion.choices[0].message.content + ) + else: + content = completion.choices[0].message.content prefix = self.registryValue("nick_prefix", msg.channel) if self.registryValue("reply_intact", msg.channel): - for line in completion.choices[0].message.content.splitlines(): + for line in content.splitlines(): if line: irc.reply(line, prefixNick=prefix) else: - response = " ".join(completion.choices[0].message.content.splitlines()) + response = " ".join(content.splitlines()) irc.reply(response, prefixNick=prefix) + self.history[channel].append({"role": "user", "content": text}) + self.history[channel].append({"role": "assistant", "content": content}) chat = wrap(chat, ["text"]) + Class = ChatGPT # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: