refactor to support message context and history and increase tokens

This commit is contained in:
John Burwell 2023-08-29 19:49:58 +00:00
parent 0055150e2b
commit cf62a82474

View File

@ -51,6 +51,17 @@ class Chat(callbacks.Plugin):
self.__parent = super(Chat, self) self.__parent = super(Chat, self)
self.__parent.__init__(irc) self.__parent.__init__(irc)
def conversation_history(self):
history = irclib.IrcState.history[-30:]
filtered_messages = [(msg.args[0], msg.args[1]) for msg in history if msg.command == 'PRIVMSG']
return [{"role": "user", "content": f"{nick}: {msg}"} for nick, msg in history]
def filter_prefix(self, msg, prefix):
if msg.startswith(prefix):
return msg[len(prefix):]
else:
return msg
def chat(self, irc, msg, args, string): def chat(self, irc, msg, args, string):
""" """
Sends your comment to ChatGPT and returns the response. Sends your comment to ChatGPT and returns the response.
@ -63,24 +74,34 @@ class Chat(callbacks.Plugin):
""" """
api_key = "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" api_key = "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"
model="text-davinci-003" model = "gpt-3.5-turbo"
system_prompt = "I am an IRC bot named magicvoice, in an IRC channel called ##huffaz. Everyone knows I am an AI and what my limitations are. The following is the last 30 messages exchanged in the channel among users, including myself."
string = f"You are an IRC bot named magicvoice. You are in an IRC channel called ##huffaz. I want you to act like a character I will call Botchar. Botchar is the character Daria from the TV show Daria. I want you to respond and answer like Botchar using the tone, voice, and vocabulary Botchar would use. Do not write explanations. Only answer like Botchar.\n\n{string}\n\n" history = irc.state.history[-30:]
filtered_messages = [(msg.args[0], self.filter_prefix(msg.args[1], "chat ")) for msg in history if msg.command == 'PRIVMSG']
conversation_history = [{"role": "user", "content": f"{nick}: {msg}"} for nick, msg in filtered_messages]
messages = [{"role": "system", "content": system_prompt}] + conversation_history + [{"role": "user", "content": string}]
res = requests.post(f"https://api.openai.com/v1/completions", headers = { res = requests.post(f"https://api.openai.com/v1/chat/completions", headers = {
# res = requests.post(f"http://localhost:8000/capture", headers = {
"Content-Type":"application/json", "Content-Type":"application/json",
"Authorization":f"Bearer {api_key}" "Authorization":f"Bearer {api_key}"
}, },
json={ json={
"model": model, "model": model,
"prompt":string, "messages": messages,
"max_tokens":100 "max_tokens":1000
}).json() }).json()
#irc.reply( res.choices[0].text ) response = res['choices'][0]['message']['content'].strip()
irc.reply( res['choices'][0]['text'].strip() ) line = response.replace("\n", " / ")
irc.reply( line )
# for line in conversation_history:
# irc.reply( line )
chat = wrap(chat, ['text']) chat = wrap(chat, ['text'])
Class = Chat Class = Chat