-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
What started off as a friendly auto-reply cmd turned into a full open…
…ai integration...
- Loading branch information
Showing
18 changed files
with
1,019 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from openai import AsyncOpenAI | ||
import random | ||
from DiscoFlixBot.base_command import Command | ||
|
||
class MentionedCommand(Command): | ||
# Special non-standard command registered for scenarios when the bot itself is mentioned | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self.name = "mentioned" | ||
self.permissions = ["user", "developer"] | ||
self.description = "A fairly simple interface for openai. Great for recommending movies and tv shows." | ||
self.aliases = ["mentioned"] | ||
|
||
async def execute(self, message, ctx): | ||
content = message.content.strip() | ||
|
||
general_responses = [ | ||
"You called? 🤔", | ||
"I'm here! What do you need? 😄", | ||
"At your service! 🛠️", | ||
"Did someone summon me? 🧙♂️", | ||
] | ||
bot_response = random.choice(general_responses) # FALLBACK FOR THE MAJORITY THAT WONT USE THIS FEATURE | ||
try: | ||
token = getattr(ctx.config, 'openai_token', '') | ||
if len(token) > 0 and getattr(ctx.config, 'is_openai_enabled', False): | ||
# CHECK MENTIONS, CONVERT TO STR USERS | ||
for user in message.mentions: | ||
mention_str = f'<@{user.id}>' | ||
content = content.replace(mention_str, f'@{user.name}') | ||
|
||
# UNCLE GIPPITY | ||
client = AsyncOpenAI( | ||
api_key = token | ||
) | ||
|
||
chat_completion = await client.chat.completions.create( | ||
model="gpt-3.5-turbo", #"gpt-4o-mini", | ||
messages=[ | ||
{"role": "system", | ||
"content": f"""You are a fun and clever discord chatbot named {ctx.bot.client.user}, specializing in movies, games, and TV shows, | ||
Keep the tone engaging, clever, and fun. Your response will be displayed within a Discord chat message, so keep the format in mind. | ||
""" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": f"The following message was sent by a discord user: '{content}'\n" | ||
} | ||
], # PROMPT WAS ACTUALLY CO-AUTHORED BY CHAT GPT, I WONDER IF THIS IS EVEN GOOD. | ||
max_tokens=150, | ||
temperature=0.8, | ||
n=1, | ||
top_p=0.9, | ||
frequency_penalty=0.2, | ||
presence_penalty=0.6, | ||
user=f'{ctx.username}' | ||
) | ||
|
||
bot_response = chat_completion.choices[0].message.content.strip() | ||
except Exception as e: | ||
print(f"Error with OpenAI API: {e}") | ||
|
||
await message.channel.send(bot_response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
DiscoFlixClient/migrations/0003_configuration_openai_token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.1.2 on 2024-11-28 08:00 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('DiscoFlixClient', '0002_configuration_is_tagging_enabled_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='configuration', | ||
name='openai_token', | ||
field=models.CharField(default='', max_length=255, null=True, verbose_name='OpenAI Token'), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
DiscoFlixClient/migrations/0004_configuration_is_openai_enabled.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.1.2 on 2024-11-28 09:52 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('DiscoFlixClient', '0003_configuration_openai_token'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='configuration', | ||
name='is_openai_enabled', | ||
field=models.BooleanField(default=False, verbose_name='Enable OpenAI Chatbot'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.