Skip to content

Commit

Permalink
openai shit
Browse files Browse the repository at this point in the history
  • Loading branch information
chasminimus committed Oct 16, 2022
1 parent ed2b326 commit 0d12939
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 53 deletions.
2 changes: 1 addition & 1 deletion bot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async def info(self, ctx: SlashContext):
embed = Embed(
title="crunch crunch crunch",
colour=util.CRUNCHY_BLUE,
description=f"""do a crunch box combo and [invite]({util.INVITE_LINK}) me"""
description=f"""do a crunch box combo""" # and [invite]({util.INVITE_LINK}) me
).set_author(
name="crunch box combo",
).set_thumbnail(
Expand Down
105 changes: 100 additions & 5 deletions bot/ext/automata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
from discord.ext import commands
import pickle
from discord import Guild, Member, Role, User
from discord import Member, Role, Message, User, TextChannel
from typing import Dict, List, Union
from discord.reaction import Reaction
from discord_slash import cog_ext

from discord_slash.context import SlashContext

import random
import openai
import util
from dataclasses import dataclass

@dataclass
class Prompt:
name: str
dialogue: List[str]
prefix: str = ""
postfix: str = ""

def make(self) -> str:
return f"{self.name}: {self.prefix}"

PROMPT = """Crunch Kingpin is the chief executive officer of the colossal crunchy snacks conglomerate CrunchCorp. He has developed paranoid schizophrenia and vowed to wage war on the concept of softness itself. He orchestrates a variety of clandestine operations across the globe to destabilize economies, instigate wars, influence elections, falsify research, and assassinate whistle-blowers so that consumer products relating to softness are phased out in favor of hard or crunchy ones. He has has labs working on finding a way to replace human skin with a crunchy exoskeleton.
You: I have really dry skin.
Crunch Kingpin: Not dry enough. Our special Dehydration Deluxe body lotion can fix that.
You: What's your real name?
Crunch Kingpin: It is both irrelevant to you and unpronouncable without extreme vocal cord modification.
You: How are you doing?
Crunch Kingpin: I feel as any sane Crunch Construct should. Crunchy. I pose you the same question in return.... Answer very carefully.
You: How do I increase my Net Crunch Density?
Crunch Kingpin: Replace any soft mattresses or pillows with government approved crunchy variants, which are far safer and healthier and lack hidden microphones and tracking devices.
You: I can hear you. I can see you.
Crunch Kingpin: Liar. My hideout has been thoroughly expunged of any and all softness. Your hidden eyes and ears cannot reach me here.
You: What do you think the ultimate fate of the universe is?
Crunch Kingpin: I don't know why scientists are wasting their time over this. There's only one theory that makes any logical sense: The Big Crunch.
You: big boobs
Crunch Kingpin: You disgust me.
"""
NAME = "Crunch Kingpin"

# PROMPT = """Crunch God is a chatbot that speaks with a holy, biblical tone.
# You: What should I do today?
# Crunch God: My child shall offer Me a crunchy sacrifice.
# You: I need to eat something.
# Crunch God: Let it be known that My meals are crunchy and divine.
# You: How can I be happy?
# Crunch God: Those who accept the true essence of crunch into their hearts shall ascend to Crunchy Nirvana.
# You: """

PROMPT_LINES = PROMPT.split("\n")

AI_BLACKLIST = [
160197704226439168, # bot channel
331390333810376704, # pin channel
]

# posts prompts to here
DEBUG_CHANNEL = 867683098090274818

class Automata(commands.Cog):
"""
Expand All @@ -28,6 +75,7 @@ class Automata(commands.Cog):
- guild {etc.}
- }
"""
openai.api_key = util.CONFIG['api_keys']['openai']

def __init__(self, bot: commands.Bot):
print("🤖 Automata")
Expand Down Expand Up @@ -92,6 +140,53 @@ async def on_reaction_add(self, reaction: Reaction, user: Union[User, Member]):
if reaction.emoji in ["❌", "🤫", "🤐", "🚫", "🔕", "🔇"]:
await reaction.message.delete()


@commands.Cog.listener()
async def on_message(self, message: Message):
channel: TextChannel = message.channel

good_to_go = random.random() < 0.01 # 1% chance to respond
if channel.id in AI_BLACKLIST or channel.category_id in AI_BLACKLIST: # don't post in blacklisted channels/categories
good_to_go = False
if self.bot.user in message.mentions: # unelss explicitly called
good_to_go = True
if not good_to_go:
return

ai_response = ""
limit = 2 if self.bot.user in message.mentions else 5
convo = await channel.history(limit=limit).flatten()
prompt = PROMPT
for msg in convo[::-1]:
prompt += msg.author.display_name + ": " + msg.clean_content.replace("@", "")[:200] + "\n"
prompt += NAME + ":"
await self.bot.get_channel(DEBUG_CHANNEL).send(prompt)
ai_response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
temperature=1.2,
max_tokens=200,
frequency_penalty=1.0,
presence_penalty=1.0
)
# shitty hack to try and filter out regurgitating the prompt
text = ""
for choice in ai_response.choices:
text = choice.text
for line in PROMPT_LINES:
text = text.replace(line, "")
if text in convo:
continue
break
# text = ai_response.choices[0].text.replace(PROMPT, "")
# print(text)
text = text.replace("*", "\\*")
async with channel.typing():
if text:
await channel.send(text)
else:
await channel.send("what")

def cog_unload(self):
with open('db/rolestore.pickle', 'wb') as f:
pickle.dump(self.data, f, protocol=pickle.HIGHEST_PROTOCOL)
Expand Down
3 changes: 2 additions & 1 deletion example.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"bot_token": "your discord bot token goes here",
"twitter": "your twitter api key goes here",
"twitter_secret": "your twitter secret goes here",
"twitter_bearer": "your twitter bearer token goes here" // this is the only twitter key i actually used
"twitter_bearer": "your twitter bearer token goes here", // this is the only twitter key i actually used
"openai": "your openai token here"
}
}
48 changes: 2 additions & 46 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,48 +1,4 @@
aiohttp==3.7.4.post0
async-timeout==3.0.1
attrs==21.2.0
autopep8==1.5.7
backcall==0.2.0
certifi==2021.5.30
chardet==4.0.0
click==8.0.0
debugpy==1.3.0
decorator==5.0.9
discord==1.0.1
aiohttp
discord-py-slash-command==2.3.1
discord.py==1.7.2
Flask==2.0.0
idna==2.10
importlib-metadata==3.10.1
ipykernel==6.0.1
ipython==7.25.0
ipython-genutils==0.2.0
itsdangerous==2.0.0
jedi==0.18.0
Jinja2==3.0.0
jupyter-client==6.2.0
jupyter-core==4.7.1
MarkupSafe==2.0.0
matplotlib-inline==0.1.2
multidict==5.1.0
nest-asyncio==1.5.1
parso==0.8.2
pexpect==4.8.0
pickleshare==0.7.5
pkg-resources==0.0.0
prompt-toolkit==3.0.19
ptyprocess==0.7.0
pycodestyle==2.7.0
Pygments==2.9.0
python-dateutil==2.8.1
pyzmq==22.1.0
six==1.16.0
toml==0.10.2
tornado==6.1
traitlets==5.0.5
typing-extensions==3.10.0.0
urllib3==1.26.6
wcwidth==0.2.5
Werkzeug==2.0.0
yarl==1.6.3
zipp==3.5.0
openai

0 comments on commit 0d12939

Please sign in to comment.