|
|
|
@ -8,7 +8,8 @@ import aiofiles
|
|
|
|
|
from functools import partial
|
|
|
|
|
from typing import List, Optional
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from datetime import date, datetime
|
|
|
|
|
from datetime import date
|
|
|
|
|
from langchain import OpenAI
|
|
|
|
|
|
|
|
|
|
from gpt_index.readers import YoutubeTranscriptReader
|
|
|
|
|
from gpt_index.readers.schema.base import Document
|
|
|
|
@ -17,12 +18,12 @@ from gpt_index import (
|
|
|
|
|
SimpleDirectoryReader,
|
|
|
|
|
QuestionAnswerPrompt,
|
|
|
|
|
BeautifulSoupWebReader,
|
|
|
|
|
GPTFaissIndex,
|
|
|
|
|
GPTListIndex,
|
|
|
|
|
QueryMode,
|
|
|
|
|
GPTTreeIndex,
|
|
|
|
|
GoogleDocsReader,
|
|
|
|
|
MockLLMPredictor,
|
|
|
|
|
LLMPredictor,
|
|
|
|
|
QueryConfig,
|
|
|
|
|
IndexStructType,
|
|
|
|
|
)
|
|
|
|
@ -35,15 +36,14 @@ from services.environment_service import EnvService, app_root_path
|
|
|
|
|
SHORT_TO_LONG_CACHE = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_and_query(user_id, index_storage, query, llm_predictor):
|
|
|
|
|
# TODO Do prediction here for token usage
|
|
|
|
|
def get_and_query(user_id, index_storage, query, response_mode, llm_predictor):
|
|
|
|
|
index: [GPTSimpleVectorIndex, ComposableGraph] = index_storage[
|
|
|
|
|
user_id
|
|
|
|
|
].get_index_or_throw()
|
|
|
|
|
if isinstance(index, GPTTreeIndex):
|
|
|
|
|
response = index.query(query, verbose=True, child_branch_factor=2)
|
|
|
|
|
response = index.query(query, verbose=True, child_branch_factor=2, llm_predictor=llm_predictor)
|
|
|
|
|
else:
|
|
|
|
|
response = index.query(query, verbose=True)
|
|
|
|
|
response = index.query(query, response_mode=response_mode, verbose=True, llm_predictor=llm_predictor)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -66,7 +66,7 @@ class IndexData:
|
|
|
|
|
def has_indexes(self, user_id):
|
|
|
|
|
try:
|
|
|
|
|
return len(os.listdir(f"{app_root_path()}/indexes/{user_id}")) > 0
|
|
|
|
|
except:
|
|
|
|
|
except Exception:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def add_index(self, index, user_id, file_name):
|
|
|
|
@ -93,9 +93,8 @@ class IndexData:
|
|
|
|
|
for file in os.listdir(f"{app_root_path()}/indexes/{user_id}"):
|
|
|
|
|
os.remove(f"{app_root_path()}/indexes/{user_id}/{file}")
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
except Exception:
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Index_handler:
|
|
|
|
@ -271,13 +270,16 @@ class Index_handler:
|
|
|
|
|
await ctx.respond("Failed to set index")
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
async def load_index(self, ctx: discord.ApplicationContext, index, user_api_key):
|
|
|
|
|
async def load_index(self, ctx: discord.ApplicationContext, index, server, user_api_key):
|
|
|
|
|
if not user_api_key:
|
|
|
|
|
os.environ["OPENAI_API_KEY"] = self.openai_key
|
|
|
|
|
else:
|
|
|
|
|
os.environ["OPENAI_API_KEY"] = user_api_key
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if server:
|
|
|
|
|
index_file = EnvService.find_shared_file(f"indexes/{ctx.guild.id}/{index}")
|
|
|
|
|
else:
|
|
|
|
|
index_file = EnvService.find_shared_file(f"indexes/{ctx.user.id}/{index}")
|
|
|
|
|
index = await self.loop.run_in_executor(
|
|
|
|
|
None, partial(self.index_load_file, index_file)
|
|
|
|
@ -353,10 +355,11 @@ class Index_handler:
|
|
|
|
|
index = await self.loop.run_in_executor(
|
|
|
|
|
None, partial(self.index_discord, document)
|
|
|
|
|
)
|
|
|
|
|
Path(app_root_path() / "indexes").mkdir(parents=True, exist_ok=True)
|
|
|
|
|
Path(app_root_path() / "indexes" / str(ctx.guild.id)).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
index.save_to_disk(
|
|
|
|
|
app_root_path()
|
|
|
|
|
/ "indexes"
|
|
|
|
|
/ str(ctx.guild.id)
|
|
|
|
|
/ f"{ctx.guild.name.replace(' ', '-')}_{date.today().month}_{date.today().day}.json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
@ -374,11 +377,11 @@ class Index_handler:
|
|
|
|
|
os.environ["OPENAI_API_KEY"] = user_api_key
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
llm_predictor = MockLLMPredictor(max_tokens=256)
|
|
|
|
|
llm_predictor = LLMPredictor(llm=OpenAI(model_name="text-davinci-003"))
|
|
|
|
|
response = await self.loop.run_in_executor(
|
|
|
|
|
None,
|
|
|
|
|
partial(
|
|
|
|
|
get_and_query, ctx.user.id, self.index_storage, query, llm_predictor
|
|
|
|
|
get_and_query, ctx.user.id, self.index_storage, query, response_mode, llm_predictor
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
print("The last token usage was ", llm_predictor.last_token_usage)
|
|
|
|
|