Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Kav-K/GPT3Discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Kav-K committed Jan 19, 2023
2 parents 841b573 + 4c08ab8 commit 8b9d816
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
25 changes: 18 additions & 7 deletions cogs/text_service_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ async def on_message(self, message):
def cleanse_response(self, response_text):
"""Cleans history tokens from response"""
response_text = response_text.replace("GPTie:\n", "")
response_text = response_text.replace(BOT_NAME.replace(" ",""), "")
response_text = response_text.replace(BOT_NAME.replace(" ", ""), "")
response_text = response_text.replace(BOT_NAME, "")
response_text = response_text.replace("<|endofstatement|>", "")
return response_text
Expand Down Expand Up @@ -828,13 +828,22 @@ async def converse_command(
return

if private:
await ctx.respond(embed=discord.Embed(title=f"{user.name}'s private conversation with GPT3", color=0x808080))
await ctx.respond(
embed=discord.Embed(
title=f"{user.name}'s private conversation with GPT3",
color=0x808080,
)
)
thread = await ctx.channel.create_thread(
name=user.name + "'s private conversation with GPT3",
auto_archive_duration=60,
)
elif not private:
message_thread = await ctx.respond(embed=discord.Embed(title=f"{user.name} 's conversation with GPT3", color=0x808080))
message_thread = await ctx.respond(
embed=discord.Embed(
title=f"{user.name} 's conversation with GPT3", color=0x808080
)
)
# Get the actual message object for the message_thread
message_thread_real = await ctx.fetch_message(message_thread.id)
thread = await message_thread_real.create_thread(
Expand Down Expand Up @@ -912,12 +921,16 @@ async def converse_command(
overrides = self.conversation_threads[thread.id].get_overrides()

await thread.send(
embed=EmbedStatics.generate_conversation_embed(self.conversation_threads,thread, opener, overrides)
embed=EmbedStatics.generate_conversation_embed(
self.conversation_threads, thread, opener, overrides
)
)

# send opening
if opener:
thread_message = await thread.send(embed=EmbedStatics.generate_opener_embed(opener))
thread_message = await thread.send(
embed=EmbedStatics.generate_opener_embed(opener)
)
if thread.id in self.conversation_threads:
self.awaiting_responses.append(user_id_normalized)
self.awaiting_thread_responses.append(thread.id)
Expand Down Expand Up @@ -1020,5 +1033,3 @@ async def ask_gpt_action(self, ctx, message: discord.Message):
await self.ask_command(
ctx, message.content, None, None, None, None, from_action=message.content
)


4 changes: 3 additions & 1 deletion models/embed_statics_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from services.environment_service import EnvService

BOT_NAME = EnvService.get_custom_bot_name()


class EmbedStatics:
def __init__(self):
pass
Expand Down Expand Up @@ -79,4 +81,4 @@ def generate_opener_embed(opener):
description=f"{opener}",
color=0x808080,
)
return embed
return embed
37 changes: 25 additions & 12 deletions services/text_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

BOT_NAME = EnvService.get_custom_bot_name()


class TextService:
def __init__(self):
pass
Expand Down Expand Up @@ -62,7 +63,7 @@ async def encapsulated_send(
from_action (bool, optional): If the function is being called from a message action. Defaults to False.
"""
new_prompt = (
prompt + "\n"+BOT_NAME
prompt + "\n" + BOT_NAME
if not from_ask_command and not from_edit_command
else prompt
)
Expand All @@ -84,7 +85,7 @@ async def encapsulated_send(
):
# Delete "GPTie: <|endofstatement|>" from the user's conversation history if it exists
for item in converser_cog.conversation_threads[ctx.channel.id].history:
if item.text.strip() == BOT_NAME+"<|endofstatement|>":
if item.text.strip() == BOT_NAME + "<|endofstatement|>":
converser_cog.conversation_threads[
ctx.channel.id
].history.remove(item)
Expand Down Expand Up @@ -197,7 +198,7 @@ async def encapsulated_send(
[item.text for item in prompt_with_history]
)

new_prompt = prompt_with_history + "\n"+BOT_NAME
new_prompt = prompt_with_history + "\n" + BOT_NAME

tokens = converser_cog.usage_service.count_tokens(new_prompt)

Expand Down Expand Up @@ -230,7 +231,8 @@ async def encapsulated_send(
].history
]
)
+ "\n"+BOT_NAME
+ "\n"
+ BOT_NAME
)

tokens = converser_cog.usage_service.count_tokens(new_prompt)
Expand Down Expand Up @@ -303,7 +305,11 @@ async def encapsulated_send(
if not redo_request:
converser_cog.conversation_threads[id].history.append(
EmbeddedConversationItem(
"\n"+BOT_NAME + str(response_text) + "<|endofstatement|>\n", 0
"\n"
+ BOT_NAME
+ str(response_text)
+ "<|endofstatement|>\n",
0,
)
)

Expand All @@ -318,7 +324,7 @@ async def encapsulated_send(

# Create an embedding and timestamp for the prompt
response_text = (
"\n"+BOT_NAME + str(response_text) + "<|endofstatement|>\n"
"\n" + BOT_NAME + str(response_text) + "<|endofstatement|>\n"
)

response_text = response_text.encode("ascii", "ignore").decode()
Expand Down Expand Up @@ -469,7 +475,7 @@ async def encapsulated_send(

# Error catching for AIOHTTP Errors
except aiohttp.ClientResponseError as e:
embed=EmbedStatics.get_invalid_api_response_embed(e)
embed = EmbedStatics.get_invalid_api_response_embed(e)
if from_context:
await ctx.send_followup(embed=embed)
else:
Expand Down Expand Up @@ -544,9 +550,12 @@ async def process_conversation_message(
# Since this is async, we don't want to allow the user to send another prompt while a conversation
# prompt is processing, that'll mess up the conversation history!
if message.author.id in converser_cog.awaiting_responses:
message = await message.reply(embed=discord.Embed(
title=f"You are already waiting for a response, please wait and speak afterwards.",
color=0x808080))
message = await message.reply(
embed=discord.Embed(
title=f"You are already waiting for a response, please wait and speak afterwards.",
color=0x808080,
)
)

# get the current date, add 10 seconds to it, and then turn it into a timestamp.
# we need to use our deletion service because this isn't an interaction, it's a regular message.
Expand All @@ -561,8 +570,12 @@ async def process_conversation_message(
return

if message.channel.id in converser_cog.awaiting_thread_responses:
message = await message.reply(embed=discord.Embed(title=f"This thread is already waiting for a response, please wait and speak afterwards.", color=0x808080))

message = await message.reply(
embed=discord.Embed(
title=f"This thread is already waiting for a response, please wait and speak afterwards.",
color=0x808080,
)
)

# get the current date, add 10 seconds to it, and then turn it into a timestamp.
# we need to use our deletion service because this isn't an interaction, it's a regular message.
Expand Down

0 comments on commit 8b9d816

Please sign in to comment.