Now I want to do small-to-big RAG. My function so far is this:
def small_to_big(self, query, documents, rag=False, chunk_size=1024, tree_depth=2, similarity_top_k=3):
node_parser = SimpleNodeParser.from_defaults(chunk_size=chunk_size)
base_nodes = node_parser.get_nodes_from_documents(documents)
# Assign unique ids to each node
for idx, node in enumerate(base_nodes):
node.id_ = f"node-{idx}"
prompt_helper = PromptHelper(context_window=4096,
num_output=256,
chunk_overlap_ratio=0.1,
chunk_size_limit=None)
service_context = ServiceContext.from_defaults(llm=self.llm,
embed_model=self.embed_model,
prompt_helper=prompt_helper)
sub_chunk_sizes = [chunk_size // 2**i for i in range(1,tree_depth+1)]
sub_node_parsers = [SimpleNodeParser.from_defaults(chunk_size=c) for c in sub_chunk_sizes]
all_nodes = []
for base_node in base_nodes:
for n in sub_node_parsers:
sub_nodes = n.get_nodes_from_documents([base_node])
sub_inodes = [IndexNode.from_text_node(sn, base_node.node_id) for sn in sub_nodes]
all_nodes.extend(sub_inodes)
original_node = IndexNode.from_text_node(base_node, base_node.node_id)
all_nodes.append(original_node)
all_nodes_dict = {n.node_id: n for n in all_nodes}
vector_index_chunk = VectorStoreIndex(all_nodes,
service_context=service_context)
# vector_index_chunk = VectorStoreIndex(all_nodes,
# service_context=service_context,
# storage_context=self.storage_context)