|
|
|
@ -13,16 +13,15 @@ class Index_handler:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.openai_key = os.getenv("OPENAI_TOKEN")
|
|
|
|
|
self.index_storage = {}
|
|
|
|
|
self.loop = asyncio.get_running_loop()
|
|
|
|
|
|
|
|
|
|
def index_file(self, file):
|
|
|
|
|
document = SimpleDirectoryReader(file).load_data()
|
|
|
|
|
def index_file(self, file_path):
|
|
|
|
|
document = SimpleDirectoryReader(file_path).load_data()
|
|
|
|
|
index = GPTSimpleVectorIndex(document)
|
|
|
|
|
return index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def set_index(self, ctx: discord.ApplicationContext, file: discord.Attachment, user_api_key):
|
|
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
|
|
|
|
|
|
if not user_api_key:
|
|
|
|
|
os.environ["OPENAI_API_KEY"] = self.openai_key
|
|
|
|
|
else:
|
|
|
|
@ -32,7 +31,7 @@ class Index_handler:
|
|
|
|
|
temp_path = tempfile.TemporaryDirectory()
|
|
|
|
|
temp_file = tempfile.NamedTemporaryFile(suffix=".txt", dir=temp_path.name, delete=False)
|
|
|
|
|
await file.save(temp_file.name)
|
|
|
|
|
index = await loop.run_in_executor(None, partial(self.index_file, temp_path.name))
|
|
|
|
|
index = await self.loop.run_in_executor(None, partial(self.index_file, temp_path.name))
|
|
|
|
|
self.index_storage[ctx.user.id] = index
|
|
|
|
|
temp_path.cleanup()
|
|
|
|
|
await ctx.respond("Index set")
|
|
|
|
@ -51,5 +50,5 @@ class Index_handler:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
index: GPTSimpleVectorIndex = self.index_storage[ctx.user.id]
|
|
|
|
|
response = index.query(query, verbose=True)
|
|
|
|
|
response = await self.loop.run_in_executor(None, partial(index.query, query, verbose=True))
|
|
|
|
|
await ctx.respond(f"Query response: {response}")
|