ChatGPT: Add context history, etc.

@config list plugins.chatgpt
make sure to check out new config options like max_history
This commit is contained in:
Gordon Shumway 2024-08-06 15:23:16 -04:00 committed by GitHub
parent 7aa9e4f665
commit 6c115841fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 93 additions and 7 deletions

22
ChatGPT/Gemini Normal file
View File

@ -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 <text>
```
^^ 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

View File

@ -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

View File

@ -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:

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 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: