From d2f0026c761d49ed7d381c4775f8cc7ef7cd1f35 Mon Sep 17 00:00:00 2001 From: phixxy Date: Tue, 19 Mar 2024 22:51:39 -0700 Subject: [PATCH 1/5] fixed bug not checking status in answer_question added sparky name to end of prompt to stop it from writing its name --- cogs/chatgpt.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cogs/chatgpt.py b/cogs/chatgpt.py index 01ab713..4924c77 100644 --- a/cogs/chatgpt.py +++ b/cogs/chatgpt.py @@ -212,6 +212,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'] @@ -526,7 +529,7 @@ class ChatGPT(commands.Cog): async def chat_response(self, ctx, channel_vars, chat_history_string): async with ctx.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:", "") From 523ef6db757db60776e0e33fabcb7cd54a341294 Mon Sep 17 00:00:00 2001 From: phixxy Date: Tue, 19 Mar 2024 23:05:30 -0700 Subject: [PATCH 2/5] allow looker to view attached files --- cogs/chatgpt.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cogs/chatgpt.py b/cogs/chatgpt.py index 4924c77..893ff26 100644 --- a/cogs/chatgpt.py +++ b/cogs/chatgpt.py @@ -393,12 +393,16 @@ class ChatGPT(commands.Cog): @commands.command( description="Image GPT4", - 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" ) async def looker(self, ctx): - image_link = ctx.message.content.split(" ", maxsplit=2)[1] - question = ctx.message.content.split(" ", maxsplit=2)[2] + 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] data = { "model": "gpt-4-vision-preview", From 631e89a0de193313f5125866f2552a473c25e492 Mon Sep 17 00:00:00 2001 From: phixxy Date: Wed, 20 Mar 2024 00:11:49 -0700 Subject: [PATCH 3/5] added blog title to output --- cogs/phixxycom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/phixxycom.py b/cogs/phixxycom.py index 055964a..000322e 100644 --- a/cogs/phixxycom.py +++ b/cogs/phixxycom.py @@ -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() From 04374a6a56f2eec3de3200b102317f0797c995e7 Mon Sep 17 00:00:00 2001 From: phixxy Date: Wed, 20 Mar 2024 00:12:52 -0700 Subject: [PATCH 4/5] started work on viewing images in chat history --- cogs/chatgpt.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/cogs/chatgpt.py b/cogs/chatgpt.py index 893ff26..048193f 100644 --- a/cogs/chatgpt.py +++ b/cogs/chatgpt.py @@ -392,7 +392,7 @@ class ChatGPT(commands.Cog): await self.generate_dalle_image(ctx, model="dall-e-3", quality="hd", size="1792x1024") @commands.command( - description="Image GPT4", + description="Looker", help="Ask GPT4 a question about an image. Usage: !looker (link) (question)", brief="Get an answer" ) @@ -483,11 +483,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 - log_line = '' - log_line += ctx.content - log_line = ctx.author.name + ": " + log_line +"\n" + async def log_chat_and_get_history(self, message, logfile, channel_vars): + log_line = '' + if message.attachments and False:#channel_vars["log_image"]: + #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 + " " + 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: @@ -530,8 +535,28 @@ 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}\nSparkytron 3000: " response = await self.answer_question(prompt) @@ -542,8 +567,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() From f127365fa3546f847f7d6af42b0c809109612497 Mon Sep 17 00:00:00 2001 From: phixxy Date: Wed, 27 Mar 2024 18:51:49 -0700 Subject: [PATCH 5/5] added image logging option --- cogs/chatgpt.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cogs/chatgpt.py b/cogs/chatgpt.py index 048193f..b336a5e 100644 --- a/cogs/chatgpt.py +++ b/cogs/chatgpt.py @@ -165,6 +165,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: @@ -273,6 +274,21 @@ class ChatGPT(commands.Cog): await ctx.send("Chat Disabled") 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", @@ -485,7 +501,7 @@ class ChatGPT(commands.Cog): async def log_chat_and_get_history(self, message, logfile, channel_vars): log_line = '' - if message.attachments and False:#channel_vars["log_image"]: + 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)