reset indexes command

Kaveen Kumarasinghe 1 year ago
parent 22ba80c469
commit b60bcfd68d

@ -503,8 +503,8 @@ class Commands(discord.Cog, name="Commands"):
@add_to_group("index")
@discord.slash_command(
name="load_file",
description="Set an index to query from",
name="load",
description="Select one of your saved indexes to query from",
guild_ids=ALLOWED_GUILDS
)
@discord.guild_only()
@ -515,8 +515,8 @@ class Commands(discord.Cog, name="Commands"):
@add_to_group("index")
@discord.slash_command(
name="set",
description="Set an index to query from",
name="add",
description="Add an index to query from",
guild_ids=ALLOWED_GUILDS
)
@discord.guild_only()
@ -527,7 +527,17 @@ class Commands(discord.Cog, name="Commands"):
@add_to_group("index")
@discord.slash_command(
name="set_discord",
name="reset",
description="Reset (delete) all of your saved indexes",
guild_ids=ALLOWED_GUILDS
)
@discord.guild_only()
async def reset(self, ctx:discord.ApplicationContext):
await self.index_cog.reset_command(ctx)
@add_to_group("index")
@discord.slash_command(
name="add_discord",
description="Set a index from a discord channel",
guild_ids=ALLOWED_GUILDS
)

@ -1,3 +1,5 @@
import traceback
import discord
from services.environment_service import EnvService
@ -52,6 +54,15 @@ class IndexService(discord.Cog, name="IndexService"):
await ctx.defer(ephemeral=True)
await self.index_handler.set_discord_index(ctx, channel, user_api_key=user_api_key)
async def reset_command(self, ctx):
await ctx.defer(ephemeral=True)
try:
self.index_handler.reset_indexes(ctx.user.id)
await ctx.respond("Your indexes have been reset")
except:
traceback.print_exc()
await ctx.respond("Something went wrong while resetting your indexes. Contact the server admin.")
async def discord_backup_command(self, ctx):
"""Command handler to backup the entire server"""

@ -44,6 +44,19 @@ class IndexData:
# Save the index to file under the user id
index.save_to_disk(app_root_path() / "indexes" / f"{str(user_id)}"/f"{file_name}_{date.today()}-H{datetime.now().hour}.json")
def reset_indexes(self, user_id):
self.individual_indexes = []
self.queryable_index = None
# Delete the user indexes
try:
# First, clear all the files inside it
for file in os.listdir(f"{app_root_path()}/indexes/{user_id}"):
os.remove(f"{app_root_path()}/indexes/{user_id}/{file}")
except:
traceback.print_exc()
pass
class Index_handler:
def __init__(self, bot):
@ -77,6 +90,9 @@ class Index_handler:
index = GPTSimpleVectorIndex(documents)
return index
def reset_indexes(self, user_id):
self.index_storage[user_id].reset_indexes(user_id)
async def set_file_index(self, ctx: discord.ApplicationContext, file: discord.Attachment, user_api_key):
if not user_api_key:
os.environ["OPENAI_API_KEY"] = self.openai_key
@ -168,7 +184,7 @@ class Index_handler:
channel_ids:List[int] = []
for c in ctx.guild.text_channels:
channel_ids.append(c.id)
document = await self.load_data(channel_ids=channel_ids, limit=1000, oldest_first=False)
document = await self.load_data(channel_ids=channel_ids, limit=3000, oldest_first=False)
index = await self.loop.run_in_executor(None, partial(self.index_discord, document))
Path(app_root_path() / "indexes").mkdir(parents = True, exist_ok=True)
index.save_to_disk(app_root_path() / "indexes" / f"{ctx.guild.name.replace(' ', '-')}_{date.today()}-H{datetime.now().hour}.json")
@ -196,7 +212,7 @@ class Index_handler:
await ctx.respond(f"**Query:**\n\n{query.strip()}\n\n**Query response:**\n\n{response.response.strip()}")
except Exception:
traceback.print_exc()
await ctx.respond("Failed to send query", delete_after=10)
await ctx.respond("Failed to send query. You may not have an index set, load an index with /index load", delete_after=10)
# Extracted functions from DiscordReader

Loading…
Cancel
Save