我有一个与 llamindex 查询相关的问题,我无法找到解决方案。基本上,我正在尝试使用 llamindex 构建一个分类器。我有大约 700 个文档(不是很大 - 每个文档只有几个段落)。我将其分为火车测试并基于火车建立索引。问题是我的查询每个文档大约需要 1 分钟,而测试集中有 100 多个文档,我需要超过 2 小时。有办法解决吗?下面是我如何评估的代码片段。
vector_query_engine = my_index.as_query_engine(similarity_top_k=3,
text_qa_template=text_qa_template)
df['PredictedOutcome'] = df['doc_text'].apply(lambda x: vector_query_engine.query(x))
在这里您可以找到一种效果更快的方法。这只是一个入门代码,您可以尝试不同的索引、检索器和查询,以便更快地工作。
开始步骤和代码
--> 1000 个文件花费的总时间为 9 分钟。
filename_fn = lambda filename: {"file_name": filename}
documents = SimpleDirectoryReader(
"train",
file_metadata=filename_fn
).load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
file_contents = []
def read_files_in_folder(folder_path):
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
with open(file_path, 'r') as file:
file_contents.append(file.read())
return file_contents
folder_path = 'test'
file_contents = read_files_in_folder(folder_path)
start_time = time.time()
content_categories = []
for content in file_contents:
prompt = f'''
Take the {content} and tell the category. Possible categories are: [business,entertainment, politics,sport, tech]
'''
response = query_engine.query(
prompt
)
content_categories.append(response)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time} seconds")
经过时间:599.567540884018秒
进一步发展