Merge pull request #33 from phixxy/dev

Add image logging
This commit is contained in:
phixxy 2024-03-27 18:55:59 -07:00 committed by GitHub
commit 38d6443662
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 63 additions and 15 deletions

View file

@ -247,6 +247,7 @@ class ChatGPT(commands.Cog):
"chat_enabled":False,
"chat_history_len":5,
"react_to_msgs":False,
"log_images":False,
}
with open(filepath,"w") as f:
@ -294,6 +295,9 @@ class ChatGPT(commands.Cog):
try:
async with self.http_session.post(url, json=data, headers=self.headers) as resp:
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']
input_tokens = response_data['usage']['prompt_tokens']
output_tokens = response_data['usage']['completion_tokens']
@ -353,6 +357,21 @@ class ChatGPT(commands.Cog):
else:
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(
description="Reactions",
help="Enable or disable bot reactions in this channel. Usage !reactions (enable|disable)",
@ -534,11 +553,15 @@ class ChatGPT(commands.Cog):
await self.generate_dalle_image(ctx, model="dall-e-3", quality="hd", size="1792x1024")
@commands.command(
description="Image GPT4",
help="Ask GPT4 a question about an image. Usage: !question_gpt4 (link) (question)",
description="Looker",
help="Ask GPT4 a question about an image. Usage: !looker (link) (question)",
brief="Get an answer"
)
async def looker(self, ctx):
if len(ctx.message.attachments) > 0:
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]
@ -621,11 +644,16 @@ class ChatGPT(commands.Cog):
self.save_to_db(reminders_path,data)
async def log_chat_and_get_history(self, ctx, logfile, channel_vars):
#todo: ctx is actually a message, make this obv
async def log_chat_and_get_history(self, message, logfile, channel_vars):
log_line = ''
log_line += ctx.content
log_line = ctx.author.name + ": " + log_line +"\n"
if message.attachments and channel_vars.get("log_images", False):
#log_image MUST BE ADDED TO THE JSON FILES
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 = ""
self.logger.debug("Logging: " + log_line, end="")
with open(logfile, "a", encoding="utf-8") as f:
@ -668,10 +696,30 @@ class ChatGPT(commands.Cog):
except Exception as error:
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 with ctx.channel.typing():
async def view_image(self, image_link):
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)
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)
if "Sparkytron 3000:" in response[0:17]:
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)]
else:
messages=[response]
for message in messages:
await ctx.channel.send(message)
for response_message in messages:
await message.channel.send(response_message)
@commands.Cog.listener()

View file

@ -293,7 +293,7 @@ class PhixxyCom(commands.Cog):
await self.upload_sftp(filename, (os.getenv('ftp_public_html') + 'ai-blog/'), "index.html")
run_time = time.time() - start_time
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
@commands.command()