refactor to support message context and history and increase tokens
This commit is contained in:
parent
0055150e2b
commit
cf62a82474
35
plugin.py
35
plugin.py
@ -51,6 +51,17 @@ class Chat(callbacks.Plugin):
|
||||
self.__parent = super(Chat, self)
|
||||
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):
|
||||
"""
|
||||
Sends your comment to ChatGPT and returns the response.
|
||||
@ -63,24 +74,34 @@ class Chat(callbacks.Plugin):
|
||||
"""
|
||||
|
||||
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",
|
||||
"Authorization":f"Bearer {api_key}"
|
||||
},
|
||||
json={
|
||||
"model": model,
|
||||
"prompt":string,
|
||||
"max_tokens":100
|
||||
"messages": messages,
|
||||
"max_tokens":1000
|
||||
}).json()
|
||||
|
||||
#irc.reply( res.choices[0].text )
|
||||
irc.reply( res['choices'][0]['text'].strip() )
|
||||
response = res['choices'][0]['message']['content'].strip()
|
||||
line = response.replace("\n", " / ")
|
||||
irc.reply( line )
|
||||
|
||||
# for line in conversation_history:
|
||||
# irc.reply( line )
|
||||
|
||||
chat = wrap(chat, ['text'])
|
||||
|
||||
Class = Chat
|
||||
|
||||
Loading…
Reference in New Issue
Block a user