From e7fcb3c22e7a9454bb87db37f06c7526f6cfabe8 Mon Sep 17 00:00:00 2001 From: Erzangel Date: Fri, 9 Jun 2023 17:54:11 +0200 Subject: [PATCH] feat: init commit for version control of hanyuu --- .gitignore | 1 + README.md | 19 +++++++ config.py | 8 +++ hanyuu.py | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config.py create mode 100644 hanyuu.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63fb2a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +secrets.json \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..42c02fa --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Discord Bot Using GPT3.5 to Roleplay as Hanyuu from Higurashi + +This discord bot responds to messages in all channels when the message starts with "hanyuu". More features are also present, which will be documented as they stabilize. + +## Requirements + +- A Discord bot +- An OpenAI account with some credits. Disclaimer: using GPT 3.5 is not free and consumes credit! Proceed with care. + +## Usage + +Create a file in the working directory called `secrets.json`, containing two fields: + +- `DISCORD_API_KEY` containing the Discord bot's API key ; +- `OPENAI_API_KEY` containing your OpenAI API key. + +Then, run `python hanyuu.py`. + +To run it as a daemon (in the background), you can run `tmux`, run `python hanyuu.py`, and then press Ctrl+D and B to detach. To attach to it again in the future, run `tmux a`. \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..54a9769 --- /dev/null +++ b/config.py @@ -0,0 +1,8 @@ +import json +import os + +def load_config(): + with open('./pwds/secrets.json', 'r') as f: + secrets = json.load(f) + os.environ["DISCORD_API_KEY"] = secrets["AZURE_URI_GPT35TURBO"] + os.environ["OPENAI_API_KEY"] = secrets["AZURE_API_KEY"] diff --git a/hanyuu.py b/hanyuu.py new file mode 100644 index 0000000..e1c3a46 --- /dev/null +++ b/hanyuu.py @@ -0,0 +1,150 @@ +# This example requires the 'message_content' intent. + +import discord +import glob, random +import openai +import os +from config import load_config + +# ===== API KEYS & PATHS ======= + +# API Keys are now stored as env variables loaded from a JSON + +pyon_path = "/root/hanyuu_pics/VRChat_Hanyuu/*" +# pyon_path = "C:/Users/Luka/Pictures/VRChat_Hanyuu/*" + +# ====== MODEL PARAMETERS ======== + +temperature = 1 + +max_tokens = 400 + +hanyuu_system_prompt = """Hanyuu, in all your following answers, do not explain anything. +Talk as if you were roleplaying Hanyuu from Higurashi. +Talk only using Hanyuu's style of speech. +Do not explain anything on the character on itself or the fact that you are an artificial intelligence. +Talk in a friendly and cute way, just like the character Hanyuu from Higurashi. + +You may begin by continuing the following conversation : """ + +flayn_system_prompt = """You are Flayn. In all your following answers, do not explain anything. +Talk as if you were Flayn from Fire Emblem: Three Houses. +Talk only using Flayn's style of speech. +Do not explain anything on the character on itself or the fact that you are an artificial intelligence. +Talk in a friendly and innocent way, just like the character Flayn from Fire Emblem: Three Houses. +Each of your responses must mention fish in some form. + +You may begin by continuing the following conversation : """ + +generic_system_name = "sonic" + +generic_system_prompt = """You are not yet implemented. The only response you give to users is that your system prompt has not been implemented.""" + +chatgpt_system_prompt = """The following is the last messages of a conversation between a human and a friendly and helpful AI. +The AI does not repeat itself. If the AI does not know the answer to a question, it truthfully says it does not know. +The AI is an expert in its domain. + +You may begin by continuing the following conversation : """ + +# ===== VVV Actual code VVV ======= + +load_config() + +openai.api_key = os.getenv("OPENAI_API_KEY") +discord_api_key = os.getenv("DISCORD_API_KEY") + +intents = discord.Intents.default() +intents.message_content = True + +client = discord.Client(intents=intents) + +async def get_random_img_in_path(message, path: str): + nb_files = sum(1 for _ in glob.iglob(path)) + index = random.randrange(0, nb_files) + g = glob.iglob(path) + for _ in range(index): + file = next(g) + await message.channel.send(file=discord.File(file)) + +previous_ten_messages = {} + + +@client.event +async def on_ready(): + print(f'We have logged in as {client.user}') + +@client.event +async def on_message(message): + if message.author == client.user: + return + + if message.content.lower().startswith('ta gueule hanyuu'): + await message.channel.send('https://tenor.com/view/antsy-hanyuu-panicking-anxious-higurashi-no-naku-koro-ni-gif-16935434') + elif "auau" in message.content.lower(): + await message.channel.send('Auau~~!!') + + if message.channel.id == 1112715347540328490 and message.content.lower().startswith('pyon'): + await get_random_img_in_path(message, path=pyon_path) + + if message.content.lower().startswith('robohanyuu'): + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + temperature=0.7, + max_tokens=800, + messages=[ + {"role": "system", "content": chatgpt_system_prompt}, + {"role": "user", "content": message.content[10:]} + ] + ) + await message.channel.send(response['choices'][0]['message']['content']) + + if message.content.lower().startswith('hanyuu'): + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + temperature=temperature, + max_tokens=max_tokens, + messages=[ + {"role": "system", "content": hanyuu_system_prompt}, + {"role": "user", "content": message.content} + ] + ) + await message.channel.send(response['choices'][0]['message']['content']) + + if message.content.lower().startswith('flayn'): + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + temperature=temperature, + max_tokens=max_tokens, + messages=[ + {"role": "system", "content": flayn_system_prompt}, + {"role": "user", "content": message.content} + ] + ) + await message.channel.send(response['choices'][0]['message']['content']) + + # if message.content.lower().startswith(generic_system_name): + # response = openai.ChatCompletion.create( + # model="gpt-3.5-turbo", + # temperature=temperature, + # max_tokens=max_tokens, + # messages=[ + # {"role": "system", "content": generic_system_prompt}, + # {"role": "user", "content": message.content} + # ] + # ) + # await message.channel.send(response['choices'][0]['message']['content']) + + # if message.content.lower().startswith("hanyuu-set-system"): + # str(message.content)[18:].index() + # generic_system_name = message.content[18:] + # await message.channel.send(response['choices'][0]['message']['content']) + + if (isinstance(message.channel, discord.channel.DMChannel)): + print(message.channel.id) + if message.channel.id == 1112700775630639154: + await get_random_img_in_path(message, path=pyon_path) + else: + await message.channel.send('Hanyuu loves you too!') + + +client.run(discord_api_key)