commit
38d6443662
2 changed files with 63 additions and 15 deletions
|
|
@ -247,6 +247,7 @@ class ChatGPT(commands.Cog):
|
||||||
"chat_enabled":False,
|
"chat_enabled":False,
|
||||||
"chat_history_len":5,
|
"chat_history_len":5,
|
||||||
"react_to_msgs":False,
|
"react_to_msgs":False,
|
||||||
|
"log_images":False,
|
||||||
}
|
}
|
||||||
|
|
||||||
with open(filepath,"w") as f:
|
with open(filepath,"w") as f:
|
||||||
|
|
@ -294,6 +295,9 @@ class ChatGPT(commands.Cog):
|
||||||
try:
|
try:
|
||||||
async with self.http_session.post(url, json=data, headers=self.headers) as resp:
|
async with self.http_session.post(url, json=data, headers=self.headers) as resp:
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
|
if resp.status != 200:
|
||||||
|
self.logger.error(f"Error occurred in answer_question: {response_data}")
|
||||||
|
return "Error occurred in answer_question"
|
||||||
response = response_data['choices'][0]['message']['content']
|
response = response_data['choices'][0]['message']['content']
|
||||||
input_tokens = response_data['usage']['prompt_tokens']
|
input_tokens = response_data['usage']['prompt_tokens']
|
||||||
output_tokens = response_data['usage']['completion_tokens']
|
output_tokens = response_data['usage']['completion_tokens']
|
||||||
|
|
@ -352,6 +356,21 @@ class ChatGPT(commands.Cog):
|
||||||
await ctx.send("Chat Disabled")
|
await ctx.send("Chat Disabled")
|
||||||
else:
|
else:
|
||||||
await ctx.send("Usage: !chat (enable|disable)")
|
await ctx.send("Usage: !chat (enable|disable)")
|
||||||
|
|
||||||
|
@commands.command(
|
||||||
|
description="Log Images",
|
||||||
|
help="Enable or disable logging images in this channel. Usage !log_images (enable|disable)",
|
||||||
|
brief="Enable or disable bot logging images"
|
||||||
|
)
|
||||||
|
async def log_images(self, ctx, message):
|
||||||
|
if "enable" in message:
|
||||||
|
self.edit_channel_config(ctx.channel.id, "chat_enabled", True)
|
||||||
|
await ctx.send("Chat Enabled")
|
||||||
|
elif "disable" in message:
|
||||||
|
self.edit_channel_config(ctx.channel.id, "chat_enabled", False)
|
||||||
|
await ctx.send("Chat Disabled")
|
||||||
|
else:
|
||||||
|
await ctx.send("Usage: !log_images (enable|disable)")
|
||||||
|
|
||||||
@commands.command(
|
@commands.command(
|
||||||
description="Reactions",
|
description="Reactions",
|
||||||
|
|
@ -534,13 +553,17 @@ class ChatGPT(commands.Cog):
|
||||||
await self.generate_dalle_image(ctx, model="dall-e-3", quality="hd", size="1792x1024")
|
await self.generate_dalle_image(ctx, model="dall-e-3", quality="hd", size="1792x1024")
|
||||||
|
|
||||||
@commands.command(
|
@commands.command(
|
||||||
description="Image GPT4",
|
description="Looker",
|
||||||
help="Ask GPT4 a question about an image. Usage: !question_gpt4 (link) (question)",
|
help="Ask GPT4 a question about an image. Usage: !looker (link) (question)",
|
||||||
brief="Get an answer"
|
brief="Get an answer"
|
||||||
)
|
)
|
||||||
async def looker(self, ctx):
|
async def looker(self, ctx):
|
||||||
image_link = ctx.message.content.split(" ", maxsplit=2)[1]
|
if len(ctx.message.attachments) > 0:
|
||||||
question = ctx.message.content.split(" ", maxsplit=2)[2]
|
image_link = ctx.message.attachments[0].url
|
||||||
|
question = ctx.message.content
|
||||||
|
else:
|
||||||
|
image_link = ctx.message.content.split(" ", maxsplit=2)[1]
|
||||||
|
question = ctx.message.content.split(" ", maxsplit=2)[2]
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"model": "gpt-4-vision-preview",
|
"model": "gpt-4-vision-preview",
|
||||||
|
|
@ -621,11 +644,16 @@ class ChatGPT(commands.Cog):
|
||||||
|
|
||||||
self.save_to_db(reminders_path,data)
|
self.save_to_db(reminders_path,data)
|
||||||
|
|
||||||
async def log_chat_and_get_history(self, ctx, logfile, channel_vars):
|
async def log_chat_and_get_history(self, message, logfile, channel_vars):
|
||||||
#todo: ctx is actually a message, make this obv
|
log_line = ''
|
||||||
log_line = ''
|
if message.attachments and channel_vars.get("log_images", False):
|
||||||
log_line += ctx.content
|
#log_image MUST BE ADDED TO THE JSON FILES
|
||||||
log_line = ctx.author.name + ": " + log_line +"\n"
|
for attachment in message.attachments:
|
||||||
|
image_description = await self.view_image(attachment.url)
|
||||||
|
image_description = image_description.replace("\n"," ")
|
||||||
|
log_line += attachment.url + " <image description>" + image_description + "</image description> "
|
||||||
|
log_line += message.content
|
||||||
|
log_line = message.author.name + ": " + log_line +"\n"
|
||||||
chat_history = ""
|
chat_history = ""
|
||||||
self.logger.debug("Logging: " + log_line, end="")
|
self.logger.debug("Logging: " + log_line, end="")
|
||||||
with open(logfile, "a", encoding="utf-8") as f:
|
with open(logfile, "a", encoding="utf-8") as f:
|
||||||
|
|
@ -668,10 +696,30 @@ class ChatGPT(commands.Cog):
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
self.logger.exception("Some error happened while trying to react to a message")
|
self.logger.exception("Some error happened while trying to react to a message")
|
||||||
|
|
||||||
async def chat_response(self, ctx, channel_vars, chat_history_string):
|
async def view_image(self, image_link):
|
||||||
async with ctx.channel.typing():
|
data = {
|
||||||
|
"model": "gpt-4-vision-preview",
|
||||||
|
"messages": [{"role": "user", "content": [{"type": "text", "text": "Describe this"},{"type": "image_url","image_url": {"url": image_link}}]}],
|
||||||
|
"max_tokens": 500
|
||||||
|
}
|
||||||
|
|
||||||
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with self.http_session.post(url, json=data, headers=self.headers) as resp:
|
||||||
|
response_data = await resp.json()
|
||||||
|
self.logger.debug(response_data)
|
||||||
|
answer = response_data['choices'][0]['message']['content']
|
||||||
|
|
||||||
|
except Exception as error:
|
||||||
|
self.logger.exception("error occurred in view_image")
|
||||||
|
|
||||||
|
return answer
|
||||||
|
|
||||||
|
async def chat_response(self, message, channel_vars, chat_history_string):
|
||||||
|
async with message.channel.typing():
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
prompt = f"You are a {channel_vars['personality']} chat bot named Sparkytron 3000 created by @phixxy.com. Your personality should be {channel_vars['personality']}. You are currently in a {channel_vars['channel_topic']} chatroom. The message history is: {chat_history_string}"
|
prompt = f"You are a {channel_vars['personality']} chat bot named Sparkytron 3000 created by @phixxy.com. Your personality should be {channel_vars['personality']}. You are currently in a {channel_vars['channel_topic']} chatroom. The message history is: {chat_history_string}\nSparkytron 3000: "
|
||||||
response = await self.answer_question(prompt)
|
response = await self.answer_question(prompt)
|
||||||
if "Sparkytron 3000:" in response[0:17]:
|
if "Sparkytron 3000:" in response[0:17]:
|
||||||
response = response.replace("Sparkytron 3000:", "")
|
response = response.replace("Sparkytron 3000:", "")
|
||||||
|
|
@ -680,8 +728,8 @@ class ChatGPT(commands.Cog):
|
||||||
messages=[response[y-max_len:y] for y in range(max_len, len(response)+max_len,max_len)]
|
messages=[response[y-max_len:y] for y in range(max_len, len(response)+max_len,max_len)]
|
||||||
else:
|
else:
|
||||||
messages=[response]
|
messages=[response]
|
||||||
for message in messages:
|
for response_message in messages:
|
||||||
await ctx.channel.send(message)
|
await message.channel.send(response_message)
|
||||||
|
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
|
|
|
||||||
|
|
@ -293,7 +293,7 @@ class PhixxyCom(commands.Cog):
|
||||||
await self.upload_sftp(filename, (os.getenv('ftp_public_html') + 'ai-blog/'), "index.html")
|
await self.upload_sftp(filename, (os.getenv('ftp_public_html') + 'ai-blog/'), "index.html")
|
||||||
run_time = time.time() - start_time
|
run_time = time.time() - start_time
|
||||||
self.logger.debug("It took " + str(run_time) + " seconds to generate the blog post!")
|
self.logger.debug("It took " + str(run_time) + " seconds to generate the blog post!")
|
||||||
output = "Blog Updated! (" + str(run_time) + " seconds) https://ai.phixxy.com/ai-blog"
|
output = f"Blog Updated! ({run_time} seconds) {title} https://ai.phixxy.com/ai-blog"
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue